Introduction to parallel computing

Similar documents
Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

Real Time Operating Systems and Middleware

Threads need to synchronize their activities to effectively interact. This includes:

Locks and semaphores. Johan Montelius KTH

POSIX Threads. Paolo Burgio

Programming Shared Address Space Platforms (cont.) Alexandre David B2-206

Faculté Polytechnique

Paralleland Distributed Programming. Concurrency

real time operating systems course

Threads. Jo, Heeseung

Synchronization Primitives

Multithread Programming. Alexandre David

Locks and semaphores. Johan Montelius KTH

Lab 12 - Concurrency 2

Multithreading Programming II

recap, what s the problem Locks and semaphores Total Store Order Peterson s algorithm Johan Montelius 0 0 a = 1 b = 1 read b read a

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

ANSI/IEEE POSIX Standard Thread management

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References

Posix Threads (Pthreads)

Operating systems and concurrency (B08)

Threads. Jo, Heeseung

EEE3052: Introduction to Operating Systems. Fall Project #3

Xu Liu Derived from John Mellor-Crummey s COMP422 at Rice University

Synchronising Threads

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Solving the Producer Consumer Problem with PThreads

POSIX Threads. HUJI Spring 2011

Shared Address Space Programming with Pthreads. CS 5334/4390 Spring 2014 Shirley Moore, Instructor February 18, 2014

Project 3-2. Mutex & CV

Process Synchronization

TCSS 422: OPERATING SYSTEMS

More Shared Memory Programming

THREADS. Jo, Heeseung

Programming Shared Address Space Platforms (Chapter 7)

Network Programming TDC 561

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Introduc)on to pthreads. Shared memory Parallel Programming

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

CS 153 Lab6. Kishore Kumar Pusukuri

Access to the same variable

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

Process Synchronization (Part I)

Condition Variables. Dongkun Shin, SKKU

CS 33. Multithreaded Programming III. CS33 Intro to Computer Systems XXXIV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CMSC421: Principles of Operating Systems

Programming RT systems with pthreads

CMSC421: Principles of Operating Systems

Pthreads (2) Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Synchronization Mechanisms

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

POSIX PTHREADS PROGRAMMING

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005

Thread and Synchronization

Parallel Programming with Threads

CS516 Programming Languages and Compilers II

Resource Access Control (2) Real-Time and Embedded Systems (M) Lecture 14

Computer Systems Laboratory Sungkyunkwan University

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Condition Variables. Dongkun Shin, SKKU

Synchronization. Semaphores implementation

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017

Motivation and definitions Processes Threads Synchronization constructs Speedup issues

Reminder from last time

Shared Memory Parallel Programming with Pthreads An overview

CS 167 Midterm Exam. Two Hours; Closed Book; Please Use a Black Pen March 23, 2017

Programming RT systems with pthreads

PRINCIPLES OF OPERATING SYSTEMS

High-level Synchronization

Synchroniza+on II COMS W4118

Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang

LSN 13 Linux Concurrency Mechanisms

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017

Real Time Operating System Support for Concurrency

CS4961 Parallel Programming. Lecture 12: Advanced Synchronization (Pthreads) 10/4/11. Administrative. Mary Hall October 4, 2011

Oct 2 and 4, 2006 Lecture 8: Threads, Contd

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

Thread Posix: Condition Variables Algoritmi, Strutture Dati e Calcolo Parallelo. Daniele Loiacono

CS 220: Introduction to Parallel Computing. Condition Variables. Lecture 24

Operating systems. Lecture 3 Interprocess communication. Narcis ILISEI, 2018

POSIX Condition Variables ADT

The University of Texas at Arlington

Midterm I SOLUTIONS October 14 th, 2015 CS162: Operating Systems and Systems Programming

W4118 Operating Systems. Instructor: Junfeng Yang

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives.

Operating System Labs. Yuanbin Wu

Example. Example. Threading Language and Support. CS528 Multithreading: Programming with Threads. The boss/worker model. Thread Programming models

Concurrent Server Design Multiple- vs. Single-Thread

CS Lecture 3! Threads! George Mason University! Spring 2010!

The course that gives CMU its Zip! Concurrency II: Synchronization Nov 14, 2000

Agenda. Process vs Thread. ! POSIX Threads Programming. Picture source:

Programming in Parallel COMP755

Final Examination (Fall 2016) (Section 1)

Synchronization. Dr. Yingwu Zhu

The University of Texas at Arlington

CS 6400 Lecture 11 Name:

Lecture 19: Shared Memory & Synchronization

Transcription:

Introduction to parallel computing Shared Memory Programming with Pthreads (3) Zhiao Shi (modifications by Will French) Advanced Computing Center for Education & Research Vanderbilt University

Last time Mutex lock int pthread_mutex_init (pthread_mutex_t *mutex_lock, const pthread_mutexattr_t *lock_attr);! int pthread_mutex_lock (pthread_mutex_t *mutex_lock); int pthread_mutex_unlock (pthread_mutex_t *mutex_lock); int pthread_mutex_destroy(pthread_mutex_t *mutex_lock); 2

BARRIERS AND CONDITION VARIABLES 3

Barriers Synchronizing the threads to make sure that they all are at the same point in a program is called a barrier. No thread can cross the barrier until all the threads have reached it. 4

Using barriers to time the slowest thread 5

Using barriers for debugging 6

Busy-waiting and a Mutex Implementing a barrier using busy-waiting and a mutex is straightforward. We use a shared counter protected by the mutex. When the counter indicates that every thread has entered the critical section, threads can leave the critical section. 7

Busy-waiting and a Mutex However, the Pthread library provides its own barrier functions 8

Creating and Initializing a Barrier To initialize a barrier, use code similar to this (which sets the number of threads to 4): pthread_barrier_t b; // declare with global scope pthread_barrier_init(&b,null,4); The second argument specifies an attribute object for finer control; using NULL yields the default attributes. To wait at a barrier, a thread call: pthread_barrier_wait(&b); To destroy a barrier: pthread_barrier_destroy(&b); CS267 Lecture 9" 6"

Condition Variables Often, a critical section is to be executed if a specific global condition exists; for example, if a certain value of a variable has been reached. With locks, the global variable would need to be examined at frequent intervals ( polled ) within a critical section. Very time-consuming and unproductive. Can be overcome by introducing so-called condition variables. 10

Condition Variables A condition variable is a data object that allows a thread to suspend execution until a certain event or condition occurs. When the event or condition occurs another thread can signal the thread to wake up. 11

Condition Variables for Synchronization A condition variable is associated with the predicate. When the predicate becomes true, the condition variable is used to signal one or more threads waiting on the condition. A condition variable always has a mutex associated with it. A thread locks this mutex and tests the predicate defined on the shared variable. 12

Condition Variables for Synchronization If the predicate is not true, the thread waits on the condition variable associated with the predicate using the function pthread_cond_wait. This also releases the lock on the mutex so that others can change the condition variable At a later time when another thread makes the predicate true, that thread calls pthread_cond_signal to unblock the waiting thread. The signaled (waiting) thread now also has the lock on the mutex 13

Condition Variables for Synchronization Pthreads provides the following functions for condition variables: int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); int pthread_cond_signal(pthread_cond_t *cond); int pthread_cond_broadcast(pthread_cond_t *cond); int pthread_cond_destroy(pthread_cond_t *cond);! 14

Condition variables: wait pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) Blocks the calling thread, waiting on cond. Unlock the mutex Re-acquires the mutex when unblocked. 15

Condition variables: signal pthread_cond_signal(pthread_cond_t *cond) Unblocks one thread waiting on cond. The scheduler determines which thread to unblock. If no thread waiting, then signal accomplishes nothing. 16

Condition variables: broadcast pthread_cond_broadcast(pthread_cond_t *cond) Unblocks all threads waiting on cond. 17

Implementing a barrier with condition variables 18

Producer consumer program without condition variables

/* Globals */ int data_avail = 0; pthread_mutex_t data_mutex; pthread_mutex_init(&data_mutex, NULL); void *producer(void *) { pthread_mutex_lock(&data_mutex); /* Produce data insert data into queue; */ data_avail=1; pthread_mutex_unlock(&data_mutex); } 20

void *consumer(void *) { while(!data_avail ); /* do nothing keep looping!!*/ pthread_mutex_lock(&data_mutex); // Extract data from queue; if (queue is empty) data_avail = 0; pthread_mutex_unlock(&data_mutex); } consume_data(); 21

Producer consumer program with condition variables

int data_avail = 0; pthread_mutex_t data_mutex; pthread_cond_t data_cond; pthread_mutex_init(&data_mutex, NULL); pthread_cond_init(&data_cond, NULL); void *producer(void *) { pthread_mutex_lock(&data_mutex); //Produce data //Insert data into queue; data_avail = 1; pthread_cond_signal(&data_cond); pthread_mutex_unlock(&data_mutex); } 23

void *consumer(void *) { pthread_mutex_lock(&data_mutex); while(!data_avail ) { } /* sleep on condition variable*/ pthread_cond_wait(&data_cond, &data_mutex); /* woken up */ /* Extract data from queue; */ if (queue_is_empty()) data_avail = 0; pthread_mutex_unlock(&data_mutex); } consume_data(); 24

Producer-Consumer Using Condition Variables Why do the previous two slides use while-loops around the pthread_cond_wait()? It seems that if we received the signal, then the while-condition must be false. If we had multiple producers or consumers, one of the other threads may have received the lock first and since invalidated the condition. It is also possible that the thread was woken up for other reasons (e.g., an OS signal). 25