\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[4.0]{device}{1} \ffreeexample[4.0]{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[4.0]{device}{2} \ffreeexample[4.0]{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[4.0]{device}{3} \ffreeexample[4.0]{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[4.5]{device}{4}