\cchapter{Devices}{devices} \label{chap:devices} The \kcode{target} construct consists of a \kcode{target} directive and an execution region. The \kcode{target} region is executed on the default device or the device specified in the \kcode{device} clause. In OpenMP version 4.0, by default, all variables within the lexical scope of the construct are copied \plc{to} and \plc{from} the device, unless the device is the host, or the data exists on the device from a previously executed data-type construct that has created space on the device and possibly copied host data to the device storage. The constructs that explicitly create storage, transfer data, and free storage on the device are categorized as structured and unstructured. The \kcode{target data} construct is structured. It creates a data region around \kcode{target} constructs, and is convenient for providing persistent data throughout multiple \kcode{target} regions. The \kcode{target enter data} and \kcode{target exit data} constructs are unstructured, because they can occur anywhere and do not support a ``structure'' (a region) for enclosing \kcode{target} constructs, as does the \kcode{target data} construct. The \kcode{map} clause is used on \kcode{target} constructs and the data-type constructs to map host data. It specifies the device storage and data movement \plc{to} and \plc{from} the device, and controls on the storage duration. There is an important change in the OpenMP 4.5 specification that alters the data model for scalar variables and C/C++ pointer variables. The default behavior for scalar variables and C/C++ pointer variables in a 4.5 compliant code is \kcode{firstprivate}. Example codes that have been updated to reflect this new behavior are annotated with a description that describes changes required for correct execution. Often it is a simple matter of mapping the variable as \kcode{tofrom} to obtain the intended 4.0 behavior. In OpenMP version 4.5 the mechanism for target execution is specified as occurring through a \plc{target task}. When the \kcode{target} construct is encountered a new target task is generated. The target task completes after the \kcode{target} region has executed and all data transfers have finished. This new specification does not affect the execution of pre-4.5 code; it is a necessary element for asynchronous execution of the \kcode{target} region when using the new \kcode{nowait} clause introduced in OpenMP 4.5. %===== Examples Sections ===== \input{devices/target} \input{devices/target_defaultmap} \input{devices/target_pointer_mapping} \input{devices/target_structure_mapping} \input{devices/target_fort_allocatable_array_mapping} \input{devices/array_sections} \input{devices/usm} \input{devices/C++_virtual_functions} \input{devices/array_shaping} \input{devices/target_mapper} \input{devices/target_data} \input{devices/target_unstructured_data} \input{devices/target_update} \input{devices/declare_target} \input{devices/lambda_expressions} \input{devices/teams} \input{devices/async_target_depend} \input{devices/async_target_with_tasks} \input{devices/async_target_nowait} \input{devices/async_target_nowait_depend} \input{devices/async_target_nowait_arg} \input{devices/device} \input{devices/device_env_traits}