NAMD
ComputeCUDAMgr.C
Go to the documentation of this file.
1 #include "NamdTypes.h"
2 #include "common.h"
3 #include "Node.h"
4 #include "ComputeCUDAMgr.h"
5 
6 #include "DeviceCUDA.h"
7 #if defined(NAMD_CUDA) || defined(NAMD_HIP)
8 #ifdef WIN32
9 #define __thread __declspec(thread)
10 #endif
11 extern __thread DeviceCUDA *deviceCUDA;
12 
13 //
14 // Class constructor
15 //
17  // __sdag_init();
18  numDevices = 0;
19  // numNodesContributed = 0;
20  // numDevicesMax = 0;
21 }
22 
23 //
24 // Class constructor
25 //
26 ComputeCUDAMgr::ComputeCUDAMgr(CkMigrateMessage *) {
27  // __sdag_init();
28  NAMD_bug("ComputeCUDAMgr cannot be migrated");
29  numDevices = 0;
30  // numNodesContributed = 0;
31  // numDevicesMax = 0;
32 }
33 
34 //
35 // Class destructor
36 //
38  for (int i=0;i < numDevices;i++) {
39  if (cudaNonbondedTablesList[i] != NULL) delete cudaNonbondedTablesList[i];
40  if (cudaComputeNonbondedList[i] != NULL) delete cudaComputeNonbondedList[i];
41 #ifdef BONDED_CUDA
42  if (computeBondedCUDAList[i] != NULL) delete computeBondedCUDAList[i];
43 #endif
44  }
45 }
46 
47 //
48 // Initialize manager
49 // This gets called on rank 0 of each node
50 //
51 void ComputeCUDAMgr::initialize(CkQdMsg *msg) {
52  if (msg != NULL) delete msg;
53 
54  numDevices = deviceCUDA->getDeviceCount();
55 
56  // Create pointers to devices
57  cudaNonbondedTablesList.resize(numDevices, NULL);
58  cudaComputeNonbondedList.resize(numDevices, NULL);
59 #ifdef BONDED_CUDA
60  computeBondedCUDAList.resize(numDevices, NULL);
61 #endif
62 
63  // Create CUDA non-bonded tables for all devices that are used for computation
64  for (int i=0;i < deviceCUDA->getNumDevice();i++) {
65  int deviceID = deviceCUDA->getDeviceIDbyRank(i);
66  cudaNonbondedTablesList[deviceID] = new CudaNonbondedTables(deviceID);
67  }
68 }
69 
70 //
71 // Update nonbonded tables
72 // Should be called only on rank 0 of each node
73 //
75  if ( CkMyRank() ) NAMD_bug("ComputeCUDAMgr::update() should be called only by rank 0");
76  for (int i=0; i < deviceCUDA->getNumDevice(); i++) {
77  int deviceID = deviceCUDA->getDeviceIDbyRank(i);
78  // calls update function from CudaNonbondedTables
79  cudaNonbondedTablesList[deviceID]->updateTables();
80  }
81 }
82 
84  // Get pointer to ComputeCUDAMgr on this node
85  CProxy_ComputeCUDAMgr computeCUDAMgrProxy = CkpvAccess(BOCclass_group).computeCUDAMgr;
86  ComputeCUDAMgr* computeCUDAMgr = computeCUDAMgrProxy.ckLocalBranch();
87  if (computeCUDAMgr == NULL)
88  NAMD_bug("getComputeCUDAMgr, unable to locate local branch of BOC entry ComputeCUDAMgr");
89  return computeCUDAMgr;
90 }
91 
92 //
93 // Creates CudaComputeNonbonded object
94 //
96  int deviceID = deviceCUDA->getDeviceID();
97  if (cudaComputeNonbondedList.at(deviceID) != NULL)
98  NAMD_bug("ComputeCUDAMgr::createCudaComputeNonbonded called twice");
99  if (cudaNonbondedTablesList.at(deviceID) == NULL)
100  NAMD_bug("ComputeCUDAMgr::createCudaComputeNonbonded, non-bonded CUDA tables not created");
101  bool doStreaming = !deviceCUDA->getNoStreaming() && !Node::Object()->simParameters->GBISOn;
102  cudaComputeNonbondedList[deviceID] = new CudaComputeNonbonded(c, deviceID, *cudaNonbondedTablesList[deviceID], doStreaming);
103  return cudaComputeNonbondedList[deviceID];
104 }
105 
106 //
107 // Returns CudaComputeNonbonded for this Pe
108 //
110  // Get device ID for this Pe
111  int deviceID = deviceCUDA->getDeviceID();
112  CudaComputeNonbonded* p = cudaComputeNonbondedList[deviceID];
113  if (p == NULL)
114  NAMD_bug("ComputeCUDAMgr::getCudaComputeNonbonded(), device not created yet");
115  return p;
116 }
117 
118 #ifdef BONDED_CUDA
119 //
120 // Creates ComputeBondedCUDA object
121 //
122 ComputeBondedCUDA* ComputeCUDAMgr::createComputeBondedCUDA(ComputeID c, ComputeMgr* computeMgr) {
123  int deviceID = deviceCUDA->getDeviceID();
124  if (computeBondedCUDAList.at(deviceID) != NULL)
125  NAMD_bug("ComputeCUDAMgr::createComputeBondedCUDA called twice");
126  if (cudaNonbondedTablesList.at(deviceID) == NULL)
127  NAMD_bug("ComputeCUDAMgr::createCudaComputeNonbonded, non-bonded CUDA tables not created");
128  computeBondedCUDAList[deviceID] = new ComputeBondedCUDA(c, computeMgr, deviceID, *cudaNonbondedTablesList[deviceID]);
129  return computeBondedCUDAList[deviceID];
130 }
131 
132 //
133 // Returns ComputeBondedCUDA for this Pe
134 //
135 ComputeBondedCUDA* ComputeCUDAMgr::getComputeBondedCUDA() {
136  // Get device ID for this Pe
137  int deviceID = deviceCUDA->getDeviceID();
138  ComputeBondedCUDA* p = computeBondedCUDAList[deviceID];
139  if (p == NULL)
140  NAMD_bug("ComputeCUDAMgr::getComputeBondedCUDA(), device not created yet");
141  return p;
142 }
143 #endif // BONDED_CUDA
144 
145 #endif // NAMD_CUDA or NAMD_HIP
146 
147 #include "ComputeCUDAMgr.def.h"
static Node * Object()
Definition: Node.h:86
int getDeviceCount()
Definition: DeviceCUDA.h:92
int ComputeID
Definition: NamdTypes.h:183
static __thread ComputeMgr * computeMgr
SimParameters * simParameters
Definition: Node.h:178
void initialize(CkQdMsg *msg)
int getNumDevice()
Definition: DeviceCUDA.h:93
void NAMD_bug(const char *err_msg)
Definition: common.C:129
static ComputeCUDAMgr * getComputeCUDAMgr()
int getDeviceID()
Definition: DeviceCUDA.h:112
int getDeviceIDbyRank(int rank)
Definition: DeviceCUDA.h:113
__thread DeviceCUDA * deviceCUDA
Definition: DeviceCUDA.C:22
int getNoStreaming()
Definition: DeviceCUDA.h:98
CudaComputeNonbonded * getCudaComputeNonbonded()
CudaComputeNonbonded * createCudaComputeNonbonded(ComputeID c)