Program Listing for File device_binding.h

↰ Return to documentation for file (framework/gpu/device_binding.h)

// SPDX-FileCopyrightText: 2026 The OpenSn Authors <https://open-sn.github.io/opensn/>
// SPDX-License-Identifier: MIT

#pragma once

#include <mpi.h>
#include <string>
#include <vector>

namespace opensn
{

/**
 * Selects the GPU that this MPI rank uses.
 *
 * Ranks on a node that see the same set of GPUs, in any order, share them round-robin in node-local
 * rank order.
 * A scheduler or launcher that gives each rank its own GPUs, by restricting the GPUs a rank can see
 * (for example with `CUDA_VISIBLE_DEVICES`, `ROCR_VISIBLE_DEVICES` or `ONEAPI_DEVICE_SELECTOR`),
 * puts each rank in a set of its own, so the rank uses the first GPU it was given.
 *
 * With CUDA or HIP, PETSc and OpenSn share the runtime's current device, and PETSc selects its
 * device while it initializes. When PETSc has a CUDA or HIP backend, this therefore sets its
 * `-device_select_cuda` or `-device_select_hip` option, whichever matches that backend, to the
 * selected device. Selecting a different PETSc device, for example through `PETSC_OPTIONS`, makes
 * PETSc and OpenSn use different GPUs. With SYCL, PETSc keeps its own selection, which does not
 * change the device OpenSn uses.
 *
 * Call once per process, after MPI is initialized and before `PetscInitialize` or any GPU work.
 * Collective over `comm`. Does nothing without GPU support or on a rank that sees no GPU. If
 * listing or selecting the GPUs fails on any rank, every rank throws `std::runtime_error` after the
 * collective calls. The message of a rank that failed names the rank and the cause.
 */
void BindDevice(MPI_Comm comm);

/**
 * Makes the GPU that BindDevice selected the current device of the calling thread. The current
 * device is per thread, so every thread other than the one that called BindDevice calls this
 * before doing GPU work. Does nothing if BindDevice selected no GPU.
 */
void UseBoundDevice();

/**
 * Returns the index, in the device order of node-local rank `node_rank`, of the GPU that the rank
 * uses, or -1 when it sees none. `visible_gpus[i]` lists the identifiers, as GetVisibleGPUIDs()
 * returns them, of the GPUs that node-local rank i sees, in its device order. Ranks that see the
 * same set of GPUs, in any order, share them round-robin in node-local rank order, taking them in
 * the order the first of those ranks lists them. An identifier listed more than once counts as one
 * GPU, and a rank uses its first device with the identifier it selects.
 */
int SelectDeviceIndex(const std::vector<std::vector<std::string>>& visible_gpus, int node_rank);

/**
 * Returns an identifier of each GPU this process can see, in device order. The identifier combines
 * the GPU's PCI bus ID and UUID, so it differs between the GPUs on a node and between the MIG
 * instances of one GPU, and is the same in every process that sees the GPU. SYCL lists a GPU once
 * for each of its backends that reaches it, so the same identifier can appear more than once.
 * When a SYCL device does not support these queries, the identifiers are the device indices.
 * Returns an empty list without GPU support.
 */
std::vector<std::string> GetVisibleGPUIDs();

/// Makes `device`, an index into GetVisibleGPUIDs(), the current GPU of the calling thread.
void SetCurrentGPU(int device);

/// Returns whether OpenSn's GPU backend is CUDA or HIP, whose current device PETSc also sets.
bool GPUBackendIsCUDAOrHIP();

} // namespace opensn