Skip to content

Kokkos::deep_copy

Christian Trott edited this page May 1, 2020 · 11 revisions

Kokkos::deep_copy()

Header File: Kokkos_Core.hpp

Usage:

  Kokkos::deep_copy(exec_space, dest, src);
  Kokkos::deep_copy(dest, src);

Copies data from src to dest, where src and dest can be Kokkos::Views or scalars under certain circumstances.

Interface

template <class ExecSpace, class ViewDest, class ViewSrc>
void Kokkos::deep_copy(const ExecSpace& exec_space, 
                       const ViewDest& dest,
                       const ViewSrc& src);
template <class ExecSpace, class ViewDest>
void Kokkos::deep_copy(const ExecSpace& exec_space, 
                       const ViewDest& dest,
                       const typename ViewDest::value_type& src);
template <class ExecSpace, class ViewSrc>
void Kokkos::deep_copy(const ExecSpace& exec_space, 
                       ViewSrc::value_type& dest,
                       const ViewSrc& src);
template <class ViewDest, class ViewSrc>
void Kokkos::deep_copy(const ViewDest& dest,
                       const ViewSrc& src);
template <class ViewDest>
void Kokkos::deep_copy(const ViewDest& dest,
                       const typename ViewDest::value_type& src);
template <class ViewSrc>
void Kokkos::deep_copy(ViewSrc::value_type& dest,
                       const ViewSrc& src);

Parameters:

Requirements:

  • If src and dest are Kokkos::Views, then all the following are true:
    • std::is_same<ViewDest::non_const_value_type,ViewSrc::non_const_value_type>::value == true
    • ViewDest::rank == ViewSrc::rank
    • For all k in [0, ViewDest::rank) dest.extent(k) == src.extent(k)
    • src.span_is_contiguous() && dest.span_is_contiguous() OR there exists an ExecutionSpace copy_space such that both SpaceAccessibility<copy_space,ViewDest::memory_space>::accessible == true and SpaceAccessibility<copy_space,ViewSrc::memory_space>::accessible == true.
  • If src is a Kokkos::View and dest is a scalar, then src.rank == 0 is true.

Semantics

  • If no ExecutionSpace argument is provided, all outstanding operations (kernels, copy operation) will be finished before the copy is executed, and the copy operation is finished before the call returns.
  • If an ExecutionSpace argument exec_space is provided the call is potentially asynchronous - i.e. the call returns before the copy operation is executed. In that case the copy operation will occur only after any already submitted work to exec_space is finished, and the copy operation will be finished before any work submitted to exec_space after the deep_copy call returns is executed.

Examples

#include<Kokkos_Core.hpp>
#include<cstdio> 

int main(int argc, char* argv[]) {
   Kokkos::initialize(argc,argv);
   {

   int N = argc>1?atoi(argv[1]):12;
   if(N<6) N = 12;

   // Contiguous Device View
   Kokkos::View<int**, Kokkos::LayoutLeft> d_a("A",N,10);

   // Non Contiguous Device View
   auto d_a_2 = Kokkos::subview(d_a,2,Kokkos::ALL);
   auto d_a_5 = Kokkos::subview(d_a,5,Kokkos::ALL);

   // Contiguous Host View
   auto h_a = Kokkos::create_mirror_view(d_a);

   // Non Contiguous Host Views
   auto h_d_a_2 = Kokkos::subview(h_a,2,Kokkos::ALL);
   auto h_a_5 = Kokkos::subview(h_a,5,Kokkos::ALL);
   
   // A Scalar View
   auto d_a_2_5 = Kokkos::subview(d_a,2,5);

   int scalar;
   
   // Deep Copy Scalar into every element of a view
   Kokkos::deep_copy(d_a,3);
   Kokkos::deep_copy(d_a_2,5);

   // Deep Copy between two non-contiguous views with a common execution space
   Kokkos::deep_copy(d_a_2,d_a5);

   // Deep Copy between two non-contiguous views with potentially no common execution space
   // This fails for example if you compile the code with Cuda
   // Kokkos::deep_copy(h_a_2,d_a_2);
   
   // Deep Copy contiguous views
   Kokkos::deep_copy(h_a,d_a);

   // Deep Copy Scalar View into a scalar
   Kokkos::deep_copy(scalar,d_a_2_5);

   }
   Kokkos::finalize();
}
Clone this wiki locally