Docsity

Pthreads - Parallel Computing - Lecture Slides, Slides of Parallel Computing and Programming

Partial preview of the text

Download Pthreads - Parallel Computing - Lecture Slides and more Slides Parallel Computing and Programming in PDF only on Docsity!

Programming Shared-memory

Platforms with Pthreads

Topics for Today

**- Shared-address space programming model taxonomy

  • The POSIX thread API (Pthreads)
  • Synchronization primitives in Pthreads**

—mutexes

—condition variables

—reader/writer locks

- Thread-specific data

Thread

**- A single, sequential stream of control in a program

  • Logical machine model**

—flat global memory shared among all threads

—local stack of frames for each thread’s active procedures

Why Threads?

- Portable, widely-available programming model

—use on both serial and parallel systems

- Useful for hiding latency

—e.g. latency due to I/O, communication

- Useful for scheduling and load balancing

—especially for dynamic concurrency

- Relatively easy to program

—significantly easier than message-passing!

PThread Creation

Asynchronously invoke thread_function in a new thread

! #include <pthread.h>

! int pthread_create(

! pthread_t thread_handle, / returns handle here */

! const pthread_attr_t *attribute,

! void * (*thread_function)(void *),

! void arg); / single argument; perhaps a structure */

attribute created by pthread_attr_init

contains details about

  • whether scheduling policy is inherited or explicit
  • scheduling policy, scheduling priority
  • stack size, stack guard region size

Thread Attributes

- Detach state

—PTHREAD_CREATE_DETACHED, PTHREAD_CREATE_JOINABLE

  • **reclaim storage at termination (detached) or retain (joinable)
  • Scheduling policy**

—SCHED_OTHER: standard round robin (priority must be 0)

—SCHED_FIFO, SCHED_RR: real time policies

  • **FIFO: re-enter priority list at head; RR: re-enter priority list at tail
  • Scheduling parameters**

—only priority

- Inherit scheduling policy

—PTHREAD_INHERIT_SCHED, PTHREAD_EXPLICIT_SCHED

- Thread scheduling scope

—PTHREAD_SCOPE_SYSTEM, PTHREAD_SCOPE_PROCESS

- Stack size

Running Example: Monte Carlo Estimation of Pi Approximate Pi

—generate random

points with x, y ∈

[-0.5, 0.5]

—test if point inside

the circle, i.e.,

x

2

+ y

2

2

—ratio of circle to

square =

πr

2

/ 4r

2

—π ≈ 4 * (number of

points inside the

circle) / (number of

points total)

default attributes 11 Example: Creation and Termination (main) #include <pthread.h> #include <stdlib.h> #define NUM_THREADS 32 void *compute_pi (void ); ... int main(...) { ... pthread_t p_threads[NUM_THREADS]; pthread_attr_t attr; pthread_attr_init(&attr); for (i=0; i< NUM_THREADS; i++) { hits[i] = i; pthread_create(&p_threads[i], &attr, compute_pi, (void) &hits[i]); } for (i=0; i< NUM_THREADS; i++) { pthread_join(p_threads[i], NULL); total_hits += hits[i]; } ... thread function thread argument

Programming and Performance Notes

- Performance on a 4-processor SGI Origin

—3.91 fold speedup at 32 threads

—parallel efficiency of 0.

- Code carefully^ minimizes^ false-sharing^ of cache lines

—false sharing

  • multiple processors access words in the same cache line
  • at least one processor updates a word in the cache line
  • no word updated by one processor is accessed by another

Example: Thread Function (compute_pi) void *compute_pi (void *s) { int seed, i, *hit_pointer; double x_coord, y_coord; int local_hits; hit_pointer = (int *) s; seed = *hit_pointer; local_hits = 0; for (i = 0; i < sample_points_per_thread; i++) { x_coord = (double)(rand_r(&seed))/((1<<30)-1) - 0.5; y_coord =(double)(rand_r(&seed))/((1<<30)-1) - 0.5; if ((x_coord * x_coord + y_coord * y_coord) < 0.25) local_hits++; } *hit_pointer = local_hits; pthread_exit(0); } avoid false sharing by using a local accumulator

Data Races in a Pthreads Program

Consider

/* threads compete to update global variable best_cost */

if (my_cost < best_cost)

best_cost = my_cost;

—two threads

—initial value of best_cost is 100

—values of my_cost are 50 and 75 for threads t1 and t

**- After execution, best_cost could be 50 or 75

  • 75 does not correspond to any serialization of the threads**

atomic operation 17

Critical Sections and Mutual Exclusion

- Critical section = must execute code by only one thread at a time / threads compete to update global variable best_cost / if (my_cost < best_cost) best_cost = my_cost; - Mutex locks enforce critical sections in Pthreads —mutex lock states: locked and unlocked **—only one thread can lock a mutex lock at any particular time

  • Using mutex locks** —request lock before executing critical section —enter critical section when lock granted **—release lock when leaving critical section
  • Operations** *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) created by pthread_mutex_attr_init specify type: normal, recursive, errorcheck

Example: Reduction Using Mutex Locks

pthread_mutex_t cost_lock;

int main() { ... pthread_mutex_init(&cost_lock, NULL); ... } void *find_best(void list_ptr) { ... pthread_mutex_lock(&cost_lock); / lock the mutex */

if (my_cost < best_cost)

best_cost = my_cost;

pthread_mutex_unlock(&cost_lock); /* unlock the mutex */ } critical section use default (normal) lock type

Producer-Consumer Using Mutex Locks

Constraints

- Producer thread

—must not overwrite the shared buffer until previous task has

picked up by a consumer

- Consumer thread

—must not pick up a task until one is available in the queue

—must pick up tasks one at a time