Skip to content

Kokkos

Kokkos is an open-source, performance-portable C++ programming model. Parallel loops and data structures are expressed once with Kokkos::parallel_for, Kokkos::View and similar abstractions; Kokkos maps them to a backend selected at build time.

Kokkos with accelerators

  • Kokkos::View manages data in the memory space of the selected backend.
  • Kokkos::create_mirror_view and Kokkos::deep_copy handle the data transfers between host and device.
  • Kokkos::parallel_for executes a lambda (KOKKOS_LAMBDA) in parallel on the device.

Backends: OpenMP and Threads for CPUs, CUDA for NVIDIA GPUs, HIP for AMD GPUs, SYCL for Intel GPUs.

Supported compilers and hardware

Compilers: all C++ compilers; nvcc_wrapper (NVIDIA nvcc) for the CUDA backend

Hardware: CPUs, NVIDIA GPUs, AMD GPUs, Intel GPUs

Code sample

The following simple C++ program shows the typical sequence of a Kokkos program:

C++ source code: exampleKokkos.cpp
#include <Kokkos_Core.hpp>
#include <err.h>
#include <cstdlib>
#include <iostream>

using std::cout, std::endl;

int main(int argc, char **argv) {
    Kokkos::initialize(argc, argv);
    std::atexit(Kokkos::finalize); // register to be called on program termination

    const std::size_t size = 1 << 16;

    // Platform information
    Kokkos::print_configuration(cout);

    // Allocate memory
    cout << "* Allocate memory on the device" << endl;
    Kokkos::View<double *> device_a(Kokkos::ViewAllocateWithoutInitializing("device_a"), size);

    cout << "* Allocate memory on the host" << endl;
    auto a = Kokkos::create_mirror_view(device_a);

    cout << "* Pre-process / initialize data on the host" << endl;
    cout << "  e.g. read data from storage" << endl;
    for (std::size_t i = 0; i < size; i++) {
        a[i] = 1.;
    }

    cout << "* Copy data from the host to the device" << endl;
    Kokkos::deep_copy(device_a, a);

    cout << "* Compute on the device" << endl;
    Kokkos::parallel_for(
        "Increment a[] on device", size,
        KOKKOS_LAMBDA(int i) {
            device_a[i]++;
        });

    cout << "* Transfer data back from the device to the host" << endl;
    Kokkos::deep_copy(a, device_a);

    cout << "* Post-process data on the host" << endl;
    cout << "  e.g. write data to storage or perform consistency checks" << endl;
    for (std::size_t i = 0; i < size; i++) {
        if (a[i] != 2.) {
            cout << "a[" << i << "] = " << a[i] << endl;
            errx(2, "Computation on GPU failed");
        }
    }

    cout << "* Automatically delete data on the device" << endl;
    cout << "* Automatically free memory on the host" << endl;

    return 0;
}

Download C++ source code: exampleKokkos.cpp

Compiling and running Kokkos code

Excerpt of the submit scripts

The snippets below show only the part of the submit scripts that loads the modules, compiles and runs the example. The #SBATCH header (partition, number of nodes, tasks, CPUs, GPUs, time limit, output file) is omitted. Please use the download links to get the complete submit scripts.

# Load modules
module load Kokkos/5.1.1-GCC-15.2.0-CUDA-13.3.0

# Compile source code
nvcc_wrapper --extended-lambda -O2 -march=native -std=c++23 -fopenmp -arch=native exampleKokkos.cpp -o exampleKokkos -lkokkoscore -lcuda

./exampleKokkos
Website: Kokkos
Download complete submit script: HoreKa Jade, HoreKa Teal, HoreKa Ruby

The submit scripts for HoreKa Jade (gpu-b200), HoreKa Teal (gpu-h100) and HoreKa Ruby (gpu-h200) differ only in the partition. The example is compiled inside the job on the GPU node, so that the compiler can optimize for the target architecture (-march=native, -arch=native).