Multi-core Programming Destroy the Castle Lab 4 Code snippets.

Slides:



Advertisements
Similar presentations
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Advertisements

Stacks, Queues, and Linked Lists
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
1 Todays Objectives Announcements Homework #1 is due next week Return Quiz 1 – answers are posted on the Yahoo discussion page site Basic Java Programming.
Computer Science 320 Clumping in Parallel Java. Sequential vs Parallel Program Initial setup Execute the computation Clean up Initial setup Create a parallel.
DATA STRUCTURES ( C++ ) This PPT is Dedicated to my inner controller AMMA BHAGAVAN-Oneness Founders. Developed by,EDITED BY, S.V.G.REDDY,M.Siva Naga Prasad,
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Written by: Dr. JJ Shepherd
CS 240Chapter 6 - StacksPage 21 Chapter 6 Stacks The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Pointer Review. Node { int value; Node * next; Node( int val =-1, Node * n = NULL) {value = val; next = n;} } Given a ordered linked list pointed to.
J AVA AND V ARIABLES. O VERVIEW Declaring a Variable Primitive Types Java Keywords Reference Variables Object Declaration and Assignment Objects and Garbage.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Introduction to Lock-free Data-structures and algorithms Micah J Best May 14/09.
General Computer Science for Engineers CISC 106 Final Exam Review Dr. John Cavazos Computer and Information Sciences 05/18/2009.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
Chapter 1 pp 1-14 Properties of Algorithms Pseudocode.
CS61B Midterm Review Winston Liaw and Amir Kamil.
Insertion into a B+ Tree Null Tree Ptr Data Pointer * Tree Node Ptr After Adding 8 and then 5… 85 Insert 1 : causes overflow – add a new level * 5 * 158.
Threads CNS What is a thread?  an independent unit of execution within a process  a "lightweight process"  an independent unit of execution within.
CS 240: Data Structures Thursday, July 12 th Lists, Templates, Vector, Algorithms.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Data Structures in the Kernel Sarah Diesburg COP 5641.
Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Programming Models using Windows* Threads Intel Software College.
CSE 380 – Computer Game Programming Render Threading Portal, by Valve,
Destroy the Castle – Example Instructions. Functional Decomposition2 Lab 1 Activity 1 Build, Run, and Benchmark.
Nachos Phase 1 Code -Hints and Comments
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence.
G053 - Lecture 16 Validating Forms Mr C Johnston ICT Teacher
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Pointers OVERVIEW.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 2-1: Process Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
OS2014 PROJECT 2 Supplemental Information. Outline Sequence Diagram of Project 2 Kernel Modules Kernel Sockets Work Queues Synchronization.
Chapter 5 Implementing UML Specification (Part II) Object-Oriented Technology From Diagram to Code with Visual Paradigm for UML Curtis H.K. Tsang, Clarence.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
CS 261 – Data Structures Introduction to C Programming.
 MergeSort is a sorting algorithm which is more on the advanced end.  It is very fast, but unfortunately uses up a lot of memory due to the recursions.
Written by: Dr. JJ Shepherd
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
Threads and Thread Synchronization in MFC By Gregory Mortensen CSIS 4330 Advanced Windows Programming.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
int [] scores = new int [10];
Random Logic l Forum.NET l State Machine Mechanism Forum.NET 1 st Meeting ● December 27, 2005.
1.NETDelegates & eventsNOEA / PQC 2007 Delegates & events Observer pattern Delegates –Semantics –Cil – code –Usage Events Asynchronious delegates.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
100e Hosted by vhs © Don Link, Indian Creek School, 2004 Jeopardy.
Introduction to Operating Systems
Multithreading / Concurrency
Specifications What? Not how!.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Iteration with While You can say that again.
Data Structures & Algorithms
CSCS-200 Data Structure and Algorithms
More concurrency issues
Presentation transcript:

Multi-core Programming Destroy the Castle Lab 4 Code snippets

2 Lab 4 Activity 1 Steps 2-5 Declare ThreadContext structure //////////////////////////////////////////////////////////////////////////// ///Lab 4 Activity 1: Declare ThreadContext structure /// /// /// Declare a wrapper class for Win32 threads. Each instance's tick() /// /// function copies thread local data and wakes up the thread by issuing /// /// a SetEvent() call. /// /// 1) As part of the lab, uncomment the macro definition for /// /// THREADCONTEXT_EVENTS_DECLARED /// /// 2) Add a declaration for the Thread handle (m_thread) /// /// 3) Add declarations for the Start and Done event handles (m_evtStart /// /// and m_evtDone) /// /// /// INSERT CODE HERE /// #define THREADCONTEXT_EVENTS_DECLARED HANDLEm_evtStart; HANDLEm_evtDone; HANDLEm_thread;

3 Lab 4 Activity 1 Steps 6-7 Create and Init ThreadContext array ///////////////////////////////////////////////////////////////////////////// /// /// ///Lab 4 Activity 1: Create and Initialize ThreadContext array /// /// /// 1) Create an instance of the ThreadContext wrapper class for each of /// /// the Win32 threads. /// /// 2) 'init'ialize each ThreadContext structure /// /// 3) Push a shared pointer to each ThreadContext onto the m_contexts /// /// vector. /// /// INSERT CODE HERE /// for (int i=0; i<num_threads; i++) { ThreadContextPtr cp(new ThreadContext(this, i)); if (!cp->init()) break; m_contexts.push_back(cp); }

4 Lab 4 Activity 1 Step 8 Initialize ThreadContext /////////////////////////////////////////////////////////////////////////// ///Lab 4 Activity 1: Initialize ThreadContext /// /// /// 1) Create Start and Done events used to signal the worker threads /// /// 2) Create a Win32 Thread using _beginthreadex() with a 5K stack set /// /// with entry point as _WorkerThreadProc /// /// /// INSERT CODE HERE /// m_evtStart=::CreateEvent(0, FALSE, FALSE, 0 ); m_evtDone=::CreateEvent(0, FALSE, FALSE, 0 ); if (m_evtDone!=0 && m_evtStart!=0) { m_thread=(HANDLE)::_beginthreadex(NULL, 512*1024, _WorkerThreadProc, this, 0, NULL ); if (m_thread!=0 && m_thread!=INVALID_HANDLE_VALUE) { return true; }

5 Lab 4 Activity 2 Steps 1-4 Divide data and notify Worker threads //////////////////////////////////////////////////////////////////////////// /// Split the AI Workload to available threads and issue a SetEvent() to /// /// let the worker threads start processing. This tick() function will /// /// wait till the worker threads' tick() functions complete. /// /// /// 1) Split getNumMOBS() amount of work between 'threads'. /// /// 2) Loop through and call the Workers' tick() functions using the /// /// ThreadContext vector with the specified chunk of data. /// /// 3) Finally, wait for all Worker threads to finish using a /// /// WaitForMultipleObjects() call. /// for (unsigned i=0; i<threads; i++) { unsigned from=i*blkSize; unsigned to=from+blkSize; if (to>=getNumMOBS()) to=getNumMOBS(); m_contexts[i]->tick(from, to, currentTime, elapsedTime ); threadEvents[i]=m_contexts[i]->getDoneEvent(); } ::WaitForMultipleObjects(threads, threadEvents, TRUE, INFINITE);

6 Lab 4 Activity 2 Steps 5-7 Implement Worker thread tick function /////////////////////////////////////////////////////////////////////////// /// /// ///Lab 4 Activity 2: Implement Worker thread function /// /// /// This function will wait till a Start event signal from the main AI /// /// thread, and then call the DemoEngine's tick with the given slice of /// /// data. /// ///1) Wait for m_evtStart event /// ///2) Check m_stopped to see if the AI DemoEngine is halted /// ///3) Issue the DemoEngine tick to handle the subset of data. /// ///4) SetEvent m_evtDone to let the main AIThread know we're done /// /// /// INSERT CODE HERE /// ::WaitForSingleObject(m_evtStart, INFINITE); if (m_stopped) break; m_engine->tick(m_from, m_to, m_currentTime,m_elapsedTime); ::SetEvent(m_evtDone);