-
Notifications
You must be signed in to change notification settings - Fork 1
/
ComputeGlobal.C
340 lines (299 loc) · 9.95 KB
/
ComputeGlobal.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
*** Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000 by
*** The Board of Trustees of the University of Illinois.
*** All rights reserved.
**/
/*
Forwards atoms to master node for force evaluation.
*/
#include "InfoStream.h"
#include "Node.h"
#include "PatchMap.h"
#include "PatchMap.inl"
#include "AtomMap.h"
#include "ComputeGlobal.h"
#include "ComputeGlobalMsgs.h"
#include "PatchMgr.h"
#include "Molecule.h"
#include "ReductionMgr.h"
#include "ComputeMgr.h"
#include "ComputeMgr.decl.h"
#include "SimParameters.h"
#include <stdio.h>
//#define DEBUGM
#define MIN_DEBUG_LEVEL 1
#include "Debug.h"
// CLIENTS
ComputeGlobal::ComputeGlobal(ComputeID c, ComputeMgr *m)
: ComputeHomePatches(c)
{
DebugM(3,"Constructing client\n");
aid.resize(0);
gdef.resize(0);
comm = m;
firsttime = 1;
isRequested = 0;
isRequestedAllocSize = 0;
endRequested = 0;
SimParameters *sp = Node::Object()->simParameters;
dofull = (sp->GBISserOn || sp->GBISOn || sp->fullDirectOn || sp->FMAOn || sp->PMEOn);
forceSendEnabled = 0;
if ( sp->tclForcesOn ) forceSendEnabled = 1;
if ( sp->colvarsOn ) forceSendEnabled = 1;
fid.resize(0);
totalForce.resize(0);
reduction = ReductionMgr::Object()->willSubmit(REDUCTIONS_BASIC);
int numPatches = PatchMap::Object()->numPatches();
forcePtrs = new Force*[numPatches];
atomPtrs = new FullAtom*[numPatches];
for ( int i = 0; i < numPatches; ++i ) { forcePtrs[i] = 0; atomPtrs[i] = 0; }
}
ComputeGlobal::~ComputeGlobal()
{
delete[] isRequested;
delete[] forcePtrs;
delete[] atomPtrs;
delete reduction;
}
void ComputeGlobal::configure(AtomIDList &newaid, AtomIDList &newgdef) {
DebugM(4,"Receiving configuration (" << newaid.size() <<
" atoms and " << newgdef.size() << " atoms/groups) on client\n");
AtomIDList::iterator a, a_e;
if ( forceSendEnabled ) {
// clear previous data
int max = -1;
for (a=newaid.begin(),a_e=newaid.end(); a!=a_e; ++a) {
if ( *a > max ) max = *a;
}
endRequested = max+1;
if ( endRequested > isRequestedAllocSize ) {
delete [] isRequested;
isRequestedAllocSize = endRequested+10;
isRequested = new char[isRequestedAllocSize];
memset(isRequested, 0, isRequestedAllocSize);
} else {
for (a=aid.begin(),a_e=aid.end(); a!=a_e; ++a) {
isRequested[*a] = 0;
}
}
}
// store data
aid.swap(newaid);
gdef.swap(newgdef);
if ( forceSendEnabled ) {
for (a=aid.begin(),a_e=aid.end(); a!=a_e; ++a) {
isRequested[*a] = 1;
}
}
}
#if 0
void ComputeGlobal::recvConfig(ComputeGlobalConfigMsg *msg) {
DebugM(3,"Receiving configure on client\n");
configure(msg->aid,msg->gdef);
delete msg;
sendData();
}
#endif
void ComputeGlobal::recvResults(ComputeGlobalResultsMsg *msg) {
DebugM(3,"Receiving results (" << msg->aid.size() << " forces, "
<< msg->newgdef.size() << " new group atoms) on client\n");
// set the forces only if we aren't going to resend the data
int setForces = !msg->resendCoordinates;
if(setForces) { // we are requested to
// Store forces to patches
AtomMap *atomMap = AtomMap::Object();
const Lattice & lattice = patchList[0].p->lattice;
ResizeArrayIter<PatchElem> ap(patchList);
Force **f = forcePtrs;
FullAtom **t = atomPtrs;
Force extForce = 0.;
Tensor extVirial;
for (ap = ap.begin(); ap != ap.end(); ap++) {
(*ap).r = (*ap).forceBox->open();
f[(*ap).patchID] = (*ap).r->f[Results::normal];
t[(*ap).patchID] = (*ap).p->getAtomList().begin();
}
AtomIDList::iterator a = msg->aid.begin();
AtomIDList::iterator a_e = msg->aid.end();
ForceList::iterator f2 = msg->f.begin();
for ( ; a != a_e; ++a, ++f2 ) {
DebugM(1,"processing atom "<<(*a)<<", F="<<(*f2)<<"...\n");
/* XXX if (*a) is out of bounds here we get a segfault */
LocalID localID = atomMap->localID(*a);
if ( localID.pid == notUsed || ! f[localID.pid] ) continue;
Force f_atom = (*f2);
f[localID.pid][localID.index] += f_atom;
FullAtom &atom = t[localID.pid][localID.index];
Position x_orig = atom.position;
Transform trans = atom.transform;
Position x_atom = lattice.reverse_transform(x_orig,trans);
extForce += f_atom;
extVirial += outer(f_atom,x_atom);
}
DebugM(1,"done with the loop\n");
// calculate forces for atoms in groups
AtomIDList::iterator g_i, g_e;
g_i = gdef.begin(); g_e = gdef.end();
ForceList::iterator gf_i = msg->gforce.begin();
//iout << iDEBUG << "recvResults\n" << endi;
for ( ; g_i != g_e; ++g_i, ++gf_i ) {
//iout << iDEBUG << *gf_i << '\n' << endi;
Vector accel = (*gf_i);
for ( ; *g_i != -1; ++g_i ) {
//iout << iDEBUG << *g_i << '\n' << endi;
LocalID localID = atomMap->localID(*g_i);
if ( localID.pid == notUsed || ! f[localID.pid] ) continue;
FullAtom &atom = t[localID.pid][localID.index];
Force f_atom = accel * atom.mass;
f[localID.pid][localID.index] += f_atom;
Position x_orig = atom.position;
Transform trans = atom.transform;
Position x_atom = lattice.reverse_transform(x_orig,trans);
extForce += f_atom;
extVirial += outer(f_atom,x_atom);
}
}
DebugM(1,"done with the groups\n");
for (ap = ap.begin(); ap != ap.end(); ap++) {
(*ap).forceBox->close(&((*ap).r));
f[(*ap).patchID] = 0;
t[(*ap).patchID] = 0;
}
ADD_VECTOR_OBJECT(reduction,REDUCTION_EXT_FORCE_NORMAL,extForce);
ADD_TENSOR_OBJECT(reduction,REDUCTION_VIRIAL_NORMAL,extVirial);
reduction->submit();
}
// done setting the forces
// Get reconfiguration if present
if ( msg->reconfig ) configure(msg->newaid, msg->newgdef);
// send another round of data if requested
if(msg->resendCoordinates) {
DebugM(3,"Sending requested data right away\n");
sendData();
}
delete msg;
DebugM(3,"Done processing results\n");
}
void ComputeGlobal::doWork()
{
DebugM(2,"doWork\n");
if(!firsttime) sendData();
else {
if ( hasPatchZero ) {
ComputeGlobalDataMsg *msg = new ComputeGlobalDataMsg;
msg->lat.add(patchList[0].p->lattice);
msg->step = -1;
msg->count = 1;
comm->sendComputeGlobalData(msg);
}
firsttime = 0;
comm->enableComputeGlobalResults();
}
DebugM(2,"done with doWork\n");
}
void ComputeGlobal::sendData()
{
DebugM(2,"sendData\n");
// Get positions from patches
AtomMap *atomMap = AtomMap::Object();
const Lattice & lattice = patchList[0].p->lattice;
ResizeArrayIter<PatchElem> ap(patchList);
FullAtom **t = atomPtrs;
int step = -1;
for (ap = ap.begin(); ap != ap.end(); ap++) {
CompAtom *x = (*ap).positionBox->open();
t[(*ap).patchID] = (*ap).p->getAtomList().begin();
step = (*ap).p->flags.step;
}
ComputeGlobalDataMsg *msg = new ComputeGlobalDataMsg;
msg->count = 0;
msg->step = step;
AtomIDList::iterator a = aid.begin();
AtomIDList::iterator a_e = aid.end();
for ( ; a != a_e; ++a ) {
LocalID localID = atomMap->localID(*a);
if ( localID.pid == notUsed || ! t[localID.pid] ) continue;
msg->aid.add(*a);
msg->count++;
FullAtom &atom = t[localID.pid][localID.index];
Position x_orig = atom.position;
Transform trans = atom.transform;
msg->p.add(lattice.reverse_transform(x_orig,trans));
}
// calculate group centers of mass
AtomIDList::iterator g_i, g_e;
g_i = gdef.begin(); g_e = gdef.end();
for ( ; g_i != g_e; ++g_i ) {
Vector com(0,0,0);
BigReal mass = 0.;
for ( ; *g_i != -1; ++g_i ) {
LocalID localID = atomMap->localID(*g_i);
if ( localID.pid == notUsed || ! t[localID.pid] ) continue;
msg->count++;
FullAtom &atom = t[localID.pid][localID.index];
Position x_orig = atom.position;
Transform trans = atom.transform;
com += lattice.reverse_transform(x_orig,trans) * atom.mass;
mass += atom.mass;
}
DebugM(1,"Adding center of mass "<<com<<"\n");
msg->gcom.add(com);
msg->gmass.add(mass);
}
for (ap = ap.begin(); ap != ap.end(); ap++) {
CompAtom *x;
(*ap).positionBox->close(&x);
}
msg->fid.swap(fid);
msg->tf.swap(totalForce);
fid.resize(0);
totalForce.resize(0);
msg->count += msg->fid.size();
DebugM(3,"Sending data (" << msg->aid.size() << " positions) on client\n");
if ( hasPatchZero ) { msg->count++; msg->lat.add(lattice); }
if ( msg->count ) comm->sendComputeGlobalData(msg);
else delete msg;
comm->enableComputeGlobalResults();
}
// This function is called by each HomePatch after force
// evaluation. It stores the indices and forces of the requested
// atoms here, to be sent to GlobalMasterServer during the next
// time step. The total force is the sum of three components:
// "normal", "nbond" and "slow", the latter two may be calculated
// less frequently, so their most recent values are stored in
// "f_saved" and used here. If we don't do full electrostatics,
// there's no "slow" part.
void ComputeGlobal::saveTotalForces(HomePatch *homePatch)
{
if ( ! forceSendEnabled ) NAMD_bug("ComputeGlobal::saveTotalForces called unexpectedly");
if ( Node::Object()->simParameters->accelMDOn && Node::Object()->simParameters->accelMDDebugOn && Node::Object()->simParameters->accelMDdihe ) {
int num=homePatch->numAtoms;
FullAtomList &atoms = homePatch->atom;
ForceList &af=homePatch->f[Results::amdf];
for (int i=0; i<num; ++i) {
int index = atoms[i].id;
if (index < endRequested && isRequested[index]) {
fid.add(index);
totalForce.add(af[i]);
}
}
return;
}
int fixedAtomsOn = Node::Object()->simParameters->fixedAtomsOn;
int num=homePatch->numAtoms;
FullAtomList &atoms = homePatch->atom;
ForceList &f1=homePatch->f[Results::normal], &f2=homePatch->f_saved[Results::nbond],
&f3=homePatch->f_saved[Results::slow];
Force f_sum;
for (int i=0; i<num; ++i) {
int index = atoms[i].id;
if (index < endRequested && isRequested[index]) {
f_sum = f1[i]+f2[i];
if (dofull)
f_sum += f3[i];
if ( fixedAtomsOn && atoms[i].atomFixed ) f_sum = 0.;
fid.add(index);
totalForce.add(f_sum);
}
}
}