next up previous contents
Next: Module: Simulation Up: Source Code Previous: Module: Sequential   Contents

Subsections

Module: Parallel

File: par.h


  1	
  2	/* Declaration of Parallel Features */
  3	
  4	#ifndef PAR_H
  5	#define PAR_H   1
  6	
  7	#include "defs.h"
  8	
  9	#define MAXPROCS        16
 10	
 11	char *ParOptStr();
 12	char *ParUsage();
 13	char ParParamStr[256];
 14	
 15	int Procs;      /* no. of processes */
 16	int ProcId;     /* process Id */
 17	int LocalId;    /* Local Id */
 18	
 19	int PopGlobal;  /* size of global population */
 20	int PopLocal;   /* size of local population */
 21	
 22	int MaxGen;     /* Maximum no. of generations */
 23	errtyp MaxErr;  /* Maximum error for succes */
 24	
 25	int SeedRand;   /* random seed */
 26	int Ntrans;     /* frequency of data transfer */
 27	
 28	void initNetwork();
 29	int handleParOpt(char opt,char* arg);
 30	int initPar();
 31	
 32	void errorexit(char *msg);
 33	void send(int proc,void *p,int len);
 34	void recv(void *p,int len);
 35	int recvfrom(void *p,int len);
 36	void setvect(int n,void *p,int len);
 37	void sendvect(int proc,int n);
 38	void recvvect(int n);
 39	int recvvectfrom(int n);
 40	
 41	#endif

File: par.c


  1	
  2	/* Implementation of parallel features */
  3	
  4	#include <csn/csn.h>
  5	#include <csn/names.h>
  6	#include <csn/csnuio.h>
  7	
  8	#include "defs.h"
  9	#include "par.h"
 10	
 11	/* Parameter Handling */
 12	
 13	#define DEFPOPSIZE      100
 14	#define DEFMAXGEN       10000
 15	#define DEFMAXERR       0.01
 16	#define DEFSEED         AUTO
 17	#define DEFTRANS        1
 18	
 19	char *ParOptStr() {return "p:g:e:s:t:\0";}
 20	
 21	char *ParUsage() {return
 22	  "Simulation Parameters:\n"
 23	  "-p <population size>:             100\n"
 24	  "-g <max. no. of generations>:     10000\n"
 25	  "-e <max. error for succes>:       0.01\n"
 26	  "-s <random seed value>:           time\n"
 27	  "-t <frequency of data transfers>  1\n\0";}
 28	
 29	/* set default values */
 30	
 31	int PopGlobal   =DEFPOPSIZE;    /* size of actual population */
 32	int MaxGen      =DEFMAXGEN;     /* Maximum no. of generations */
 33	errtyp MaxErr   =DEFMAXERR;     /* Maximum error for succes */
 34	int SeedRand    =DEFSEED;       /* random seed */
 35	int Ntrans      =DEFTRANS;      /* frequency of data transfers */
 36	
 37	/* Network variables */
 38	
 39	Transport Tr;
 40	netid_t NetId[MAXPROCS];
 41	char Name[MAXPROCS][2];
 42	struct iovec Vector[CSN_MAX_IOVCNT];
 43	
 44	int handleParOpt(char opt,char* arg)
 45	{
 46	  switch(opt)
 47	  {
 48	    case 'p': return (PopGlobal =getint(arg,2,1000000))<0;
 49	    case 'g': return (MaxGen    =getint(arg,1,1000000000))<0;
 50	    case 'e': return getfloat(&MaxErr,arg,0,1000000);
 51	    case 's': return (SeedRand  =getint(arg,0,MAXRANDOM))<0;
 52	    case 't': return (Ntrans    =getint(arg,1,10000))<0;
 53	    default: return 1;
 54	  };
 55	}
 56	
 57	void errorexit(char *msg)
 58	{
 59	  cs_abort(msg,-1);
 60	}
 61	
 62	void send(int proc,void *p,int len)
 63	{
 64	  if(csn_tx(Tr,0,NetId[proc],(char*)p,len)!=len)
 65	    errorexit("send error\n");
 66	}
 67	
 68	void recv(void *p,int len)
 69	{
 70	  netid_t from;
 71	
 72	  if(csn_rx(Tr,&from,(char*)p,len)!=len)
 73	    errorexit("receive error\n");
 74	}
 75	
 76	int recvfrom(void *p,int len)
 77	{
 78	  netid_t from;
 79	  int i;
 80	
 81	  if(csn_rx(Tr,&from,(char*)p,len)!=len)
 82	    errorexit("receive error\n");
 83	  for(i=0;i<Procs;i++)
 84	    if(i!=ProcId && NetId[i]==from) return i;
 85	  errorexit("sender not found\n");
 86	  return -1;
 87	}
 88	
 89	void setvect(int n,void *p,int len)
 90	{
 91	  if(n>=CSN_MAX_IOVCNT)
 92	    errorexit("too many vectors\n");
 93	  Vector[n].iov_base=(caddr_t)p;
 94	  Vector[n].iov_len=len;
 95	}
 96	
 97	void sendvect(int proc,int n)
 98	{
 99	  csn_txv(Tr,0,NetId[proc],Vector,n);
100	}
101	
102	void recvvect(int n)
103	{
104	  netid_t from;
105	
106	  csn_rxv(Tr,&from,Vector,n);
107	}
108	
109	int recvvectfrom(int n)
110	{
111	  netid_t from;
112	  int i;
113	
114	  csn_rxv(Tr,&from,Vector,n);
115	  for(i=0;i<Procs;i++)
116	    if(i!=ProcId && NetId[i]==from) return i;
117	  errorexit("sender not found\n");
118	  return -1;
119	}
120	
121	void initNetwork()
122	{
123	  csn_init();
124	  cs_getinfo(&Procs,&ProcId,&LocalId);
125	}
126	
127	int initPar()
128	{
129	  int i,k,n,h;
130	  word *p;
131	  ind* pop;
132	
133	  if(csn_open(-1,&Tr)!=CSN_OK)
134	    errorexit("Can't open transport.\n");
135	  for(i=0;i<Procs;i++)
136	  {
137	    Name[i][0]='A'+i;
138	    Name[i][1]=0;
139	  };
140	  if(csn_registername(Tr,Name[ProcId])!=CSN_OK)
141	    errorexit("Can't register name\n");
142	  for(i=0;i<Procs;i++)
143	  {
144	    if(i==ProcId)
145	      NetId[i]=CSN_NULL_ID;
146	    else
147	      if(csn_lookupname(&NetId[i],Name[i],1)!=CSN_OK)
148	        errorexit("Can't find process name\n");
149	  };
150	  PopLocal=(PopGlobal-1)/Procs+1;
151	  PopGlobal=PopLocal*Procs;
152	  if(ProcId==0)
153	  {
154	    if(SeedRand==AUTO) SeedRand=gettime();
155	    for(i=1;i<Procs;i++)
156	      send(i,&SeedRand,WORDLEN);
157	  } else {
158	    recv(&SeedRand,WORDLEN);
159	    SeedRand+=ProcId;
160	  };
161	  seedrand(SeedRand);
162	
163	  MaxGen=((MaxGen-1)/Ntrans+1)*Ntrans;
164	
165	  sprintf(ParParamStr,
166	    "Simulation: Procs = %d, PopSize = %d, MaxGen = %d, "
167	    "MaxErr = %7.4f\n            RandomSeed = %d\n",
168	    Procs,PopGlobal,MaxGen,MaxErr,SeedRand);
169	  return 0;
170	}



(c) Bernhard Ömer - oemer@tph.tuwien.ac.at - http://tph.tuwien.ac.at/~oemer/