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 
82  int soaPid = h_globalToLocalID[lid.pid]; // Converts global patch ID to its local position in our SOA data structures
83  int soaIndex = localRecords[soaPid].bufferOffset + lid.index;
84  int mapoffset= consAtomsIndexMap[gid];
85  h_consForceSOA[mapoffset] = soaIndex;
86  consForceLocalAtomsIndex.push_back(mapoffset);
87  DebugM(2, "ComputeConsForceCUDA::updateConsForceAtoms gid " << gid << " lid " << lid.pid << ":" <<lid.index <<" mapoffset "<< mapoffset << " soaIndex " << soaIndex << " consForceLocalAtomsIndexSize "<<consForceLocalAtomsIndex.size() <<"\n" << endi);
88  copy_HtoD_sync<int>(h_consForceSOA.data(), d_consForceSOA, consForceLocalAtomsIndex.size());
89  copy_HtoD_sync<int>(consForceLocalAtomsIndex.data(), d_consForceID, consForceLocalAtomsIndex.size());
90 
91  }
92  }
93 }
94 
95 ComputeConsForceCUDA::~ComputeConsForceCUDA(){
96  DebugM(4, "ComputeConsForceCUDA::~ComputeConsForceCuda "<< consForceLocalAtomsIndex.size() <<"\n"<< endi);
97  deallocate_device<unsigned int>(&d_tbcatomic);
98  deallocate_device<int>(&d_consForceSOA);
99  deallocate_device<int>(&d_consForceID);
100  deallocate_device<double3>(&d_consForce);
101 }
102 
103 void ComputeConsForceCUDA::doForce( const Lattice lat,
104  bool doVirial,
105  double* d_pos_x,
106  double* d_pos_y,
107  double* d_pos_z,
108  double* f_normal_x,
109  double* f_normal_y,
110  double* f_normal_z,
111  char3* d_transform,
112  double3* d_netForce,
113  double3* h_netForce,
114  cudaTensor* d_virial,
115  cudaTensor* h_virial,
116  cudaStream_t stream
117  )
118 {
121  DebugM(4, "ComputeConsForceCUDA::doForce "<< consForceLocalAtomsIndex.size() << " virial " << doVirial <<"\n"<< endi);
122 
123  if(resetVirial)
124  { // if we're rebuilt with new forces, new virial
125  doVirial=1;
126  resetVirial=false;
127  }
128 #ifdef DEBUGM
129  std::vector <double3> printspace;
130  printspace.resize(nConsForceAtoms);
131  cudaCheck(cudaStreamSynchronize(stream));
132  copy_DtoH_sync<double3>(d_consForce, printspace.data(), nConsForceAtoms);
133  for(int forceID=0; forceID<nConsForceAtoms; forceID++)
134  {
135  DebugM(2, "ComputeConsForceCUDA::doForce b forceid "<< forceID << " consForce[forceID] "<< printspace[forceID] << "\n"<< endi);
136  }
137 #endif
138  if(consForceLocalAtomsIndex.size() > 0)
139  {
140  computeConsForce( doVirial,
141  consForceLocalAtomsIndex.size(),
142  d_consForceSOA,
143  d_consForceID,
144  d_pos_x,
145  d_pos_y,
146  d_pos_z,
147  d_consForce,
148  d_transform,
149  f_normal_x,
150  f_normal_y,
151  f_normal_z,
152  d_netForce,
153  h_netForce,
154  d_virial,
155  h_virial,
156  scaling,
157  lat,
158  d_tbcatomic,
159  stream);
160  DebugM(4, "ComputeConsForceCUDA::doForce a h_netForce "
161  << h_netForce->x << "," << h_netForce->y <<
162  "," << h_netForce->z <<"\n" << endi);
163  DebugM(4, "ComputeConsForceCUDA::doForce a h_virial " <<
164  h_virial->xx <<
165  "," << h_virial->xz <<
166  "," << h_virial->xz <<
167  "," << h_virial->yx <<
168  "," << h_virial->yy <<
169  "," << h_virial->yz <<
170  "," << h_virial->zx <<
171  "," << h_virial->zy <<
172  "," << h_virial->zz << "\n" << endi);
173  }
174 }
175 
176 
177 #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:175
int32 index
Definition: NamdTypes.h:290
void NAMD_bug(const char *err_msg)
Definition: common.C:195
int numAtoms
Definition: Molecule.h:585
PatchID pid
Definition: NamdTypes.h:289
#define simParams
Definition: Output.C:129
int32 * consForceIndexes
Definition: Molecule.h:646
Vector * consForce
Definition: Molecule.h:647
#define cudaCheck(stmt)
Definition: CudaUtils.h:233
Molecule * molecule
Definition: Node.h:179
double BigReal
Definition: common.h:123
BigReal consForceScaling