1 /*-------------------------------------------------------------------
2 Copyright 2011 Ravishankar Sundararaman
3 
4 This file is part of JDFTx.
5 
6 JDFTx is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 JDFTx is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with JDFTx.  If not, see <http://www.gnu.org/licenses/>.
18 -------------------------------------------------------------------*/
19 
20 #ifndef JDFTX_CORE_GPUUTIL_H
21 #define JDFTX_CORE_GPUUTIL_H
22 
23 #ifdef GPU_ENABLED
24 
25 #include <cstdio>
26 #include <cuda_runtime.h>
27 #include <cublas_v2.h>
28 #include <cufft.h>
29 #include <vector>
30 
31 //! @addtogroup Utilities
32 //! @{
33 
34 //! @file GpuUtil.h
35 
36 extern cublasHandle_t cublasHandle; //!< global handle to cublas (defined in GpuUtil.cpp)
37 #ifdef CUSOLVER_ENABLED
38 #include <cusolverDn.h>
39 extern cusolverDnHandle_t cusolverHandle;  //!< global handle to cusolverDn (defined in GpuUtil.cpp)
40 #endif
41 
42 //! Must be called before any GPU use (preferably from main(), see #isGpuMine)
43 //! If mpiHostGpu (group of GPU processes on the same node) is specified, divide compatible GPUs amongst processes on same node, else select one with max memory
44 //! nGPUs returns the number of physical GPUs used (fraction if a GPU is shared with other processes)
45 //! Returns false on failure to find a suitable GPU
46 bool gpuInit(FILE* fpLog=stdout, const class MPIUtil* mpiHostGpu=0, double* nGPUs=0);
47 
48 //! Only the thread that called gpuInit() is allowed to use GPU resources
49 //! This function will return true only on the one thread that rules the gpu.
50 bool isGpuMine();
51 
52 //! Check for gpu errors, and if any, abort with a useful messsage
53 extern void gpuErrorCheck();
54 
55 #endif //GPU_ENABLED
56 
57 
58 //! A utility function to eliminate ugly \#ifdef's when not required
isGpuEnabled()59 inline bool isGpuEnabled()
60 {
61 	#ifdef GPU_ENABLED
62 	return true;
63 	#else
64 	return false;
65 	#endif
66 }
67 
68 //! @}
69 #endif // JDFTX_CORE_GPUUTIL_H
70