Sierra Toolkit Version of the Day
CudaDeviceMgr.cpp
00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 
00010 #include <iostream>
00011 
00012 #ifdef STK_HAVE_CUDA
00013 
00014 #include <stk_algsup/CudaDeviceMgr.hpp>
00015 
00016 namespace stk {
00017 
00018 CudaDeviceMgr& CudaDeviceMgr::get_singleton()
00019 {
00020   static CudaDeviceMgr cuda_device_mgr;
00021   return cuda_device_mgr;
00022 }
00023 
00024 CudaDeviceMgr::CudaDeviceMgr(int device)
00025  : m_device(device)
00026 {
00027   int deviceCount = 0;
00028   cudaGetDeviceCount(&deviceCount);
00029   if (deviceCount < 1) {
00030     std::cout << "CudaDeviceMgr: no devices detected." << std::endl;
00031     //what should we do here? Abort? Throw? Continue?
00032   }
00033 
00034   if (m_device >= deviceCount) {
00035     std::cout << "CudaDeviceMgr: specified device not valid, using device 0." << std::endl;
00036     m_device = 0;
00037   }
00038 
00039   //for now: if a cuda device is already in use, just use that one. In future we may
00040   //want to allow for using multiple different devices...
00041 
00042   int deviceAlreadyBeingUsed = -1;
00043   cudaGetDevice( &deviceAlreadyBeingUsed );
00044   if (deviceAlreadyBeingUsed >= 0 && deviceAlreadyBeingUsed < deviceCount) {
00045     m_device = deviceAlreadyBeingUsed;
00046   }
00047   else {
00048     cudaSetDevice(m_device);
00049   }
00050 
00051   cudaDeviceProp deviceProp;
00052 
00053   cudaGetDeviceProperties(&deviceProp, m_device);
00054 
00055   //TODO: make this output only occur in debug mode or verbose mode, or something:
00056   std::cout << "\nCudaDeviceMgr attached to device #"<<m_device<<" '"
00057     << deviceProp.name << "', compute capability " << deviceProp.major << "." << deviceProp.minor
00058     << std::endl;
00059 }
00060 
00061 }//namespace stk
00062 
00063 #endif
00064 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends