The University of Adelaide, School of Computer Science

Slides:



Advertisements
Similar presentations
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Advertisements

CS427 Multicore Architecture and Parallel Computing
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
SMP threads an Introduction to Posix Threads. Technical Definition 1.Independent stream of instructions that can be scheduled to run by an operating system.
B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy.
Threads? Threads allow us to have multiple tasks active at the same time in one executable –think of a server handling multiple connections Each thread.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Netprog Threads Programming1 Threads Programming Refs: Chapter 23.
10/04/2011CS4961 CS4961 Parallel Programming Lecture 12: Advanced Synchronization (Pthreads) Mary Hall October 4, 2011.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
1 OpenMP Writing programs that use OpenMP. Using OpenMP to parallelize many serial for loops with only small changes to the source code. Task parallelism.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
ICS 145B -- L. Bic1 Project: Process/Thread Synchronization Textbook: pages ICS 145B L. Bic.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
Includes slides from course CS194 at UC Berkeley, by prof. Katherine Yelick Shared Memory Programming Pthreads: an overview Ing. Andrea Marongiu
Professor: Shu-Ching Chen TA: Samira Pouyanfar.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int.
Pthreads: A shared memory programming model
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Lecture 5 Barriers and MPI Introduction Topics Barriers Uses implementations MPI Introduction Readings – Semaphore handout dropboxed January 24, 2012 CSCE.
Pthreads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Threads A thread is an alternative model of program execution
Barriers and Condition Variables
3/12/2013Computer Engg, IIT(BHU)1 OpenMP-1. OpenMP is a portable, multiprocessing API for shared memory computers OpenMP is not a “language” Instead,
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Chapter 4 Shared Memory Programming with Pthreads An Introduction to Parallel Programming Peter Pacheco.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Shared Memory Programming with Pthreads T. Yang. UCSB CS240A. Spring 2016.
Tutorial 4. In this tutorial session we’ll see Threads.
Realizing Concurrency using the thread model
Realizing Concurrency using the thread model
PThreads.
Shared Memory Programming with Pthreads
Threads in C Caryl Rahn.
Pthreads – Create and Join
Threads Threads.
Boost String API & Threads
Multithreading Tutorial
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
Lecture 14: Pthreads Mutex and Condition Variables
Realizing Concurrency using the thread model
PTHREADS AND SEMAPHORES
Multithreading Tutorial
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
Realizing Concurrency using the thread model
The University of Adelaide, School of Computer Science
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
Multithreading Tutorial
Programming with Shared Memory
Multithreading Tutorial
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Threads Synchronization
Programming with Shared Memory
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
Programming with Shared Memory - 2 Issues with sharing data
CS 144 Advanced C++ Programming May 7 Class Meeting
Shared Memory Programming with Pthreads
POSIX Threads(pthreads)
Presentation transcript:

The University of Adelaide, School of Computer Science An Introduction to Parallel Programming Peter Pacheco The University of Adelaide, School of Computer Science 23 April 2017 Chapter 4 Shared Memory Programming with Pthreads Chapter 2 — Instructions: Language of the Computer

The University of Adelaide, School of Computer Science 23 April 2017 Roadmap Problems programming shared memory systems. Controlling access to a critical section. Thread synchronization. Programming with POSIX threads. Mutexes. Producer-consumer synchronization and semaphores. Barriers and condition variables. Read-write locks. Thread safety. Chapter 2 — Instructions: Language of the Computer

A Shared Memory System

Processes and Threads A process is an instance of a running (or suspended) program. Threads are analogous to a “light-weight” process. In a shared memory program a single process may have multiple threads of control.

POSIX® Threads Also known as Pthreads. A standard for Unix-like operating systems. A library that can be linked with C programs. Specifies an application programming interface (API) for multi-threaded programming.

Hello World! (1) declares the various Pthreads functions, constants, types, etc.

Hello World! (2)

Hello World! (3)

Compiling a Pthread program gcc −g −Wall −o pth_hello pth_hello . c −lpthread link in the Pthreads library

Running a Pthreads program . / pth_hello <number of threads> . / pth_hello 1 Hello from the main thread Hello from thread 0 of 1 . / pth_hello 4 Hello from the main thread Hello from thread 0 of 4 Hello from thread 1 of 4 Hello from thread 2 of 4 Hello from thread 3 of 4

Global variables Can introduce subtle and confusing bugs! Limit use of global variables to situations in which they’re really needed. Shared variables.

Starting the Threads Processes in MPI are usually started by a script. In Pthreads the threads are started by the program executable.

Starting the Threads pthread.h pthread_t One object for each thread. int pthread_create ( pthread_t* thread_p /* out */ , const pthread_attr_t* attr_p /* in */ , void* (*start_routine ) ( void ) /* in */ , void* arg_p /* in */ ) ;

pthread_t objects Opaque The actual data that they store is system-specific. Their data members aren’t directly accessible to user code. However, the Pthreads standard guarantees that a pthread_t object does store enough information to uniquely identify the thread with which it’s associated.

A closer look (1) int pthread_create ( pthread_t* thread_p /* out */ , const pthread_attr_t* attr_p /* in */ , void* (*start_routine ) ( void ) /* in */ , void* arg_p /* in */ ) ; We won’t be using, so we just pass NULL. Allocate before calling.

A closer look (2) int pthread_create ( pthread_t* thread_p /* out */ , const pthread_attr_t* attr_p /* in */ , void* (*start_routine ) ( void ) /* in */ , void* arg_p /* in */ ) ; Pointer to the argument that should be passed to the function start_routine. The function that the thread is to run.

Function started by pthread_create Prototype: void* thread_function ( void* args_p ) ; Void* can be cast to any pointer type in C. So args_p can point to a list containing one or more values needed by thread_function. Similarly, the return value of thread_function can point to a list of one or more values.

Running the Threads Main thread forks and joins two threads.

Stopping the Threads We call the function pthread_join once for each thread. A single call to pthread_join will wait for the thread associated with the pthread_t object to complete.

Matrix-Vector Multiplication in pthreads

Serial pseudo-code

Using 3 Pthreads thread 0 general case

Pthreads matrix-vector multiplication

Critical sections

Estimating π

Using a dual core processor Note that as we increase n, the estimate with one thread gets better and better. We are using SUM as a shared variable and each thread accesses it.

A thread function for computing π

Possible race condition

Busy-Waiting A thread repeatedly tests a condition, but, effectively, does no useful work until the condition has the appropriate value. Beware of optimizing compilers, though! They can move code, like x=x+y; //Protecting x flag initialized to 0 by main thread

Pthreads global sum with busy-waiting

Global sum function with critical section after loop (1)

Global sum function with critical section after loop (2)

Mutexes A thread that is busy-waiting may continually use the CPU accomplishing nothing. Mutex (mutual exclusion) is a special type of variable that can be used to restrict access to a critical section to a single thread at a time.

Mutexes Used to guarantee that one thread “excludes” all other threads while it executes the critical section. The Pthreads standard includes a special type for mutexes: pthread_mutex_t.

Mutexes When a Pthreads program finishes using a mutex, it should call In order to gain access to a critical section a thread calls

Mutexes When a thread is finished executing the code in a critical section, it should call

Global sum function that uses a mutex (1)

Global sum function that uses a mutex (2)

Run-times (in seconds) of π programs using n = 108 terms on a system with two four-core processors.

Possible sequence of events with busy-waiting and more threads than cores.

Producer-Consumer Synchronization and Semaphores

Issues Busy-waiting enforces the order threads access a critical section. Using mutexes, the order is left to chance and the system. There are applications where we need to control the order threads access the critical section.

Problems with a mutex solution The application problem is to find the product of several n x n matrices (Product_mat = A1 * A2 * A3 * ….. Ak ). Each thread multiplies its matrix by the product_mat to accumulate a product. But with mutexes, the order is not fixed and matrix multiplication is not commutative.

A first attempt at sending messages using pthreads Needs to be while

Syntax of the various semaphore functions Semaphores are not part of Pthreads; you need to add this. Semaphore starts with 1 (unlocked). sem_wait blocks if semaphore is 0 (locked); continues if/when semaphore is 1 and decrements semaphore. sem_post increments the semaphore (use when done with protected area) and any blocked semaphore can continue.