forked from OpenMP/Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExamples_device.tex
57 lines (38 loc) · 2.07 KB
/
Examples_device.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
\pagebreak
\section{Device Routines}
\label{sec:device}
\subsection{\code{omp\_is\_initial\_device} Routine}
\label{subsec:device_is_initial}
The following example shows how the \code{omp\_is\_initial\_device} runtime library routine
can be used to query if a code is executing on the initial host device or on a
target device. The example then sets the number of threads in the \code{parallel}
region based on where the code is executing.
\cexample{device}{1}
\ffreeexample{device}{1}
\subsection{\code{omp\_get\_num\_devices} Routine}
\label{subsec:device_num_devices}
The following example shows how the \code{omp\_get\_num\_devices} runtime library routine
can be used to determine the number of devices.
\cexample{device}{2}
\ffreeexample{device}{2}
\subsection{\code{omp\_set\_default\_device} and \\
\code{omp\_get\_default\_device} Routines}
\label{subsec:device_is_set_get_default}
The following example shows how the \code{omp\_set\_default\_device} and \code{omp\_get\_default\_device}
runtime library routines can be used to set the default device and determine the
default device respectively.
\cexample{device}{3}
\ffreeexample{device}{3}
\subsection{Target Memory and Device Pointers Routines}
\label{subsec:target_mem_and_device_ptrs}
The following example shows how to create space on a device, transfer data
to and from that space, and free the space, using API calls. The API calls
directly execute allocation, copy and free operations on the device, without invoking
any mapping through a \code{target} directive. The \code{omp\_target\_alloc} routine allocates space
and returns a device pointer for referencing the space in the \code{omp\_target\_memcpy}
API routine on the host. The \code{omp\_target\_free} routine frees the space on the device.
The example also illustrates how to access that space
in a \code{target} region by exposing the device pointer in an \code{is\_device\_ptr} clause.
The example creates an array of cosine values on the default device, to be used
on the host device. The function fails if a default device is not available.
\cexample{device}{4}