10 #define GF_OVERLAPCHECK_FREQ 1000 11 #define MIN_DEBUG_LEVEL 4 14 #ifdef NODEGROUP_FORCE_REGISTER 16 ComputeGridForceCUDA::ComputeGridForceCUDA(
17 std::vector<HomePatch*> &a_patchList,
18 std::vector<AtomMap*> &atomMapsList,
23 this->patchList=a_patchList;
26 allocate_device<unsigned int>(&d_tbcatomic, 1);
33 numGriddedAtoms=createGriddedLists();
40 int ComputeGridForceCUDA::updateGriddedAtoms(
41 std::vector<AtomMap*> &atomMapsList,
42 std::vector<CudaLocalRecord> &localRecords,
43 std::vector<HomePatch*> &patches,
44 const int *h_globalToLocalID,
49 DebugM(4,
"ComputeGridForceCUDA::updateGriddedLists "<< numGriddedAtomsLocal <<
"\n"<<
endi);
54 for(
int i = 0; i <patches.size(); ++i){
64 int soaPid = h_globalToLocalID[lid.
pid];
65 std::pair<int,int> mapkey(gridnum,gid);
66 int soaIndex = localRecords[soaPid].bufferOffset + lid.
index;
67 DebugM(1,
"["<< CkMyPe() <<
"] ComputeGridForceCUDA::updateGriddedLists gid "<< gid <<
" patch " << lid.
pid <<
" atom " << j <<
" soaIndex[" << griddedAtomsGlobalIndexMap[mapkey] <<
"] = "<< griddedAtomsSOAIndex[griddedAtomsGlobalIndexMap[mapkey]] <<
"->" << soaIndex <<
"\n"<<
endi);
68 griddedAtomsSOAIndex[griddedAtomsGlobalIndexMap[mapkey]] = soaIndex;
76 for(
int i = 0 ; i < numGriddedAtoms; i++){
77 int gid = griddedAtomsGlobalIndex[i];
80 for(
int j = 0 ; j < atomMapsList.size(); j++)
82 lid = atomMapsList[j]->localID(gid);
83 if( lid.
pid != -1)
break;
89 NAMD_bug(
" LocalAtomID not found in patchMap");
91 int soaPid = h_globalToLocalID[lid.
pid];
92 int soaIndex = localRecords[soaPid].bufferOffset + lid.
index;
93 DebugM(1,
"ComputeGridForceCUDA::updateGriddedLists soa[" << i <<
"] <- "<< griddedAtomsSOAIndex[i] <<
"->" << soaIndex <<
"\n"<<
endi);
94 griddedAtomsSOAIndex[i] = soaIndex;
102 gridOffsetsLocal.clear();
103 griddedAtomsLocalIndex.clear();
104 gridOffsetsLocal.push_back(0);
108 DebugM(2,
"ComputeGridForceCUDA::updateGriddedLists patches "<< patches.size() <<
"\n" <<
endi);
109 for(
int i = 0; i <patches.size(); ++i){
115 std::pair<int,int> mapkey(gridnum,gid);
116 int mapoffset= griddedAtomsGlobalIndexMap[mapkey];
119 NAMD_bug(
"how is this not in our map?");
121 griddedAtomsLocalIndex.push_back(mapoffset);
122 DebugM(1,
"ComputeGridForceCUDA::updateGriddedLists patch " << p->
getPatchID() <<
" atom "<< j <<
" gid "<<gid<<
" gridnum " << gridnum <<
" mapoffset "<< mapoffset <<
" localIndex.size() " << griddedAtomsLocalIndex.size()<<
"\n" <<
endi);
125 DebugM(2,
"ComputeGridForceCUDA::updateGriddedLists patch " << p->
getPatchID() <<
" gridded atoms "<< griddedAtomsLocalIndex.size() <<
"\n" <<
endi);
127 int size = griddedAtomsLocalIndex.size();
128 gridOffsetsLocal.push_back(size);
131 numGriddedAtomsLocal = griddedAtomsLocalIndex.size();
132 DebugM(4,
"ComputeGridForceCUDA::updateGriddedLists numGriddedAtomsLocal now "<< numGriddedAtomsLocal <<
"\n" <<
endi);
134 if(numGriddedAtomsLocal>0)
136 int* soaPtr=griddedAtomsSOAIndex.data();
137 copy_HtoD_sync<int>(soaPtr, d_griddedAtomsSOAIndex, griddedAtomsSOAIndex.size());
138 int* idxPtr=griddedAtomsLocalIndex.data();
139 copy_HtoD_sync<int>(idxPtr, d_griddedAtomsLocalIndex, griddedAtomsLocalIndex.size());
141 return numGriddedAtomsLocal;
158 int ComputeGridForceCUDA::createGriddedLists()
162 DebugM(4,
"ComputeGridForceCUDA::createGriddedLists\n" <<
endi);
168 numGriddedAtomsLocal=0;
170 gridOffsets.push_back(0);
171 gridOffsetsLocal.push_back(0);
172 gridCoordsOffsets.push_back(0);
173 griddedAtomsGlobalIndex.clear();
174 griddedAtomsSOAIndex.clear();
178 for(
int j = 0; j < numAtoms; j++){
180 griddedAtomsGlobalIndex.push_back(j);
183 std::pair<int,int> mapkey(gridnum,j);
184 griddedAtomsGlobalIndexMap[mapkey]= griddedAtomsGlobalIndex.size();
187 gridOffsets.push_back(griddedAtomsGlobalIndex.size());
190 if( mol_grid->get_grid_type() == 1)
193 NAMD_bug(
"GridforceGridCUDA::createGriddedLists called with unsupported grid type!");
194 int size =h_grid->
size;
195 gridCoordsOffsets.push_back(size);
196 gridTotalSize += size;
197 DebugM(2,
"ComputeGridForceCUDA::createGriddedLists grid "<<gridnum <<
" has " << size <<
" points" <<
" and " << gridOffsets[gridnum+1] - gridOffsets[gridnum] <<
" atoms\n"<<
endi);
202 for(
int i = 0; i < patchList.size(); i++){
207 std::pair<int,int> mapkey(gridnum,gid);
208 int globalIndex=griddedAtomsGlobalIndexMap[mapkey];
209 griddedAtomsLocalIndex.push_back(globalIndex);
213 gridOffsetsLocal.push_back(griddedAtomsLocalIndex.size());
216 numGriddedAtoms=griddedAtomsGlobalIndex.size();
217 DebugM(3,
"ComputeGridForceCUDA::createGriddedLists numGriddedAtoms " << numGriddedAtoms <<
" numAtoms "<< molecule->
numAtoms <<
"grids " << molecule->
numGridforceGrids <<
"\n" <<
endi);
219 griddedAtomsSOAIndex.resize(numGriddedAtoms);
220 numGriddedAtomsLocal=0;
221 if(numGriddedAtoms > 0)
223 allocate_host<float>(&h_gridded_charge, numGriddedAtoms);
224 allocate_host<float>(&h_gridded_scale, numGriddedAtoms);
225 allocate_device<float>(&d_gridded_charge, numGriddedAtoms);
226 allocate_device<float>(&d_gridded_scale, numGriddedAtoms);
228 allocate_device<int>(&d_griddedAtomsSOAIndex, numGriddedAtoms);
229 allocate_device<int>(&d_griddedAtomsLocalIndex, numGriddedAtoms);
233 allocate_device<float>(&d_gridCoords, gridTotalSize);
244 int offset = gridOffsets[gridnum];
247 if( mol_grid->get_grid_type() == 1)
250 NAMD_bug(
"GridforceGridCUDA::createGriddedLists called with unsupported grid type!");
251 int grid_size = h_grid->
size;
252 for(
int gridIdx = offset; gridIdx < gridOffsets[gridnum+1]; gridIdx++)
254 int gid = griddedAtomsGlobalIndex[gridIdx];
256 molecule->
get_gridfrc_params(h_gridded_scale[gridIdx], h_gridded_charge[gridIdx], gid, gridnum);
261 h_grids.push_back(GridforceGridCUDA(h_grid->
get_k0(),
285 &d_gridCoords[gridCoordsOffsets[gridnum]]));
286 copy_HtoD_sync<float>(h_grid->
grid, &d_gridCoords[gridCoordsOffsets[gridnum]], h_grid->
size );
289 GridforceGridCUDA* gPtr=h_grids.data();
291 copy_HtoD_sync<float>(h_gridded_charge, d_gridded_charge, numGriddedAtoms);
292 copy_HtoD_sync<float>(h_gridded_scale, d_gridded_scale, numGriddedAtoms);
293 cudaCheck(cudaMemset(d_tbcatomic, 0,
sizeof(
unsigned int)));
295 return numGriddedAtoms;
301 void ComputeGridForceCUDA::doForce(
306 const double* d_pos_x,
307 const double* d_pos_y,
308 const double* d_pos_z,
309 const char3* d_transform,
316 DebugM(4,
"ComputeGridForceCUDA::doForce\n" <<
endi);
319 if(numGriddedAtomsLocal>0)
334 sprintf(errmsg,
"Warning: Periodic cell basis too small for Gridforce grid %d. Set gridforcechecksize off in configuration file to ignore.\n", gridnuml);
348 DebugM(3,
"ComputeGridForceCUDA::doForce grid "<< gridnum <<
" from local offset " << gridOffsetsLocal[gridnum] <<
" to " << gridOffsetsLocal[gridnum+1]<<
"\n" <<
endi);
349 h_extEnergy_G[gridnum] = 0.;
350 h_netForce_G[gridnum].x = 0;
351 h_netForce_G[gridnum].y = 0;
352 h_netForce_G[gridnum].z = 0;
353 h_extVirial_G[gridnum].xx = 0;
354 h_extVirial_G[gridnum].xy = 0;
355 h_extVirial_G[gridnum].xz = 0;
356 h_extVirial_G[gridnum].yx = 0;
357 h_extVirial_G[gridnum].yy = 0;
358 h_extVirial_G[gridnum].yz = 0;
359 h_extVirial_G[gridnum].zx = 0;
360 h_extVirial_G[gridnum].zy = 0;
361 h_extVirial_G[gridnum].zz = 0;
362 computeGridForce(doEnergy, doVirial, d_grids[gridnum], lat, d_pos_x, d_pos_y, d_pos_z, d_transform, d_griddedAtomsSOAIndex, &d_griddedAtomsLocalIndex[gridOffsetsLocal[gridnum]], d_gridded_charge, d_gridded_scale, f_normal_x, f_normal_y, f_normal_z, &h_netForce_G[gridnum], &d_netForce_G[gridnum], &h_extEnergy_G[gridnum], &d_extEnergy_G[gridnum], &h_extVirial_G[gridnum], &d_extVirial_G[gridnum], gridOffsetsLocal[gridnum+1]-gridOffsetsLocal[gridnum], d_tbcatomic, stream);
370 void ComputeGridForceCUDA::zeroOutEnergyVirialForcesAcrossGrids(
371 double* h_extEnergy, double3* h_netForce,
cudaTensor* h_virial)
388 h_extEnergy_G[gridnum] = 0.;
389 h_netForce_G[gridnum].x = 0;
390 h_netForce_G[gridnum].y = 0;
391 h_netForce_G[gridnum].z = 0;
392 h_extVirial_G->xx = 0;
393 h_extVirial_G->xy = 0;
394 h_extVirial_G->xz = 0;
395 h_extVirial_G->yx = 0;
396 h_extVirial_G->yy = 0;
397 h_extVirial_G->yz = 0;
398 h_extVirial_G->zx = 0;
399 h_extVirial_G->zy = 0;
400 h_extVirial_G->zz = 0;
405 void ComputeGridForceCUDA::sumEnergyVirialForcesAcrossGrids(
double* h_extEnergy,
411 DebugM(2,
"ComputeGridForceCUDA::sumEnergyVirialForcesAcrossGrids in grid "<< gridnum <<
" energy "<< h_extEnergy_G[gridnum] <<
"\n" <<
endi);
412 h_extEnergy[0] += h_extEnergy_G[gridnum];
415 h_netForce->x += h_netForce_G[gridnum].x;
416 h_netForce->y += h_netForce_G[gridnum].y;
417 h_netForce->z += h_netForce_G[gridnum].z;
418 h_virial[0] += h_extVirial_G[gridnum];
419 DebugM(2,
"ComputeGridForceCUDA::sumEnergyVirialForcesAcrossGrids out grid "<< gridnum <<
" energy "<< h_extEnergy_G[gridnum] <<
"\n" <<
endi);
425 void ComputeGridForceCUDA::destroyGriddedLists()
427 DebugM(4,
"ComputeGridForceCUDA::destroyGriddedLists\n" <<
endi);
428 if(numGriddedAtoms>0){
429 deallocate_host<float>(&h_gridded_charge);
430 deallocate_host<float>(&h_gridded_scale);
431 deallocate_device<float>(&d_gridded_charge);
432 deallocate_device<float>(&d_gridded_scale);
433 deallocate_device<float>(&d_gridCoords);
434 deallocate_device<int>(&d_griddedAtomsSOAIndex);
435 deallocate_device<int>(&d_griddedAtomsLocalIndex);
436 deallocate_device<GridforceGridCUDA>(&d_grids);
437 deallocate_device<float>(&d_gridCoords);
438 deallocate_device<double>(&d_extEnergy_G);
439 deallocate_device<double3>(&d_netForce_G);
440 deallocate_device<cudaTensor>(&d_extVirial_G);
441 deallocate_host<double>(&h_extEnergy_G);
442 deallocate_host<double3>(&h_netForce_G);
443 deallocate_host<cudaTensor>(&h_extVirial_G);
448 ComputeGridForceCUDA::~ComputeGridForceCUDA()
450 DebugM(4,
"ComputeGridForceCUDA::~ComputeGridForceCUDA\n" <<
endi);
451 destroyGriddedLists();
452 deallocate_device<unsigned int>(&d_tbcatomic);
457 #endif //NODEGROUP_FORCE_REGISTER
Vector get_scale(void) const
GridforceGrid * get_gridfrc_grid(int gridnum) const
Position get_center(void) const
Position get_origin(void) const
SimParameters * simParameters
std::ostream & endi(std::ostream &s)
Molecule stores the structural information for the system.
FullAtomList & getAtomList()
bool fits_lattice(const Lattice &lattice)
void NAMD_bug(const char *err_msg)
PatchID getPatchID() const
void get_gridfrc_params(Real &k, Charge &q, int atomnum, int gridnum) const
void NAMD_die(const char *err_msg)
virtual Bool get_checksize(void) const =0
#define GF_OVERLAPCHECK_FREQ
Bool is_atom_gridforced(int atomnum, int gridnum) const