NAMD
GlobalMasterIMD.C
Go to the documentation of this file.
1 
7 #include "InfoStream.h"
8 #include "common.h"
9 #include "vmdsock.h"
10 #include "Node.h"
11 #include "IMDOutput.h"
12 #include "imd.h"
13 #include "SimParameters.h"
14 #include "UniqueSortedArray.h"
15 #include "GlobalMaster.h"
16 #include "GlobalMasterIMD.h"
17 #include "Vector.h"
18 #include "ReductionMgr.h"
19 
20 #include <errno.h>
21 #include <string>
22 
23 //#define DEBUGM
24 #define MIN_DEBUG_LEVEL 1
25 #include "Debug.h"
26 
27 struct vmdforce {
28  int index;
30  int operator <(const vmdforce& v) {return index < v.index;}
31  // XXX the following is an abuse of overloading!
32  int operator ==(const vmdforce& v) {return index == v.index;}
34  index=v.index;
35  force=v.force;
36  return *this;
37  }
38 };
39 
40 //
41 // XXX static and global variables are unsafe for shared memory builds.
42 // The use of global and static vars should be eliminated.
43 //
45 
46 // Search for a free port in the range 1025-4096; return the successful port,
47 // or -1 if we failed.
48 
49 static int find_free_port(void *sock, int defport) {
50  if (vmdsock_bind(sock, defport)==0) return defport; // success
51  for (int port=1025; port < 4096; port++)
52  if (vmdsock_bind(sock, port)==0) return port;
53  return -1;
54 }
55 
57  DebugM(3,"Constructing\n");
59  int port = simparams->IMDport;
60  IMDversion = simparams->IMDversion;
61  IMDwait = simparams->IMDwait;
62  IMDignore = simparams->IMDignore;
63  IMDignoreForces = simparams->IMDignoreForces;
64  IMDsendsettings = simparams->IMDsendsettings;
65  coordtmp = NULL;
66  coordtmpsize = 0;
67  veltmp = NULL;
68  veltmpsize = 0;
69  forcetmp = NULL;
70  forcetmpsize = 0;
71 
72  if ( vmdsock_init() ) {
73  NAMD_die("Unable to initialize socket interface for IMD.\n");
74  }
75  sock = vmdsock_create();
76  int newport = find_free_port(sock, port);
77  if (newport != port) {
78  iout << iWARN << "Interactive MD failed to bind to port "
79  << port << ".\n" << endi;
80  }
81  if (newport < 0) {
83  NAMD_die("Interactive MD failed to find free port.\n");
84  }
86  iout << iINFO << "Interactive MD listening on port "
87  << newport << ".\n" << endi;
88  DebugM(2,"Done constructing ("<<requestedGroups().size()<<" initial groups)\n");
89 
101  if (IMDignore || IMDignoreForces) {
102  if (reduction) {
103  delete reduction;
104  reduction = nullptr;
105  }
106 #if (defined (NAMD_CUDA) || defined (NAMD_HIP))
107  if (reductionGpuResident) {
108  delete reductionGpuResident;
109  reductionGpuResident = nullptr;
110  }
111 #endif
112  }
113  Node::Object()->imd->use_imd(this);
114 
115  IMDv3TimeoutSeconds = 60;
116 }
117 
119  if (sock)
121  for (int i=0; i<clients.size(); i++)
123  if (coordtmp) delete [] coordtmp;
124  if (veltmp) delete[] veltmp;
125  if (forcetmp) delete[] forcetmp;
126 }
127 
128 static int my_imd_connect(void *s, const int IMDversion, const IMDSessionInfo *sessionInfo) {
129  if (imd_handshake(s, static_cast<int>(IMDversion))) {
130  iout << iWARN << "IMD handshake failed\n" << endi;
131  return 0;
132  }
133 
134  if (IMDversion == IMDversion_t::IMDv3) {
135  iout << iWARN << "IMD protocol version 3 detected\n" << endi;
136  if (imd_sessioninfo(s, sessionInfo)) {
137  iout << iWARN << "IMD sessioninfo not sent\n" << endi;
138  return 0;
139  }
140  }
141 
142  // Wait a second, then see if VMD has responded.
143  int32 length;
144  if (vmdsock_selread(s,1) != 1 || imd_recv_header(s, &length) != IMD_GO) {
145  iout << iWARN << "Incompatible Interactive MD, use VMD v1.4b2 or higher\n"
146  << endi;
147  return 0;
148  }
149  return 1;
150 }
151 
153  // check for incoming connection
154  do {
155  int rc;
156  if (IMDwait && !clients.size()) {
157  iout << iINFO << "INTERACTIVE MD AWAITING CONNECTION\n" << endi;
158  do { rc = vmdsock_selread(sock, 3600); } while (rc <= 0);
159  } else {
160  rc = vmdsock_selread(sock, 0);
161  }
162  if (rc > 0) {
163  void *clientsock = vmdsock_accept(sock);
164  if (!clientsock) {
165  iout << iWARN << "IMD socket accept failed\n" << endi;
166  } else {
167  if (!my_imd_connect(clientsock, IMDversion, &IMDsendsettings)) {
168  iout << iWARN << "IMD connection failed\n" << endi;
169  vmdsock_destroy(clientsock);
170  } else {
171  iout << iINFO << "IMD connection opened\n" <<endi;
172  clients.add(clientsock);
173  }
174  }
175  }
176  } while (IMDwait && !clients.size());
177 }
178 
180  if ((IMDversion == IMDversion_t::IMDv2) ||
181  (IMDwait && !clients.size())) {
182  // Check for incoming connections
183  imd_connection();
184  }
185 
187  /* clear out the requested forces first! */
188  if (!IMDignore && !IMDignoreForces) {
192  }
193 
194  // Assume for now that the only thing we get from VMD is a set of forces.
195  // Later we'll want to look for and implement more sophisticated control
196  // parameters. ie have a specified protocol
197 
198  // Check/get new forces from VMD
199  get_vmd_forces();
200 
201  // Right now I don't check to see if any new forces were obtained.
202  // An optimization would be cache the results message. However, there
203  // would still be copying since it looks like the messages get deleted
204  // by the receiver.
205 
206  /* set our arrays to be big enough to hold all of the forces */
207  int num = vmdforces.size();
208 
209  DebugM(2,"Setting " << num << " forces.\n");
210 
211  if (!IMDignore && !IMDignoreForces) {
212  modifyForcedAtoms().resize(num);
214 
215  int i;
217  for ( i = 0; i < num; ++i, ++v_i) {
218  modifyForcedAtoms().item(i) = v_i->index;
219  modifyAppliedForces().item(i) = v_i->force;
220  }
221  }
222  }
223 }
224 
226  IMDType type;
227  int32 length;
228  int32 *vmd_atoms;
229  float *vmd_forces;
230  int paused = 0;
231  int warned = 0;
232  vmdforce *vtest, vnew;
233 
234  // Loop through each socket one at a time. By doing this, rather than
235  // polling all sockets at once, NAMD only has to keep up with one IMD
236  // connection; if it tried to read from all of them, it could more easily
237  // fall behind and never finish draining all the sockets.
238  // It would be better to have a system where VMD couldn't DOS NAMD by
239  // spamming it with messages, but in practice NAMD is able to keep up with
240  // VMD's send rate.
241  for (int i_client=0; i_client<clients.size(); i_client++) {
242  void *clientsock = clients[i_client];
243  while (vmdsock_selread(clientsock,0) > 0 || paused) { // Drain the socket
244  type = imd_recv_header(clientsock, &length);
245  switch (type) {
246  case IMD_MDCOMM:
247  // Expect the msglength to give number of indicies, and the data
248  // message to consist of first the indicies, then the coordinates
249  // in xyz1 xyz2... format.
250  vmd_atoms = new int32[length];
251  vmd_forces = new float[3*length];
252  if (imd_recv_mdcomm(clientsock, length, vmd_atoms, vmd_forces)) {
253  iout << iWARN <<
254  "Error reading IMD forces, killing connection\n" << endi;
255  goto vmdDestroySocket;
256  }
258  iout << iWARN << "Ignoring IMD forces as receiving forces is not supported in IMDv3 as of now\n" << endi;
259  }
260  else if (IMDignore || IMDignoreForces) {
261  if ( ! warned ) {
262  warned = 1;
263  char option[16];
264  if (IMDignore) strcpy(option, "IMDignore");
265  else strcpy(option, "IMDignoreForces");
266  iout << iWARN << "Ignoring IMD forces due to " << option << "\n" << endi;
267  }
268  } else {
269  for (int i=0; i<length; i++) {
270  vnew.index = vmd_atoms[i];
271  if ( (vtest=vmdforces.find(vnew)) != NULL) {
272  // find was successful, so overwrite the old force values
273  if (vmd_forces[3*i] != 0.0f || vmd_forces[3*i+1] != 0.0f
274  || vmd_forces[3*i+2] != 0.0f) {
275  vtest->force.x = vmd_forces[3*i];
276  vtest->force.y = vmd_forces[3*i+1];
277  vtest->force.z = vmd_forces[3*i+2];
278  } else {
279  // or delete it from the list if the new force is ZERO
280  vmdforces.del(vnew);
281  }
282  }
283  else {
284  // Create a new entry in the table if the new force isn't ZERO
285  if (vmd_forces[3*i] != 0.0f || vmd_forces[3*i+1] != 0.0f
286  || vmd_forces[3*i+2] != 0.0f) {
287  vnew.force.x = vmd_forces[3*i];
288  vnew.force.y = vmd_forces[3*i+1];
289  vnew.force.z = vmd_forces[3*i+2];
290  vmdforces.add(vnew);
291  }
292  }
293  }
294  }
295  delete [] vmd_atoms;
296  delete [] vmd_forces;
297  break;
298  case IMD_TRATE:
299  iout << iINFO << "Setting transfer rate to " << length<<'\n'<<endi;
300  Node::Object()->imd->set_transrate(length);
301  break;
302  case IMD_PAUSE:
303  if (IMDignore) {
304  iout << iWARN << "Ignoring IMD pause due to IMDignore\n" << endi;
305  break;
306  }
308  if ( paused ) {
309  iout << iINFO << "Resuming IMD\n" << endi;
311  }
312  paused = ! paused;
313  if ( paused ) {
314  iout << iINFO << "Pausing IMD\n" << endi;
315  IMDwait = 1;
316  }
317  } else if (IMDversion == IMDversion_t::IMDv3) {
318  if ( paused ) {
319  iout << iINFO << "Already paused\n" << endi;
320  }
321  if ( ! paused ) {
322  iout << iINFO << "Pausing IMD\n" << endi;
323  paused = ! paused;
324  }
325  }
326  break;
327  case IMD_RESUME:
329  if ( paused ) {
330  iout << iINFO << "Resuming IMD\n" << endi;
331  }
332  paused = ! paused;
333  }
334  break;
335  case IMD_IOERROR:
336  iout << iWARN << "IMD connection lost\n" << endi;
337  case IMD_DISCONNECT:
338  iout << iINFO << "IMD connection detached\n" << endi;
339  vmdDestroySocket:
340  vmdsock_destroy(clientsock);
341  clients.del(i_client);
342  // Adjust index to account for the deleted client
343  i_client--;
344  // Remove any paused state before moving onto next client
345  if ( paused ) paused = ! paused;
346  // Enable the MD to continue after detach if IMD is v2
348  if (IMDwait && !clients.size()) imd_connection();
349  goto vmdEnd;
350  case IMD_KILL:
351  if (IMDignore) {
352  iout << iWARN << "Ignoring IMD kill due to IMDignore\n" << endi;
353  break;
354  }
355  NAMD_quit("Received IMD kill from client\n");
356  break;
357  case IMD_ENERGIES:
358  IMDEnergies junk;
359  imd_recv_energies(clientsock, &junk);
360  break;
361  case IMD_FCOORDS:
362  vmd_forces = new float[3*length];
363  imd_recv_fcoords(clientsock, length, vmd_forces);
364  delete [] vmd_forces;
365  break;
366  case IMD_WAIT:
368  IMDwait = length;
369  iout << iINFO << "IMD set to " << (IMDwait ? "wait" : "not wait") << " for incoming connection" << '\n' << endi;
370  }
371  break;
372  default: ;
373  }
374  }
375  vmdEnd: ;
376  }
377 }
378 
380  const SimParameters *simparams = Node::Object()->simParameters;
381  for (int i=0; i<clients.size(); i++) {
382  void *clientsock = clients[i];
383  if (simparams->IMDversion == IMDversion_t::IMDv2) {
384  if (!clientsock || !vmdsock_selwrite(clientsock,0)) continue;
385  } else if (simparams->IMDversion == IMDversion_t::IMDv3) {
386  /* NOTE (from Lawson Woods):
387  * If vmdsock_selwrite returns 0, indicating that the socket
388  * isn't immediately available (within 0 seconds), the above
389  * IMDv2 code just doesn't send the packet.
390  * But in IMDv3, packets aren't skippable, the simulation
391  * engine has to wait until the socket is ready.
392  */
393  if (!clientsock) continue;
394  else if (!vmdsock_selwrite(clientsock, IMDv3TimeoutSeconds)) {
395  const std::string error =
396  std::string{"IMDv3: GlobalMasterIMD::send_energies socket timeout in "} +
397  std::to_string(IMDv3TimeoutSeconds) + "\n";
398  NAMD_die(error.c_str());
399  }
400  }
401  imd_send_energies(clientsock, energies);
402  }
403 }
404 
406  const SimParameters *simparams = Node::Object()->simParameters;
407  for (int i=0; i<clients.size(); i++) {
408  void *clientsock = clients[i];
409  if (simparams->IMDversion == IMDversion_t::IMDv2) {
410  if (!clientsock || !vmdsock_selwrite(clientsock,0)) continue;
411  } else if (simparams->IMDversion == IMDversion_t::IMDv3) {
412  /* NOTE (from Lawson Woods):
413  * If vmdsock_selwrite returns 0, indicating that the socket
414  * isn't immediately available (within 0 seconds), the above
415  * IMDv2 code just doesn't send the packet.
416  * But in IMDv3, packets aren't skippable, the simulation
417  * engine has to wait until the socket is ready.
418  */
419  if (!clientsock) continue;
420  else if (!vmdsock_selwrite(clientsock, IMDv3TimeoutSeconds)) {
421  const std::string error =
422  std::string{"IMDv3: GlobalMasterIMD::send_fcoords socket timeout in "} +
423  std::to_string(IMDv3TimeoutSeconds) + "\n";
424  NAMD_die(error.c_str());
425  }
426  }
427  if (sizeof(FloatVector) == 3*sizeof(float)) {
428  imd_send_fcoords(clientsock, N, (float *)coords);
429  } else {
430  if (coordtmpsize < N) {
431  if (coordtmp) delete [] coordtmp;
432  coordtmp = new float[3*N];
433  coordtmpsize = N;
434  }
435  for (int i=0; i<N; i++) {
436  coordtmp[3*i] = coords[i].x;
437  coordtmp[3*i+1] = coords[i].y;
438  coordtmp[3*i+2] = coords[i].z;
439  }
440  imd_send_fcoords(clientsock, N, coordtmp);
441  }
442  }
443 }
444 
446  for (int i=0; i<clients.size(); i++) {
447  void *clientsock = clients[i];
448  /*
449  * Only IMDv3 may require the velocities.
450  */
451  if (!clientsock) continue;
452  else if (!vmdsock_selwrite(clientsock, IMDv3TimeoutSeconds)) {
453  const std::string error =
454  std::string{"IMDv3: GlobalMasterIMD::send_velocities socket timeout in "} +
455  std::to_string(IMDv3TimeoutSeconds) + "\n";
456  NAMD_die(error.c_str());
457  }
458  if (sizeof(FloatVector) == 3*sizeof(float)) {
459  imd_send_velocities(clientsock, N, (float *)velocities);
460  } else {
461  if (veltmpsize < N) {
462  if (veltmp) delete [] veltmp;
463  veltmp = new float[3*N];
464  veltmpsize = N;
465  }
466  for (int i=0; i<N; i++) {
467  veltmp[3*i] = velocities[i].x;
468  veltmp[3*i+1] = velocities[i].y;
469  veltmp[3*i+2] = velocities[i].z;
470  }
471  imd_send_velocities(clientsock, N, veltmp);
472  }
473  }
474 }
475 
477  for (int i=0; i<clients.size(); i++) {
478  void *clientsock = clients[i];
479  /*
480  * Only IMDv3 may require the forces.
481  */
482  if (!clientsock) continue;
483  else if (!vmdsock_selwrite(clientsock, IMDv3TimeoutSeconds)) {
484  const std::string error =
485  std::string{"IMDv3: GlobalMasterIMD::send_forces socket timeout in "} +
486  std::to_string(IMDv3TimeoutSeconds) + "\n";
487  NAMD_die(error.c_str());
488  }
489  if (sizeof(FloatVector) == 3*sizeof(float)) {
490  imd_send_forces(clientsock, N, (float *)forces);
491  } else {
492  if (forcetmpsize < N) {
493  if (forcetmp) delete [] forcetmp;
494  forcetmp = new float[3*N];
495  forcetmpsize = N;
496  }
497  for (int i=0; i<N; i++) {
498  forcetmp[3*i] = forces[i].x;
499  forcetmp[3*i+1] = forces[i].y;
500  forcetmp[3*i+2] = forces[i].z;
501  }
502  imd_send_forces(clientsock, N, forcetmp);
503  }
504  }
505 }
506 
508  for (int i=0; i<clients.size(); i++) {
509  void *clientsock = clients[i];
510  /*
511  * Only IMDv3 may require the box.
512  */
513  if (!clientsock) continue;
514  else if (!vmdsock_selwrite(clientsock, IMDv3TimeoutSeconds)) {
515  const std::string error =
516  std::string{"IMDv3: GlobalMasterIMD::send_box socket timeout in "} +
517  std::to_string(IMDv3TimeoutSeconds) + "\n";
518  NAMD_die(error.c_str());
519  }
520  imd_send_box(clientsock, box);
521  }
522 }
523 
525  for (int i=0; i<clients.size(); i++) {
526  void *clientsock = clients[i];
527  /*
528  * Only IMDv3 may require the time.
529  */
530  if (!clientsock) continue;
531  else if (!vmdsock_selwrite(clientsock, IMDv3TimeoutSeconds)) {
532  const std::string error =
533  std::string{"IMDv3: GlobalMasterIMD::send_time socket timeout in "} +
534  std::to_string(IMDv3TimeoutSeconds) + "\n";
535  NAMD_die(error.c_str());
536  }
537  imd_send_time(clientsock, time);
538  }
539 }
static Node * Object()
Definition: Node.h:86
int vmdsock_selwrite(void *v, int sec)
Definition: vmdsock.C:194
void * vmdsock_create(void)
Definition: vmdsock.C:76
Definition: imd.h:33
ForceList & modifyAppliedForces()
Definition: GlobalMaster.C:203
std::ostream & iINFO(std::ostream &s)
Definition: InfoStream.C:81
static int find_free_port(void *sock, int defport)
Vector force
int vmdsock_selread(void *v, int sec)
Definition: vmdsock.C:177
int size(void) const
Definition: ResizeArray.h:131
void send_time(IMDTime *)
static int my_imd_connect(void *s, const int IMDversion, const IMDSessionInfo *sessionInfo)
Definition: common.h:275
void NAMD_quit(const char *err_msg)
Definition: common.C:125
IMDOutput * imd
Definition: Node.h:186
int imd_send_energies(void *s, const IMDEnergies *energies)
Definition: imd.C:182
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
BigReal z
Definition: Vector.h:74
float y
Definition: Vector.h:26
std::ostream & iWARN(std::ostream &s)
Definition: InfoStream.C:82
int imd_send_box(void *s, const IMDBox *box)
Definition: imd.C:222
Definition: imd.h:19
Definition: imd.h:22
#define iout
Definition: InfoStream.h:51
int add(const Elem &elem)
Definition: ResizeArray.h:101
IMDType
Definition: imd.h:15
const ResizeArray< AtomIDList > & requestedGroups()
Definition: GlobalMaster.C:190
void resize(int i)
Definition: ResizeArray.h:84
int vmdsock_init(void)
Definition: vmdsock.C:52
void send_velocities(int, FloatVector *)
Definition: imd.h:55
SubmitReduction * reductionGpuResident
Definition: GlobalMaster.h:196
int imd_send_fcoords(void *s, int32 n, const float *coords)
Definition: imd.C:192
float x
Definition: Vector.h:26
SubmitReduction * reduction
Definition: GlobalMaster.h:198
vmdforce & operator=(const vmdforce &v)
float z
Definition: Vector.h:26
int vmdsock_listen(void *v)
Definition: vmdsock.C:123
int imd_send_forces(void *s, int32 n, const float *forces)
Definition: imd.C:212
int vmdsock_bind(void *v, int port)
Definition: vmdsock.C:114
void send_box(IMDBox *)
int imd_recv_fcoords(void *s, int32 n, float *coords)
Definition: imd.C:298
int imd_send_time(void *s, const IMDTime *time)
Definition: imd.C:232
int imd_recv_mdcomm(void *s, int32 n, int32 *indices, float *forces)
Definition: imd.C:287
BigReal x
Definition: Vector.h:74
int imd_sessioninfo(void *s, const IMDSessionInfo *info)
Definition: imd.C:160
AtomIDList & modifyForcedAtoms()
Definition: GlobalMaster.C:198
void NAMD_die(const char *err_msg)
Definition: common.C:147
Definition: imd.h:24
ForceList & modifyGroupForces()
Definition: GlobalMaster.C:208
Definition: imd.h:49
Elem & item(int i)
Definition: ResizeArray.h:119
int imd_handshake(void *s, const int IMDVersion)
Definition: imd.C:132
IMDSessionInfo IMDsendsettings
ResizeArray< void * > clients
static UniqueSortedArray< vmdforce > vmdforces
void send_energies(IMDEnergies *)
virtual void calculate()
void vmdsock_destroy(void *v)
Definition: vmdsock.C:164
BigReal y
Definition: Vector.h:74
void send_forces(int, FloatVector *)
int operator<(const vmdforce &v)
void del(int index, int num=1)
Definition: ResizeArray.h:108
Definition: imd.h:23
int imd_recv_energies(void *s, IMDEnergies *energies)
Definition: imd.C:293
int operator==(const vmdforce &v)
Definition: common.h:275
Definition: imd.h:28
IMDSessionInfo IMDsendsettings
Definition: imd.h:21
IMDType imd_recv_header(void *s, int32 *length)
Definition: imd.C:276
void set_transrate(int newrate)
Definition: IMDOutput.h:42
static float * coords
Definition: ScriptTcl.C:67
void use_imd(GlobalMasterIMD *)
Definition: IMDOutput.C:28
void send_fcoords(int, FloatVector *)
int imd_send_velocities(void *s, int32 n, const float *vels)
Definition: imd.C:202
void * vmdsock_accept(void *v)
Definition: vmdsock.C:128