13.11.2016 Views

OpenMP Application Programming Interface Examples

2fZ58Wr

2fZ58Wr

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

C / C++<br />

The if clauses work the same way for the following Fortran code. When the if clause conditional<br />

expression on the target construct evaluates to false, the target region will execute on the host<br />

device. However, the target data construct created an enclosing device data environment that<br />

mapped the p array (and v1 and v2) to a device data environment on the default target device. At the<br />

end of the target data region the p array will be assigned from the device data environment to<br />

the corresponding variable in the data environment of the task that encountered the target data<br />

construct, resulting in undefined values in p.<br />

Example target_data.7.f90<br />

Fortran<br />

S-1 module params<br />

S-2 integer, parameter :: THRESHOLD=1000000<br />

S-3 end module<br />

S-4 subroutine vec_mult(p, v1, v2, N)<br />

S-5 use params<br />

S-6 real :: p(N), v1(N), v2(N)<br />

S-7 integer :: i<br />

S-8 call init(v1, v2, N)<br />

S-9 !$omp target data map(from: p)<br />

S-10 !$omp target if(N>THRESHOLD) map(to: v1, v2)<br />

S-11 !$omp parallel do<br />

S-12 do i=1,N<br />

S-13 p(i) = v1(i) * v2(i)<br />

S-14 end do<br />

S-15 !$omp end target<br />

S-16 !$omp end target data<br />

S-17 call output(p, N) !*** UNDEFINED behavior if N

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!