Programming Models using Windows* Threads Intel Software College.

Slides:



Advertisements
Similar presentations
Shared-Memory Model and Threads Intel Software College Introduction to Parallel Programming – Part 2.
Advertisements

Implementing Domain Decompositions Intel Software College Introduction to Parallel Programming – Part 3.
Operating Systems Semaphores II
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
INTEL CONFIDENTIAL Threading for Performance with Intel® Threading Building Blocks Session:
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Ch 7 B.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Scalable Synchronous Queues By William N. Scherer III, Doug Lea, and Michael L. Scott Presented by Ran Isenberg.
INTEL CONFIDENTIAL Deadlock Introduction to Parallel Programming – Part 7.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Friday, June 16, 2006 "In order to maintain secrecy, this posting will self-destruct in five seconds. Memorize it, then eat your computer." - Anonymous.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
1 Semaphores Special variable called a semaphore is used for signaling If a process is waiting for a signal, it is suspended until that signal is sent.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
INTEL CONFIDENTIAL OpenMP for Domain Decomposition Introduction to Parallel Programming – Part 5.
INTEL CONFIDENTIAL Confronting Race Conditions Introduction to Parallel Programming – Part 6.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
INTEL CONFIDENTIAL OpenMP for Task Decomposition Introduction to Parallel Programming – Part 8.
CS444/CS544 Operating Systems Classic Synchronization Problems 2/26/2007 Prof. Searleman
INTEL CONFIDENTIAL Why Parallel? Why Now? Introduction to Parallel Programming – Part 1.
INTEL CONFIDENTIAL Reducing Parallel Overhead Introduction to Parallel Programming – Part 12.
INTEL CONFIDENTIAL Parallel Decomposition Methods Introduction to Parallel Programming – Part 2.
Monitor  Giving credit where it is due:  The lecture notes are borrowed from Dr. I-Ling Yen at University of Texas at Dallas  I have modified them and.
Multi-core Programming: Basic Concepts. Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
INTEL CONFIDENTIAL Predicting Parallel Performance Introduction to Parallel Programming – Part 10.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Programming with POSIX* Threads Intel Software College.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Correcting Threading Errors with Intel® Parallel Inspector.
2001 Networking Operating Systems (CO32010) 1. Operating Systems 2. Processes and scheduling 4.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
INTEL CONFIDENTIAL Shared Memory Considerations Introduction to Parallel Programming – Part 4.
Synchronizing Threads with Semaphores
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Thinking in Parallel - Introduction New Mexico Supercomputing Challenge in partnership with Intel Corp. and NM EPSCoR.
Tuning Threaded Code with Intel® Parallel Amplifier.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Web Server Architecture Client Main Thread for(j=0;j
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Tutorial 2: Homework 1 and Project 1
Concurrency: Mutual Exclusion and Synchronization
Critical Section and Critical Resources
Critical Section and Critical Resources
ე ვ ი ო Ш Е Т И О А С Д Ф К Ж З В Н М W Y U I O S D Z X C V B N M
Chapter 6 Synchronization Principles
CSE 451 Section 1/27/2000.
Synchronization CS Spring 2002.
Chapter 6 – Distributed Processing and File Systems
Presentation transcript:

Programming Models using Windows* Threads Intel Software College

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 2 Programming Models using Windows Threads Objectives At the completion of this module you will be able to Use a dynamic allocation models to distribute computation to threads

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 3 Programming Models using Windows Threads Programming Models Static allocation of tasks is easy Divide work by number of threads Dynamic allocation schemes Assign work as needed Boss-Worker Producer/Consumer

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 4 Programming Models using Windows Threads Rendezvous Two threads “meet” to exchange data Properties: Symmetrical waiting (first thread to arrive must wait for second) Single transaction is completed Two-way exchange of information Mutual exclusion of other threads attempting rendezvous

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 5 Programming Models using Windows Threads Rendezvous - Eskimo Example From “Principles of Concurrent Processes” by M. Ben-Ari Eskimos represent threads Several Eskimos riding around in dog sleds hunt for meat Another Eskimo owns a lodge lodge holds only two Eskimos, 1 loaf of bread, meat Hunting Eskimos stop at lodge for a snack

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 6 Programming Models using Windows Threads Rendezvous - Eskimo Example (contd.) If hunter arrives before lodge owner, hunter crawls into sleeping bag to wait If lodge owner arrives before hunter, owner waits outside (will not heat empty lodge) When both a hunter and owner are at lodge lodge is opened and both go inside hunter exchanges meat for sandwich prepared by owner

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 7 Programming Models using Windows Threads Rendezvous - Eskimo Example If several hunters are waiting only one is allowed to interact with owner before returning to hunt owner must return to bakery to get new loaf of bread before returning to lodge upon owner’s return, another hunter may get sandwich

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 8 Programming Models using Windows Threads Boss-Worker Rendezvous Common version of rendezvous between threads is Boss- Worker model Single thread “generates” tasks to be worked on [Boss thread] Other threads request new task when done with previous task [Worker threads] Good model for unequal amounts of computation between tasks

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 9 Programming Models using Windows Threads Boss-Worker Declarations CRITICAL_SECTION cs; HANDLE hEvent; int num_waiting = 0; InitializeCriticalSection(&cs); hEvent = CreateEvent( NULL, // default security FALSE, // auto-reset FALSE, // start unsignaled NULL ); // no name needed Use auto-reset event

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 10 Programming Models using Windows Threads Boss Thread Code EnterCriticalSection(&cs); num_waiting++; if (num_waiting !=2) { // if no worker waiting... LeaveCriticalSection(&cs); WaitForSingleObject(hEvent, INFINITE); //...wait } else { SetEvent(hEvent); // wake up worker num_waiting = 0; LeaveCriticalSection(&cs); }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 11 Programming Models using Windows Threads Boss Code Explanation Boss uses CRITICAL_SECTION cs To protect num_waiting test and increment If no Worker waiting ( num_waiting != 2 ) Wait for Worker to show up Else (Worker is waiting) Wake up Worker with event signal Reset num_waiting for next rendezvous

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 12 Programming Models using Windows Threads Worker Thread Code Hypothesis: same code as Boss Boss and Worker know which is what If Boss not waiting ( num_waiting != 2 ) Wait for Boss to show up Else (Boss is waiting) Wake up Boss with event signal Reset num_waiting for next rendezvous

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 13 Programming Models using Windows Threads Test the Hypothesis What if Boss arrives before Worker? What if Worker arrives before Boss? What if both arrive at same time? What about multiple workers waiting? Must protect worker code from multiple workers add worker mutual exclusion declaration

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 14 Programming Models using Windows Threads Worker Thread Code WaitForSingleObject(hWorkerSem, INFINITE); // gate workers EnterCriticalSection(&cs); num_waiting++; if (num_waiting !=2) { // if no boss waiting... LeaveCriticalSection(&cs); WaitForSingleObject(hEvent, INFINITE); //...wait } else { SetEvent(hEvent); // wake up boss num_waiting = 0; LeaveCriticalSection(&cs); } ReleaseSemaphore (hWorkerSem, 1, NULL);

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 15 Programming Models using Windows Threads Producer/Consumer Producer thread places data in memory Consumer thread retrieves data Faster producer? Use queue to hold data Better model for threads because no active transfer of data

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 16 Programming Models using Windows Threads Prod/Cons Declarations int buffer[BUFSIZE]; int in=1; /* index to store next element */ int out=0; /* index of last removed element */ CRITICAL_SECTION b_lock; // Auto-reset Events HANDLE b_NotFull; // initially SIGNALED HANDLE b_NotEmpty; // initially NONSIGNALED in out

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 17 Programming Models using Windows Threads Producer Thread Code while (more to produce){ EnterCriticalSection(&b_lock); if (out == in) { // buffer is full LeaveCriticalSection(&b_lock); WaitForSingleObject(b_NotFull, INFINITE); EnterCriticalSection(&b_lock); } buffer[in++]= ; in %= BUFSIZE; SetEvent(b_NotEmpty); LeaveCriticalSection(&b_lock); }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 18 Programming Models using Windows Threads Producer Code Explained CRITICAL_SECTION controls access to buffer Conditional expression checks buffer status If buffer full, producer waits After element stored in buffer, increment in index pointer Wake up any consumer waiting on empty buffer

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 19 Programming Models using Windows Threads Consumer Thread Code while (more to consume) { EnterCriticalSection(&b_lock); while (in == ((out+1) % BUFSIZE)) { // buffer is empty LeaveCriticalSection(&b_lock); WaitForSingleObject(b_NotEmpty, INFINITE); EnterCriticalSection(&b_lock); } ++out %= BUFSIZE; = buffer[out]; SetEvent(b_NotFull); // signal Producer at least one slot open if (in != ((out+1) % BUFSIZE)) // if buffer still not empty... SetEvent(b_NotEmpty); // signal consumer one slot full LeaveCriticalSection(&b_lock); }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 20 Programming Models using Windows Threads Consumer Code Explained CRITICAL_SECTION controls access to buffer Conditional expression checks buffer status If buffer empty, consumer waits Increment out index pointer before element removed from buffer Signal producer that may be waiting on full buffer Signal consumer if more items left in buffer

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 21 Programming Models using Windows Threads P/C: Does It Work Correctly? One producer, one consumer One producer, multiple consumers Multiple producers, one consumer Multiple producers, multiple consumers

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 22 Programming Models using Windows Threads Summary Use standard models when possible Boss-Worker Producer/Consumer

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. Intel ® Software College 23 Programming Models using Windows Threads