NAMD
GlobalMasterIMD.C
Go to the documentation of this file.
1 
7 #include "InfoStream.h"
8 #include "vmdsock.h"
9 #include "Node.h"
10 #include "IMDOutput.h"
11 #include "imd.h"
12 #include "SimParameters.h"
13 #include "UniqueSortedArray.h"
14 #include "GlobalMaster.h"
15 #include "GlobalMasterIMD.h"
16 #include "Vector.h"
17 
18 #include <errno.h>
19 
20 //#define DEBUGM
21 #define MIN_DEBUG_LEVEL 1
22 #include "Debug.h"
23 
24 struct vmdforce {
25  int index;
27  int operator <(const vmdforce& v) {return index < v.index;}
28  // XXX the following is an abuse of overloading!
29  int operator ==(const vmdforce& v) {return index == v.index;}
31  index=v.index;
32  force=v.force;
33  return *this;
34  }
35 };
36 
37 //
38 // XXX static and global variables are unsafe for shared memory builds.
39 // The use of global and static vars should be eliminated.
40 //
42 
43 // Search for a free port in the range 1025-4096; return the successful port,
44 // or -1 if we failed.
45 
46 static int find_free_port(void *sock, int defport) {
47  if (vmdsock_bind(sock, defport)==0) return defport; // success
48  for (int port=1025; port < 4096; port++)
49  if (vmdsock_bind(sock, port)==0) return port;
50  return -1;
51 }
52 
54  DebugM(3,"Constructing\n");
56  int port = simparams->IMDport;
57  IMDwait = simparams->IMDwait;
58  IMDignore = simparams->IMDignore;
59  IMDignoreForces = simparams->IMDignoreForces;
60  coordtmp = NULL;
61  coordtmpsize = 0;
62 
63  if ( vmdsock_init() ) {
64  NAMD_die("Unable to initialize socket interface for IMD.\n");
65  }
66  sock = vmdsock_create();
67  int newport = find_free_port(sock, port);
68  if (newport != port) {
69  iout << iWARN << "Interactive MD failed to bind to port "
70  << port << ".\n" << endi;
71  }
72  if (newport < 0) {
74  NAMD_die("Interactive MD failed to find free port.\n");
75  }
77  iout << iINFO << "Interactive MD listening on port "
78  << newport << ".\n" << endi;
79  DebugM(2,"Done constructing ("<<requestedGroups().size()<<" initial groups)\n");
80 
81  Node::Object()->imd->use_imd(this);
82 }
83 
85  if (sock)
87  for (int i=0; i<clients.size(); i++)
89  delete [] coordtmp;
90 }
91 
92 static int my_imd_connect(void *s) {
93  if (imd_handshake(s)) {
94  iout << iWARN << "IMD handshake failed\n" << endi;
95  return 0;
96  }
97 
98  // Wait a second, then see if VMD has responded.
99  int32 length;
100  if (vmdsock_selread(s,1) != 1 || imd_recv_header(s, &length) != IMD_GO) {
101  iout << iWARN << "Incompatible Interactive MD, use VMD v1.4b2 or higher\n"
102  << endi;
103  return 0;
104  }
105  return 1;
106 }
107 
109  /* clear out the requested forces first! */
110  if (!IMDignore && !IMDignoreForces) {
114  }
115 
116  // check for incoming connection
117  do {
118  int rc;
119  if (IMDwait && !clients.size()) {
120  iout << iINFO << "INTERACTIVE MD AWAITING CONNECTION\n" << endi;
121  do { rc = vmdsock_selread(sock, 3600); } while (rc <= 0);
122  } else {
123  rc = vmdsock_selread(sock, 0);
124  }
125  if (rc > 0) {
126  void *clientsock = vmdsock_accept(sock);
127  if (!clientsock) {
128  iout << iWARN << "IMD socket accept failed\n" << endi;
129  } else {
130  if (!my_imd_connect(clientsock)) {
131  iout << iWARN << "IMD connection failed\n" << endi;
132  vmdsock_destroy(clientsock);
133  } else {
134  iout << iINFO << "IMD connection opened\n" <<endi;
135  clients.add(clientsock);
136  }
137  }
138  }
139  } while (IMDwait && !clients.size());
140 
141  // Assume for now that the only thing we get from VMD is a set of forces.
142  // Later we'll want to look for and implement more sophisticated control
143  // parameters. ie have a specified protocol
144 
145  // Check/get new forces from VMD
146  get_vmd_forces();
147 
148  // Right now I don't check to see if any new forces were obtained.
149  // An optimization would be cache the results message. However, there
150  // would still be copying since it looks like the messages get deleted
151  // by the receiver.
152 
153  /* set our arrays to be big enough to hold all of the forces */
154  int num = vmdforces.size();
155 
156  DebugM(2,"Setting " << num << " forces.\n");
157 
158  if (!IMDignore && !IMDignoreForces) {
159  modifyForcedAtoms().resize(num);
161 
162  int i;
164  for ( i = 0; i < num; ++i, ++v_i) {
165  modifyForcedAtoms().item(i) = v_i->index;
166  modifyAppliedForces().item(i) = v_i->force;
167  }
168  }
169 }
170 
172  IMDType type;
173  int32 length;
174  int32 *vmd_atoms;
175  float *vmd_forces;
176  int paused = 0;
177  int warned = 0;
178  vmdforce *vtest, vnew;
179 
180  // Loop through each socket one at a time. By doing this, rather than
181  // polling all sockets at once, NAMD only has to keep up with one IMD
182  // connection; if it tried to read from all of them, it could more easily
183  // fall behind and never finish draining all the sockets.
184  // It would be better to have a system where VMD couldn't DOS NAMD by
185  // spamming it with messages, but in practice NAMD is able to keep up with
186  // VMD's send rate.
187  for (int i_client=0; i_client<clients.size(); i_client++) {
188  void *clientsock = clients[i_client];
189  while (vmdsock_selread(clientsock,0) > 0 || paused) { // Drain the socket
190  type = imd_recv_header(clientsock, &length);
191  switch (type) {
192  case IMD_MDCOMM:
193  // Expect the msglength to give number of indicies, and the data
194  // message to consist of first the indicies, then the coordinates
195  // in xyz1 xyz2... format.
196  vmd_atoms = new int32[length];
197  vmd_forces = new float[3*length];
198  if (imd_recv_mdcomm(clientsock, length, vmd_atoms, vmd_forces)) {
199  iout << iWARN <<
200  "Error reading IMD forces, killing connection\n" << endi;
201  goto vmdDestroySocket;
202  }
203  if (IMDignore || IMDignoreForces) {
204  if ( ! warned ) {
205  warned = 1;
206  char option[16];
207  if (IMDignore) strcpy(option, "IMDignore");
208  else strcpy(option, "IMDignoreForces");
209  iout << iWARN << "Ignoring IMD forces due to " << option << "\n" << endi;
210  }
211  } else {
212  for (int i=0; i<length; i++) {
213  vnew.index = vmd_atoms[i];
214  if ( (vtest=vmdforces.find(vnew)) != NULL) {
215  // find was successful, so overwrite the old force values
216  if (vmd_forces[3*i] != 0.0f || vmd_forces[3*i+1] != 0.0f
217  || vmd_forces[3*i+2] != 0.0f) {
218  vtest->force.x = vmd_forces[3*i];
219  vtest->force.y = vmd_forces[3*i+1];
220  vtest->force.z = vmd_forces[3*i+2];
221  } else {
222  // or delete it from the list if the new force is ZERO
223  vmdforces.del(vnew);
224  }
225  }
226  else {
227  // Create a new entry in the table if the new force isn't ZERO
228  if (vmd_forces[3*i] != 0.0f || vmd_forces[3*i+1] != 0.0f
229  || vmd_forces[3*i+2] != 0.0f) {
230  vnew.force.x = vmd_forces[3*i];
231  vnew.force.y = vmd_forces[3*i+1];
232  vnew.force.z = vmd_forces[3*i+2];
233  vmdforces.add(vnew);
234  }
235  }
236  }
237  }
238  delete [] vmd_atoms;
239  delete [] vmd_forces;
240  break;
241  case IMD_TRATE:
242  iout << iINFO << "Setting transfer rate to " << length<<'\n'<<endi;
243  Node::Object()->imd->set_transrate(length);
244  break;
245  case IMD_PAUSE:
246  if (IMDignore) {
247  iout << iWARN << "Ignoring IMD pause due to IMDignore\n" << endi;
248  break;
249  }
250  if ( paused ) {
251  iout << iINFO << "Resuming IMD\n" << endi;
253  }
254  paused = ! paused;
255  if ( paused ) {
256  iout << iINFO << "Pausing IMD\n" << endi;
257  IMDwait = 1;
258  }
259  break;
260  case IMD_IOERROR:
261  iout << iWARN << "IMD connection lost\n" << endi;
262  case IMD_DISCONNECT:
263  iout << iINFO << "IMD connection detached\n" << endi;
264  vmdDestroySocket:
265  vmdsock_destroy(clientsock);
266  clients.del(i_client);
267  // Enable the MD to continue after detach
268  if (IMDwait) IMDwait = 0;
269  goto vmdEnd;
270  case IMD_KILL:
271  if (IMDignore) {
272  iout << iWARN << "Ignoring IMD kill due to IMDignore\n" << endi;
273  break;
274  }
275  NAMD_quit("Received IMD kill from client\n");
276  break;
277  case IMD_ENERGIES:
278  IMDEnergies junk;
279  imd_recv_energies(clientsock, &junk);
280  break;
281  case IMD_FCOORDS:
282  vmd_forces = new float[3*length];
283  imd_recv_fcoords(clientsock, length, vmd_forces);
284  delete [] vmd_forces;
285  break;
286  default: ;
287  }
288  }
289  vmdEnd: ;
290  }
291 }
292 
294  for (int i=0; i<clients.size(); i++) {
295  void *clientsock = clients[i];
296  if (!clientsock || !vmdsock_selwrite(clientsock,0)) continue;
297  imd_send_energies(clientsock, energies);
298  }
299 }
300 
302  for (int i=0; i<clients.size(); i++) {
303  void *clientsock = clients[i];
304  if (!clientsock || !vmdsock_selwrite(clientsock,0)) continue;
305  if (sizeof(FloatVector) == 3*sizeof(float)) {
306  imd_send_fcoords(clientsock, N, (float *)coords);
307  } else {
308  if (coordtmpsize < N) {
309  delete [] coordtmp;
310  coordtmp = new float[3*N];
311  coordtmpsize = N;
312  }
313  for (int i=0; i<N; i++) {
314  coordtmp[3*i] = coords[i].x;
315  coordtmp[3*i+1] = coords[i].y;
316  coordtmp[3*i+2] = coords[i].z;
317  }
318  imd_send_fcoords(clientsock, N, coordtmp);
319  }
320  }
321 }
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
ForceList & modifyAppliedForces()
Definition: GlobalMaster.C:162
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
static int my_imd_connect(void *s)
void NAMD_quit(const char *err_msg)
Definition: common.C:64
short int32
Definition: dumpdcd.c:24
IMDOutput * imd
Definition: Node.h:183
void del(int index, int num=1)
Definition: ResizeArray.h:104
int imd_send_energies(void *s, const IMDEnergies *energies)
Definition: imd.C:159
Definition: Vector.h:64
SimParameters * simParameters
Definition: Node.h:178
#define DebugM(x, y)
Definition: Debug.h:59
std::ostream & endi(std::ostream &s)
Definition: InfoStream.C:54
BigReal z
Definition: Vector.h:66
float y
Definition: Vector.h:18
std::ostream & iWARN(std::ostream &s)
Definition: InfoStream.C:82
Definition: imd.h:17
Definition: imd.h:20
#define iout
Definition: InfoStream.h:51
Elem & item(int i)
Definition: ResizeArray.h:115
IMDType
Definition: imd.h:13
const ResizeArray< AtomIDList > & requestedGroups()
Definition: GlobalMaster.C:149
int vmdsock_init(void)
Definition: vmdsock.C:52
int imd_send_fcoords(void *s, int32 n, const float *coords)
Definition: imd.C:169
float x
Definition: Vector.h:18
vmdforce & operator=(const vmdforce &v)
float z
Definition: Vector.h:18
int vmdsock_listen(void *v)
Definition: vmdsock.C:123
int vmdsock_bind(void *v, int port)
Definition: vmdsock.C:114
int imd_recv_fcoords(void *s, int32 n, float *coords)
Definition: imd.C:236
int imd_recv_mdcomm(void *s, int32 n, int32 *indices, float *forces)
Definition: imd.C:225
BigReal x
Definition: Vector.h:66
int index(const Elem &elem)
Definition: SortedArray.h:75
AtomIDList & modifyForcedAtoms()
Definition: GlobalMaster.C:157
int imd_handshake(void *s)
Definition: imd.C:133
void NAMD_die(const char *err_msg)
Definition: common.C:85
Definition: imd.h:22
ForceList & modifyGroupForces()
Definition: GlobalMaster.C:167
int add(const Elem &elem)
Definition: ResizeArray.h:97
ResizeArray< void * > clients
int add(const Elem &elem)
static UniqueSortedArray< vmdforce > vmdforces
void send_energies(IMDEnergies *)
void resize(int i)
Definition: ResizeArray.h:84
virtual void calculate()
void vmdsock_destroy(void *v)
Definition: vmdsock.C:164
BigReal y
Definition: Vector.h:66
int operator<(const vmdforce &v)
void del(const Elem &elem)
Definition: SortedArray.h:59
Elem * find(const Elem &elem)
Definition: SortedArray.h:94
Definition: imd.h:21
int imd_recv_energies(void *s, IMDEnergies *energies)
Definition: imd.C:231
int operator==(const vmdforce &v)
int size(void) const
Definition: ResizeArray.h:127
Definition: imd.h:19
IMDType imd_recv_header(void *s, int32 *length)
Definition: imd.C:214
void set_transrate(int newrate)
Definition: IMDOutput.h:37
static float * coords
Definition: ScriptTcl.C:66
void use_imd(GlobalMasterIMD *)
Definition: IMDOutput.C:19
void send_fcoords(int, FloatVector *)
void * vmdsock_accept(void *v)
Definition: vmdsock.C:128
iterator begin(void)
Definition: ResizeArray.h:36