Choose the Right Threading Model (Task-Parallel or Data-Parallel Threading)


Challenge

Choose task-level or data-parallel threading for various parts of an application. Choosing the right threading method minimizes the amount of time spent modifying, debugging, and tuning threaded code.


Solution

Describe your application (or an individual operation in that application) in terms of one of two models based on fit for the particular job:

  • Choose the task-parallel model when independent threads can readily service separate functions. Task-level concurrency calls for independent work encapsulated in functions to be mapped to individual threads, which execute asynchronously. Thread libraries (e.g., the Win32* thread API or POSIX* threads) are designed to express task-level concurrency.
  • Choose the data-parallel threading model for compute-intensive loops, that is, where the same, independent operation (i.e., comparing a word in a file against a dictionary) is performed repeatedly. Data parallelism implies that the same independent operation is applied repeatedly to different data. Image processing algorithms that apply a filter to each pixel are a common example of data parallelism.

 

A Personal Information Manager (PIM) is a good example of an application that contains task-level concurrency (e.g., database access to the address book, in-box, and calendar). Independent functions are mapped to threads, as illustrated in the following pseudo-code:

Function addressBook;

Function inBox;

Function calendar;

Program PIM

{

CreateThread (addressBook);

CreateThread (inBox);

CreateThread (calendar);

}

 

A good example of a data-parallel operation, a spell checker, is shown in the following pseudo-code:

Function SpellCheck

{

loop (word = 1, words_in_file) compareToDictionary (word);

}

 

Note that both task-parallel and data-parallel operations might exist in the same application.

Sometimes it is possible for the compiler to automatically express data parallelism. Otherwise, the programmer describes the parallelism using OpenMP* directive syntax. The compiler is responsible for converting the directives to threaded code. For an overview of compiler commands associated with generating parallelism, see the Intel® Compiler Quick Start Guide and other resources available from the Intel Compiler web site.


Source

Threading Methodology: Principles and Practices

 


For more complete information about compiler optimizations, see our Optimization Notice.
Categories: