Date: 2025-03-29T05:58:44+00:00

Actual source code: cupmdevice.cxx

      1: #include <petsc/private/cpp/memory.hpp>

      3: #include "cupmdevice.hpp"

      5: #include <algorithm>
      6: #include <csetjmp> // for cuda mpi awareness
      7: #include <csignal> // SIGSEGV
      8: #include <iterator>
      9: #include <type_traits>

     11: namespace Petsc
     12: {

     14: namespace device
     15: {

     17: namespace cupm
     18: {

     20: // internal "impls" class for CUPMDevice. Each instance represents a single cupm device
     21: template <DeviceType T>
     22: class Device<T>::DeviceInternal {
     23:   const int        id_;
     24:   bool             devInitialized_ = false;
     25:   cupmDeviceProp_t dprop_{}; // cudaDeviceProp appears to be an actual struct, i.e. you can't
     26:                              // initialize it with nullptr or NULL (i've tried)

     28:   static PetscErrorCode CUPMAwareMPI_(bool *) noexcept;

     30: public:
     31:   // default constructor
     32:   explicit constexpr DeviceInternal(int dev) noexcept : id_(dev) { }

     34:   // gather all relevant information for a particular device, a cupmDeviceProp_t is
     35:   // usually sufficient here
     36:   PetscErrorCode initialize() noexcept;
     37:   PetscErrorCode configure() noexcept;
     38:   PetscErrorCode view(PetscViewer) const noexcept;
     39:   PetscErrorCode getattribute(PetscDeviceAttribute, void *) const noexcept;

     41:   PETSC_NODISCARD auto id() const -> decltype(id_) { return id_; }
     42:   PETSC_NODISCARD auto initialized() const -> decltype(devInitialized_) { return devInitialized_; }
     43:   PETSC_NODISCARD auto prop() const -> const decltype(dprop_) & { return dprop_; }
     44: };

     46: // the goal here is simply to get the cupm backend to create its context, not to do any type of
     47: // modification of it, or create objects (since these may be affected by subsequent
     48: // configuration changes)
     49: template <DeviceType T>
     50: PetscErrorCode Device<T>::DeviceInternal::initialize() noexcept
     51: {
     52:   PetscFunctionBegin;
     53:   if (initialized()) PetscFunctionReturn(PETSC_SUCCESS);
     54:   devInitialized_ = true;
     55:   // need to do this BEFORE device has been set, although if the user
     56:   // has already done this then we just ignore it
     57:   if (cupmSetDeviceFlags(cupmDeviceMapHost) == cupmErrorSetOnActiveProcess) {
     58:     // reset the error if it was cupmErrorSetOnActiveProcess
     59:     const auto PETSC_UNUSED unused = cupmGetLastError();
     60:   } else PetscCallCUPM(cupmGetLastError());
     61:   // cuda 5.0+ will create a context when cupmSetDevice is called
     62:   if (cupmSetDevice(id()) != cupmErrorDeviceAlreadyInUse) PetscCallCUPM(cupmGetLastError());
     63:   // and in case it doesn't, explicitly call init here
     64:   PetscCallCUPM(cupmInit(0));
     65:   // where is this variable defined and when is it set? who knows! but it is defined and set
     66:   // at this point. either way, each device must make this check since I guess MPI might not be
     67:   // aware of all of them?
     68:   if (use_gpu_aware_mpi) {
     69:     bool aware;

     71:     PetscCall(CUPMAwareMPI_(&aware));
     72:     // For Open MPI, we could do a compile time check with
     73:     // "defined(PETSC_HAVE_OPENMPI) && defined(MPIX_CUDA_AWARE_SUPPORT) &&
     74:     // MPIX_CUDA_AWARE_SUPPORT" to see if it is CUDA-aware. However, recent versions of IBM
     75:     // Spectrum MPI (e.g., 10.3.1) on Summit meet above conditions, but one has to use jsrun
     76:     // --smpiargs=-gpu to really enable GPU-aware MPI. So we do the check at runtime with a
     77:     // code that works only with GPU-aware MPI.
     78:     if (PetscUnlikely(!aware)) {
     79:       PetscCall((*PetscErrorPrintf)("PETSc is configured with GPU support, but your MPI is not GPU-aware. For better performance, please use a GPU-aware MPI.\n"));
     80:       PetscCall((*PetscErrorPrintf)("If you do not care, add option -use_gpu_aware_mpi 0. To not see the message again, add the option to your .petscrc, OR add it to the env var PETSC_OPTIONS.\n"));
     81:       PetscCall((*PetscErrorPrintf)("If you do care, for IBM Spectrum MPI on OLCF Summit, you may need jsrun --smpiargs=-gpu.\n"));
     82:       PetscCall((*PetscErrorPrintf)("For Open MPI, you need to configure it --with-cuda (https://www.open-mpi.org/faq/?category=buildcuda)\n"));
     83:       PetscCall((*PetscErrorPrintf)("For MVAPICH2-GDR, you need to set MV2_USE_CUDA=1 (http://mvapich.cse.ohio-state.edu/userguide/gdr/)\n"));
     84:       PetscCall((*PetscErrorPrintf)("For Cray-MPICH, you need to set MPICH_GPU_SUPPORT_ENABLED=1 (man mpi to see manual of cray-mpich)\n"));
     85:       PETSCABORT(PETSC_COMM_SELF, PETSC_ERR_LIB);
     86:     }
     87:   }
     88:   PetscFunctionReturn(PETSC_SUCCESS);
     89: }

     91: template <DeviceType T>
     92: PetscErrorCode Device<T>::DeviceInternal::configure() noexcept
     93: {
     94:   PetscFunctionBegin;
     95:   PetscAssert(initialized(), PETSC_COMM_SELF, PETSC_ERR_COR, "Device %d being configured before it was initialized", id());
     96:   // why on EARTH nvidia insists on making otherwise informational states into
     97:   // fully-fledged error codes is beyond me. Why couldn't a pointer to bool argument have
     98:   // sufficed?!?!?!
     99:   if (cupmSetDevice(id_) != cupmErrorDeviceAlreadyInUse) PetscCallCUPM(cupmGetLastError());
    100:   // need to update the device properties
    101:   PetscCallCUPM(cupmGetDeviceProperties(&dprop_, id_));
    102:   PetscDeviceCUPMRuntimeArch = dprop_.major * 10 + dprop_.minor;
    103:   PetscCall(PetscInfo(nullptr, "Configured device %d\n", id_));
    104:   PetscFunctionReturn(PETSC_SUCCESS);
    105: }

    107: template <DeviceType T>
    108: PetscErrorCode Device<T>::DeviceInternal::view(PetscViewer viewer) const noexcept
    109: {
    110:   PetscBool iascii;

    112:   PetscFunctionBegin;
    113:   PetscAssert(initialized(), PETSC_COMM_SELF, PETSC_ERR_COR, "Device %d being viewed before it was initialized or configured", id());
    114:   // we don't print device-specific info in CI-mode
    115:   if (PetscUnlikely(PetscCIEnabled)) PetscFunctionReturn(PETSC_SUCCESS);
    116:   PetscCall(PetscObjectTypeCompare(PetscObjectCast(viewer), PETSCVIEWERASCII, &iascii));
    117:   if (iascii) {
    118:     MPI_Comm    comm;
    119:     PetscMPIInt rank;
    120:     PetscViewer sviewer;

    122:     PetscCall(PetscObjectGetComm(PetscObjectCast(viewer), &comm));
    123:     PetscCallMPI(MPI_Comm_rank(comm, &rank));
    124:     PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
    125:     PetscCall(PetscViewerASCIIPrintf(sviewer, "[%d] name: %s\n", rank, dprop_.name));
    126:     PetscCall(PetscViewerASCIIPushTab(sviewer));
    127:     PetscCall(PetscViewerASCIIPrintf(sviewer, "Compute capability: %d.%d\n", dprop_.major, dprop_.minor));
    128:     PetscCall(PetscViewerASCIIPrintf(sviewer, "Multiprocessor Count: %d\n", dprop_.multiProcessorCount));
    129:     PetscCall(PetscViewerASCIIPrintf(sviewer, "Maximum Grid Dimensions: %d x %d x %d\n", dprop_.maxGridSize[0], dprop_.maxGridSize[1], dprop_.maxGridSize[2]));
    130:     PetscCall(PetscViewerASCIIPrintf(sviewer, "Maximum Block Dimensions: %d x %d x %d\n", dprop_.maxThreadsDim[0], dprop_.maxThreadsDim[1], dprop_.maxThreadsDim[2]));
    131:     PetscCall(PetscViewerASCIIPrintf(sviewer, "Maximum Threads Per Block: %d\n", dprop_.maxThreadsPerBlock));
    132:     PetscCall(PetscViewerASCIIPrintf(sviewer, "Warp Size: %d\n", dprop_.warpSize));
    133:     PetscCall(PetscViewerASCIIPrintf(sviewer, "Total Global Memory (bytes): %zu\n", dprop_.totalGlobalMem));
    134:     PetscCall(PetscViewerASCIIPrintf(sviewer, "Total Constant Memory (bytes): %zu\n", dprop_.totalConstMem));
    135:     PetscCall(PetscViewerASCIIPrintf(sviewer, "Shared Memory Per Block (bytes): %zu\n", dprop_.sharedMemPerBlock));
    136:     PetscCall(PetscViewerASCIIPrintf(sviewer, "Multiprocessor Clock Rate (KHz): %d\n", dprop_.clockRate));
    137:     PetscCall(PetscViewerASCIIPrintf(sviewer, "Memory Clock Rate (KHz): %d\n", dprop_.memoryClockRate));
    138:     PetscCall(PetscViewerASCIIPrintf(sviewer, "Memory Bus Width (bits): %d\n", dprop_.memoryBusWidth));
    139:     PetscCall(PetscViewerASCIIPrintf(sviewer, "Peak Memory Bandwidth (GB/s): %f\n", 2.0 * dprop_.memoryClockRate * (dprop_.memoryBusWidth / 8) / 1.0e6));
    140:     PetscCall(PetscViewerASCIIPrintf(sviewer, "Can map host memory: %s\n", dprop_.canMapHostMemory ? "PETSC_TRUE" : "PETSC_FALSE"));
    141:     PetscCall(PetscViewerASCIIPrintf(sviewer, "Can execute multiple kernels concurrently: %s\n", dprop_.concurrentKernels ? "PETSC_TRUE" : "PETSC_FALSE"));
    142:     PetscCall(PetscViewerASCIIPopTab(sviewer));
    143:     PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
    144:   }
    145:   PetscFunctionReturn(PETSC_SUCCESS);
    146: }

    148: template <DeviceType T>
    149: PetscErrorCode Device<T>::DeviceInternal::getattribute(PetscDeviceAttribute attr, void *value) const noexcept
    150: {
    151:   PetscFunctionBegin;
    152:   PetscAssert(initialized(), PETSC_COMM_SELF, PETSC_ERR_COR, "Device %d was not initialized", id());
    153:   switch (attr) {
    154:   case PETSC_DEVICE_ATTR_SIZE_T_SHARED_MEM_PER_BLOCK:
    155:     *static_cast<std::size_t *>(value) = prop().sharedMemPerBlock;
    156:   case PETSC_DEVICE_ATTR_MAX:
    157:     break;
    158:   }
    159:   PetscFunctionReturn(PETSC_SUCCESS);
    160: }

    162: static std::jmp_buf cupmMPIAwareJumpBuffer;
    163: static bool         cupmMPIAwareJumpBufferSet;

    165: // godspeed to anyone that attempts to call this function
    166: void SilenceVariableIsNotNeededAndWillNotBeEmittedWarning_ThisFunctionShouldNeverBeCalled()
    167: {
    168:   PETSCABORT(MPI_COMM_NULL, (PetscErrorCode)INT_MAX);
    169:   if (cupmMPIAwareJumpBufferSet) (void)cupmMPIAwareJumpBuffer;
    170: }

    172: template <DeviceType T>
    173: PetscErrorCode Device<T>::DeviceInternal::CUPMAwareMPI_(bool *awareness) noexcept
    174: {
    175:   constexpr int hbuf[]            = {1, 0};
    176:   int          *dbuf              = nullptr;
    177:   const auto    cupmSignalHandler = [](int signal, void *ptr) -> PetscErrorCode {
    178:     if ((signal == SIGSEGV) && cupmMPIAwareJumpBufferSet) std::longjmp(cupmMPIAwareJumpBuffer, 1);
    179:     return PetscSignalHandlerDefault(signal, ptr);
    180:   };

    182:   PetscFunctionBegin;
    183:   *awareness = false;
    184:   PetscCallCUPM(cupmMalloc(reinterpret_cast<void **>(&dbuf), sizeof(hbuf)));
    185:   PetscCallCUPM(cupmMemcpy(dbuf, hbuf, sizeof(hbuf), cupmMemcpyHostToDevice));
    186:   PetscCallCUPM(cupmDeviceSynchronize());
    187:   PetscCall(PetscPushSignalHandler(cupmSignalHandler, nullptr));
    188:   cupmMPIAwareJumpBufferSet = true;
    189:   if (!setjmp(cupmMPIAwareJumpBuffer) && !MPI_Allreduce(dbuf, dbuf + 1, 1, MPI_INT, MPI_SUM, PETSC_COMM_SELF)) *awareness = true;
    190:   cupmMPIAwareJumpBufferSet = false;
    191:   PetscCall(PetscPopSignalHandler());
    192:   PetscCallCUPM(cupmFree(dbuf));
    193:   PetscFunctionReturn(PETSC_SUCCESS);
    194: }

    196: template <DeviceType T>
    197: PetscErrorCode Device<T>::finalize_() noexcept
    198: {
    199:   PetscFunctionBegin;
    200:   if (PetscUnlikely(!initialized_)) PetscFunctionReturn(PETSC_SUCCESS);
    201:   for (auto &&device : devices_) device.reset();
    202:   defaultDevice_ = PETSC_CUPM_DEVICE_NONE; // disabled by default
    203:   initialized_   = false;
    204:   PetscFunctionReturn(PETSC_SUCCESS);
    205: }

    207: template <DeviceType T>
    208: PETSC_NODISCARD static PETSC_CONSTEXPR_14 const char *CUPM_VISIBLE_DEVICES() noexcept
    209: {
    210:   switch (T) {
    211:   case DeviceType::CUDA:
    212:     return "CUDA_VISIBLE_DEVICES";
    213:   case DeviceType::HIP:
    214:     return "HIP_VISIBLE_DEVICES";
    215:   }
    216:   PetscUnreachable();
    217:   return "PETSC_ERROR_PLIB";
    218: }

    220: /*
    221:      The default device ID is
    222:        MPI     -- rank % number_local_devices
    223:        PyTorch -- getenv("LOCAL_RANK")
    224: */
    225: template <DeviceType T>
    226: PetscErrorCode Device<T>::initialize(MPI_Comm comm, PetscInt *defaultDeviceId, PetscBool *defaultView, PetscDeviceInitType *defaultInitType) noexcept
    227: {
    228:   auto initId   = std::make_pair(*defaultDeviceId, PETSC_FALSE);
    229:   auto initView = std::make_pair(*defaultView, PETSC_FALSE);
    230:   auto initType = std::make_pair(*defaultInitType, PETSC_FALSE);
    231:   int  ndev     = 0;

    233:   PetscFunctionBegin;
    234:   if (initialized_) PetscFunctionReturn(PETSC_SUCCESS);
    235:   initialized_ = true;
    236:   PetscCall(PetscRegisterFinalize(finalize_));
    237:   PetscCall(base_type::PetscOptionDeviceAll(comm, initType, initId, initView));

    239:   if (initType.first == PETSC_DEVICE_INIT_NONE) {
    240:     initId.first = PETSC_CUPM_DEVICE_NONE;
    241:   } else if (const auto cerr = cupmGetDeviceCount(&ndev)) {
    242:     auto PETSC_UNUSED ignored = cupmGetLastError();

    244:     PetscCheck((initType.first != PETSC_DEVICE_INIT_EAGER) && !initView.first, comm, PETSC_ERR_USER_INPUT, "Cannot eagerly initialize %s, as doing so results in %s error %d (%s) : %s", cupmName(), cupmName(), static_cast<PetscErrorCode>(cerr), cupmGetErrorName(cerr), cupmGetErrorString(cerr));
    245:     // we won't be initializing anything anyways
    246:     initType.first = PETSC_DEVICE_INIT_NONE;
    247:     // save the error code for later
    248:     initId.first = -static_cast<decltype(initId.first)>(cerr);
    249:   }

    251:   // check again for init type, since the device count may have changed it
    252:   if (initType.first == PETSC_DEVICE_INIT_NONE) {
    253:     // id < 0 (excluding PETSC_DECIDE) indicates an error has occurred during setup
    254:     if ((initId.first > 0) || (initId.first == PETSC_DECIDE)) initId.first = PETSC_CUPM_DEVICE_NONE;
    255:     // initType overrides initView
    256:     initView.first = PETSC_FALSE;
    257:   } else {
    258:     PetscCall(PetscDeviceCheckDeviceCount_Internal(ndev));
    259:     if (initId.first == PETSC_DECIDE) {
    260:       if (ndev) {
    261:         /* TORCHELASTIC_RUN_ID is used as a proxy to determine if the current process was launched with torchrun */
    262:         char *pytorch_exists = (char *)getenv("TORCHELASTIC_RUN_ID");
    263:         char *pytorch_rank   = (char *)getenv("LOCAL_RANK");

    265:         if (pytorch_exists && pytorch_rank) {
    266:           char *endptr;

    268:           initId.first = (PetscInt)strtol(pytorch_rank, &endptr, 10);
    269:           PetscCheck(initId.first < ndev, PETSC_COMM_SELF, PETSC_ERR_LIB, "PyTorch environmental variable LOCAL_RANK %s > number devices %d", pytorch_rank, ndev);
    270:         } else {
    271:           PetscMPIInt rank;

    273:           PetscCallMPI(MPI_Comm_rank(comm, &rank));
    274:           initId.first = rank % ndev;
    275:         }
    276:       } else initId.first = 0;
    277:     }
    278:     if (initView.first) initType.first = PETSC_DEVICE_INIT_EAGER;
    279:   }

    281:   static_assert(std::is_same<PetscMPIInt, decltype(defaultDevice_)>::value, "");
    282:   // initId.first is PetscInt, _defaultDevice is int
    283:   PetscCall(PetscMPIIntCast(initId.first, &defaultDevice_));
    284:   // record the results of the initialization
    285:   *defaultDeviceId = initId.first;
    286:   *defaultView     = initView.first;
    287:   *defaultInitType = initType.first;
    288:   PetscFunctionReturn(PETSC_SUCCESS);
    289: }

    291: template <DeviceType T>
    292: PetscErrorCode Device<T>::init_device_id_(PetscInt *inid) const noexcept
    293: {
    294:   const auto id   = *inid == PETSC_DECIDE ? defaultDevice_ : (int)*inid;
    295:   const auto cerr = static_cast<cupmError_t>(-defaultDevice_);

    297:   PetscFunctionBegin;
    298:   PetscCheck(defaultDevice_ != PETSC_CUPM_DEVICE_NONE, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Trying to retrieve a %s PetscDevice when it has been disabled", cupmName());
    299:   PetscCheck(defaultDevice_ >= 0, PETSC_COMM_SELF, PETSC_ERR_GPU, "Cannot lazily initialize PetscDevice: %s error %d (%s) : %s", cupmName(), static_cast<PetscErrorCode>(cerr), cupmGetErrorName(cerr), cupmGetErrorString(cerr));
    300:   PetscAssert(static_cast<decltype(devices_.size())>(id) < devices_.size(), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Only supports %zu number of devices but trying to get device with id %d", devices_.size(), id);

    302:   if (!devices_[id]) devices_[id] = util::make_unique<DeviceInternal>(id);
    303:   PetscAssert(id == devices_[id]->id(), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Entry %d contains device with mismatching id %d", id, devices_[id]->id());
    304:   PetscCall(devices_[id]->initialize());
    305:   *inid = id;
    306:   PetscFunctionReturn(PETSC_SUCCESS);
    307: }

    309: template <DeviceType T>
    310: PetscErrorCode Device<T>::configure_device_(PetscDevice device) noexcept
    311: {
    312:   PetscFunctionBegin;
    313:   PetscCall(devices_[device->deviceId]->configure());
    314:   PetscFunctionReturn(PETSC_SUCCESS);
    315: }

    317: template <DeviceType T>
    318: PetscErrorCode Device<T>::view_device_(PetscDevice device, PetscViewer viewer) noexcept
    319: {
    320:   PetscFunctionBegin;
    321:   // now this __shouldn't__ reconfigure the device, but there is a petscinfo call to indicate
    322:   // it is being reconfigured
    323:   PetscCall(devices_[device->deviceId]->configure());
    324:   PetscCall(devices_[device->deviceId]->view(viewer));
    325:   PetscFunctionReturn(PETSC_SUCCESS);
    326: }

    328: template <DeviceType T>
    329: PetscErrorCode Device<T>::get_attribute_(PetscInt id, PetscDeviceAttribute attr, void *value) noexcept
    330: {
    331:   PetscFunctionBegin;
    332:   PetscCall(devices_[id]->getattribute(attr, value));
    333:   PetscFunctionReturn(PETSC_SUCCESS);
    334: }

    336: // explicitly instantiate the classes
    337: #if PetscDefined(HAVE_CUDA)
    338: template class Device<DeviceType::CUDA>;
    339: #endif
    340: #if PetscDefined(HAVE_HIP)
    341: template class Device<DeviceType::HIP>;
    342: #endif

    344: } // namespace cupm

    346: } // namespace device

    348: } // namespace Petsc