Mirrors

1. Overview

Mirrors are views that reference data in a possibly different memory space. They are used to access data in a different memory space without copying it.

Mirroring schematic
Kokkos::View<double**, Space> view(...);
auto host_mirror = Kokkos::create_mirror_view(view);
kokkos mirrors schematic

Two views are created: view in the memory space Space and host_mirror in the host memory space. The data are copied back and forth between the two views using the Kokkos::deep_copy function.

2. Mirroring pattern

  1. Create a view in a specific memory space.

Kokkos::View<double**, Space> view(...);
  1. Create a mirror view in the host memory space, host_mirror.

auto host_mirror = Kokkos::create_mirror_view(view);
  1. Populate host_mirror with data (from file, user input, etc.).

  2. Copy data from host_mirror to view using Kokkos::deep_copy.

  3. Perform computations on view:

Kokkos::parallel_for("Operation",
    RangePolicy<Space>(0, view.extent(0)),
    KOKKOS_LAMBDA(...) { /* use and change view */ }
);
  1. If nedded, copy data back to host_mirror using Kokkos::deep_copy.

Kokkos::deep_copy(host_mirror, view);
create_mirror_view allocates data only if the host process cannon access view 's data. Otherwise, it returns a view that references the same data as view. The command create_mirror makes always make data allocation.

C++