changed reading hint
[gromacs/adressmacs.git] / src / gmxlib / pvmio.c
blob9bfe70ee6a9ccc8196dfd0d92eddf675ba36b997
1 /*
2 * $Id$
3 *
4 * This source code is part of
5 *
6 * G R O M A C S
7 *
8 * GROningen MAchine for Chemical Simulations
9 *
10 * VERSION 2.0
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
16 * Please refer to:
17 * GROMACS: A message-passing parallel molecular dynamics implementation
18 * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19 * Comp. Phys. Comm. 91, 43-56 (1995)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
23 * or e-mail to:
24 * gromacs@chem.rug.nl
26 * And Hey:
27 * Green Red Orange Magenta Azure Cyan Skyblue
29 static char *SRCID_pvmio_c = "$Id$";
31 #include <stdio.h>
32 #include "typedefs.h"
33 #include "network.h"
34 #include "macros.h"
35 #include "main.h"
37 #ifdef USE_PVM3
39 #include "pvm3.h"
41 #define DEFAULT_STRIDE 1
42 #define ANY_TID -1
44 typedef struct {
45 int errnok;
46 char *msgk;
47 } t_pvmerr;
49 static int tids[MAXPROC];
50 static int mypid=0,mynprocs=1;
51 static unsigned long idle_send=0,idle_rec=0;
53 void pvmio_tx(int dest,void *buf,int bufsize)
55 int bufid,info;
56 #ifdef DEBUG
57 fprintf(stderr,"dest: %d, type: %d \n",dest,PVM_BYTE);
58 #endif
59 bufid = pvm_initsend(PvmDataRaw);
60 info = pvm_pkbyte(buf,bufsize,DEFAULT_STRIDE);
61 info = pvm_send(dest,PVM_BYTE);
64 char *pvm_error(int errorno)
66 static t_pvmerr pvmerrs[] = {
67 -2, "PvmBadParam (giving an invalid argument value)",
68 -6, "PvmNoHost (specified host is not on the virtual machine)",
69 -7, "PvmNoFile (Can not find executable)",
70 -10, "PvmNoMem (not enough memory)",
71 -14, "PvmSysErr (pvmd not responding)",
72 -27, "PvmOutOfRes (out of resources)"
74 static char *deferr="Unknown Pvm Error";
75 int i;
77 for(i=0; (i<asize(pvmerrs)); i++)
78 if (errorno == pvmerrs[i].errnok)
79 return pvmerrs[i].msgk;
81 return deferr;
84 void pvmio_tx_wait(int dest)
86 #ifdef PROFILING
87 idle_send++;
88 #endif
91 void pvmio_txs(int dest,void *buf,int bufsize)
93 pvmio_tx(dest,buf,bufsize);
94 pvmio_tx_wait(dest);
97 void pvmio_rx(int src,void *buf,int bufsize)
99 int bufid;
101 bufid = pvm_recv(src,PVM_BYTE);
102 if (bufid)
103 pvm_upkbyte(buf,bufsize,DEFAULT_STRIDE);
104 else
105 fatal_error(0,"Did not receive %d bytes from src %x",bufsize,src);
108 void pvmio_rx_wait(int src)
110 #ifdef PROFILING
111 idle_rec++;
112 #endif
115 void pvmio_rxs(int src,void *buf,int bufsize)
117 pvmio_rx(src,buf,bufsize);
118 pvmio_rx_wait(src);
122 int pvmio_setup(char *argv[],int nprocs)
124 int i,bufid,info;
125 int pid,numt;
127 if (pvm_parent() == PvmNoParent) {
128 /* I am the MASTER! Obey me... */
129 pid=0;
130 tids[0]=pvm_mytid();
131 fprintf(stderr,"PVM_MD: Spawning %d tasks\n",nprocs-1);
132 numt=pvm_spawn(argv[0],&(argv[1]),PvmTaskDefault,
133 "",nprocs-1,tids+1);
134 if (numt != nprocs-1) {
135 fprintf(stderr,"Could only spawn %d tasks!\n",numt);
136 for(i=0; (i<nprocs); i++) {
137 if (tids[i] < 0)
138 fprintf(stderr,"PVM_MD: tids[%d] = %d --> %s\n",
139 i,tids[i],pvm_error(tids[i]));
140 else
141 fprintf(stderr,"PVM_MD: tids[%d] = %d\n",i,tids[i]);
143 fatal_error(0,"Could not spawn %d processes, numt=%d",nprocs-1,numt);
145 for(i=0; (i<nprocs); i++)
146 fprintf(stderr,"tids[%d]=%x\n",i,tids[i]);
148 for (i=1; (i<nprocs); i++) {
149 fprintf(stderr,"pid %d: sending tids to pid %d = tid %x \n",
150 pid,i,tids[i]);
151 /* Send target id's to everyone */
152 pvmio_tx(tids[i],tids,nprocs*sizeof(tids[0]));
154 /* Send index to other CPUS */
155 pvmio_tx(tids[i],&i,sizeof(i));
158 else {
159 /* I am a slave, get some tids */
160 bufid = pvm_recv(ANY_TID,PVM_BYTE);
161 info = pvm_upkbyte((char *)tids,nprocs*sizeof(tids[0]),DEFAULT_STRIDE);
162 fprintf(stderr,"Got tids...\n");
164 /* Get my own number */
165 pvmio_rx(tids[0],&pid,sizeof(pid));
167 mypid = pid;
168 mynprocs = nprocs;
170 fprintf(stderr,"my pid is: %d\n",pid);
172 return pid;
175 void pvmio_stat(FILE *fp,char *msg)
177 fprintf(fp,"pvmio_stat message: %s\n",msg);
178 fprintf(fp,"Idle Send: %d\n",idle_send);
179 fprintf(fp,"Idle Receive: %d\n",idle_rec);
182 int pvmnodenumber(void)
184 return mypid;
187 int pvmnodecount(void)
189 return mynprocs;
192 int pvm_idle_send(void)
194 return idle_send;
197 int pvm_idle_rec(void)
199 return idle_rec;
202 void pvm_left_right(int nprocs,int pid,int *left,int *right)
204 *left=tids[(pid-1+nprocs) % nprocs];
205 *right=tids[(pid+1) % nprocs];
206 fprintf(stderr,"PVM: left=%d, right=%d\n",*left,*right); fflush(stderr);
209 void pvm_reset_idle()
211 idle_send=0,idle_rec=0;
214 void pvmio_tx_rx(int send_pid,void *send_buf,int send_bufsize,
215 int rec_pid,void *rec_buf,int rec_bufsize)
217 fatal_error(0,"pvmio_tx_rx called");
220 void pvmio_wait(int send,int receive)
222 pvmio_tx_wait(send);
223 pvmio_rx_wait(receive);
226 void pvmio_sync_ring(int pid,int nprocs,int left,int right)
228 fatal_error(0,"pvmio_sync_ring called");
231 void pvm_abort(int pid,int nprocs,int errno)
233 int i;
235 for(i=0; i<nprocs; i++)
236 if (i != pid)
237 pvm_sendsig(tids[i],SIGKILL);
239 exit(errno);
242 #endif
243 /* USE_PVM3 */