Sierra Toolkit  Version of the Day
CudaDeviceMgr.cpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 
10 #include <iostream>
11 
12 #ifdef STK_HAVE_CUDA
13 
14 #include <stk_algsup/CudaDeviceMgr.hpp>
15 
16 namespace stk_classic {
17 
18 CudaDeviceMgr& CudaDeviceMgr::get_singleton()
19 {
20  static CudaDeviceMgr cuda_device_mgr;
21  return cuda_device_mgr;
22 }
23 
24 CudaDeviceMgr::CudaDeviceMgr(int device)
25  : m_device(device)
26 {
27  int deviceCount = 0;
28  cudaGetDeviceCount(&deviceCount);
29  if (deviceCount < 1) {
30  std::cout << "CudaDeviceMgr: no devices detected." << std::endl;
31  //what should we do here? Abort? Throw? Continue?
32  }
33 
34  if (m_device >= deviceCount) {
35  std::cout << "CudaDeviceMgr: specified device not valid, using device 0." << std::endl;
36  m_device = 0;
37  }
38 
39  //for now: if a cuda device is already in use, just use that one. In future we may
40  //want to allow for using multiple different devices...
41 
42  int deviceAlreadyBeingUsed = -1;
43  cudaGetDevice( &deviceAlreadyBeingUsed );
44  if (deviceAlreadyBeingUsed >= 0 && deviceAlreadyBeingUsed < deviceCount) {
45  m_device = deviceAlreadyBeingUsed;
46  }
47  else {
48  cudaSetDevice(m_device);
49  }
50 
51  cudaDeviceProp deviceProp;
52 
53  cudaGetDeviceProperties(&deviceProp, m_device);
54 
55  //TODO: make this output only occur in debug mode or verbose mode, or something:
56  std::cout << "\nCudaDeviceMgr attached to device #"<<m_device<<" '"
57  << deviceProp.name << "', compute capability " << deviceProp.major << "." << deviceProp.minor
58  << std::endl;
59 }
60 
61 }//namespace stk_classic
62 
63 #endif
64 
Sierra Toolkit.