OpenMP-Examples/devices/target_unstructured_data.tex
2022-04-18 15:02:25 -07:00

58 lines
3.2 KiB
TeX

%begin
\pagebreak
\section{\code{target} \code{enter} \code{data} and \code{target} \code{exit} \code{data} Constructs}
\label{sec:target_enter_exit_data}
%\section{Simple target enter data and target exit data Constructs}
\index{constructs!target enter data@\code{target} \code{enter} \code{data}}
\index{constructs!target exit data@\code{target} \code{exit} \code{data}}
\index{target enter data construct@\code{target} \code{enter} \code{data} construct}
\index{target exit data construct@\code{target} \code{exit} \code{data} construct}
The structured data construct (\code{target}~\code{data}) provides persistent data on a
device for subsequent \code{target} constructs as shown in the
\code{target}~\code{data} examples above. This is accomplished by creating a single
\code{target}~\code{data} region containing \code{target} constructs.
The unstructured data constructs allow the creation and deletion of data on
the device at any appropriate point within the host code, as shown below
with the \code{target}~\code{enter}~\code{data} and \code{target}~\code{exit}~\code{data} constructs.
\index{map clause@\code{map} clause!alloc map-type@\code{alloc} map-type}
\index{map clause@\code{map} clause!delete map-type@\code{delete} map-type}
\index{alloc map-type@\code{alloc} map-type}
\index{delete map-type@\code{delete} map-type}
The following C++ code creates/deletes a vector in a constructor/destructor
of a class. The constructor creates a vector with \code{target}~\code{enter}~\code{data}
and uses an \code{alloc} modifier in the \code{map} clause to avoid copying values
to the device. The destructor deletes the data (\code{target}~\code{exit}~\code{data})
and uses the \code{delete} modifier in the \code{map} clause to avoid copying data
back to the host. Note, the stand-alone \code{target}~\code{enter}~\code{data} occurs
after the host vector is created, and the \code{target}~\code{exit}~\code{data}
construct occurs before the host data is deleted.
\cppexample[4.5]{target_unstructured_data}{1}
\pagebreak
The following C code allocates and frees the data member of a Matrix structure.
The \code{init\_matrix} function allocates the memory used in the structure and
uses the \code{target}~\code{enter}~\code{data} directive to map it to the target device. The
\code{free\_matrix} function removes the mapped array from the target device
and then frees the memory on the host. Note, the stand-alone
\code{target}~\code{enter}~\code{data} occurs after the host memory is allocated, and the
\code{target}~\code{exit}~\code{data} construct occurs before the host data is freed.
\cexample[4.5]{target_unstructured_data}{1}
\pagebreak
The following Fortran code allocates and deallocates a module array. The
\code{initialize} subroutine allocates the module array and uses the
\code{target}~\code{enter}~\code{data} directive to map it to the target device. The
\code{finalize} subroutine removes the mapped array from the target device and
then deallocates the array on the host. Note, the stand-alone
\code{target}~\code{enter}~\code{data} occurs after the host memory is allocated, and the
\code{target}~\code{exit}~\code{data} construct occurs before the host data is deallocated.
\ffreeexample[4.5]{target_unstructured_data}{1}
%end