NAMD
ComputeRestraintsCUDA.C
Go to the documentation of this file.
2 #include "Molecule.h"
3 #include "Node.h"
4 #include "HomePatch.h"
5 #include "SimParameters.h"
7 #define MIN_DEBUG_LEVEL 2
8 //#define DEBUGM
9 #include "Debug.h"
10 
11 #ifdef NODEGROUP_FORCE_REGISTER
12 
13 ComputeRestraintsCUDA::ComputeRestraintsCUDA(
14  std::vector<HomePatch*> &patchList,
15  std::vector<AtomMap*> &atomMapsList,
16  cudaStream_t stream,
17  bool _mGpuOn
18 ) {
19 
20  mGpuOn=_mGpuOn;
21  Molecule *molecule = Node::Object()->molecule;
23  nConstrainedAtoms = 0;
24  // Set the constraints flags
25  consExp = simParams->constraintExp;
26  // consScaling = simParams->constraintScaling;
27 
28  // Selective constraints flags
29  selConsOn = simParams->selectConstraintsOn;
30 
31  if (selConsOn){
32  consSelectX = simParams->constrXOn;
33  consSelectY = simParams->constrYOn;
34  consSelectZ = simParams->constrZOn;
35  }
36 
37  // Moving Constraints flags
38  movConsOn = simParams->movingConstraintsOn;
39  if (movConsOn) {
40  moveVel.x = simParams->movingConsVel.x;
41  moveVel.y = simParams->movingConsVel.y;
42  moveVel.z = simParams->movingConsVel.z;
43  }
44 
45  // Rotating cosntraints flags
46  rotConsOn = simParams->rotConstraintsOn;
47  if (rotConsOn) {
48  rotVel = simParams->rotConsVel; // velocity is a scalar here
49  rotAxis.x = simParams->rotConsAxis.x;
50  rotAxis.y = simParams->rotConsAxis.y;
51  rotAxis.z = simParams->rotConsAxis.z;
52  }
53 
54  // Spherical Constraints flags
55  spheConsOn = simParams->sphericalConstraintsOn;
56  if(spheConsOn){
57  spheConsCenter.x = simParams->sphericalConstrCenter.x;
58  spheConsCenter.y = simParams->sphericalConstrCenter.y;
59  spheConsCenter.z = simParams->sphericalConstrCenter.z;
60  }
61 
62  // Set all flags, now allocate constraints data structures
63  // We need to go through the molecule and add stuff to a constraint vector
64  int numAtoms = molecule->numAtoms;
65  for(int gid = 0; gid < numAtoms; gid++){
66  if (molecule->is_atom_constrained(gid)){
67  nConstrainedAtoms++;
68  constrainedAtomsIndexMap[gid]=h_constrainedID.size();
69  h_constrainedID.push_back(gid); // Pushes back global ID vector
70  float k;
71  Vector refPos;
72  molecule->get_cons_params(k, refPos, gid);
73  h_k.push_back(k);
74  h_cons_x.push_back(refPos.x);
75  h_cons_y.push_back(refPos.y);
76  h_cons_z.push_back(refPos.z);
77  }
78  }
79  this->stream = stream;
80 
81  allocate_host<int>(&h_constrainedSOA, nConstrainedAtoms);
82  allocate_device<unsigned int>(&d_tbcatomic, 1);
83  allocate_device<int>(&d_constrainedSOA, nConstrainedAtoms);
84  allocate_device<int>(&d_constrainedID, nConstrainedAtoms);
85  allocate_device<double>(&d_k, nConstrainedAtoms);
86  allocate_device<double>(&d_cons_x, nConstrainedAtoms);
87  allocate_device<double>(&d_cons_y, nConstrainedAtoms);
88  allocate_device<double>(&d_cons_z, nConstrainedAtoms);
89 
90  copy_HtoD_sync<double>(h_k.data(), d_k, nConstrainedAtoms);
91  copy_HtoD_sync<double>(h_cons_x.data(), d_cons_x, nConstrainedAtoms);
92  copy_HtoD_sync<double>(h_cons_y.data(), d_cons_y, nConstrainedAtoms);
93  copy_HtoD_sync<double>(h_cons_z.data(), d_cons_z, nConstrainedAtoms);
94  copy_HtoD_sync<int>(h_constrainedID.data(), d_constrainedID, nConstrainedAtoms);
95 
96  cudaCheck(cudaMemset(d_tbcatomic, 0, sizeof(unsigned int))); // sets the scalar to zero
97 
98  // this->updateRestrainedAtoms(atomMapsList, h_globalToLocalID, h_patchOffsets);
99 }
100 
101 void ComputeRestraintsCUDA::updateRestrainedAtoms(
102  std::vector<AtomMap*> &atomMapsList,
103  std::vector<CudaLocalRecord> &localRecords,
104  const int* h_globalToLocalID
105 ){
106  // JM NOTE: This gets called for every migration step, so it would be good to have this somewhat fast
107  // This is serialized, so if we have a lot of constrained atoms, it might become a bottleneck in the future
108  DebugM(4, "ComputeGridForceCUDA::updateRestrainedAtoms "<< nConstrainedAtoms <<"\n"<< endi);
109  constrainedLocalAtomsIndex.clear();
110  int gid;
111 
112  for(int i = 0; i < nConstrainedAtoms; i++){
113  // translates the global ID to the SOA ID.
114  gid = h_constrainedID[i];
115  LocalID lid;
116  // Search for a valid localID in all atoms
117  for(int j = 0 ; j < atomMapsList.size(); j++){
118  lid = atomMapsList[j]->localID(gid);
119  if( lid.pid != -1) break;
120  }
121 
122  //JM NOTE: Fields of lid need to be != -1, bc the atom needs to be somewhere
123  // otherwise we have a bug
124  // unless mGpuOn, then it is in a patch not on our device
125  if(lid.pid == -1) {
126  if(!mGpuOn)
127  NAMD_bug(" LocalAtomID not found in patchMap");
128  }
129  else
130  {
131  // JM: Now that we have a patchID and a localPosition inside the patch, I can figure out
132  // the SOA position for each constrained atom
133 
134  int soaPid = h_globalToLocalID[lid.pid]; // Converts global patch ID to its local position in our SOA data structures
135  int soaIndex = localRecords[soaPid].bufferOffset + lid.index;
136  int mapoffset= constrainedAtomsIndexMap[gid];
137  h_constrainedSOA[mapoffset] = soaIndex;
138  constrainedLocalAtomsIndex.push_back(mapoffset);
139  DebugM(2, "ComputeRestraintsCUDA::updateRestrainedAtoms gid " << gid <<" mapoffset "<< mapoffset << " constrainedLocalAtomsIndexSize "<<constrainedLocalAtomsIndex.size() <<"\n" << endi);
140  }
141  }
142 
143  // Copy the h_constrainedSOA data structure over to the GPU
144  copy_HtoD_sync<int>(h_constrainedSOA, d_constrainedSOA, nConstrainedAtoms);
145  int* idxPtr = constrainedLocalAtomsIndex.data();
146  // Copy the indices for this device over to the GPU
147  copy_HtoD_sync<int>(idxPtr, d_constrainedID, constrainedLocalAtomsIndex.size());
148 
149 }
150 
151 // doForce is called every time step, so no copies here
152 void ComputeRestraintsCUDA::doForce(
153  const Lattice *lat,
154  const bool doEnergy,
155  const bool doVirial,
156  const int timeStep,
157  double* d_pos_x,
158  double* d_pos_y,
159  double* d_pos_z,
160 
161  double* f_normal_x,
162  double* f_normal_y,
163  double* f_normal_z,
164  double* d_bcEnergy,
165  double* h_bcEnergy,
166  double3* d_netForce,
167  double3* h_netForce,
168  cudaTensor* d_virial,
169  cudaTensor* h_virial
170 ){
171  if (constrainedLocalAtomsIndex.empty()) return;
173 
174  computeRestrainingForce(
175  doEnergy,
176  doVirial,
177  timeStep,
178  constrainedLocalAtomsIndex.size(),
179  consExp,
180  // consScaling,
181  simParams->constraintScaling, // read directly from SimParameters
182  // to make sure we always calculate using latest value
183  movConsOn,
184  rotConsOn,
185  selConsOn,
186  spheConsOn,
187  consSelectX,
188  consSelectY,
189  consSelectZ,
190  rotVel,
191  rotAxis,
192  rotPivot,
193  moveVel,
194  spheConsCenter,
195  d_constrainedSOA,
196  d_constrainedID,
197  d_pos_x,
198  d_pos_y,
199  d_pos_z,
200  d_k,
201  d_cons_x,
202  d_cons_y,
203  d_cons_z,
204  f_normal_x,
205  f_normal_y,
206  f_normal_z,
207  d_bcEnergy,
208  h_bcEnergy,
209  d_netForce,
210  h_netForce,
211  lat,
212  d_virial,
213  h_virial,
214  rotationMatrix,
215  d_tbcatomic,
216  stream
217  );
218 
219 }
220 
221 
222 ComputeRestraintsCUDA::~ComputeRestraintsCUDA()
223 {
224  deallocate_device<unsigned int>(&d_tbcatomic);
225  deallocate_device<int>(&d_constrainedSOA);
226  deallocate_device<int>(&d_constrainedID);
227  deallocate_device<double>(&d_cons_x);
228  deallocate_device<double>(&d_cons_y);
229  deallocate_device<double>(&d_cons_z);
230  deallocate_device<double>(&d_k);
231 }
232 
233 #endif // NODEGROUP_FORCE_REGISTER
static Node * Object()
Definition: Node.h:86
Definition: Vector.h:72
SimParameters * simParameters
Definition: Node.h:181
#define DebugM(x, y)
Definition: Debug.h:75
std::ostream & endi(std::ostream &s)
Definition: InfoStream.C:54
BigReal z
Definition: Vector.h:74
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
void get_cons_params(Real &k, Vector &refPos, int atomnum) const
Definition: Molecule.h:1349
BigReal x
Definition: Vector.h:74
int numAtoms
Definition: Molecule.h:585
PatchID pid
Definition: NamdTypes.h:289
#define simParams
Definition: Output.C:129
BigReal y
Definition: Vector.h:74
#define cudaCheck(stmt)
Definition: CudaUtils.h:233
Bool is_atom_constrained(int atomnum) const
Definition: Molecule.h:1265
Molecule * molecule
Definition: Node.h:179