| United States-English |
|
|
|
![]() |
Parallel Programming Guide for HP-UX Systems: K-Class and V-Class Servers > Chapter 9 Parallel programming
techniquesParallelizing tasks |
|
The compiler does not automatically parallelize code outside a loop. However, you can use tasking directives and pragmas to instruct the compiler to parallelize this type of code.
The sections of code delimited by these directives are referred to as a task list. Within a task list, the compiler does not check for data dependences, perform variable privatization, or perform parallelization analysis. You must manually synchronize any dependences between tasks and manually privatize data as necessary. The forms of these directives and pragmas are shown in Table 9-8 “Forms of task parallelization directives and pragmas”. Table 9-8 Forms of task parallelization directives and pragmas
where attribute-list can contain one of the case-insensitive attributes noted in Table 9-9 “Attributes for task parallelization”. The optional attribute-list can contain one of the following attribute combinations, with m being an integer constant. Table 9-9 Attributes for task parallelization
Parallelizing tasks The following Fortran example shows how to insert tasking directives into a section of code containing three tasks that can be run in parallel: C$DIR BEGIN_TASKS parallel task 1 C$DIR NEXT_TASK parallel task 2 C$DIR NEXT_TASK parallel task 3 C$DIR END_TASKS The example above specifies thread-parallelism by default. The compiler transforms the code into a parallel loop and creates machine code equivalent to the following Fortran code: C$DIR LOOP_PARALLEL DO 40 I = 1,3 GOTO (10,20,30)I 10 parallel task 1 GOTO 40 20 parallel task 2 GOTO 40 30 parallel task 3 GOTO 40 40 CONTINUE If there are more tasks than available threads, some threads execute multiple tasks. If there are more threads than tasks, some threads do not execute tasks. In this example, the END_TASKS directive and pragma acts as a barrier. All parallel tasks must complete before the code following END_TASKS can execute. Parallelizing tasks The following C example illustrates how to use these directives to specify simple task-parallelization:
In this example, one thread executes the for loop, another thread executes the tsub(x,y) function call, and a third thread assigns the elements of the array d to every other element of c. These threads execute in parallel, but their starting and ending orders are indeterminate. The tasks are thread-parallelized. This means that there is no room for nested parallelization within the individual parallel tasks of this example, so the forward LCD on the for I loop is inconsequential. There is no way for the loop to run but serially. The loop induction variable i must be manually privatized here because it is used to control loops in two different tasks. If i were not private, both tasks would modify it, causing wrong answers. The task_private directive and pragma is described in detail in the section “task_private”. Task parallelism can become even more involved, as described in Chapter 12 “Parallel synchronization”. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||