📄 par.c
字号:
/* Implementation of parallel features */#include <csn/csn.h>#include <csn/names.h>#include <csn/csnuio.h>#include "defs.h"#include "par.h"/* Parameter Handling */#define DEFPOPSIZE 100#define DEFMAXGEN 10000#define DEFMAXERR 0.01#define DEFSEED AUTO#define DEFTRANS 1char *ParOptStr() {return "p:g:e:s:t:\0";}char *ParUsage() {return "Simulation Parameters:\n" "-p <population size>: 100\n" "-g <max. no. of generations>: 10000\n" "-e <max. error for succes>: 0.01\n" "-s <random seed value>: time\n" "-t <frequency of data transfers> 1\n\0";}/* set default values */int PopGlobal =DEFPOPSIZE; /* size of actual population */int MaxGen =DEFMAXGEN; /* Maximum no. of generations */errtyp MaxErr =DEFMAXERR; /* Maximum error for succes */int SeedRand =DEFSEED; /* random seed */int Ntrans =DEFTRANS; /* frequency of data transfers *//* Network variables */Transport Tr;netid_t NetId[MAXPROCS];char Name[MAXPROCS][2];struct iovec Vector[CSN_MAX_IOVCNT]; int handleParOpt(char opt,char* arg){ switch(opt) { case 'p': return (PopGlobal =getint(arg,2,1000000))<0; case 'g': return (MaxGen =getint(arg,1,1000000000))<0; case 'e': return getfloat(&MaxErr,arg,0,1000000); case 's': return (SeedRand =getint(arg,0,MAXRANDOM))<0; case 't': return (Ntrans =getint(arg,1,10000))<0; default: return 1; };}void errorexit(char *msg){ cs_abort(msg,-1);}void send(int proc,void *p,int len){ if(csn_tx(Tr,0,NetId[proc],(char*)p,len)!=len) errorexit("send error\n");}void recv(void *p,int len){ netid_t from; if(csn_rx(Tr,&from,(char*)p,len)!=len) errorexit("receive error\n");}int recvfrom(void *p,int len){ netid_t from; int i; if(csn_rx(Tr,&from,(char*)p,len)!=len) errorexit("receive error\n"); for(i=0;i<Procs;i++) if(i!=ProcId && NetId[i]==from) return i; errorexit("sender not found\n"); return -1;}void setvect(int n,void *p,int len){ if(n>=CSN_MAX_IOVCNT) errorexit("too many vectors\n"); Vector[n].iov_base=(caddr_t)p; Vector[n].iov_len=len;}void sendvect(int proc,int n){ csn_txv(Tr,0,NetId[proc],Vector,n);}void recvvect(int n){ netid_t from; csn_rxv(Tr,&from,Vector,n);}int recvvectfrom(int n){ netid_t from; int i; csn_rxv(Tr,&from,Vector,n); for(i=0;i<Procs;i++) if(i!=ProcId && NetId[i]==from) return i; errorexit("sender not found\n"); return -1;}void initNetwork(){ csn_init(); cs_getinfo(&Procs,&ProcId,&LocalId);}int initPar(){ int i,k,n,h; word *p; ind* pop; if(csn_open(-1,&Tr)!=CSN_OK) errorexit("Can't open transport.\n"); for(i=0;i<Procs;i++) { Name[i][0]='A'+i; Name[i][1]=0; }; if(csn_registername(Tr,Name[ProcId])!=CSN_OK) errorexit("Can't register name\n"); for(i=0;i<Procs;i++) { if(i==ProcId) NetId[i]=CSN_NULL_ID; else if(csn_lookupname(&NetId[i],Name[i],1)!=CSN_OK) errorexit("Can't find process name\n"); }; PopLocal=(PopGlobal-1)/Procs+1; PopGlobal=PopLocal*Procs; if(ProcId==0) { if(SeedRand==AUTO) SeedRand=gettime(); for(i=1;i<Procs;i++) send(i,&SeedRand,WORDLEN); } else { recv(&SeedRand,WORDLEN); SeedRand+=ProcId; }; seedrand(SeedRand); MaxGen=((MaxGen-1)/Ntrans+1)*Ntrans; sprintf(ParParamStr, "Simulation: Procs = %d, PopSize = %d, MaxGen = %d, " "MaxErr = %7.4f\n RandomSeed = %d\n", Procs,PopGlobal,MaxGen,MaxErr,SeedRand); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -