Mapping the array variables and computation loops onto the template specifies the array variable distribution and computation loop parallelization. The align declaration directive is used to associate the array variables with the template, and the loop execution directive is used to associate the computation loops with the template.

!$xmp nodes p(2)
!$xmp template t(10)
!$xmp distribute t(block) onto p

integer a(10)
!$xmp align a(i) with t(i)

!$xmp loop on t(i)
do i=1,10
	a(i)=i*2
end do

write(*,*) a
#pragma xmp nodes p(2)
#pragma xmp template t(0:9)
#pragma xmp distribute t(block) onto p
int a[10];
#pragma xmp align a[i] with t(i)

#pragma xmp loop on t(i)
for(i=0;i<10;i++)
	a[i] = (i+1)*2;

#pragma xmp loop on t(i)
for(i=0;i<10;i++)
	printf("%d ", a[i]);

The align directive is coded in the declaration section of the program and specifies the mapping of the variables and the template. In this example, in Fortran, the five elements a(1) through a(5) are distributed to node p(1), and elements a(6) through a(10) are distributed to node p(2) through template t. For C, the five elements a[0] through a[4] are distributed to node p(1), and the five elements a[5] through a[9] are distributed to node p(2). An array for which mapping has been specified using the align directive is referred to as a distributed array. An array for which there is no directive is allocated in its entirety to all execution nodes and is thus referred to as a duplicate array. A scalar variable is allocated to all execution nodes. The duplicate array and the scalar variables are collectively referred to as duplicate variables.

The loop directive is applied to Do Loops or For Statements, and described immediately before them. The directive specifies the mapping between the loop iterations and the template. In this example, in Fortran, iterations i=1 through i=5 are executed on p(1), and i=6 through i=10 are executed on p(2), through template t. For C, iterations i=0 through i=4 are executed on p(1), and i=5 through i=9 are executed on p(2).

If this program is compiled and run on two nodes, there will be output from the two nodes, from each of the five elements for which they are responsible, as shown below.

2 4 6 8 10
12 14 16 18 20

The first line is the output result from node p(1), and the second line is the output result from node p(2). In this example, the output is from redundant execution, so which output will appear first is not determined. It is possible that, depending on the system, the output lines will be mixed.

To clearly establish which node is outputting which line, one would try specifying for p(1) alone to execute the output. The task directive construct is used to limit execution to specific nodes.

!$xmp nodes p(2)
!$xmp template t(10)
!$xmp distribute t(block) onto p

integer a(10)
!$xmp align a(i) with t(i)

!$xmp loop on t(i)
do i=1,10
	a(i)=i*2
end do

!$xmp task on p(1)
write(*,*) a
!$xmp end task
#pragma xmp nodes p(2)
#pragma xmp template t(0:9)
#pragma xmp distribute t(block) onto p
int a[10];
#pragma xmp align a[i] with t(i)

#pragma xmp loop on t(i)
for(i=0;i<10;i++)
	a[i] = (i+1)*2;

#pragma xmp loop on t(i)
for(i=0;i<10;i++)
#pragma xmp task on p(1)
	{
	printf("%d ", a[i]);
	}
}

The task directive construct instructs the execution of a single group of nodes as specified in the On clause, which in the case of Fortran is for the execution section enclosed between the task and end task directives, and in C is for the region contained inside the parentheses. In this example, only p(1) will be executed. The results are shown below.

2 4 6 8 10

We have thus determined that the program has been properly parallelized.