xref: /petsc/src/sys/objects/device/impls/cupm/cupmdevice.cxx (revision 4d81c09ef4cefe32a382d98cf0a16804df0ea70d)
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 PetscErrorCode Device<T>::initialize(MPI_Comm comm, PetscInt *defaultDeviceId, PetscDeviceInitType *defaultInitType) noexcept
295 {
296   PetscInt       initTypeCUPM = *defaultInitType,id = *defaultDeviceId;
297   PetscBool      view = PETSC_FALSE,flg;
298   int            ndev;
299   cupmError_t    cerr;
300   PetscErrorCode ierr;
301 
302   PetscFunctionBegin;
303   if (initialized_) PetscFunctionReturn(0);
304   initialized_ = true;
305   ierr = PetscRegisterFinalize(finalize_);CHKERRQ(ierr);
306 
307   {
308     // the functions to populate the command line strings are named after the string they return
309     ierr = PetscOptionsBegin(comm,nullptr,PetscDevice_CUPMTYPE_Options<T>(),"Sys");CHKERRQ(ierr);
310     ierr = PetscOptionsEList(device_enable_cupmtype<T>(),"How (or whether) to initialize a device","CUPMDevice<CUPMDeviceType>::initialize()",PetscDeviceInitTypes,3,PetscDeviceInitTypes[initTypeCUPM],&initTypeCUPM,nullptr);CHKERRQ(ierr);
311     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);
312     ierr = PetscOptionsBool(device_view_cupmtype<T>(),"Display device information and assignments (forces eager initialization)",nullptr,view,&view,&flg);CHKERRQ(ierr);
313     ierr = PetscOptionsEnd();CHKERRQ(ierr);
314   }
315 
316   cerr = cupmGetDeviceCount(&ndev);
317   // post-process the options and lay the groundwork for initialization if needs be
318   if (PetscUnlikely((cerr == cupmErrorStubLibrary) || (cerr == cupmErrorNoDevice))) {
319     if (PetscUnlikely((initTypeCUPM == PETSC_DEVICE_INIT_EAGER) || (view && flg))) {
320       const auto name    = cupmGetErrorName(cerr);
321       const auto desc    = cupmGetErrorString(cerr);
322       const auto backend = cupmName();
323       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);
324     }
325     id   = -cerr;
326     cerr = cupmGetLastError(); // reset error
327     initTypeCUPM = PETSC_DEVICE_INIT_NONE;
328   } else CHKERRCUPM(cerr);
329 
330   if (initTypeCUPM == PETSC_DEVICE_INIT_NONE) {
331     if ((id > 0) || (id == PETSC_DECIDE)) id = PETSC_CUPM_DEVICE_NONE;
332   } else {
333     ierr = PetscDeviceCheckDeviceCount_Internal(ndev);CHKERRQ(ierr);
334     if (id == PETSC_DECIDE) {
335       if (ndev) {
336         PetscMPIInt rank;
337 
338         ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr);
339         id   = rank % ndev;
340       } else id = 0;
341     }
342     view = static_cast<decltype(view)>(view && flg);
343     if (view) initTypeCUPM = PETSC_DEVICE_INIT_EAGER;
344   }
345 
346   static_assert(std::is_same<PetscMPIInt,decltype(defaultDevice_)>::value,"");
347   // id is PetscInt, _defaultDevice is int
348   ierr = PetscMPIIntCast(id,&defaultDevice_);CHKERRQ(ierr);
349   if (initTypeCUPM == PETSC_DEVICE_INIT_EAGER) {
350     devices_[defaultDevice_] = DeviceInternal::makeDevice(defaultDevice_);
351     ierr = devices_[defaultDevice_]->initialize();CHKERRQ(ierr);
352     ierr = devices_[defaultDevice_]->configure();CHKERRQ(ierr);
353     if (view) {
354       PetscViewer vwr;
355 
356       ierr = PetscLogInitialize();CHKERRQ(ierr);
357       ierr = PetscViewerASCIIGetStdout(comm,&vwr);CHKERRQ(ierr);
358       ierr = devices_[defaultDevice_]->view(vwr);CHKERRQ(ierr);
359     }
360   }
361 
362   // record the results of the initialization
363   *defaultInitType = static_cast<PetscDeviceInitType>(initTypeCUPM);
364   *defaultDeviceId = id;
365   PetscFunctionReturn(0);
366 }
367 
368 template <DeviceType T>
369 PetscErrorCode Device<T>::getDevice(PetscDevice device, PetscInt id) const noexcept
370 {
371   const auto     cerr = static_cast<cupmError_t>(-defaultDevice_);
372   PetscErrorCode ierr;
373 
374   PetscFunctionBegin;
375   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());
376   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));
377   if (id == PETSC_DECIDE) id = defaultDevice_;
378   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);
379   if (devices_[id]) {
380     PetscAssert(id == devices_[id]->id(),PETSC_COMM_SELF,PETSC_ERR_PLIB,"Entry %" PetscInt_FMT " contains device with mismatching id %d",id,devices_[id]->id());
381   } else devices_[id] = DeviceInternal::makeDevice(id);
382   ierr = devices_[id]->initialize();CHKERRQ(ierr);
383   device->deviceId           = devices_[id]->id(); // technically id = _devices[id]->_id here
384   device->ops->createcontext = create_;
385   device->ops->configure     = this->configureDevice;
386   device->ops->view          = this->viewDevice;
387   PetscFunctionReturn(0);
388 }
389 
390 template <DeviceType T>
391 PetscErrorCode Device<T>::configureDevice(PetscDevice device) noexcept
392 {
393   PetscErrorCode ierr;
394 
395   PetscFunctionBegin;
396   ierr = devices_[device->deviceId]->configure();CHKERRQ(ierr);
397   PetscFunctionReturn(0);
398 }
399 
400 template <DeviceType T>
401 PetscErrorCode Device<T>::viewDevice(PetscDevice device, PetscViewer viewer) noexcept
402 {
403   PetscErrorCode ierr;
404 
405   PetscFunctionBegin;
406   // now this __shouldn't__ reconfigure the device, but there is a petscinfo call to indicate
407   // it is being reconfigured
408   ierr = devices_[device->deviceId]->configure();CHKERRQ(ierr);
409   ierr = devices_[device->deviceId]->view(viewer);CHKERRQ(ierr);
410   PetscFunctionReturn(0);
411 }
412 
413 // explicitly instantiate the classes
414 #if PetscDefined(HAVE_CUDA)
415 template class Device<DeviceType::CUDA>;
416 #endif
417 #if PetscDefined(HAVE_HIP)
418 template class Device<DeviceType::HIP>;
419 #endif
420 
421 } // namespace CUPM
422 
423 } // namespace Device
424 
425 } // namespace Petsc
426