NAMD
ComputeConsForceCUDA.C
Go to the documentation of this file.
1 #include "ComputeConsForceCUDA.h"
2 #include "Molecule.h"
3 #include "Node.h"
4 #include "SimParameters.h"
6 #define MIN_DEBUG_LEVEL 4
7 //#define DEBUGM
8 #include "Debug.h"
9 
10 #ifdef NODEGROUP_FORCE_REGISTER
11 
12 
13 
14 
15 ComputeConsForceCUDA::ComputeConsForceCUDA(
16  std::vector<HomePatch*> &patchList,
17  std::vector<AtomMap*> &atomMapList,
18  bool _mGpuOn)
19 {
20  mGpuOn=_mGpuOn;
21  resetVirial=true;
22  Molecule *molecule = Node::Object()->molecule;
24  Vector *cf = molecule->consForce; // Force array
25  nConsForceAtoms = 0;
26  int32 *index = molecule->consForceIndexes; // Indexes into the force array
27  int numAtoms = molecule->numAtoms;
28  int forceID;
29  for(int gid = 0; gid < numAtoms; gid++){
30  if ((forceID=index[gid])!=-1)
31  {
32  consAtomsIndexMap[gid]=h_consForce.size();
33  h_consForce.push_back(cf[forceID]);
34  h_consForceID.push_back(gid); // Pushes back global ID vector
35  DebugM(4, "ComputeConsForceCUDA::ComputeConsForcedCUDA gid " << gid <<" forceid "<< forceID << " cf[forceID] "<< cf[forceID] <<" nConsForceAtoms " << h_consForce.size() <<"\n"<< endi);
36  }
37  }
38  nConsForceAtoms= h_consForce.size();
39 
40  h_consForceSOA.resize(nConsForceAtoms);
41  allocate_device<unsigned int>(&d_tbcatomic, 1);
42  allocate_device<int>(&d_consForceSOA, nConsForceAtoms);
43  allocate_device<int>(&d_consForceID, nConsForceAtoms);
44  allocate_device<double3>(&d_consForce, nConsForceAtoms);
45  copy_HtoD_sync<int>(h_consForceID.data(), d_consForceID, nConsForceAtoms);
46  copy_HtoD_sync<double3>(h_consForce.data(), d_consForce, nConsForceAtoms);
47  cudaCheck(cudaMemset(d_tbcatomic, 0, sizeof(unsigned int))); // sets the scalar to zero
48 
49 }
50 
51 void ComputeConsForceCUDA::updateConsForceAtoms(
52  std::vector<AtomMap*> &atomMapsList,
53  std::vector<CudaLocalRecord> &localRecords,
54  const int* h_globalToLocalID)
55 {
56  DebugM(4, "ComputeConsForceCUDA::updateConsForcedAtoms "<< nConsForceAtoms <<"\n"<< endi);
57  consForceLocalAtomsIndex.clear();
58  int gid;
59 
60  for(int i = 0; i < nConsForceAtoms; i++){
61  // translates the global ID to the SOA ID.
62  gid = h_consForceID[i];
63  LocalID lid;
64  // Search for a valid localID in all atoms
65  for(int j = 0 ; j < atomMapsList.size(); j++){
66  lid = atomMapsList[j]->localID(gid);
67  if( lid.pid != -1) break;
68  }
69 
70  //JM NOTE: Fields of lid need to be != -1, bc the atom needs to be somewhere
71  // otherwise we have a bug
72  // unless mGpuOn, then it is in a patch not on our device
73  if(lid.pid == -1) {
74  if(!mGpuOn)
75  NAMD_bug(" LocalAtomID not found in patchMap");
76  }
77  else
78  {
79  // JM: Now that we have a patchID and a localPosition inside the patch, I can figure out
80  // the SOA position for each atom
81  int soaPid = h_globalToLocalID[lid.pid]; // Converts global patch ID to its local position in our SOA data structures
82  int soaIndex = localRecords[soaPid].bufferOffset + lid.index;
83  int mapoffset= consAtomsIndexMap[gid];
84  h_consForceSOA[mapoffset] = soaIndex;
85  consForceLocalAtomsIndex.push_back(mapoffset);
86  DebugM(2, "ComputeConsForceCUDA::updateConsForceAtoms gid " << gid << " lid " << lid.pid << ":" <<lid.index <<" mapoffset "<< mapoffset << " soaIndex " << soaIndex << " consForceLocalAtomsIndexSize "<<consForceLocalAtomsIndex.size() <<"\n" << endi);
87  copy_HtoD_sync<int>(h_consForceSOA.data(), d_consForceSOA, consForceLocalAtomsIndex.size());
88  copy_HtoD_sync<int>(consForceLocalAtomsIndex.data(), d_consForceID, consForceLocalAtomsIndex.size());
89 
90  }
91  }
92 }
93 
94 ComputeConsForceCUDA::~ComputeConsForceCUDA(){
95  DebugM(4, "ComputeConsForceCUDA::~ComputeConsForceCuda "<< consForceLocalAtomsIndex.size() <<"\n"<< endi);
96  deallocate_device<unsigned int>(&d_tbcatomic);
97  deallocate_device<int>(&d_consForceSOA);
98  deallocate_device<int>(&d_consForceID);
99  deallocate_device<double3>(&d_consForce);
100 }
101 
102 void ComputeConsForceCUDA::doForce( const Lattice lat,
103  bool doVirial,
104  double* d_pos_x,
105  double* d_pos_y,
106  double* d_pos_z,
107  double* f_normal_x,
108  double* f_normal_y,
109  double* f_normal_z,
110  char3* d_transform,
111  double3* d_netForce,
112  double3* h_netForce,
113  cudaTensor* d_virial,
114  cudaTensor* h_virial,
115  cudaStream_t stream
116  )
117 {
120  DebugM(4, "ComputeConsForceCUDA::doForce "<< consForceLocalAtomsIndex.size() << " virial " << doVirial <<"\n"<< endi);
121 
122  if(resetVirial)
123  { // if we're rebuilt with new forces, new virial
124  doVirial=1;
125  resetVirial=false;
126  }
127 #ifdef DEBUGM
128  std::vector <double3> printspace;
129  printspace.resize(nConsForceAtoms);
130  cudaCheck(cudaStreamSynchronize(stream));
131  copy_DtoH_sync<double3>(d_consForce, printspace.data(), nConsForceAtoms);
132  for(int forceID=0; forceID<nConsForceAtoms; forceID++)
133  {
134  DebugM(2, "ComputeConsForceCUDA::doForce b forceid "<< forceID << " consForce[forceID] "<< printspace[forceID] << "\n"<< endi);
135  }
136 #endif
137  if(consForceLocalAtomsIndex.size() > 0)
138  {
139  computeConsForce( doVirial,
140  consForceLocalAtomsIndex.size(),
141  d_consForceSOA,
142  d_consForceID,
143  d_pos_x,
144  d_pos_y,
145  d_pos_z,
146  d_consForce,
147  d_transform,
148  f_normal_x,
149  f_normal_y,
150  f_normal_z,
151  d_netForce,
152  h_netForce,
153  d_virial,
154  h_virial,
155  scaling,
156  lat,
157  d_tbcatomic,
158  stream);
159  DebugM(4, "ComputeConsForceCUDA::doForce a h_netForce "
160  << h_netForce->x << "," << h_netForce->y <<
161  "," << h_netForce->z <<"\n" << endi);
162  DebugM(4, "ComputeConsForceCUDA::doForce a h_virial " <<
163  h_virial->xx <<
164  "," << h_virial->xz <<
165  "," << h_virial->xz <<
166  "," << h_virial->yx <<
167  "," << h_virial->yy <<
168  "," << h_virial->yz <<
169  "," << h_virial->zx <<
170  "," << h_virial->zy <<
171  "," << h_virial->zz << "\n" << endi);
172  }
173 }
174 
175 
176 #endif
static Node * Object()
Definition: Node.h:86
Definition: Vector.h:72
SimParameters * simParameters
Definition: Node.h:181
int32_t int32
Definition: common.h:38
#define DebugM(x, y)
Definition: Debug.h:75
std::ostream & endi(std::ostream &s)
Definition: InfoStream.C:54
Molecule stores the structural information for the system.
Definition: Molecule.h:174
int32 index
Definition: NamdTypes.h:300
void NAMD_bug(const char *err_msg)
Definition: common.C:195
int numAtoms
Definition: Molecule.h:586
PatchID pid
Definition: NamdTypes.h:299
#define simParams
Definition: Output.C:131
int32 * consForceIndexes
Definition: Molecule.h:648
Vector * consForce
Definition: Molecule.h:649
#define cudaCheck(stmt)
Definition: CudaUtils.h:233
Molecule * molecule
Definition: Node.h:179
double BigReal
Definition: common.h:123
BigReal consForceScaling