GPU-aware I/O

The Put and Get functions in the default file engine (BP5) and some streaming engines (SST, DataMan) can receive user buffers allocated on the host or the device in both Sync and Deferred modes.

Note

Buffers allocated on the device with CUDA, HIP and SYCL are supported.

If ADIOS2 is built without GPU support, only buffers allocated on the host are supported. When GPU support is enabled with CUDA or HIP backends, the default behavior is for ADIOS2 to automatically detect where the buffer memory physically resides.

Note

When using the SYCL backend, automatic memory space detection is not supported: buffers default to host memory, and device buffers must be declared with the SetMemorySpace function. Kokkos Views carry their memory space and need no declaration.

Users can also provide information about where the buffer was allocated by using the SetMemorySpace function within each variable.

enum class MemorySpace
{
    Detect, ///< Detect the memory space automatically
    Host,   ///< Host memory space (default)
    GPU     ///< GPU memory spaces
};

If ADIOS2 is built without GPU support, the available MemorySpace values are only Detect and Host.

ADIOS2 can use a CUDA or Kokkos backend for enabling GPU support. Only one backend can be active at a given time based on how ADIOS2 is build.

Building ADIOS2 with a GPU backend

By default both backends are OFF even if CUDA or Kokkos are installed and available to avoid a possible conflict between if both backends are enabled at the same time.

Building with CUDA enabled

The ADIOS2 default behavior is to turn OFF the CUDA backend. Building with the CUDA backend requires -DADIOS2_USE_CUDA=ON and an available CUDA toolkit on the system.

When building ADIOS2 with CUDA enabled, the user is responsible with setting the correct CMAKE_CUDA_ARCHITECTURES (e.g. for Summit the CMAKE_CUDA_ARCHITECTURES needs to be set to 70 to match the NVIDIA Volta V100).

Building with Kokkos enabled

The Kokkos library can be used to enable GPU within ADIOS2. Based on how Kokkos is build, either the CUDA, HIP or SYCL backend will be enabled. Building with Kokkos requires -DADIOS2_USE_Kokkos=ON. The CMAKE_CUDA_ARCHITECTURES is set automanically to point to the same architecture used when configuring the Kokkos library.

Note

Kokkos version >= 3.7 is required to enable the GPU backend in ADIOS2

Writing GPU buffers

The ADIOS2 API for Device pointers is the same as for Host buffers for both the read and write logic, with one addition: a raw device pointer carries no layout, so a multidimensional variable read or written through one needs its layout set with SetArrayLayout (set_array_layout in Python, adios2_set_array_layout in C and Fortran) before the first Put or Get. Otherwise the call throws. A layout that matches the language’s order (RowMajor for C, C++ and Python, ColumnMajor for Fortran) keeps the shape as declared; the other one reverses the variable’s dimensions, since a file keeps a single array order. One-dimensional variables and Kokkos::View buffers, which carry their layout, need no call. Internally each ADIOS2 variable holds a memory space for the data it receives. Once the memory space is set (eithr directly by the user through calls to SetMemorySpace or after detecting the buffer memory space the first Put or Get call) to either Host or Device, it cannot be changed.

The examples/hello folder contains several codes that use Device buffers:
  • bpStepsWriteRead{Cuda|Hip} show CUDA and HIP codes using BP5 with GPU pointers

  • bpStepsWriteReadKokkos contains Fortran and C++ codes using Kokkos::View with different memory spaces and a Kokkos code using different layouts on Host buffers

  • datamanKokkos shows an example of streaming a Kokkos::View with DataMan using different memory spaces

  • sstKokkos shows an example of streaming a Kokkos::View with SST using different memory spaces

Example using a Device buffer

The following is a simple example of writing data to storage directly from a GPU buffer allocated with CUDA relying on the automatic detection of device pointers in ADIOS2.

float *gpuSimData;
cudaMalloc(&gpuSimData, N * sizeof(float));
cudaMemset(gpuSimData, 0, N);
auto data = io.DefineVariable<float>("data", shape, start, count);
// required for GPU buffers if shape has more than one dimension
data.SetArrayLayout(adios2::ArrayOrdering::RowMajor);

io.SetEngine("BP5");
adios2::Engine bpWriter = io.Open(fname, adios2::Mode::Write);
// Simulation steps
for (size_t step = 0; step < nSteps; ++step)
{
    bpWriter.BeginStep();
    bpWriter.Put(data, gpuSimData, adios2::Mode::Deferred); // or Sync
    bpWriter.EndStep();
}

If the SetMemorySpace function is used, the ADIOS2 library will not detect automatically where the buffer was allocated and will use the information provided by the user for all subsequent Puts or Gets. Example:

data.SetMemorySpace(adios2::MemorySpace::GPU);
for (size_t step = 0; step < nSteps; ++step)
{
    bpWriter.BeginStep();
    bpWriter.Put(data, gpuSimData, adios2::Mode::Deferred); // or Sync
    bpWriter.EndStep();
}

Underneath, ADIOS2 relies on the backend used at build time to transfer the data. If ADIOS2 was build with CUDA, only CUDA buffers can be provided. If ADIOS2 was build with Kokkos (with CUDA enabled) only CUDA buffers can be provided. If ADIOS2 was build with Kokkos (with HIP enabled) only HIP buffers can be provided.

Note

The SYCL backend in Kokkos can be used to run on Nvida, AMD and Intel GPUs, but we recommand using SYCL for Intel, HIP for AMD and CUDA for Nvidia.

Warning

When using SYCL, automatic memory space detection is not available. You must use SetMemorySpace(adios2::MemorySpace::GPU) before any Put or Get operations with device buffers. Otherwise the buffer is treated as host memory, which typically ends in a segmentation fault.

Kokkos applications

ADIOS2 supports GPU buffers provided in the form of Kokkos::View directly in the Put/Get calls. The memory space is automatically detected from the View information. In addition to the memory space, for Kokkos::View ADIOS2 also extracts the layout of the array and adjust the variable dimensions to be able to build the global shape (across ranks) of the array.

Kokkos::View<float *, Kokkos::CudaSpace> gpuSimData("data", N);
bpWriter.Put(data, gpuSimData);

If the CUDA backend is being used (and not Kokkos) to enable GPU support in ADIOS2, Kokkos applications can still directly pass Kokkos::View as long as the correct external header is included: #include <adios2/cxx/KokkosView.h>.

Reading GPU buffers

The GPU-aware backend allows different layouts for global arrays without requiring the user to update the code for each case. The user defines the shape of the global array and ADIOS2 adjusts the dimensions for each rank according to the buffer layout and memory space.

The following example shows a global array of shape (4, 3) when running with 2 ranks, each contributing half of it.

Write on LayoutRight, read on LayoutRight
1 1 1  // rank 0
2 2 2
3 3 3  // rank 1
4 4 4
Write on LayoutRight, read on LayoutLeft
1 2 3 4
1 2 3 4
1 2 3 4

On the read side, the Shape function can take a layout and returns the dimensions of the variable in that layout. For the previous example, a C++ code using two ranks that reads into column-major buffers (a LayoutLeft view, or a raw device pointer after SetArrayLayout(adios2::ArrayOrdering::ColumnMajor)) sees the local array as (3, 2); in the declared row-major order it is (2, 3):

auto dims_row = data.Shape();
auto dims_col = data.Shape(adios2::ArrayOrdering::ColumnMajor);

As on the write side, a Get of a multidimensional variable into a raw device pointer throws unless SetArrayLayout has been called.

Reading in a layout other than the writer’s is valid: the reader sees the array transposed, with its dimensions reversed. To find out how an array was written, GetWriterArrayLayout (writer_array_layout in Python, adios2_writer_array_layout in C and Fortran) returns the layout of the writer’s buffers, RowMajor or ColumnMajor, recorded per variable in BP5 files. Engines that do not record it return Auto.

if (data.GetWriterArrayLayout() == adios2::ArrayOrdering::ColumnMajor)
{
    // read into a LayoutLeft view to get the shape as written
}

Build scripts

The scripts/build_scripts folder contains scripts for building ADIOS2 with CUDA or Kokkos backends for several DOE system: Summit (OLCF Nvidia), Crusher (OLCFi AMD), Perlmutter (NERSC Nvidia), Polaris (ALCF Nvidia).

Note

Perlmutter requires Kokkos >= 4.0