SlideShare a Scribd company logo
1
Shared Memory
Programming with
Pthreads & OpenMP
Dilum Bandara
Dilum.Bandara@uom.lk
Slides extended from
An Introduction to Parallel Programming by
Peter Pacheco
2
Shared Memory System
Copyright © 2010, Elsevier Inc. All rights Reserved
3
POSIX® Threads
 Also known as Pthreads
 Standard for Unix-like operating systems
 Library that can be linked with C programs
 Specifies an API for multi-threaded
programming
Copyright © 2010, Elsevier Inc. All rights Reserved
4
Hello World!
Copyright © 2010, Elsevier Inc. All rights Reserved
Declares various Pthreads
functions, constants, types, etc.
5
Hello World! (Cont.)
Copyright © 2010, Elsevier Inc. All rights Reserved
6
Hello World! (Cont.)
Copyright © 2010, Elsevier Inc. All rights Reserved
7
Compiling a Pthread program
Copyright © 2010, Elsevier Inc. All rights Reserved
gcc −g −Wall −o pth_hello pth_hello.c −lpthread
Link Pthreads library
8
Running a Pthreads program
Copyright © 2010, Elsevier Inc. All rights Reserved
. /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 3 of 4
Hello from thread 2 of 4
Hello from thread 1 of 4
9
Running the Threads
Copyright © 2010, Elsevier Inc. All rights Reserved
Main thread forks & joins 2 threads
10
Global Variables
 Can introduce subtle & confusing bugs!
 Use them only when they are essential
 Shared variables
Copyright © 2010, Elsevier Inc. All rights Reserved
11
Starting Threads
Copyright © 2010, Elsevier Inc. All rights Reserved
pthread.h
pthread_t
int pthread_create (
pthread_t* thread_p, /* out */
const pthread_attr_t* attr_p, /* in */
void* (*start_routine) (void), /* in */
void* arg_p); /* in */
One object for
each thread
We ignore return value
from pthread_create
12
Function Started by pthread_create
 Function start by pthread_create should have
following 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, return value of thread_function can
point to a list of one or more values
Copyright © 2010, Elsevier Inc. All rights Reserved
13
Stopping Threads
 Single call to pthread_join will wait for
thread associated with pthread_t object to
complete
 Suspend execution of calling thread until
target thread terminates, unless it has already
terminated
 Call pthread_join once for each thread
int pthread_join(
pthread_t* thread /* in */ ,
void** ret_val_p /* out */ ) ;
Copyright © 2010, Elsevier Inc. All rights Reserved
14
Matrix-Vector Multiplication in
Pthreads
Copyright © 2010, Elsevier Inc. All rights Reserved
15
Serial Pseudo-code
Copyright © 2010, Elsevier Inc. All rights Reserved
16
Using 3 Pthreads
 Assign each row to a separate thread
 Suppose 6x6 matrix & 3 threads
Copyright © 2010, Elsevier Inc. All rights Reserved
Thread 0
General case
17
Pthreads Matrix-Vector Multiplication
Copyright © 2010, Elsevier Inc. All rights Reserved
18
Estimating π
Copyright © 2010, Elsevier Inc. All rights Reserved
19
Thread Function for Computing π
Copyright © 2010, Elsevier Inc. All rights Reserved
20
Using a dual core processor
Copyright © 2010, Elsevier Inc. All rights Reserved
As we increase n, estimate with 1
thread gets better & better
2 thread case produce different
answers in different runs
Why?
21
Pthreads Global Sum with Busy-Waiting
Copyright © 2010, Elsevier Inc. All rights Reserved
Shared variable
22
Mutexes
 Make sure only 1 thread in critical region
 Pthreads standard includes a special type
for mutexes: pthread_mutex_t
Copyright © 2010, Elsevier Inc. All rights Reserved
23
Mutexes
 Lock
 To gain access to a critical section
 Unlock
 When a thread is finished executing code in a
critical section
 Termination
 When a program finishes using a mutex
Copyright © 2010, Elsevier Inc. All rights Reserved
24
Global Sum Function Using a Mutex
Copyright © 2010, Elsevier Inc. All rights Reserved
25
Global Sum Function Using a Mutex (Cont.)
Copyright © 2010, Elsevier Inc. All rights Reserved
26
Busy-Waiting vs. Mutex
Copyright © 2010, Elsevier Inc. All rights Reserved
Run-times (in seconds) of π programs using n = 108
terms on a system with 2x4-core processors
27
Semaphores
Copyright © 2010, Elsevier Inc. All rights Reserved
Semaphores are not part of Pthreads;
you need to add this
28
Read-Write Locks
 While controlling access to a large, shared
data structure
 Example
 Suppose shared data structure is a sorted
linked list of ints, & operations of interest are
Member, Insert, & Delete
Copyright © 2010, Elsevier Inc. All rights Reserved
29
Linked Lists
Copyright © 2010, Elsevier Inc. All rights Reserved
30
Linked List Membership
Copyright © 2010, Elsevier Inc. All rights Reserved
31
Inserting New Node Into a List
Copyright © 2010, Elsevier Inc. All rights Reserved
32
Inserting New Node Into a List (Cont.)
Copyright © 2010, Elsevier Inc. All rights Reserved
33
Deleting a Node From a Linked List
Copyright © 2010, Elsevier Inc. All rights Reserved
34
Deleting a Node From a Linked List (Cont.)
Copyright © 2010, Elsevier Inc. All rights Reserved
35
Multi-Threaded Linked List
 To share access to the list, we can define
head_p to be a global variable
 This will simplify function headers for Member,
Insert, & Delete
 Because we won’t need to pass in either
head_p or a pointer to head_p: we’ll only need
to pass in the value of interest
Copyright © 2010, Elsevier Inc. All rights Reserved
36
Simultaneous Access by 2 Threads
Copyright © 2010, Elsevier Inc. All rights Reserved
37
Solution #1
 Simply lock the list any time that a thread
attempts to access it
 Call to each of the 3 functions can be
protected by a mutex
Copyright © 2010, Elsevier Inc. All rights Reserved
In place of calling Member(value).
38
Issues
 Serializing access to the list
 If vast majority of our operations are calls
to Member
 We fail to exploit opportunity for parallelism
 If most of our operations are calls to Insert
& Delete
 This may be the best solution
Copyright © 2010, Elsevier Inc. All rights Reserved
39
Solution #2
 Instead of locking entire list, we could try to
lock individual nodes
 A “finer-grained” approach
Copyright © 2010, Elsevier Inc. All rights Reserved
40
Issues
 Much more complex than original Member
function
 Much slower
 Because each time a node is accessed, a
mutex must be locked & unlocked
 Addition of a mutex field to each node
substantially increase memory needed for the
list
Copyright © 2010, Elsevier Inc. All rights Reserved
41
Pthreads Read-Write Locks
 Neither multi-threaded linked lists exploits
potential for simultaneous access to any node by
threads that are executing Member
 1st solution only allows 1 thread to access the entire
list at any instant
 2nd only allows 1 thread to access any given node at
any instant
 Read-write lock is somewhat like a mutex except
that it provides 2 lock functions
 1st locks the read-write lock for reading
 2nd locks it for writing
Copyright © 2010, Elsevier Inc. All rights Reserved
42
Pthreads Read-Write Locks (Cont.)
 Multiple threads can simultaneously obtain lock
by calling read-lock function
 While only 1 thread can obtain lock by calling
write-lock function
 Thus
 If any thread owns lock for reading, any thread that
wants to obtain a lock for writing will be blocked
 If any thread owns lock for writing, any threads that
want to obtain lock for reading or writing will be
blocked
Copyright © 2010, Elsevier Inc. All rights Reserved
43
Protecting Our Linked List Functions
Copyright © 2010, Elsevier Inc. All rights Reserved
44
Linked List Performance
Copyright © 2010, Elsevier Inc. All rights Reserved
100,000 ops/thread
99.9% Member
0.05% Insert
0.05% Delete
100,000 ops/thread
80% Member
10% Insert
10% Delete
45
OpenMP
Copyright © 2010, Elsevier Inc. All rights Reserved
46
OpenMP
 High-level API for shared-memory parallel
programming
 MP = multiprocessing
 Use Pragmas
 Special preprocessor instructions
 #pragma
 Typically added to support behaviors that aren’t
part of the basic C specification
 Compilers that don’t support pragmas ignore
them
Copyright © 2010, Elsevier Inc. All rights Reserved
47
Copyright © 2010, Elsevier Inc. All rights Reserved
48
Compiling & Running
Copyright © 2010, Elsevier Inc. All rights Reserved
gcc −g −Wall −fopenmp −o omp_hello omp_hello.c
. / omp_hello 4
compiling
running with 4 threads
Hello from thread 0 of 4
Hello from thread 1 of 4
Hello from thread 2 of 4
Hello from thread 3 of 4 Hello from thread 1 of 4
Hello from thread 2 of 4
Hello from thread 0 of 4
Hello from thread 3 of 4
Hello from thread 3 of 4
Hello from thread 1 of 4
Hello from thread 2 of 4
Hello from thread 0 of 4
possible
outcomes
49
OpenMp pragmas
Copyright © 2010, Elsevier Inc. All rights Reserved
 # pragma omp parallel
 Most basic parallel directive
 Original thread is called master
 Additional threads are called slaves
 Original thread & new threads called a team
50
Clause
 Text that modifies a directive
 num_threads clause can be added to a
parallel directive
 Allows programmer to specify no of
threads that should execute following block
Copyright © 2010, Elsevier Inc. All rights Reserved
# pragma omp parallel num_threads ( thread_count )
51
Be Aware…
 There may be system-defined limitations on
number of threads that a program can start
 OpenMP standard doesn’t guarantee that this
will actually start thread_count threads
 Most current systems can start hundreds or even
1,000s of threads
 Unless we’re trying to start a lot of threads, we
will almost always get desired no of threads
Copyright © 2010, Elsevier Inc. All rights Reserved
52
Mutual Exclusion
Copyright © 2010, Elsevier Inc. All rights Reserved
# pragma omp critical
{
global_result += my_result ;
}
only 1 thread can execute following
structured block at a time
53
Trapezoidal Rule
Copyright © 2010, Elsevier Inc. All rights Reserved
Serial algorithm
54
Assignment of Trapezoids to Threads
Copyright © 2010, Elsevier Inc. All rights Reserved
55
Copyright © 2010, Elsevier Inc. All rights Reserved
56
Copyright © 2010, Elsevier Inc. All rights Reserved

More Related Content

PPT
Shared Memory Programming with Pthreads (1).ppt
PPTX
6-9-2017-slides-vFinal.pptx
PDF
20100730 phpstudy
PPT
PPT
Md09 multithreading
PPTX
Multithreading in java
PDF
Pthread Library
Shared Memory Programming with Pthreads (1).ppt
6-9-2017-slides-vFinal.pptx
20100730 phpstudy
Md09 multithreading
Multithreading in java
Pthread Library

Similar to Shared Memory Programming with Pthreads and OpenMP (20)

PPT
Operating System Chapter 4 Multithreaded programming
PPT
Multithreading
PPT
Intro To .Net Threads
PPTX
P-Threads
PPTX
Distributed Memory Programming with MPI
PDF
PDF
Delphi - Howto Threads
PPTX
PDCCLECTUREE.pptx
PDF
MultiThreading in Python
PPTX
Multi-threaded Programming in JAVA
PPT
Introto netthreads-090906214344-phpapp01
PPTX
Threads
PDF
Socket programming-in-python
PPT
Threads
PPTX
The Veil-Framework
PDF
Threads operating system slides easy understand
PDF
Ppl for students unit 4 and 5
PDF
Ppl for students unit 4 and 5
PPTX
Operating System Chapter 4 Multithreaded programming
Multithreading
Intro To .Net Threads
P-Threads
Distributed Memory Programming with MPI
Delphi - Howto Threads
PDCCLECTUREE.pptx
MultiThreading in Python
Multi-threaded Programming in JAVA
Introto netthreads-090906214344-phpapp01
Threads
Socket programming-in-python
Threads
The Veil-Framework
Threads operating system slides easy understand
Ppl for students unit 4 and 5
Ppl for students unit 4 and 5
Ad

More from Dilum Bandara (20)

PPTX
Designing for Multiple Blockchains in Industry Ecosystems
PPTX
Introduction to Machine Learning
PPTX
Time Series Analysis and Forecasting in Practice
PPTX
Introduction to Dimension Reduction with PCA
PPTX
Introduction to Descriptive & Predictive Analytics
PPTX
Introduction to Concurrent Data Structures
PPTX
Hard to Paralelize Problems: Matrix-Vector and Matrix-Matrix
PPTX
Introduction to Map-Reduce Programming with Hadoop
PPTX
Embarrassingly/Delightfully Parallel Problems
PPTX
Introduction to Warehouse-Scale Computers
PPTX
Introduction to Thread Level Parallelism
PPTX
CPU Memory Hierarchy and Caching Techniques
PPTX
Data-Level Parallelism in Microprocessors
PDF
Instruction Level Parallelism – Hardware Techniques
PPTX
Instruction Level Parallelism – Compiler Techniques
PPTX
CPU Pipelining and Hazards - An Introduction
PPTX
Advanced Computer Architecture – An Introduction
PPTX
High Performance Networking with Advanced TCP
PPTX
Introduction to Content Delivery Networks
PPTX
Peer-to-Peer Networking Systems and Streaming
Designing for Multiple Blockchains in Industry Ecosystems
Introduction to Machine Learning
Time Series Analysis and Forecasting in Practice
Introduction to Dimension Reduction with PCA
Introduction to Descriptive & Predictive Analytics
Introduction to Concurrent Data Structures
Hard to Paralelize Problems: Matrix-Vector and Matrix-Matrix
Introduction to Map-Reduce Programming with Hadoop
Embarrassingly/Delightfully Parallel Problems
Introduction to Warehouse-Scale Computers
Introduction to Thread Level Parallelism
CPU Memory Hierarchy and Caching Techniques
Data-Level Parallelism in Microprocessors
Instruction Level Parallelism – Hardware Techniques
Instruction Level Parallelism – Compiler Techniques
CPU Pipelining and Hazards - An Introduction
Advanced Computer Architecture – An Introduction
High Performance Networking with Advanced TCP
Introduction to Content Delivery Networks
Peer-to-Peer Networking Systems and Streaming
Ad

Recently uploaded (20)

PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPT
Introduction Database Management System for Course Database
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
ai tools demonstartion for schools and inter college
PPTX
Transform Your Business with a Software ERP System
PDF
medical staffing services at VALiNTRY
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Understanding Forklifts - TECH EHS Solution
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Odoo Companies in India – Driving Business Transformation.pdf
Introduction Database Management System for Course Database
wealthsignaloriginal-com-DS-text-... (1).pdf
Reimagine Home Health with the Power of Agentic AI​
ai tools demonstartion for schools and inter college
Transform Your Business with a Software ERP System
medical staffing services at VALiNTRY
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Operating system designcfffgfgggggggvggggggggg
Computer Software and OS of computer science of grade 11.pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Understanding Forklifts - TECH EHS Solution
How to Choose the Right IT Partner for Your Business in Malaysia
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PTS Company Brochure 2025 (1).pdf.......
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
How to Migrate SBCGlobal Email to Yahoo Easily
Digital Systems & Binary Numbers (comprehensive )
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf

Shared Memory Programming with Pthreads and OpenMP

  • 1. 1 Shared Memory Programming with Pthreads & OpenMP Dilum Bandara Dilum.Bandara@uom.lk Slides extended from An Introduction to Parallel Programming by Peter Pacheco
  • 2. 2 Shared Memory System Copyright © 2010, Elsevier Inc. All rights Reserved
  • 3. 3 POSIX® Threads  Also known as Pthreads  Standard for Unix-like operating systems  Library that can be linked with C programs  Specifies an API for multi-threaded programming Copyright © 2010, Elsevier Inc. All rights Reserved
  • 4. 4 Hello World! Copyright © 2010, Elsevier Inc. All rights Reserved Declares various Pthreads functions, constants, types, etc.
  • 5. 5 Hello World! (Cont.) Copyright © 2010, Elsevier Inc. All rights Reserved
  • 6. 6 Hello World! (Cont.) Copyright © 2010, Elsevier Inc. All rights Reserved
  • 7. 7 Compiling a Pthread program Copyright © 2010, Elsevier Inc. All rights Reserved gcc −g −Wall −o pth_hello pth_hello.c −lpthread Link Pthreads library
  • 8. 8 Running a Pthreads program Copyright © 2010, Elsevier Inc. All rights Reserved . /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 3 of 4 Hello from thread 2 of 4 Hello from thread 1 of 4
  • 9. 9 Running the Threads Copyright © 2010, Elsevier Inc. All rights Reserved Main thread forks & joins 2 threads
  • 10. 10 Global Variables  Can introduce subtle & confusing bugs!  Use them only when they are essential  Shared variables Copyright © 2010, Elsevier Inc. All rights Reserved
  • 11. 11 Starting Threads Copyright © 2010, Elsevier Inc. All rights Reserved pthread.h pthread_t int pthread_create ( pthread_t* thread_p, /* out */ const pthread_attr_t* attr_p, /* in */ void* (*start_routine) (void), /* in */ void* arg_p); /* in */ One object for each thread We ignore return value from pthread_create
  • 12. 12 Function Started by pthread_create  Function start by pthread_create should have following 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, return value of thread_function can point to a list of one or more values Copyright © 2010, Elsevier Inc. All rights Reserved
  • 13. 13 Stopping Threads  Single call to pthread_join will wait for thread associated with pthread_t object to complete  Suspend execution of calling thread until target thread terminates, unless it has already terminated  Call pthread_join once for each thread int pthread_join( pthread_t* thread /* in */ , void** ret_val_p /* out */ ) ; Copyright © 2010, Elsevier Inc. All rights Reserved
  • 14. 14 Matrix-Vector Multiplication in Pthreads Copyright © 2010, Elsevier Inc. All rights Reserved
  • 15. 15 Serial Pseudo-code Copyright © 2010, Elsevier Inc. All rights Reserved
  • 16. 16 Using 3 Pthreads  Assign each row to a separate thread  Suppose 6x6 matrix & 3 threads Copyright © 2010, Elsevier Inc. All rights Reserved Thread 0 General case
  • 17. 17 Pthreads Matrix-Vector Multiplication Copyright © 2010, Elsevier Inc. All rights Reserved
  • 18. 18 Estimating π Copyright © 2010, Elsevier Inc. All rights Reserved
  • 19. 19 Thread Function for Computing π Copyright © 2010, Elsevier Inc. All rights Reserved
  • 20. 20 Using a dual core processor Copyright © 2010, Elsevier Inc. All rights Reserved As we increase n, estimate with 1 thread gets better & better 2 thread case produce different answers in different runs Why?
  • 21. 21 Pthreads Global Sum with Busy-Waiting Copyright © 2010, Elsevier Inc. All rights Reserved Shared variable
  • 22. 22 Mutexes  Make sure only 1 thread in critical region  Pthreads standard includes a special type for mutexes: pthread_mutex_t Copyright © 2010, Elsevier Inc. All rights Reserved
  • 23. 23 Mutexes  Lock  To gain access to a critical section  Unlock  When a thread is finished executing code in a critical section  Termination  When a program finishes using a mutex Copyright © 2010, Elsevier Inc. All rights Reserved
  • 24. 24 Global Sum Function Using a Mutex Copyright © 2010, Elsevier Inc. All rights Reserved
  • 25. 25 Global Sum Function Using a Mutex (Cont.) Copyright © 2010, Elsevier Inc. All rights Reserved
  • 26. 26 Busy-Waiting vs. Mutex Copyright © 2010, Elsevier Inc. All rights Reserved Run-times (in seconds) of π programs using n = 108 terms on a system with 2x4-core processors
  • 27. 27 Semaphores Copyright © 2010, Elsevier Inc. All rights Reserved Semaphores are not part of Pthreads; you need to add this
  • 28. 28 Read-Write Locks  While controlling access to a large, shared data structure  Example  Suppose shared data structure is a sorted linked list of ints, & operations of interest are Member, Insert, & Delete Copyright © 2010, Elsevier Inc. All rights Reserved
  • 29. 29 Linked Lists Copyright © 2010, Elsevier Inc. All rights Reserved
  • 30. 30 Linked List Membership Copyright © 2010, Elsevier Inc. All rights Reserved
  • 31. 31 Inserting New Node Into a List Copyright © 2010, Elsevier Inc. All rights Reserved
  • 32. 32 Inserting New Node Into a List (Cont.) Copyright © 2010, Elsevier Inc. All rights Reserved
  • 33. 33 Deleting a Node From a Linked List Copyright © 2010, Elsevier Inc. All rights Reserved
  • 34. 34 Deleting a Node From a Linked List (Cont.) Copyright © 2010, Elsevier Inc. All rights Reserved
  • 35. 35 Multi-Threaded Linked List  To share access to the list, we can define head_p to be a global variable  This will simplify function headers for Member, Insert, & Delete  Because we won’t need to pass in either head_p or a pointer to head_p: we’ll only need to pass in the value of interest Copyright © 2010, Elsevier Inc. All rights Reserved
  • 36. 36 Simultaneous Access by 2 Threads Copyright © 2010, Elsevier Inc. All rights Reserved
  • 37. 37 Solution #1  Simply lock the list any time that a thread attempts to access it  Call to each of the 3 functions can be protected by a mutex Copyright © 2010, Elsevier Inc. All rights Reserved In place of calling Member(value).
  • 38. 38 Issues  Serializing access to the list  If vast majority of our operations are calls to Member  We fail to exploit opportunity for parallelism  If most of our operations are calls to Insert & Delete  This may be the best solution Copyright © 2010, Elsevier Inc. All rights Reserved
  • 39. 39 Solution #2  Instead of locking entire list, we could try to lock individual nodes  A “finer-grained” approach Copyright © 2010, Elsevier Inc. All rights Reserved
  • 40. 40 Issues  Much more complex than original Member function  Much slower  Because each time a node is accessed, a mutex must be locked & unlocked  Addition of a mutex field to each node substantially increase memory needed for the list Copyright © 2010, Elsevier Inc. All rights Reserved
  • 41. 41 Pthreads Read-Write Locks  Neither multi-threaded linked lists exploits potential for simultaneous access to any node by threads that are executing Member  1st solution only allows 1 thread to access the entire list at any instant  2nd only allows 1 thread to access any given node at any instant  Read-write lock is somewhat like a mutex except that it provides 2 lock functions  1st locks the read-write lock for reading  2nd locks it for writing Copyright © 2010, Elsevier Inc. All rights Reserved
  • 42. 42 Pthreads Read-Write Locks (Cont.)  Multiple threads can simultaneously obtain lock by calling read-lock function  While only 1 thread can obtain lock by calling write-lock function  Thus  If any thread owns lock for reading, any thread that wants to obtain a lock for writing will be blocked  If any thread owns lock for writing, any threads that want to obtain lock for reading or writing will be blocked Copyright © 2010, Elsevier Inc. All rights Reserved
  • 43. 43 Protecting Our Linked List Functions Copyright © 2010, Elsevier Inc. All rights Reserved
  • 44. 44 Linked List Performance Copyright © 2010, Elsevier Inc. All rights Reserved 100,000 ops/thread 99.9% Member 0.05% Insert 0.05% Delete 100,000 ops/thread 80% Member 10% Insert 10% Delete
  • 45. 45 OpenMP Copyright © 2010, Elsevier Inc. All rights Reserved
  • 46. 46 OpenMP  High-level API for shared-memory parallel programming  MP = multiprocessing  Use Pragmas  Special preprocessor instructions  #pragma  Typically added to support behaviors that aren’t part of the basic C specification  Compilers that don’t support pragmas ignore them Copyright © 2010, Elsevier Inc. All rights Reserved
  • 47. 47 Copyright © 2010, Elsevier Inc. All rights Reserved
  • 48. 48 Compiling & Running Copyright © 2010, Elsevier Inc. All rights Reserved gcc −g −Wall −fopenmp −o omp_hello omp_hello.c . / omp_hello 4 compiling running with 4 threads Hello from thread 0 of 4 Hello from thread 1 of 4 Hello from thread 2 of 4 Hello from thread 3 of 4 Hello from thread 1 of 4 Hello from thread 2 of 4 Hello from thread 0 of 4 Hello from thread 3 of 4 Hello from thread 3 of 4 Hello from thread 1 of 4 Hello from thread 2 of 4 Hello from thread 0 of 4 possible outcomes
  • 49. 49 OpenMp pragmas Copyright © 2010, Elsevier Inc. All rights Reserved  # pragma omp parallel  Most basic parallel directive  Original thread is called master  Additional threads are called slaves  Original thread & new threads called a team
  • 50. 50 Clause  Text that modifies a directive  num_threads clause can be added to a parallel directive  Allows programmer to specify no of threads that should execute following block Copyright © 2010, Elsevier Inc. All rights Reserved # pragma omp parallel num_threads ( thread_count )
  • 51. 51 Be Aware…  There may be system-defined limitations on number of threads that a program can start  OpenMP standard doesn’t guarantee that this will actually start thread_count threads  Most current systems can start hundreds or even 1,000s of threads  Unless we’re trying to start a lot of threads, we will almost always get desired no of threads Copyright © 2010, Elsevier Inc. All rights Reserved
  • 52. 52 Mutual Exclusion Copyright © 2010, Elsevier Inc. All rights Reserved # pragma omp critical { global_result += my_result ; } only 1 thread can execute following structured block at a time
  • 53. 53 Trapezoidal Rule Copyright © 2010, Elsevier Inc. All rights Reserved Serial algorithm
  • 54. 54 Assignment of Trapezoids to Threads Copyright © 2010, Elsevier Inc. All rights Reserved
  • 55. 55 Copyright © 2010, Elsevier Inc. All rights Reserved
  • 56. 56 Copyright © 2010, Elsevier Inc. All rights Reserved

Editor's Notes

  • #2: 8 January 2024
  • #12: Pthread_tobject Thread attributes Function that thread is to run Pointer to arguments passed to function
  • #23: Actual implementation uses a semaphore
  • #53: Can put brackets