0% found this document useful (0 votes)
7 views24 pages

Part8 ChoosingAppropriateThreadModel

This document discusses the selection of appropriate thread models in parallel programming, comparing OpenMP with Win32, Java, and POSIX threads. It highlights the advantages of OpenMP, such as ease of use and thread safety, while also noting scenarios where other threading models may be more suitable. The document includes examples, case studies, and design considerations for implementing parallel code effectively.

Uploaded by

Boy Tân
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views24 pages

Part8 ChoosingAppropriateThreadModel

This document discusses the selection of appropriate thread models in parallel programming, comparing OpenMP with Win32, Java, and POSIX threads. It highlights the advantages of OpenMP, such as ease of use and thread safety, while also noting scenarios where other threading models may be more suitable. The document includes examples, case studies, and design considerations for implementing parallel code effectively.

Uploaded by

Boy Tân
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Introduction to Parallel Programming – Part 8

Choosing the Appropriate Thread Model


Intel Software College
Intel® Software College

Objectives
At the end of this module, you should be able to
Explain the relative advantages and
disadvantages of OpenMP compared to
Win32/Java/POSIX threads
Give examples of
Applications more suitable for a parallel
program based on OpenMP
Applications more suitable for a parallel
program based on Win32/Java/
POSIX threads

Choosing the Appropriate Thread Model


2

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

OpenMP’s Parallel Patterns

Fork/Join Nested
Fork/Join

Choosing the Appropriate Thread Model


3

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

Flexibility of General Threads


Model

Thread terminates
without join
Join

Choosing the Appropriate Thread Model


4

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

Function pthread_create

int pthread_create (
/* Pointer to thread ID */
pthread_t *thread,
/* Pointer to thread attribute object */
const pthread_attr_t *attr,
/* Function thread is to execute */
void *(*start)(void *),
/* Pointer to function argument */
void *arg);

Choosing the Appropriate Thread Model


5

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

Function pthread_join

int pthread_join (
/* Thread ID */
pthread_t thread,
/* Address of thread status pointer */
void **status);

Choosing the Appropriate Thread Model


6

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

Case Study: Fancy Web Browser

Choosing the Appropriate Thread Model


7

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

Case Study: Fancy Web Browser

You can see snapshot of


page before deciding
whether to click on the link
Choosing the Appropriate Thread Model
8

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

C Code

page = retrieve_page (url);


find_links (page, &num_links, &link_url);
for (i = 0; i < num_links; i++)
snapshots[i].image = NULL;
for (i = 0; i < num_links; i++)
generate_preview (&snapshots[i]);
display_page (page);

Choosing the Appropriate Thread Model


9

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

OpenMP Design

Threads = Number of links + 1

Forked threads
follow links and
create snapshot
images

Main thread creates Main thread waits


Web page image for created threads
to finish

Choosing the Appropriate Thread Model


10

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

General Threads Design

Threads = Number of links + 1

Forked threads
follow links and
create snapshot
images

Main thread creates


Web page image,
waits for threads
corresponding to
initially visible links,
then continues

Choosing the Appropriate Thread Model


11

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

Parallel Code Design

Main thread
Retrieves page
Identifies links
Forks one thread per link to generate previews
Draws page
Waits for threads responsible for initially visible links
Created thread
Retrieves page and builds snapshot image

Choosing the Appropriate Thread Model


12

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

C / Pthreads Code

page = retrieve_page (url);


find_links (page, &num_links, &link_url);
for (i = 0; i < num_links; i++)
snapshots[i].image = NULL;
for (i = 0; i < num_links; i++)
pthread_create (&thread[i], NULL,
generate_preview, &snapshots[i]);
display_page (page);
for (i = 0; i < num_vis; i++)
if (initially_visible (page, i))
pthread_join (thread[i], NULL);
Choosing the Appropriate Thread Model
13

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

Thread States

State Meaning
Ready Able to run, waiting for CPU
Running Executing on a CPU
Blocked Waiting for an something, such as the
completion of an I/O operation or the
availability of a mutex
Terminated Has finished execution by returning
from start function or calling
pthread_exit or being cancelled

Choosing the Appropriate Thread Model


14

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

Beveridge and Wiener’s Tips

Minimize number of data structures shared by


threads
Avoid using global variables
Keep track of the state of all threads, and don’t exit
without waiting for them to complete their work
Let the main thread handle the user interface

Choosing the Appropriate Thread Model


15

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

Key Difference Between Win32/POSIX


and Java Threads
Win32 and POSIX threads can’t survive termination
of main thread
Java threads may still execute when main thread
terminates

Choosing the Appropriate Thread Model


16

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

Why Choose OpenMP?

Easier to make data structures thread-safe


High-level constructs eliminate need to write synchronization
constructs
Easier to change scheduling
Tools for OpenMP are simpler to use than tools for hand
threaded programs (parallelize, debug, tune)
Fewer modifications to source program
Bottom line: Converting threaded C programs to OpenMP can
“increase robustness without sacrificing performance” (Kuhn
et al.)

Choosing the Appropriate Thread Model


17

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

Balanced, Static Applications

g h i
parallel
sections
parallel for j

Choosing the Appropriate Thread Model


18

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

Why Choose X* Threads?


Not all compilers support OpenMP, and those that do don’t support the
same versions/extensions
A hand-threaded program can duplicate all functions of
OpenMP, and more besides
More flexible, dynamic creation of threads
Greater selection of synchronization functions
Can implement a barrier or critical section that affects only some of the
threads
Implementing task-parallel programs is easier
Dynamic, irregular applications that don’t map well to OpenMP
Exception-handling from parallel code not always possible in OpenMP
You can’t set thread priorities with OpenMP
OpenMP doesn’t support real-time scheduling

* X = Win32, Java, or POSIX


Choosing the Appropriate Thread Model
19

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

Grid Refinement

Used in numerical solutions of partial differential equations


Refinement done locally based on error estimates
Regions needing refinement can move as computation
progresses
Local coarsening can also be helpful

Choosing the Appropriate Thread Model


20

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

Prioritizing Threads

Monster 3
Hero (Low priority)

Monster 1 Monster 2
(High priority) (medium priority)

Choosing the Appropriate Thread Model


21

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

Conclusion

Keep things as simple as possible


If you’re doing a domain decomposition, it is easier
to create, debug, and maintain an OpenMP
program than a WIN32/POSIX threads program
First explore higher-level OpenMP solution
Fall back to lower-level threads API if necessary
If you’re using Java, OpenMP not an option

Choosing the Appropriate Thread Model


22

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

References

Jim Beveridge and Robert Wiener, Multithreading Applications in


Win32®, Addison-Wesley (1997).
David R. Butenhof, Programming with POSIX Threads, Addison-
Wesley (1997).
Richard H. Carver and Kuo-Chung Tai, Modern Multithreading:
Implementing, Testing, and Debugging Multithreaded Java and
C++/Pthreads/Win32 Programs, Wiley-Interscience (2006).
Rohit Chandra, Leonardo Dagum, Dave Kohr, Dror Maydan, Jeff
McDonald, and Ramesh Menon, Parallel Programming in
OpenMP, Morgan Kaufmann (2001).
Bob Kuhn, Paul Peterson, and Eamonn O’Toole, “OpenMP versus
Threading in C/C++,” Concurrency—Practice and Experience
12, 12 (2000).

Choosing the Appropriate Thread Model


23

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Intel® Software College

Choosing the Appropriate Thread Model


24

Copyright © 2006, Intel Corporation. All rights reserved.


Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.

You might also like