CMS 3D CMS Logo

ScopedSetDevice.h
Go to the documentation of this file.
1 #ifndef HeterogeneousCore_CUDAUtilities_ScopedSetDevice_h
2 #define HeterogeneousCore_CUDAUtilities_ScopedSetDevice_h
3 
5 
6 #include <cuda_runtime.h>
7 
8 namespace cms {
9  namespace cuda {
11  public:
12  // Store the original device, without setting a new one
14  // Store the original device
15  cudaCheck(cudaGetDevice(&originalDevice_));
16  }
17 
18  // Store the original device, and set a new current device
19  explicit ScopedSetDevice(int device) : ScopedSetDevice() {
20  // Change the current device
21  set(device);
22  }
23 
24  // Restore the original device
26  // Intentionally don't check the return value to avoid
27  // exceptions to be thrown. If this call fails, the process is
28  // doomed anyway.
29  cudaSetDevice(originalDevice_);
30  }
31 
32  // Set a new current device, without changing the original device
33  // that will be restored when this object is destroyed
34  void set(int device) {
35  // Change the current device
36  cudaCheck(cudaSetDevice(device));
37  }
38 
39  private:
41  };
42  } // namespace cuda
43 } // namespace cms
44 
45 #endif
Namespace of DDCMS conversion namespace.
#define cudaCheck(ARG,...)
Definition: cudaCheck.h:69