NAMD
imd.C
Go to the documentation of this file.
1 
2 
3 #include "imd.h"
4 #include "vmdsock.h"
5 #include <string.h>
6 #include <errno.h>
7 #include <stdlib.h>
8 
9 typedef struct {
12 } IMDheader;
13 
14 #define HEADERSIZE 8
15 #define IMDVERSION 2
16 
17 static void swap4(char *data, int ndata) {
18  int i;
19  char *dataptr;
20  char b0, b1;
21 
22  dataptr = data;
23  for (i=0; i<ndata; i+=4) {
24  b0 = dataptr[0];
25  b1 = dataptr[1];
26  dataptr[0] = dataptr[3];
27  dataptr[1] = dataptr[2];
28  dataptr[2] = b1;
29  dataptr[3] = b0;
30  dataptr += 4;
31  }
32 }
33 
35 typedef union {
37  struct {
38  unsigned int highest : 8;
39  unsigned int high : 8;
40  unsigned int low : 8;
41  unsigned int lowest : 8;
42  } b;
43 } netint;
44 
45 static int32 imd_htonl(int32 h) {
46  netint n;
47  n.b.highest = h >> 24;
48  n.b.high = h >> 16;
49  n.b.low = h >> 8;
50  n.b.lowest = h;
51  return n.i;
52 }
53 
54 static int32 imd_ntohl(int32 n) {
55  netint u;
56  u.i = n;
57  return (u.b.highest << 24 | u.b.high << 16 | u.b.low << 8 | u.b.lowest);
58 }
59 
60 static void fill_header(IMDheader *header, IMDType type, int32 length) {
61  header->type = imd_htonl((int32)type);
62  header->length = imd_htonl(length);
63 }
64 
65 static void swap_header(IMDheader *header) {
66  header->type = imd_ntohl(header->type);
67  header->length= imd_ntohl(header->length);
68 }
69 
70 static int32 imd_readn(void *s, char *ptr, int32 n) {
71  int32 nleft;
72  int32 nread;
73 
74  nleft = n;
75  while (nleft > 0) {
76  if ((nread = vmdsock_read(s, ptr, nleft)) < 0) {
77  if (errno == EINTR)
78  nread = 0; /* and call read() again */
79  else
80  return -1;
81  } else if (nread == 0)
82  break; /* EOF */
83  nleft -= nread;
84  ptr += nread;
85  }
86  return n-nleft;
87 }
88 
89 static int32 imd_writen(void *s, const char *ptr, int32 n) {
90  int32 nleft;
91  int32 nwritten;
92 
93  nleft = n;
94  while (nleft > 0) {
95  if ((nwritten = vmdsock_write(s, ptr, nleft)) <= 0) {
96  if (errno == EINTR)
97  nwritten = 0;
98  else
99  return -1;
100  }
101  nleft -= nwritten;
102  ptr += nwritten;
103  }
104  return n;
105 }
106 
107 
108 int imd_disconnect(void *s) {
109  IMDheader header;
110  fill_header(&header, IMD_DISCONNECT, 0);
111  return (imd_writen(s, (char *)&header, HEADERSIZE) != HEADERSIZE);
112 }
113 
114 int imd_pause(void *s) {
115  IMDheader header;
116  fill_header(&header, IMD_PAUSE, 0);
117  return (imd_writen(s, (char *)&header, HEADERSIZE) != HEADERSIZE);
118 }
119 
120 int imd_kill(void *s) {
121  IMDheader header;
122  fill_header(&header, IMD_KILL, 0);
123  return (imd_writen(s, (char *)&header, HEADERSIZE) != HEADERSIZE);
124 }
125 
126 static int imd_go(void *s) {
127  IMDheader header;
128  fill_header(&header, IMD_GO, 0);
129  return (imd_writen(s, (char *)&header, HEADERSIZE) != HEADERSIZE);
130 }
131 
132 
133 int imd_handshake(void *s) {
134  IMDheader header;
135  fill_header(&header, IMD_HANDSHAKE, 1);
136  header.length = IMDVERSION; // Not byteswapped!
137  return (imd_writen(s, (char *)&header, HEADERSIZE) != HEADERSIZE);
138 }
139 
140 int imd_trate(void *s, int32 rate) {
141  IMDheader header;
142  fill_header(&header, IMD_TRATE, rate);
143  return (imd_writen(s, (char *)&header, HEADERSIZE) != HEADERSIZE);
144 }
145 
146 // Data methods
147 
148 int imd_send_mdcomm(void *s,int32 n,const int32 *indices,const float *forces) {
149  int32 size = HEADERSIZE+16*n;
150  char *buf = new char[size];
151  fill_header((IMDheader *)buf, IMD_MDCOMM, n);
152  memcpy((void *)(buf+HEADERSIZE), (const void *)indices, 4*n);
153  memcpy((void *)(buf+HEADERSIZE+4*n), (const void *)forces, 12*n);
154  int rc = (imd_writen(s, buf, size) != size);
155  delete [] buf;
156  return rc;
157 }
158 
159 int imd_send_energies(void *s, const IMDEnergies *energies) {
160  int32 size = HEADERSIZE+sizeof(IMDEnergies);
161  char *buf = new char[size];
162  fill_header((IMDheader *)buf, IMD_ENERGIES, 1);
163  memcpy((void *)(buf+HEADERSIZE), (const void *)energies, sizeof(IMDEnergies));
164  int rc = (imd_writen(s, buf, size) != size);
165  delete [] buf;
166  return rc;
167 }
168 
169 int imd_send_fcoords(void *s, int32 n, const float *coords) {
170  int32 size = HEADERSIZE+12*n;
171  char *buf = new char[size];
172  fill_header((IMDheader *)buf, IMD_FCOORDS, n);
173  memcpy((void *)(buf+HEADERSIZE), (const void *)coords, 12*n);
174  int rc = (imd_writen(s, buf, size) != size);
175  delete [] buf;
176  return rc;
177 }
178 
179 // The IMD receive functions
180 
181 // The IMD receive functions
183  IMDheader header;
184  if (imd_readn(s, (char *)&header, HEADERSIZE) != HEADERSIZE)
185  return IMD_IOERROR;
186  *length = header.length;
187  swap_header(&header);
188  return IMDType(header.type);
189 }
190 
191 int imd_recv_handshake(void *s) {
192  // Wait 5 seconds for the handshake to come
193  if (vmdsock_selread(s, 5) != 1) return -1;
194 
195  // Check to see that a valid handshake was received
196  int32 buf;
197  IMDType type = imd_recv_header_nolengthswap(s, &buf);
198  if (type != IMD_HANDSHAKE) return -1;
199 
200  // Check its endianness, as well as the IMD version.
201  if (buf == IMDVERSION) {
202  if (!imd_go(s)) return 0;
203  return -1;
204  }
205  swap4((char *)&buf, 4);
206  if (buf == IMDVERSION) {
207  if (!imd_go(s)) return 1;
208  }
209 
210  // We failed to determine endianness.
211  return -1;
212 }
213 
214 IMDType imd_recv_header(void *s, int32 *length) {
215  IMDheader header;
216  if (imd_readn(s, (char *)&header, HEADERSIZE) != HEADERSIZE)
217  return IMD_IOERROR;
218  int i;
219  char *ch = (char*)(&header);
220  swap_header(&header);
221  *length = header.length;
222  return IMDType(header.type);
223 }
224 
225 int imd_recv_mdcomm(void *s, int32 n, int32 *indices, float *forces) {
226  if (imd_readn(s, (char *)indices, 4*n) != 4*n) return 1;
227  if (imd_readn(s, (char *)forces, 12*n) != 12*n) return 1;
228  return 0;
229 }
230 
231 int imd_recv_energies(void *s, IMDEnergies *energies) {
232  return (imd_readn(s, (char *)energies, sizeof(IMDEnergies))
233  != sizeof(IMDEnergies));
234 }
235 
236 int imd_recv_fcoords(void *s, int32 n, float *coords) {
237  return (imd_readn(s, (char *)coords, 12*n) != 12*n);
238 }
239 
static int32 imd_writen(void *s, const char *ptr, int32 n)
Definition: imd.C:89
static void fill_header(IMDheader *header, IMDType type, int32 length)
Definition: imd.C:60
Definition: imd.C:9
int imd_send_mdcomm(void *s, int32 n, const int32 *indices, const float *forces)
Definition: imd.C:148
int vmdsock_selread(void *v, int sec)
Definition: vmdsock.C:177
static int imd_go(void *s)
Definition: imd.C:126
short int32
Definition: dumpdcd.c:24
IMDType imd_recv_header_nolengthswap(void *s, int32 *length)
Definition: imd.C:182
structure used to perform byte swapping operations
Definition: imd.C:35
int imd_send_energies(void *s, const IMDEnergies *energies)
Definition: imd.C:159
static __thread float4 * forces
#define HEADERSIZE
Definition: imd.C:14
int32 i
Definition: imd.C:36
unsigned int low
Definition: imd.C:40
Definition: imd.h:17
Definition: imd.h:20
static int32 imd_ntohl(int32 n)
Definition: imd.C:54
IMDType
Definition: imd.h:13
static void swap4(char *data, int ndata)
Definition: imd.C:17
int imd_trate(void *s, int32 rate)
Definition: imd.C:140
int imd_pause(void *s)
Definition: imd.C:114
static int32 imd_htonl(int32 h)
Definition: imd.C:45
int imd_send_fcoords(void *s, int32 n, const float *coords)
Definition: imd.C:169
unsigned int lowest
Definition: imd.C:41
static void swap_header(IMDheader *header)
Definition: imd.C:65
unsigned int high
Definition: imd.C:39
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
static int32 imd_readn(void *s, char *ptr, int32 n)
Definition: imd.C:70
#define IMDVERSION
Definition: imd.C:15
int imd_handshake(void *s)
Definition: imd.C:133
Definition: imd.h:22
struct netint::@47 b
int imd_disconnect(void *s)
Definition: imd.C:108
int32 length
Definition: imd.C:11
int imd_recv_handshake(void *s)
Receive header and data.
Definition: imd.C:191
int imd_kill(void *s)
Definition: imd.C:120
int vmdsock_write(void *v, const void *buf, int len)
Definition: vmdsock.C:145
Definition: imd.h:21
int imd_recv_energies(void *s, IMDEnergies *energies)
Definition: imd.C:231
unsigned int highest
Definition: imd.C:38
Definition: imd.h:19
IMDType imd_recv_header(void *s, int32 *length)
Definition: imd.C:214
int32 type
Definition: imd.C:10
static float * coords
Definition: ScriptTcl.C:66
int vmdsock_read(void *v, void *buf, int len)
Definition: vmdsock.C:154