1 #include "../../interface/cupmdevice.hpp" 2 #include <algorithm> 3 #include <csetjmp> // for cuda mpi awareness 4 #include <csignal> // SIGSEGV 5 #include <iterator> 6 #include <type_traits> 7 8 #if PetscDefined(USE_LOG) 9 PETSC_INTERN PetscErrorCode PetscLogInitialize(void); 10 #else 11 #define PetscLogInitialize() 0 12 #endif 13 14 namespace Petsc 15 { 16 17 namespace Device 18 { 19 20 namespace CUPM 21 { 22 23 // internal "impls" class for CUPMDevice. Each instance represents a single cupm device 24 template <DeviceType T> 25 class Device<T>::DeviceInternal 26 { 27 const int id_; 28 bool devInitialized_ = false; 29 cupmDeviceProp_t dprop_; // cudaDeviceProp appears to be an actual struct, i.e. you can't 30 // initialize it with nullptr or NULL (i've tried) 31 32 PETSC_CXX_COMPAT_DECL(bool CUPMAwareMPI_()); 33 34 public: 35 // default constructor 36 explicit constexpr DeviceInternal(int dev) noexcept : id_(dev) { } 37 38 // gather all relevant information for a particular device, a cupmDeviceProp_t is 39 // usually sufficient here 40 PETSC_NODISCARD PetscErrorCode initialize() noexcept; 41 PETSC_NODISCARD PetscErrorCode configure() noexcept; 42 PETSC_NODISCARD PetscErrorCode view(PetscViewer) const noexcept; 43 PETSC_NODISCARD PetscErrorCode finalize() noexcept; 44 45 PETSC_NODISCARD auto id() const -> decltype(id_) { return id_; } 46 PETSC_NODISCARD auto initialized() const -> decltype(devInitialized_) { return devInitialized_; } 47 PETSC_NODISCARD auto prop() const -> const decltype(dprop_)& { return dprop_; } 48 49 // factory 50 PETSC_CXX_COMPAT_DECL(std::unique_ptr<DeviceInternal> makeDevice(int i)) 51 { 52 return std::unique_ptr<DeviceInternal>(new DeviceInternal(i)); 53 } 54 }; 55 56 // the goal here is simply to get the cupm backend to create its context, not to do any type of 57 // modification of it, or create objects (since these may be affected by subsequent 58 // configuration changes) 59 template <DeviceType T> 60 PetscErrorCode Device<T>::DeviceInternal::initialize() noexcept 61 { 62 cupmError_t cerr; 63 64 PetscFunctionBegin; 65 if (devInitialized_) PetscFunctionReturn(0); 66 devInitialized_ = true; 67 // need to do this BEFORE device has been set, although if the user 68 // has already done this then we just ignore it 69 if (cupmSetDeviceFlags(cupmDeviceMapHost) == cupmErrorSetOnActiveProcess) { 70 // reset the error if it was cupmErrorSetOnActiveProcess 71 const auto PETSC_UNUSED unused = cupmGetLastError(); 72 } else {CHKERRCUPM(cupmGetLastError());} 73 // cuda 5.0+ will create a context when cupmSetDevice is called 74 if (cupmSetDevice(id_) != cupmErrorDeviceAlreadyInUse) CHKERRCUPM(cupmGetLastError()); 75 // forces cuda < 5.0 to initialize a context 76 cerr = cupmFree(nullptr);CHKERRCUPM(cerr); 77 // where is this variable defined and when is it set? who knows! but it is defined and set 78 // at this point. either way, each device must make this check since I guess MPI might not be 79 // aware of all of them? 80 if (use_gpu_aware_mpi) { 81 // For OpenMPI, we could do a compile time check with 82 // "defined(PETSC_HAVE_OMPI_MAJOR_VERSION) && defined(MPIX_CUDA_AWARE_SUPPORT) && 83 // MPIX_CUDA_AWARE_SUPPORT" to see if it is CUDA-aware. However, recent versions of IBM 84 // Spectrum MPI (e.g., 10.3.1) on Summit meet above conditions, but one has to use jsrun 85 // --smpiargs=-gpu to really enable GPU-aware MPI. So we do the check at runtime with a 86 // code that works only with GPU-aware MPI. 87 if (PetscUnlikely(!CUPMAwareMPI_())) { 88 (*PetscErrorPrintf)("PETSc is configured with GPU support, but your MPI is not GPU-aware. For better performance, please use a GPU-aware MPI.\n"); 89 (*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"); 90 (*PetscErrorPrintf)("If you do care, for IBM Spectrum MPI on OLCF Summit, you may need jsrun --smpiargs=-gpu.\n"); 91 (*PetscErrorPrintf)("For OpenMPI, you need to configure it --with-cuda (https://www.open-mpi.org/faq/?category=buildcuda)\n"); 92 (*PetscErrorPrintf)("For MVAPICH2-GDR, you need to set MV2_USE_CUDA=1 (http://mvapich.cse.ohio-state.edu/userguide/gdr/)\n"); 93 (*PetscErrorPrintf)("For Cray-MPICH, you need to set MPICH_RDMA_ENABLED_CUDA=1 (https://www.olcf.ornl.gov/tutorials/gpudirect-mpich-enabled-cuda/)\n"); 94 PETSCABORT(PETSC_COMM_SELF,PETSC_ERR_LIB); 95 } 96 } 97 PetscFunctionReturn(0); 98 } 99 100 template <DeviceType T> 101 PetscErrorCode Device<T>::DeviceInternal::configure() noexcept 102 { 103 cupmError_t cerr; 104 PetscErrorCode ierr; 105 106 PetscFunctionBegin; 107 PetscAssert(devInitialized_,PETSC_COMM_SELF,PETSC_ERR_COR,"Device %d being configured before it was initialized",id_); 108 // why on EARTH nvidia insists on making otherwise informational states into 109 // fully-fledged error codes is beyond me. Why couldn't a pointer to bool argument have 110 // sufficed?!?!?! 111 if (cupmSetDevice(id_) != cupmErrorDeviceAlreadyInUse) CHKERRCUPM(cupmGetLastError()); 112 // need to update the device properties 113 cerr = cupmGetDeviceProperties(&dprop_,id_);CHKERRCUPM(cerr); 114 ierr = PetscInfo(nullptr,"Configured device %d\n",id_);CHKERRQ(ierr); 115 PetscFunctionReturn(0); 116 } 117 118 template <DeviceType T> 119 PetscErrorCode Device<T>::DeviceInternal::view(PetscViewer viewer) const noexcept 120 { 121 PetscBool iascii; 122 PetscErrorCode ierr; 123 124 PetscFunctionBegin; 125 PetscAssert(devInitialized_,PETSC_COMM_SELF,PETSC_ERR_COR,"Device %d being viewed before it was initialized or configured",id_); 126 ierr = PetscObjectTypeCompare(PetscObjectCast(viewer),PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 127 if (iascii) { 128 MPI_Comm comm; 129 PetscMPIInt rank; 130 PetscViewer sviewer; 131 132 ierr = PetscObjectGetComm(PetscObjectCast(viewer),&comm);CHKERRQ(ierr); 133 ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 134 ierr = PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);CHKERRQ(ierr); 135 ierr = PetscViewerASCIIPrintf(sviewer,"[%d] device %d: %s\n",rank,id_,dprop_.name);CHKERRQ(ierr); 136 ierr = PetscViewerASCIIPushTab(sviewer);CHKERRQ(ierr); 137 ierr = PetscViewerASCIIPrintf(sviewer,"Compute capability: %d.%d\n",dprop_.major,dprop_.minor);CHKERRQ(ierr); 138 ierr = PetscViewerASCIIPrintf(sviewer,"Multiprocessor Count: %d\n",dprop_.multiProcessorCount);CHKERRQ(ierr); 139 ierr = PetscViewerASCIIPrintf(sviewer,"Maximum Grid Dimensions: %d x %d x %d\n",dprop_.maxGridSize[0],dprop_.maxGridSize[1],dprop_.maxGridSize[2]);CHKERRQ(ierr); 140 ierr = PetscViewerASCIIPrintf(sviewer,"Maximum Block Dimensions: %d x %d x %d\n",dprop_.maxThreadsDim[0],dprop_.maxThreadsDim[1],dprop_.maxThreadsDim[2]);CHKERRQ(ierr); 141 ierr = PetscViewerASCIIPrintf(sviewer,"Maximum Threads Per Block: %d\n",dprop_.maxThreadsPerBlock);CHKERRQ(ierr); 142 ierr = PetscViewerASCIIPrintf(sviewer,"Warp Size: %d\n",dprop_.warpSize);CHKERRQ(ierr); 143 ierr = PetscViewerASCIIPrintf(sviewer,"Total Global Memory (bytes): %zu\n",dprop_.totalGlobalMem);CHKERRQ(ierr); 144 ierr = PetscViewerASCIIPrintf(sviewer,"Total Constant Memory (bytes): %zu\n",dprop_.totalConstMem);CHKERRQ(ierr); 145 ierr = PetscViewerASCIIPrintf(sviewer,"Shared Memory Per Block (bytes): %zu\n",dprop_.sharedMemPerBlock);CHKERRQ(ierr); 146 ierr = PetscViewerASCIIPrintf(sviewer,"Multiprocessor Clock Rate (KHz): %d\n",dprop_.clockRate);CHKERRQ(ierr); 147 ierr = PetscViewerASCIIPrintf(sviewer,"Memory Clock Rate (KHz): %d\n",dprop_.memoryClockRate);CHKERRQ(ierr); 148 ierr = PetscViewerASCIIPrintf(sviewer,"Memory Bus Width (bits): %d\n",dprop_.memoryBusWidth);CHKERRQ(ierr); 149 ierr = PetscViewerASCIIPrintf(sviewer,"Peak Memory Bandwidth (GB/s): %f\n",2.0*dprop_.memoryClockRate*(dprop_.memoryBusWidth/8)/1.0e6);CHKERRQ(ierr); 150 ierr = PetscViewerASCIIPrintf(sviewer,"Can map host memory: %s\n",dprop_.canMapHostMemory ? "PETSC_TRUE" : "PETSC_FALSE");CHKERRQ(ierr); 151 ierr = PetscViewerASCIIPrintf(sviewer,"Can execute multiple kernels concurrently: %s\n",dprop_.concurrentKernels ? "PETSC_TRUE" : "PETSC_FALSE");CHKERRQ(ierr); 152 ierr = PetscViewerASCIIPopTab(sviewer);CHKERRQ(ierr); 153 ierr = PetscViewerFlush(sviewer);CHKERRQ(ierr); 154 ierr = PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);CHKERRQ(ierr); 155 ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 156 } 157 PetscFunctionReturn(0); 158 } 159 160 static std::jmp_buf cupmMPIAwareJumpBuffer; 161 static bool cupmMPIAwareJumpBufferSet; 162 163 // godspeed to anyone that attempts to call this function 164 void SilenceVariableIsNotNeededAndWillNotBeEmittedWarning_ThisFunctionShouldNeverBeCalled() 165 { 166 PETSCABORT(MPI_COMM_NULL,INT_MAX); 167 if (cupmMPIAwareJumpBufferSet) (void)cupmMPIAwareJumpBuffer; 168 } 169 170 #define CHKCUPMAWARE(expr) if (PetscUnlikely((expr) != cupmSuccess)) return false 171 172 template <DeviceType T> 173 PETSC_CXX_COMPAT_DEFN(bool Device<T>::DeviceInternal::CUPMAwareMPI_()) 174 { 175 constexpr int bufSize = 2; 176 constexpr int hbuf[bufSize] = {1,0}; 177 int *dbuf = nullptr; 178 constexpr auto bytes = bufSize*sizeof(*dbuf); 179 auto awareness = false; 180 cupmError_t cerr; 181 PetscErrorCode ierr; 182 const auto cupmSignalHandler = [](int signal, void *ptr) -> PetscErrorCode { 183 if ((signal == SIGSEGV) && cupmMPIAwareJumpBufferSet) std::longjmp(cupmMPIAwareJumpBuffer,1); 184 return PetscSignalHandlerDefault(signal,ptr); 185 }; 186 187 PetscFunctionBegin; 188 cerr = cupmMalloc(reinterpret_cast<void**>(&dbuf),bytes);CHKCUPMAWARE(cerr); 189 cerr = cupmMemcpy(dbuf,hbuf,bytes,cupmMemcpyHostToDevice);CHKCUPMAWARE(cerr); 190 ierr = PetscPushSignalHandler(cupmSignalHandler,nullptr);CHKERRABORT(PETSC_COMM_SELF,ierr); 191 cupmMPIAwareJumpBufferSet = true; 192 if (setjmp(cupmMPIAwareJumpBuffer)) { 193 // if a segv was triggered in the MPI_Allreduce below, it is very likely due to MPI not 194 // being GPU-aware 195 awareness = false; 196 // control flow up until this point: 197 // 1. CUPMDevice<T>::CUPMDeviceInternal::MPICUPMAware__() 198 // 2. MPI_Allreduce 199 // 3. SIGSEGV 200 // 4. PetscSignalHandler_Private 201 // 5. cupmSignalHandler (lambda function) 202 // 6. here 203 // PetscSignalHandler_Private starts with PetscFunctionBegin and is pushed onto the stack 204 // so we must undo this. This would be most naturally done in cupmSignalHandler, however 205 // the C/C++ standard dictates: 206 // 207 // After invoking longjmp(), non-volatile-qualified local objects should not be accessed if 208 // their values could have changed since the invocation of setjmp(). Their value in this 209 // case is considered indeterminate, and accessing them is undefined behavior. 210 // 211 // so for safety (since we don't know what PetscStackPop may try to read/declare) we do it 212 // outside of the longjmp control flow 213 PetscStackPop; 214 } else if (!MPI_Allreduce(dbuf,dbuf+1,1,MPI_INT,MPI_SUM,PETSC_COMM_SELF)) awareness = true; 215 cupmMPIAwareJumpBufferSet = false; 216 ierr = PetscPopSignalHandler();CHKERRABORT(PETSC_COMM_SELF,ierr); 217 cerr = cupmFree(dbuf);CHKCUPMAWARE(cerr); 218 PetscFunctionReturn(awareness); 219 } 220 221 #undef CHKCUPMAWARE 222 223 template <DeviceType T> 224 PetscErrorCode Device<T>::DeviceInternal::finalize() noexcept 225 { 226 PetscFunctionBegin; 227 devInitialized_ = false; 228 PetscFunctionReturn(0); 229 } 230 231 template <DeviceType T> 232 PetscErrorCode Device<T>::finalize_() noexcept 233 { 234 PetscFunctionBegin; 235 if (!initialized_) PetscFunctionReturn(0); 236 for (auto&& device : devices_) { 237 if (device) { 238 const auto ierr = device->finalize();CHKERRQ(ierr); 239 device.reset(); 240 } 241 } 242 defaultDevice_ = PETSC_CUPM_DEVICE_NONE; // disabled by default 243 initialized_ = false; 244 PetscFunctionReturn(0); 245 } 246 247 // these functions should be named identically to the option they produce where "CUPMTYPE" and 248 // "cupmtype" are the uppercase and lowercase string versions of the cupm backend respectively 249 template <DeviceType T> 250 PETSC_CXX_COMPAT_DECL(PETSC_CONSTEXPR_14 const char* PetscDevice_CUPMTYPE_Options()) 251 { 252 switch (T) { 253 case DeviceType::CUDA: return "PetscDevice CUDA Options"; 254 case DeviceType::HIP: return "PetscDevice HIP Options"; 255 } 256 PetscUnreachable(); 257 return "PETSC_ERROR_PLIB"; 258 } 259 260 template <DeviceType T> 261 PETSC_CXX_COMPAT_DECL(PETSC_CONSTEXPR_14 const char* device_enable_cupmtype()) 262 { 263 switch (T) { 264 case DeviceType::CUDA: return "-device_enable_cuda"; 265 case DeviceType::HIP: return "-device_enable_hip"; 266 } 267 PetscUnreachable(); 268 return "PETSC_ERROR_PLIB"; 269 } 270 271 template <DeviceType T> 272 PETSC_CXX_COMPAT_DECL(PETSC_CONSTEXPR_14 const char* device_select_cupmtype()) 273 { 274 switch (T) { 275 case DeviceType::CUDA: return "-device_select_cuda"; 276 case DeviceType::HIP: return "-device_select_hip"; 277 } 278 PetscUnreachable(); 279 return "PETSC_ERROR_PLIB"; 280 } 281 282 template <DeviceType T> 283 PETSC_CXX_COMPAT_DECL(PETSC_CONSTEXPR_14 const char* device_view_cupmtype()) 284 { 285 switch (T) { 286 case DeviceType::CUDA: return "-device_view_cuda"; 287 case DeviceType::HIP: return "-device_view_hip"; 288 } 289 PetscUnreachable(); 290 return "PETSC_ERROR_PLIB"; 291 } 292 293 template <DeviceType T> 294 PETSC_CXX_COMPAT_DECL(PETSC_CONSTEXPR_14 const char* CUPM_VISIBLE_DEVICES()) 295 { 296 switch (T) { 297 case DeviceType::CUDA: return "CUDA_VISIBLE_DEVICES"; 298 case DeviceType::HIP: return "HIP_VISIBLE_DEVICES"; 299 } 300 PetscUnreachable(); 301 return "PETSC_ERROR_PLIB"; 302 } 303 304 template <DeviceType T> 305 PetscErrorCode Device<T>::initialize(MPI_Comm comm, PetscInt *defaultDeviceId, PetscDeviceInitType *defaultInitType) noexcept 306 { 307 PetscInt initTypeCUPM = *defaultInitType,id = *defaultDeviceId; 308 PetscBool view = PETSC_FALSE,flg; 309 int ndev; 310 cupmError_t cerr; 311 PetscErrorCode ierr; 312 313 PetscFunctionBegin; 314 if (initialized_) PetscFunctionReturn(0); 315 initialized_ = true; 316 ierr = PetscRegisterFinalize(finalize_);CHKERRQ(ierr); 317 318 { 319 // the functions to populate the command line strings are named after the string they return 320 ierr = PetscOptionsBegin(comm,nullptr,PetscDevice_CUPMTYPE_Options<T>(),"Sys");CHKERRQ(ierr); 321 ierr = PetscOptionsEList(device_enable_cupmtype<T>(),"How (or whether) to initialize a device","CUPMDevice<CUPMDeviceType>::initialize()",PetscDeviceInitTypes,3,PetscDeviceInitTypes[initTypeCUPM],&initTypeCUPM,nullptr);CHKERRQ(ierr); 322 ierr = PetscOptionsRangeInt(device_select_cupmtype<T>(),"Which device to use. Pass " PetscStringize(PETSC_DECIDE) " to have PETSc decide or (given they exist) [0-NUM_DEVICE) for a specific device","PetscDeviceCreate",id,&id,nullptr,PETSC_DECIDE,std::numeric_limits<decltype(defaultDevice_)>::max());CHKERRQ(ierr); 323 ierr = PetscOptionsBool(device_view_cupmtype<T>(),"Display device information and assignments (forces eager initialization)",nullptr,view,&view,&flg);CHKERRQ(ierr); 324 ierr = PetscOptionsEnd();CHKERRQ(ierr); 325 } 326 327 cerr = cupmGetDeviceCount(&ndev); 328 switch (cerr) { 329 case cupmErrorNoDevice: { 330 PetscBool found; 331 PetscBool ignoreCupmError = PETSC_FALSE; 332 auto buf = std::array<char,PETSC_DEVICE_MAX_DEVICES>{}; 333 334 ierr = PetscOptionsGetenv(comm,CUPM_VISIBLE_DEVICES<T>(),buf.data(),buf.size(),&found);CHKERRQ(ierr); 335 if (found) { 336 for (auto it = buf.cbegin(); it != buf.cend(); ++it) { 337 if (!*it) continue; 338 // find out the first non-empty characters in buf are '-<some number>', which indicates 339 // no devices should be visible so we can ignore the errors about not finding devices 340 ignoreCupmError = static_cast<PetscBool>((*it == '-') && (std::next(it) != buf.cend()) && isdigit(*std::next(it))); 341 break; 342 } 343 } 344 id = PETSC_CUPM_DEVICE_NONE; // there are no devices anyway 345 if (ignoreCupmError) {auto PETSC_UNUSED ignored = cupmGetLastError(); break;} 346 // if we don't outright ignore the error we then drop and check if the user tried to 347 // eagerly initialize the device 348 } 349 case cupmErrorStubLibrary: 350 if (PetscUnlikely((initTypeCUPM == PETSC_DEVICE_INIT_EAGER) || (view && flg))) { 351 const auto name = cupmGetErrorName(cerr); 352 const auto desc = cupmGetErrorString(cerr); 353 const auto backend = cupmName(); 354 SETERRQ(comm,PETSC_ERR_USER_INPUT,"Cannot eagerly initialize %s, as doing so results in %s error %d (%s) : %s",backend,backend,static_cast<PetscErrorCode>(cerr),name,desc); 355 } 356 initTypeCUPM = PETSC_DEVICE_INIT_NONE; 357 {auto PETSC_UNUSED ignored = cupmGetLastError();} 358 break; 359 default: 360 CHKERRCUPM(cerr); 361 break; 362 } 363 364 if (initTypeCUPM == PETSC_DEVICE_INIT_NONE) { 365 if ((id > 0) || (id == PETSC_DECIDE)) id = PETSC_CUPM_DEVICE_NONE; 366 } else { 367 ierr = PetscDeviceCheckDeviceCount_Internal(ndev);CHKERRQ(ierr); 368 if (id == PETSC_DECIDE) { 369 if (ndev) { 370 PetscMPIInt rank; 371 372 ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 373 id = rank % ndev; 374 } else id = 0; 375 } 376 view = static_cast<decltype(view)>(view && flg); 377 if (view) initTypeCUPM = PETSC_DEVICE_INIT_EAGER; 378 } 379 380 static_assert(std::is_same<PetscMPIInt,decltype(defaultDevice_)>::value,""); 381 // id is PetscInt, _defaultDevice is int 382 ierr = PetscMPIIntCast(id,&defaultDevice_);CHKERRQ(ierr); 383 if (initTypeCUPM == PETSC_DEVICE_INIT_EAGER) { 384 devices_[defaultDevice_] = DeviceInternal::makeDevice(defaultDevice_); 385 ierr = devices_[defaultDevice_]->initialize();CHKERRQ(ierr); 386 ierr = devices_[defaultDevice_]->configure();CHKERRQ(ierr); 387 if (view) { 388 PetscViewer vwr; 389 390 ierr = PetscLogInitialize();CHKERRQ(ierr); 391 ierr = PetscViewerASCIIGetStdout(comm,&vwr);CHKERRQ(ierr); 392 ierr = devices_[defaultDevice_]->view(vwr);CHKERRQ(ierr); 393 } 394 } 395 396 // record the results of the initialization 397 *defaultInitType = static_cast<PetscDeviceInitType>(initTypeCUPM); 398 *defaultDeviceId = id; 399 PetscFunctionReturn(0); 400 } 401 402 template <DeviceType T> 403 PetscErrorCode Device<T>::getDevice(PetscDevice device, PetscInt id) const noexcept 404 { 405 const auto cerr = static_cast<cupmError_t>(-defaultDevice_); 406 PetscErrorCode ierr; 407 408 PetscFunctionBegin; 409 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()); 410 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)); 411 if (id == PETSC_DECIDE) id = defaultDevice_; 412 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 %" PetscInt_FMT,devices_.size(),id); 413 if (devices_[id]) { 414 PetscAssert(id == devices_[id]->id(),PETSC_COMM_SELF,PETSC_ERR_PLIB,"Entry %" PetscInt_FMT " contains device with mismatching id %d",id,devices_[id]->id()); 415 } else devices_[id] = DeviceInternal::makeDevice(id); 416 ierr = devices_[id]->initialize();CHKERRQ(ierr); 417 device->deviceId = devices_[id]->id(); // technically id = _devices[id]->_id here 418 device->ops->createcontext = create_; 419 device->ops->configure = this->configureDevice; 420 device->ops->view = this->viewDevice; 421 PetscFunctionReturn(0); 422 } 423 424 template <DeviceType T> 425 PetscErrorCode Device<T>::configureDevice(PetscDevice device) noexcept 426 { 427 PetscErrorCode ierr; 428 429 PetscFunctionBegin; 430 ierr = devices_[device->deviceId]->configure();CHKERRQ(ierr); 431 PetscFunctionReturn(0); 432 } 433 434 template <DeviceType T> 435 PetscErrorCode Device<T>::viewDevice(PetscDevice device, PetscViewer viewer) noexcept 436 { 437 PetscErrorCode ierr; 438 439 PetscFunctionBegin; 440 // now this __shouldn't__ reconfigure the device, but there is a petscinfo call to indicate 441 // it is being reconfigured 442 ierr = devices_[device->deviceId]->configure();CHKERRQ(ierr); 443 ierr = devices_[device->deviceId]->view(viewer);CHKERRQ(ierr); 444 PetscFunctionReturn(0); 445 } 446 447 // explicitly instantiate the classes 448 #if PetscDefined(HAVE_CUDA) 449 template class Device<DeviceType::CUDA>; 450 #endif 451 #if PetscDefined(HAVE_HIP) 452 template class Device<DeviceType::HIP>; 453 #endif 454 455 } // namespace CUPM 456 457 } // namespace Device 458 459 } // namespace Petsc 460