NAMD
Classes | Macros | Functions
imd.C File Reference
#include "imd.h"
#include "vmdsock.h"
#include <string.h>
#include <errno.h>
#include <stdlib.h>

Go to the source code of this file.

Classes

struct  IMDheader
 
union  netint
 structure used to perform byte swapping operations More...
 

Macros

#define HEADERSIZE   8
 
#define IMDVERSION   2
 

Functions

static void swap4 (char *data, int ndata)
 
static int32 imd_htonl (int32 h)
 
static int32 imd_ntohl (int32 n)
 
static void fill_header (IMDheader *header, IMDType type, int32 length)
 
static void swap_header (IMDheader *header)
 
static int32 imd_readn (void *s, char *ptr, int32 n)
 
static int32 imd_writen (void *s, const char *ptr, int32 n)
 
int imd_disconnect (void *s)
 
int imd_pause (void *s)
 
int imd_kill (void *s)
 
static int imd_go (void *s)
 
int imd_handshake (void *s)
 
int imd_trate (void *s, int32 rate)
 
int imd_send_mdcomm (void *s, int32 n, const int32 *indices, const float *forces)
 
int imd_send_energies (void *s, const IMDEnergies *energies)
 
int imd_send_fcoords (void *s, int32 n, const float *coords)
 
IMDType imd_recv_header_nolengthswap (void *s, int32 *length)
 
int imd_recv_handshake (void *s)
 Receive header and data. More...
 
IMDType imd_recv_header (void *s, int32 *length)
 
int imd_recv_mdcomm (void *s, int32 n, int32 *indices, float *forces)
 
int imd_recv_energies (void *s, IMDEnergies *energies)
 
int imd_recv_fcoords (void *s, int32 n, float *coords)
 

Macro Definition Documentation

#define HEADERSIZE   8
#define IMDVERSION   2

Definition at line 15 of file imd.C.

Referenced by imd_handshake(), and imd_recv_handshake().

Function Documentation

static void fill_header ( IMDheader header,
IMDType  type,
int32  length 
)
static

Definition at line 60 of file imd.C.

References imd_htonl(), IMDheader::length, and IMDheader::type.

Referenced by imd_disconnect(), imd_go(), imd_handshake(), imd_kill(), imd_pause(), imd_send_energies(), imd_send_fcoords(), imd_send_mdcomm(), and imd_trate().

60  {
61  header->type = imd_htonl((int32)type);
62  header->length = imd_htonl(length);
63 }
short int32
Definition: dumpdcd.c:24
static int32 imd_htonl(int32 h)
Definition: imd.C:45
int32 length
Definition: imd.C:11
int32 type
Definition: imd.C:10
int imd_disconnect ( void s)

Definition at line 108 of file imd.C.

References fill_header(), HEADERSIZE, IMD_DISCONNECT, and imd_writen().

108  {
109  IMDheader header;
110  fill_header(&header, IMD_DISCONNECT, 0);
111  return (imd_writen(s, (char *)&header, HEADERSIZE) != HEADERSIZE);
112 }
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
#define HEADERSIZE
Definition: imd.C:14
static int imd_go ( void s)
static

Definition at line 126 of file imd.C.

References fill_header(), HEADERSIZE, IMD_GO, and imd_writen().

Referenced by imd_recv_handshake().

126  {
127  IMDheader header;
128  fill_header(&header, IMD_GO, 0);
129  return (imd_writen(s, (char *)&header, HEADERSIZE) != HEADERSIZE);
130 }
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
#define HEADERSIZE
Definition: imd.C:14
Definition: imd.h:17
int imd_handshake ( void s)

Definition at line 133 of file imd.C.

References fill_header(), HEADERSIZE, IMD_HANDSHAKE, imd_writen(), IMDVERSION, and IMDheader::length.

Referenced by my_imd_connect().

133  {
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 }
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
#define HEADERSIZE
Definition: imd.C:14
#define IMDVERSION
Definition: imd.C:15
int32 length
Definition: imd.C:11
static int32 imd_htonl ( int32  h)
static

Definition at line 45 of file imd.C.

References netint::b, netint::high, netint::highest, netint::i, netint::low, and netint::lowest.

Referenced by fill_header().

45  {
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 }
structure used to perform byte swapping operations
Definition: imd.C:35
int32 i
Definition: imd.C:36
unsigned int low
Definition: imd.C:40
unsigned int lowest
Definition: imd.C:41
unsigned int high
Definition: imd.C:39
struct netint::@47 b
unsigned int highest
Definition: imd.C:38
int imd_kill ( void s)

Definition at line 120 of file imd.C.

References fill_header(), HEADERSIZE, IMD_KILL, and imd_writen().

120  {
121  IMDheader header;
122  fill_header(&header, IMD_KILL, 0);
123  return (imd_writen(s, (char *)&header, HEADERSIZE) != HEADERSIZE);
124 }
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
#define HEADERSIZE
Definition: imd.C:14
Definition: imd.h:19
static int32 imd_ntohl ( int32  n)
static

Definition at line 54 of file imd.C.

References netint::b, netint::high, netint::highest, netint::i, netint::low, and netint::lowest.

Referenced by swap_header().

54  {
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 }
structure used to perform byte swapping operations
Definition: imd.C:35
int32 i
Definition: imd.C:36
unsigned int low
Definition: imd.C:40
unsigned int lowest
Definition: imd.C:41
unsigned int high
Definition: imd.C:39
struct netint::@47 b
unsigned int highest
Definition: imd.C:38
int imd_pause ( void s)

Definition at line 114 of file imd.C.

References fill_header(), HEADERSIZE, IMD_PAUSE, and imd_writen().

114  {
115  IMDheader header;
116  fill_header(&header, IMD_PAUSE, 0);
117  return (imd_writen(s, (char *)&header, HEADERSIZE) != HEADERSIZE);
118 }
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
#define HEADERSIZE
Definition: imd.C:14
Definition: imd.h:21
static int32 imd_readn ( void s,
char *  ptr,
int32  n 
)
static

Definition at line 70 of file imd.C.

References vmdsock_read().

Referenced by imd_recv_energies(), imd_recv_fcoords(), imd_recv_header(), imd_recv_header_nolengthswap(), and imd_recv_mdcomm().

70  {
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 }
short int32
Definition: dumpdcd.c:24
int vmdsock_read(void *v, void *buf, int len)
Definition: vmdsock.C:154
int imd_recv_energies ( void s,
IMDEnergies energies 
)

Definition at line 231 of file imd.C.

References imd_readn().

Referenced by GlobalMasterIMD::get_vmd_forces().

231  {
232  return (imd_readn(s, (char *)energies, sizeof(IMDEnergies))
233  != sizeof(IMDEnergies));
234 }
static int32 imd_readn(void *s, char *ptr, int32 n)
Definition: imd.C:70
int imd_recv_fcoords ( void s,
int32  n,
float *  coords 
)

Definition at line 236 of file imd.C.

References imd_readn().

Referenced by GlobalMasterIMD::get_vmd_forces().

236  {
237  return (imd_readn(s, (char *)coords, 12*n) != 12*n);
238 }
static int32 imd_readn(void *s, char *ptr, int32 n)
Definition: imd.C:70
static float * coords
Definition: ScriptTcl.C:66
int imd_recv_handshake ( void s)

Receive header and data.

Definition at line 191 of file imd.C.

References imd_go(), IMD_HANDSHAKE, imd_recv_header_nolengthswap(), IMDVERSION, swap4(), and vmdsock_selread().

191  {
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 }
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
IMDType
Definition: imd.h:13
static void swap4(char *data, int ndata)
Definition: imd.C:17
#define IMDVERSION
Definition: imd.C:15
IMDType imd_recv_header ( void s,
int32 length 
)

Definition at line 214 of file imd.C.

References HEADERSIZE, IMD_IOERROR, imd_readn(), IMDheader::length, swap_header(), and IMDheader::type.

Referenced by GlobalMasterIMD::get_vmd_forces(), and my_imd_connect().

214  {
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 }
Definition: imd.C:9
#define HEADERSIZE
Definition: imd.C:14
IMDType
Definition: imd.h:13
static void swap_header(IMDheader *header)
Definition: imd.C:65
static int32 imd_readn(void *s, char *ptr, int32 n)
Definition: imd.C:70
int32 length
Definition: imd.C:11
int32 type
Definition: imd.C:10
IMDType imd_recv_header_nolengthswap ( void s,
int32 length 
)

Definition at line 182 of file imd.C.

References HEADERSIZE, IMD_IOERROR, imd_readn(), IMDheader::length, swap_header(), and IMDheader::type.

Referenced by imd_recv_handshake().

182  {
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 }
Definition: imd.C:9
#define HEADERSIZE
Definition: imd.C:14
IMDType
Definition: imd.h:13
static void swap_header(IMDheader *header)
Definition: imd.C:65
static int32 imd_readn(void *s, char *ptr, int32 n)
Definition: imd.C:70
int32 length
Definition: imd.C:11
int32 type
Definition: imd.C:10
int imd_recv_mdcomm ( void s,
int32  n,
int32 indices,
float *  forces 
)

Definition at line 225 of file imd.C.

References imd_readn().

Referenced by GlobalMasterIMD::get_vmd_forces().

225  {
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 }
static __thread float4 * forces
static int32 imd_readn(void *s, char *ptr, int32 n)
Definition: imd.C:70
int imd_send_energies ( void s,
const IMDEnergies energies 
)

Definition at line 159 of file imd.C.

References fill_header(), HEADERSIZE, IMD_ENERGIES, and imd_writen().

Referenced by GlobalMasterIMD::send_energies().

159  {
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 }
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
short int32
Definition: dumpdcd.c:24
#define HEADERSIZE
Definition: imd.C:14
int imd_send_fcoords ( void s,
int32  n,
const float *  coords 
)

Definition at line 169 of file imd.C.

References fill_header(), HEADERSIZE, IMD_FCOORDS, and imd_writen().

Referenced by GlobalMasterIMD::send_fcoords().

169  {
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 }
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
short int32
Definition: dumpdcd.c:24
#define HEADERSIZE
Definition: imd.C:14
static float * coords
Definition: ScriptTcl.C:66
int imd_send_mdcomm ( void s,
int32  n,
const int32 indices,
const float *  forces 
)

Definition at line 148 of file imd.C.

References fill_header(), HEADERSIZE, IMD_MDCOMM, and imd_writen().

148  {
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 }
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
short int32
Definition: dumpdcd.c:24
static __thread float4 * forces
#define HEADERSIZE
Definition: imd.C:14
Definition: imd.h:20
int imd_trate ( void s,
int32  rate 
)

Definition at line 140 of file imd.C.

References fill_header(), HEADERSIZE, IMD_TRATE, and imd_writen().

140  {
141  IMDheader header;
142  fill_header(&header, IMD_TRATE, rate);
143  return (imd_writen(s, (char *)&header, HEADERSIZE) != HEADERSIZE);
144 }
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
#define HEADERSIZE
Definition: imd.C:14
Definition: imd.h:22
static int32 imd_writen ( void s,
const char *  ptr,
int32  n 
)
static

Definition at line 89 of file imd.C.

References vmdsock_write().

Referenced by imd_disconnect(), imd_go(), imd_handshake(), imd_kill(), imd_pause(), imd_send_energies(), imd_send_fcoords(), imd_send_mdcomm(), and imd_trate().

89  {
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 }
short int32
Definition: dumpdcd.c:24
int vmdsock_write(void *v, const void *buf, int len)
Definition: vmdsock.C:145
static void swap4 ( char *  data,
int  ndata 
)
static

Definition at line 17 of file imd.C.

Referenced by imd_recv_handshake().

17  {
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 }
static void swap_header ( IMDheader header)
static

Definition at line 65 of file imd.C.

References imd_ntohl(), IMDheader::length, and IMDheader::type.

Referenced by imd_recv_header(), and imd_recv_header_nolengthswap().

65  {
66  header->type = imd_ntohl(header->type);
67  header->length= imd_ntohl(header->length);
68 }
static int32 imd_ntohl(int32 n)
Definition: imd.C:54
int32 length
Definition: imd.C:11
int32 type
Definition: imd.C:10