📄 mainpar.c
字号:
/* Main file parallel */#include "defs.h"#include "par.h"#include "ind.h"#include "gen.h"#include "back.h"#include "genback.h"#define DEFLOG AUTO#define DEFFIRST 0#define DEFINFO 1#define DEFSTAT 1extern char *optarg;extern int optind;char *OutOptStr() {return "l:f:i:r:\0";}char *OutUsage() {return "Output Parameters:\n" "-l <frequency of log-output>: auto\n" "-f <show first indiv. (0/1)>: 0\n" "-i <show parameter info>: 1\n" "-r <show statistic (0/1/2)>: 1\n\0";}/* set default values */int OptLog =DEFLOG;int OptFirst =DEFFIRST;int OptInfo =DEFINFO;int OptStat =DEFSTAT;int Gen; /* current generation */int StartTime;int EndTime;#define SimTime() (gettime()-StartTime)int handleOutOpt(char opt,char* arg){ switch(opt) { case 'l': OptLog =getint(arg,0,1000000000); return 0; case 'f': OptFirst =getint(arg,0,2); return 0; case 'i': OptInfo =getint(arg,0,1); return 0; case 'r': OptStat =getint(arg,0,2); return 0; default: return 1; };}int initOut(){ StartTime=gettime(); if(OptLog==AUTO) { if(MaxGen<=10) OptLog=1; else if(Ntrain) OptLog=rounddec(500000/((1+Nback)* ((1+Nin+Nout)*(1+Nhid)*NoTrain*PopLocal))); else OptLog=rounddec(500000/((1+Nback)* ((1+Nin+Nout)*(1+Nhid)*Nin*PopLocal))); }; if(OptLog%Ntrans) OptLog=(OptLog/Ntrans+1)*Ntrans; return 0;}void printinfo(){ printf("Simulation Parameters:\n%s%s%s%s\n", IndParamStr,ParParamStr,GenParamStr,BackParamStr);}void printlog(){ printf("Gen%7d: t=%7ld, MinErr=%8.4f, AvgErr=%8.4f\n", Gen,SimTime(),ErrMin,ErrAvg,ptrain); if(OptFirst==2) printind(Pop[TopInd]); }void printstat(){ float t,g,i,l,b,p; t=SimTime(); if(t==0.0) t=1.0; g=Gen; i=g*PopGlobal; l=g*PopLocal; b=i*Nback; p= Nback ? GenCalcs+BackCalcs : GenCalcs; printf("\n" "Statistic total per sec. time\n" "----------------------------------------------------------\n" "Generations: %10.0f%14.6f%12.6f s\n" "Individuals global:%10.0f%14.6f%12.6f s\n" "Individuals local: %10.0f%14.6f%12.6f s\n", g,g/t,t/g,i,i/t,t/i,l,l/t,t/l); if(Nback) printf( "Backprop. steps: %10.0f%14.6f%12.6f s\n",b,b/t,t/b); printf( "evaluated Patterns:%10.0f%14.6f%12.6f ms\n",p,p/t,1000*t/p);}int main(int argc,char **argv){ int opt,i,j,k,n; char optstr[128]="\0"; errtyp locErrMin,locErrAvg; int locTopInd; strcat(optstr,ParOptStr()); strcat(optstr,IndOptStr()); strcat(optstr,GenOptStr()); strcat(optstr,BackOptStr()); strcat(optstr,OutOptStr()); initNetwork(); while((opt=getopt(argc,argv,optstr)) != EOF) { if( handleParOpt(opt,optarg) && handleIndOpt(opt,optarg) && handleGenOpt(opt,optarg) && handleBackOpt(opt,optarg) && handleOutOpt(opt,optarg) ) { if(ProcId==0) printf("%s\n%s\n%s\n%s\n%s", ParUsage(),IndUsage(),GenUsage(), BackUsage(),OutUsage()); exit(1); }; }; if(initPar()) errorexit("parallel init failed\n"); if(initInd()) errorexit("individual init failed\n"); if(initGen(PopLocal,ProcId==0 ? PopGlobal : PopLocal)) errorexit("genetic init failed\n"); if(Nback && initBack()) errorexit("backpropagation init failed\n"); if(initOut()) errorexit("output init failed\n"); if(Nback) randomNetPop(PopLocal,InitWeight); if(OptInfo && ProcId==0) printinfo(); setvect(0,Pop[0],PopLocal*CrWords*WORDLEN); setvect(1,&Err[0],PopLocal*ERRLEN); setvect(2,&ErrMin,FLOATLEN); setvect(3,&ErrAvg,FLOATLEN); setvect(4,&TopInd,WORDLEN); for(Gen=1;Gen<=MaxGen;Gen++) { calcerrors(PopLocal); if(Gen%Ntrans) { selection(PopLocal); continue; }; if(ProcId==0) /* Master */ { for(i=1;i<Procs;i++) { k=PopLocal*i; setvect(0,Pop[k],PopLocal*CrWords*WORDLEN); setvect(1,&Err[k],PopLocal*ERRLEN); setvect(2,&locErrMin,FLOATLEN); setvect(3,&locErrAvg,FLOATLEN); setvect(4,&locTopInd,WORDLEN); n=recvvectfrom(5); if(ErrMin>locErrMin) { ErrMin=locErrMin; TopInd=locTopInd+k; }; ErrAvg+=locErrAvg; }; ErrAvg/=Procs; if(ErrMin<=MaxErr) { for(i=1;i<Procs;i++) send(i,&ErrMin,FLOATLEN); printlog(); if(OptStat) printstat(); if(OptFirst==1) {printf("\n"); printind(Pop[TopInd]);}; printf("\nprogram succeeded.\n"); exit(0); } if(OptLog && (Gen==1 || Gen%OptLog==0)) printlog(); selection(PopGlobal); for(i=1;i<Procs;i++) { k=PopLocal*i; send(i,&ErrMin,FLOATLEN); send(i,Pop[k],PopLocal*CrWords*WORDLEN); }; } else /* Slaves */ { sendvect(0,5); recv(&ErrMin,FLOATLEN); if(ErrMin<=MaxErr) exit(0); recv(Pop[0],PopLocal*CrWords*WORDLEN); }; if((Ntrain>NoTrain) && (ErrMin*NoTrain<=MaxErr*Ntrain)) NoTrain=min(Ntrain,NoTrain*2); OffsTrain++; if(OffsTrain>=Ntrain) OffsTrain=0; }; if(ProcId==0) { if(OptStat==2) printstat(); printf("\ntime out - program failed.\n"); }; exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -