lecture 3 posix threads topics pthreadsreadings january 17, 2012 csce 713 advanced computer...

47
Lecture 3 Posix Threads Topics Topics Pthreads Readings Readings January 17, 2012 CSCE 713 Advanced Computer Architecture

Upload: shanon-manning

Post on 28-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

Lecture 3Posix Threads

Lecture 3Posix Threads

Topics Topics Pthreads

ReadingsReadings

January 17, 2012

CSCE 713 Advanced Computer Architecture

Page 2: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 2 –CSCE 713 Spring 2012

OverviewOverviewLast TimeLast Time

Finish Slides from Lecture 1 NP-Completeness and the Dwarves Posix Pthreads

Readings for todayReadings for today Chapter 23 Threads from Network Programming vol 1 2nd ed.

Richard Stevens Chapters 11 and 12 Advanced Unix Programming 2nd ed.

Richard Stevens http://www.kohala.com/start/ CSE Department Unix machines: /class/csce713-006

NewNew Lawrence Livermore National Labs Pthreads tutorial

Hello.c, hello -args2.c

Posix Pthreads Next time performance evaluation, barriers and MPI intro

Page 3: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 3 –CSCE 713 Spring 2012

CSAPP – Bryant O’HallaronCSAPP – Bryant O’Hallaron

..

Computer Systems: A Programmers Perspective, Bryant and O’Hallaron

Page 4: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 4 –CSCE 713 Spring 2012

Books by Richard StevensBooks by Richard StevensUNIX Network Programming, Volume 2, Second Edition: Interprocess Communications

, Prentice Hall, 1999. , Prentice Hall, 1999.

UNIX Network Programming, Volume 1, Second Edition: Networking APIs: Sockets and XTI

, Prentice Hall, 1998. , Prentice Hall, 1998.

TCP/IP Illustrated, Volume 3: TCP for Transactions, HTTP, NNTP, and the UNIX Domain Protocols

, Addison-Wesley, 1996., Addison-Wesley, 1996.

TCP/IP Illustrated, Volume 2: The Implementation, Addison-Wesley, 1995., Addison-Wesley, 1995.

TCP/IP Illustrated, Volume 1: The Protocols, Addison-Wesley, 1994., Addison-Wesley, 1994.

Advanced Programming in the UNIX Environment, Addison-Wesley, 1992., Addison-Wesley, 1992.

UNIX Network Programming, Prentice Hall, 1990., Prentice Hall, 1990.

Page 5: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 5 –CSCE 713 Spring 2012

Network Programming vol 1 2nd editionNetwork Programming vol 1 2nd edition

Chapter 23. not Chapter 25; Network Prog vol 1 not APUEChapter 23. not Chapter 25; Network Prog vol 1 not APUE

23.1 Introduction 23.1 Introduction

23.2 Basic Thread Functions: Creation and Termination23.2 Basic Thread Functions: Creation and Termination

23.3 str_cli Function Using Threads23.3 str_cli Function Using Threads

23.4 TCP Echo Server Using Threads23.4 TCP Echo Server Using Threads

23.5 Thread-Specific Data23.5 Thread-Specific Data

23.6 Web Client and Simultaneous Connections (Cont.)23.6 Web Client and Simultaneous Connections (Cont.)

23.7 Mutexes: Mutual Exclusion23.7 Mutexes: Mutual Exclusion

23.8 Condition Variables23.8 Condition Variables

23.9 Web Client and Simultaneous Connections (Cont.)23.9 Web Client and Simultaneous Connections (Cont.)

23.10 Summary23.10 Summary

Note chapters 11 and 12 of APUE2 (Adv. Prog. Unix Env. 2Note chapters 11 and 12 of APUE2 (Adv. Prog. Unix Env. 2ndnd edition) edition) might be bettermight be better

Page 6: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 6 –CSCE 713 Spring 2012

POSIX Threads Programming (LLNL)POSIX Threads Programming (LLNL)

Author: Blaise Barney, Lawrence Livermore National Author: Blaise Barney, Lawrence Livermore National LaboratoryLaboratory

/usr/class/csce713-006/usr/class/csce713-006

• Code/LLNLCode/LLNL1. hello.c -

2. hello_arg2.c –

3. !? hello_arg3.c -

4. join.c

5. condvar.c

6. dotprod_serial.c

7. dotprod_mutex.c

8. stack.c

Page 7: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 7 –CSCE 713 Spring 2012

ExampleExample

/* http://en.wikipedia.org/wiki/POSIX_Threads*//* http://en.wikipedia.org/wiki/POSIX_Threads*/

#include <pthread.h>#include <pthread.h>

#include <stdio.h>#include <stdio.h>

#include <stdlib.h>#include <stdlib.h>

#include <assert.h>#include <assert.h>

#define NUM_THREADS 5#define NUM_THREADS 5

/* http://en.wikipedia.org/wiki/POSIX_Threads*/

Page 8: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 8 –CSCE 713 Spring 2012

void *TaskCode(void *argument)void *TaskCode(void *argument)

{{

int tid;int tid;

tid = *((int *) argument);tid = *((int *) argument);

printf("Hello World! It's me, thread %d!\n", tid);printf("Hello World! It's me, thread %d!\n", tid);

/* optionally: insert more useful stuff here *//* optionally: insert more useful stuff here */

return NULL;return NULL;

}}

int main (int argc, char *argv[])int main (int argc, char *argv[])

{{

pthread_t threads[NUM_THREADS];pthread_t threads[NUM_THREADS];

int thread_args[NUM_THREADS];int thread_args[NUM_THREADS];

int rc, i;int rc, i;/* http://en.wikipedia.org/wiki/POSIX_Threads*/

Page 9: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 9 –CSCE 713 Spring 2012

/* create all threads *//* create all threads */

for (i=0; i<NUM_THREADS; ++i) {for (i=0; i<NUM_THREADS; ++i) {

thread_args[i] = i;thread_args[i] = i;

printf("In main: creating thread %d\n", i);printf("In main: creating thread %d\n", i);

rc = pthread_create(&threads[i], NULL, TaskCode, (void *) rc = pthread_create(&threads[i], NULL, TaskCode, (void *) &thread_args[i]);&thread_args[i]);

assert(0 == rc);assert(0 == rc);

}}

/* wait for all threads to complete *//* wait for all threads to complete */

for (i=0; i<NUM_THREADS; ++i) {for (i=0; i<NUM_THREADS; ++i) {

rc = pthread_join(threads[i], NULL);rc = pthread_join(threads[i], NULL);

assert(0 == rc);assert(0 == rc);

}}

exit(EXIT_SUCCESS);exit(EXIT_SUCCESS);

}} /* http://en.wikipedia.org/wiki/POSIX_Threads*/

Page 10: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 10 –CSCE 713 Spring 2012

Designing Threaded ProgramsDesigning Threaded Programs

• What type of parallel programming model to use? What type of parallel programming model to use?

• Problem partitioning Problem partitioning

• Load balancing Load balancing

• Communications Communications

• Data dependencies Data dependencies

• Synchronization and race conditions Synchronization and race conditions

• Memory issues Memory issues

• I/O issues I/O issues

• Program complexity Program complexity

• Programmer effort/costs/time Programmer effort/costs/time https://computing.llnl.gov/tutorials/pthreads/

Page 11: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 11 –CSCE 713 Spring 2012

Scheduling Independent RoutinesScheduling Independent Routines

https://computing.llnl.gov/tutorials/pthreads/

Page 12: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 12 –CSCE 713 Spring 2012

Programs suitable for MultithreadingPrograms suitable for Multithreading

• Work that can be executed, or data that can be Work that can be executed, or data that can be operated on, by multiple tasks simultaneously operated on, by multiple tasks simultaneously

• Block for potentially long I/O waits Block for potentially long I/O waits

• Use many CPU cycles in some places but not others Use many CPU cycles in some places but not others

• Must respond to asynchronous events Must respond to asynchronous events

• Some work is more important than other work Some work is more important than other work (priority interrupts) (priority interrupts)

https://computing.llnl.gov/tutorials/pthreads/

Page 13: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 13 –CSCE 713 Spring 2012

Client-Server ApplicationsClient-Server Applications

Page 14: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 14 –CSCE 713 Spring 2012

Common models for threaded programsCommon models for threaded programs

• Manager/worker:Manager/worker: • a single thread, the manager assigns work to other threads,

the workers. Typically, the manager handles all input and parcels out work to the other tasks. At least two forms of the manager/worker model are common: static worker pool and dynamic worker pool.

• Pipeline:Pipeline: • a task is broken into a series of suboperations, each of

which is handled in series, but concurrently, by a different thread.

• Peer:Peer: • similar to the manager/worker model, but after the main

thread creates other threads, it participates in the work.

https://computing.llnl.gov/tutorials/pthreads/

Page 15: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 15 –CSCE 713 Spring 2012

Shared Memory ModelShared Memory Model

https://computing.llnl.gov/tutorials/pthreads/

Page 16: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 16 –CSCE 713 Spring 2012

Thread-safenessThread-safeness

Thread-safeness: in a nutshell, refers an application's Thread-safeness: in a nutshell, refers an application's ability to execute multiple threads simultaneously ability to execute multiple threads simultaneously without "clobbering" shared data or creating "race" without "clobbering" shared data or creating "race" conditions. conditions.

For example, suppose that your application creates For example, suppose that your application creates several threads, each of which makes a call to the several threads, each of which makes a call to the same library routine: same library routine: This library routine accesses/modifies a global structure or

location in memory. As each thread calls this routine it is possible that they may

try to modify this global structure/memory location at the same time.

If the routine does not employ some sort of synchronization constructs to prevent data corruption, then it is not thread-safe.

https://computing.llnl.gov/tutorials/pthreads/

Page 17: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 17 –CSCE 713 Spring 2012

The Pthreads APIThe Pthreads API

Routine Prefix Functional Group

pthread_Threads themselves and miscellaneous subroutines

pthread_attr_ Thread attributes objects

pthread_mutex_ Mutexes

pthread_mutexattr_ Mutex attributes objects.

pthread_cond_ Condition variables

pthread_condattr_ Condition attributes objects

pthread_key_ Thread-specific data keys

pthread_rwlock_ Read/write locks

pthread_barrier_ Synchronization barriers

https://computing.llnl.gov/tutorials/pthreads/

Page 18: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 18 –CSCE 713 Spring 2012

Compiling Threaded ProgramsCompiling Threaded Programs

..

Page 19: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 19 –CSCE 713 Spring 2012

Creating and Terminating ThreadsCreating and Terminating Threads

https://computing.llnl.gov/tutorials/pthreads/

Page 20: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 20 –CSCE 713 Spring 2012

Thread TerminationThread Termination

https://computing.llnl.gov/tutorials/pthreads/

Page 21: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 21 –CSCE 713 Spring 2012

Mutex VariablesMutex Variables

pthread_mutex_lock (mutex) pthread_mutex_lock (mutex)

pthread_mutex_trylock (mutex) pthread_mutex_trylock (mutex)

pthread_mutex_unlock (mutex) pthread_mutex_unlock (mutex)

https://computing.llnl.gov/tutorials/pthreads/

Page 22: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 22 –CSCE 713 Spring 2012

The pthread_mutex_lock() routine is used by a thread The pthread_mutex_lock() routine is used by a thread to acquire a lock on the specified to acquire a lock on the specified mutexmutex variable. If variable. If the mutex is already locked by another thread, this the mutex is already locked by another thread, this call will block the calling thread until the mutex is call will block the calling thread until the mutex is unlocked. unlocked.

https://computing.llnl.gov/tutorials/pthreads/

Page 23: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 23 –CSCE 713 Spring 2012

• pthread_mutex_trylock() will attempt to lock a mutex. pthread_mutex_trylock() will attempt to lock a mutex. However, if the mutex is already locked, the routine However, if the mutex is already locked, the routine will return immediately with a "busy" error code. This will return immediately with a "busy" error code. This routine may be useful in preventing deadlock routine may be useful in preventing deadlock conditions, as in a priority-inversion situation. conditions, as in a priority-inversion situation.

• pthread_mutex_unlock() will unlock a mutex if called pthread_mutex_unlock() will unlock a mutex if called by the owning thread. Calling this routine is required by the owning thread. Calling this routine is required after a thread has completed its use of protected after a thread has completed its use of protected data if other threads are to acquire the mutex for data if other threads are to acquire the mutex for their work with the protected data. An error will be their work with the protected data. An error will be returned if: returned if: • If the mutex was already unlocked• If the mutex is owned by another thread

https://computing.llnl.gov/tutorials/pthreads/

Page 24: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 24 –CSCE 713 Spring 2012

Hello.c from LLNP tutorialHello.c from LLNP tutorial#include <pthread.h>#include <pthread.h>#include <stdio.h>#include <stdio.h>#include <stdlib.h>#include <stdlib.h>#include <assert.h>#include <assert.h> #define NUM_THREADS 5#define NUM_THREADS 5 void *TaskCode(void *argument)void *TaskCode(void *argument){{ int tid;int tid; tid = *((int *) argument);tid = *((int *) argument); printf("Hello World! It's me, thread %d!\n", tid);printf("Hello World! It's me, thread %d!\n", tid);

/* optionally: insert more useful stuff here *//* optionally: insert more useful stuff here */ return NULL;return NULL;}}

https://computing.llnl.gov/tutorials/pthreads/https://computing.llnl.gov/tutorials/pthreads/

Page 25: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 25 –CSCE 713 Spring 2012

int main (int argc, char *argv[])int main (int argc, char *argv[]){{ pthread_t threads[NUM_THREADS];pthread_t threads[NUM_THREADS]; int thread_args[NUM_THREADS];int thread_args[NUM_THREADS]; int rc, i;int rc, i; /* create all threads *//* create all threads */ for (i=0; i<NUM_THREADS; ++i) {for (i=0; i<NUM_THREADS; ++i) { thread_args[i] = i;thread_args[i] = i; printf("In main: creating thread %d\n", i);printf("In main: creating thread %d\n", i); rc = pthread_create(&threads[i], NULL, rc = pthread_create(&threads[i], NULL,

TaskCode, (void *) &thread_args[i]);TaskCode, (void *) &thread_args[i]); assert(0 == rc);assert(0 == rc); }}

/* wait for all threads to complete *//* wait for all threads to complete */ for (i=0; i<NUM_THREADS; ++i) {for (i=0; i<NUM_THREADS; ++i) { rc = pthread_join(threads[i], NULL);rc = pthread_join(threads[i], NULL); assert(0 == rc);assert(0 == rc); }} exit(EXIT_SUCCESS);exit(EXIT_SUCCESS);}}

https://computing.llnl.gov/tutorials/pthreads/

Page 26: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 26 –CSCE 713 Spring 2012

Hello output - nondeterminism of timeHello output - nondeterminism of time

saluda> ./thread1saluda> ./thread1

In main: creating thread 0In main: creating thread 0

In main: creating thread 1In main: creating thread 1

Hello World! It's me, thread 0!Hello World! It's me, thread 0!

Hello World! It's me, thread 1!Hello World! It's me, thread 1!

In main: creating thread 2In main: creating thread 2

In main: creating thread 3In main: creating thread 3

Hello World! It's me, thread 2!Hello World! It's me, thread 2!

In main: creating thread 4In main: creating thread 4

Hello World! It's me, thread 3!Hello World! It's me, thread 3!

Hello World! It's me, thread 4!Hello World! It's me, thread 4!

https://computing.llnl.gov/tutorials/pthreads/

Page 27: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 27 –CSCE 713 Spring 2012

Hello_args2.c - declarationsHello_args2.c - declarations

#include <pthread.h>#include <pthread.h>

#include <stdio.h>#include <stdio.h>

#include <stdlib.h>#include <stdlib.h>

#define NUM_THREADS 8#define NUM_THREADS 8

char *messages[NUM_THREADS];char *messages[NUM_THREADS];

struct thread_datastruct thread_data

{{

int thread_id;int thread_id;

int sum;int sum;

char *message;char *message;

};};

struct thread_data thread_data_array[NUM_THREADS];struct thread_data thread_data_array[NUM_THREADS];

https://computing.llnl.gov/tutorials/pthreads/

Page 28: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 28 –CSCE 713 Spring 2012

Hello_args2.c - thread functionHello_args2.c - thread function

void *PrintHello(void *threadarg)void *PrintHello(void *threadarg)

{{

int taskid, sum;int taskid, sum;

char *hello_msg;char *hello_msg;

struct thread_data *my_data;struct thread_data *my_data;

sleep(1);sleep(1);

my_data = (struct thread_data *) threadarg;my_data = (struct thread_data *) threadarg;

taskid = my_data->thread_id;taskid = my_data->thread_id;

sum = my_data->sum;sum = my_data->sum;

hello_msg = my_data->message;hello_msg = my_data->message;

printf("Thread %d: %s Sum=%d\n", taskid, hello_msg, sumprintf("Thread %d: %s Sum=%d\n", taskid, hello_msg, sum

););

pthread_exit(NULL);pthread_exit(NULL);

}}https://computing.llnl.gov/tutorials/pthreads/

Page 29: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 29 –CSCE 713 Spring 2012

Hello_args2.c - MainHello_args2.c - Mainint main(int argc, char *argv[])int main(int argc, char *argv[])

{{

pthread_t threads[NUM_THREADS];pthread_t threads[NUM_THREADS];

int *taskids[NUM_THREADS];int *taskids[NUM_THREADS];

int rc, t, sum;int rc, t, sum;

sum=0;sum=0;

messages[0] = "English: Hello World!";messages[0] = "English: Hello World!";

messages[1] = "French: Bonjour, le monde!";messages[1] = "French: Bonjour, le monde!";

messages[2] = "Spanish: Hola al mundo";messages[2] = "Spanish: Hola al mundo";

messages[3] = "Klingon: Nuq neH!";messages[3] = "Klingon: Nuq neH!";

messages[4] = "German: Guten Tag, Welt!"; messages[4] = "German: Guten Tag, Welt!";

messages[5] = "Russian: Zdravstvytye, mir!";messages[5] = "Russian: Zdravstvytye, mir!";

messages[6] = "Japan: Sekai e konnichiwa!";messages[6] = "Japan: Sekai e konnichiwa!";

messages[7] = "Latin: Orbis, te saluto!";messages[7] = "Latin: Orbis, te saluto!";

for(t=0;t<NUM_THREADS;t++) {for(t=0;t<NUM_THREADS;t++) {

sum = sum + t;sum = sum + t;

thread_data_array[t].thread_id = t;thread_data_array[t].thread_id = t;

thread_data_array[t].sum = sum;thread_data_array[t].sum = sum;

thread_data_array[t].message = messages[t];thread_data_array[t].message = messages[t];

printf("Creating thread %d\n", t);printf("Creating thread %d\n", t);

rc = pthread_create(&threads[t], NULL, PrintHello, (void rc = pthread_create(&threads[t], NULL, PrintHello, (void

*) *)

&thread_data_array[t]);&thread_data_array[t]);https://computing.llnl.gov/tutorials/pthreads/

Page 30: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 30 –CSCE 713 Spring 2012

Hello_args2.c – main loopHello_args2.c – main loop

for(t=0;t<NUM_THREADS;t++) {for(t=0;t<NUM_THREADS;t++) {

sum = sum + t;sum = sum + t;

thread_data_array[t].thread_id = t;thread_data_array[t].thread_id = t;

thread_data_array[t].sum = sum;thread_data_array[t].sum = sum;

thread_data_array[t].message = messages[t];thread_data_array[t].message = messages[t];

printf("Creating thread %d\n", t);printf("Creating thread %d\n", t);

rc = pthread_create(&threads[t], NULL, PrintHello, (void *) rc = pthread_create(&threads[t], NULL, PrintHello, (void *)

&thread_data_array[t]);&thread_data_array[t]);

if (rc) {if (rc) {

printf("ERROR; return code from pthread_create() is %d\n", rc);printf("ERROR; return code from pthread_create() is %d\n", rc);

exit(-1);exit(-1);

}}

}}

pthread_exit(NULL);pthread_exit(NULL);

}}https://computing.llnl.gov/tutorials/pthreads/

Page 31: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 31 –CSCE 713 Spring 2012

Mutex excerpt – dotprod_mutex.cMutex excerpt – dotprod_mutex.c… … /* excerpt from code of thread_function *//* excerpt from code of thread_function */

mysum = 0;mysum = 0;

for (i=start; i<end ; i++) for (i=start; i<end ; i++)

{{

mysum += (x[i] * y[i]);mysum += (x[i] * y[i]);

}}

/*/*

Lock a mutex prior to updating the value in the sharedLock a mutex prior to updating the value in the shared

structure, and unlock it upon updating.structure, and unlock it upon updating.

*/*/

pthread_mutex_lock (&mutexsum);pthread_mutex_lock (&mutexsum);

dotstr.sum += mysum;dotstr.sum += mysum;

pthread_mutex_unlock (&mutexsum);pthread_mutex_unlock (&mutexsum);

pthread_exit((void*) 0);pthread_exit((void*) 0);https://computing.llnl.gov/tutorials/pthreads/

Page 32: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 32 –CSCE 713 Spring 2012

Mixing MPI with Pthreads: Mixing MPI with Pthreads:

Design: Design: Each MPI process typically creates and then manages N

threads, where N makes the best use of the available CPUs/node.

Finding the best value for N will vary with the platform and your application's characteristics.

For IBM SP systems with two communication adapters per node, it may prove more efficient to use two (or more) MPI tasks per node.

In general, there may be problems if multiple threads make MPI calls. The program may fail or behave unexpectedly. If MPI calls must be made from within a thread, they should be made only by one thread.

https://computing.llnl.gov/tutorials/pthreads/

Page 33: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 33 –CSCE 713 Spring 2012

LLNL MPI/Pthreads ExamplesLLNL MPI/Pthreads Examples

Compiling: Compiling: Use the appropriate MPI compile command for the platform

and language of choice Be sure to include the required Pthreads flag as shown in

the Compiling Threaded Programs section.

An example code that uses both MPI and Pthreads is An example code that uses both MPI and Pthreads is available below. The serial, threads-only, MPI-only available below. The serial, threads-only, MPI-only and MPI-with-threads versions demonstrate one and MPI-with-threads versions demonstrate one possible progression. possible progression. Serial Pthreads only MPI only MPI with pthreads makefile (for IBM SP)

https://computing.llnl.gov/tutorials/pthreads/

Page 34: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 34 –CSCE 713 Spring 2012

apue.2e/threadsapue.2e/threads

apue.2e/threads/apue.2e/threads/

apue.2e/threads/badexit2.capue.2e/threads/badexit2.c

apue.2e/threads/cleanup.capue.2e/threads/cleanup.c

apue.2e/threads/condvar.capue.2e/threads/condvar.c

apue.2e/threads/exitstatus.capue.2e/threads/exitstatus.c

apue.2e/threads/linux.mkapue.2e/threads/linux.mk

apue.2e/threads/macos.mkapue.2e/threads/macos.mk

apue.2e/threads/mutex1.capue.2e/threads/mutex1.c

apue.2e/threads/mutex2.capue.2e/threads/mutex2.c

apue.2e/threads/mutex3.capue.2e/threads/mutex3.c

apue.2e/threads/rwlock.capue.2e/threads/rwlock.c

apue.2e/threads/threadid.capue.2e/threads/threadid.c

http://www.apuebook.com/

Page 35: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 35 –CSCE 713 Spring 2012

apue.2e/threadctlapue.2e/threadctl

apue.2e/threadctl/detach.capue.2e/threadctl/detach.c

apue.2e/threadctl/getenv1.capue.2e/threadctl/getenv1.c

apue.2e/threadctl/getenv2.capue.2e/threadctl/getenv2.c

apue.2e/threadctl/getenv3.capue.2e/threadctl/getenv3.c

apue.2e/threadctl/linux.mkapue.2e/threadctl/linux.mk

apue.2e/threadctl/macos.mkapue.2e/threadctl/macos.mk

apue.2e/threadctl/suspend.capue.2e/threadctl/suspend.c

apue.2e/threadctl/timeout.capue.2e/threadctl/timeout.c

http://www.apuebook.com/

Page 36: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 36 –CSCE 713 Spring 2012

Ten Questions with David Butenhof about Parallel Programming and POSIX Threads

Ten Questions with David Butenhof about Parallel Programming and POSIX Threads

Michael: Are there any specific tools you would like to Michael: Are there any specific tools you would like to recommend to people who want to program in POSIX recommend to people who want to program in POSIX Threads? IDEs? Editors? Debuggers? Profilers? Threads? IDEs? Editors? Debuggers? Profilers? Correctness Tools? Any others?Correctness Tools? Any others?

David: Tru64’s ladebug and Visual Threads were David: Tru64’s ladebug and Visual Threads were awesome tools, and ATOM allowed constructing awesome tools, and ATOM allowed constructing simple analyzers. Nobody else really has anything simple analyzers. Nobody else really has anything that comprehensive, despite various gdb add-ons. that comprehensive, despite various gdb add-ons. (Then again, Intel has ladebug… but hasn’t really (Then again, Intel has ladebug… but hasn’t really done anything with it.) Totalview is a great portable done anything with it.) Totalview is a great portable thread debugging environment, although the GUI is a thread debugging environment, although the GUI is a bit “opaque”.bit “opaque”.

http://www.thinkingparallel.com

Page 37: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 37 –CSCE 713 Spring 2012

Debugging - gdbDebugging - gdb

4.10 Debugging Programs with Multiple Threads4.10 Debugging Programs with Multiple Threads

gdb provides these facilities for debugging multi-thread gdb provides these facilities for debugging multi-thread programs: programs:

automatic notification of new threads automatic notification of new threads

`thread `thread threadnothreadno', a command to switch among threads ', a command to switch among threads

`info threads', a command to inquire about existing `info threads', a command to inquire about existing threads threads

`thread apply [`thread apply [threadnothreadno] [] [allall] ] argsargs', a command to apply ', a command to apply a command to a list of threads a command to a list of threads

thread-specific breakpoints thread-specific breakpoints

Page 38: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 38 –CSCE 713 Spring 2012

`set print thread-events', which controls printing of `set print thread-events', which controls printing of messages on thread start and exit. messages on thread start and exit.

`set libthread-db-search-path `set libthread-db-search-path pathpath', which lets the user ', which lets the user specify which libthread_db to use if the default specify which libthread_db to use if the default choice isn't compatible with the program. choice isn't compatible with the program.

Warning:Warning: These facilities are not yet available on every These facilities are not yet available on every gdb configuration where the operating system gdb configuration where the operating system supports threads. If your gdb does not support supports threads. If your gdb does not support threads, these commands have no effect. For threads, these commands have no effect. For example, a system without thread support shows no example, a system without thread support shows no output from `info threads', and always rejects the output from `info threads', and always rejects the thread command, like this: (gdb) info threads (gdb) thread command, like this: (gdb) info threads (gdb) thread 1 Thread ID 1 not known. Use the "info thread 1 Thread ID 1 not known. Use the "info threads" command to see the IDs of currently known threads" command to see the IDs of currently known threads. threads.

Page 39: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 39 –CSCE 713 Spring 2012

http://numericalmethods.eng.usf.edu

Gauss-Seidel MethodGauss-Seidel Method

AlgorithmA set of n equations and n unknowns:

11313212111 ... bxaxaxaxa nn

2323222121 ... bxaxaxaxa n2n

nnnnnnn bxaxaxaxa ...332211

. . . . . .

If: the diagonal elements are non-zero

Rewrite each equation solving for the corresponding unknown

ex:

First equation, solve for x1

Second equation, solve for x2

Page 40: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 40 –CSCE 713 Spring 2012

http://numericalmethods.eng.usf.edu

Gauss-Seidel MethodGauss-Seidel Method

AlgorithmRewriting each equation

11

131321211 a

xaxaxacx nn

nn

nnnnnnn

nn

nnnnnnnnnn

nn

a

xaxaxacx

a

xaxaxaxacx

a

xaxaxacx

11,2211

1,1

,122,122,111,111

22

232312122

From Equation 1

From equation 2

From equation n-1

From equation n

Page 41: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 41 –CSCE 713 Spring 2012

http://numericalmethods.eng.usf.edu

Gauss-Seidel MethodGauss-Seidel Method

AlgorithmGeneral Form of each equation

11

11

11

1 a

xac

x

n

jj

jj

22

21

22

2 a

xac

x

j

n

jj

j

1,1

11

,11

1

nn

n

njj

jjnn

n a

xac

x

nn

n

njj

jnjn

n a

xac

x

1

Page 42: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 42 –CSCE 713 Spring 2012

BarriersBarriers

Synchronize threads at a point e.g., after an iteration.Synchronize threads at a point e.g., after an iteration.

Implementation of barrierImplementation of barrier

1.1. Mutex to control access to int variable threads_finishedMutex to control access to int variable threads_finished threads_fini = num_threads; /* initial value at start of iteration */

2.2. As thread reaches boundary As thread reaches boundary Grab mutex decrement threads_fini If count == 0 start next iteration Free mutex

3.3. Busy wait while (threads_fini != 0) ;Busy wait while (threads_fini != 0) ;

Page 43: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 43 –CSCE 713 Spring 2012

BusyWaitBusyWait

ProblemsProblems

SolutionsSolutions

1.1. Semahore sets (next time)Semahore sets (next time)

Page 44: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 44 –CSCE 713 Spring 2012

Threads programming AssignmentThreads programming Assignment

1.1. Matrix addition (embarassingly parallel)Matrix addition (embarassingly parallel)

2.2. VersionsVersionsa. Sequential

b. Sequential with blocking factor

c. Sequential Read without conversions

d. Multi threaded passing number of threads as command line argument (args.c code should be distributed as an example)

3.3. Plot of several runsPlot of several runs

4.4. Next timeNext time

Page 45: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 45 –CSCE 713 Spring 2012

Time in the Computer WorldTime in the Computer World

..

Computer Systems: A Programmers Perspective, Bryant and O’Hallaron

Page 46: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 46 –CSCE 713 Spring 2012

Time CommandTime Command

RealReal

User User

SystemSystem

Page 47: Lecture 3 Posix Threads Topics PthreadsReadings January 17, 2012 CSCE 713 Advanced Computer Architecture

– 47 –CSCE 713 Spring 2012

#include <sys/times.h>#include <sys/times.h>

struct tmsstruct tms

clock_t tms_utime; /* user time * /clock_t tms_utime; /* user time * /

clock_t tms_s time; /* system time * /clock_t tms_s time; /* system time * /

clock_t tms_cutime; /* user time of reaped children */clock_t tms_cutime; /* user time of reaped children */

clock_t tms_cstime; /* system time of reaped children */clock_t tms_cstime; /* system time of reaped children */

} ;} ;

clock_t times(struct tms *buf);clock_t times(struct tms *buf);

Returns: number of clock ticks elapsed since system startedReturns: number of clock ticks elapsed since system started

Computer Systems: A Programmers Perspective, Bryant and O’Hallaron