int client(const char *hostname, int port, int nmin, int nmax, int nstep, int nrep){ int sock=connect_port(hostname, port, 0, 1); if(sock<0 || stwriteint(sock, nstep) || stwriteint(sock, nmin) || stwriteint(sock, nmax) || stwriteint(sock, nrep)) { warning("Unable to connecto to %s\n", hostname); close(sock); return 1; } buf1=(char*)malloc(nmax*nstep); for(int i=0;i<10;i++){//warm up stwrite(sock, buf1, nmax); stread(sock, buf1, 64); usleep(500); } double tim1, tim2, tim3; int nlen=(nmax-nmin+nstep)/nstep; dmat *timing=dnew(nrep, nlen); dmat *timing2=dnew(nrep, nlen); int ilen=-1; for(int len=nmin; len<=nmax; len+=nstep){ ilen++; info("len=%d\n", len); for(int irep=0; irep<nrep; irep++){ if(irep%800==0){ info("irep=%d of %d\n", irep, nrep); } usleep(500); tim1=myclockd(); stwrite(sock, buf1, len); tim2=myclockd(); stread(sock, buf1, 64); tim3=myclockd(); timing->p[irep+ilen*nrep]=tim3-tim1; timing2->p[irep+ilen*nrep]=tim2-tim1; } } close(sock); writebin(timing, "pix_timing_%s_%d_%d_%d", HOST, nmin, nmax, nstep); writebin(timing2, "pix_timing2_%s_%d_%d_%d", HOST, nmin, nmax, nstep); dbg("done\n"); return 0; }
int server(int sock){ int nstep; int nmin; int nmax; int nrep; if(stread(sock, &nstep, sizeof(int)) || stread(sock, &nmin, sizeof(int)) || stread(sock, &nmax, sizeof(int)) || stread(sock, &nrep, sizeof(int))){ return -1; } info("nstep=%d, nmin=%d, nmax=%d, nrep=%d\n", nstep,nmin, nmax, nrep); buf2=(char*)malloc(nmax*nstep); //warm up for(int i=0; i<10; i++){ if(stread(sock, buf2, nmax) || stwrite(sock, buf2, 64)){ close(sock); return -1; } } for(int len=nmin; len<=nmax; len+=nstep){ for(int irep=0; irep<nrep; irep++){ if(stread(sock, buf2, len) || stwrite(sock, buf2, 64)){ close(sock); return -1; } } } return -1; }
/** Read a string from socket */ int streadstr(int sfd, char **str){ int len; int ans=streadint(sfd, &len); if(!ans && len>0){ *str=calloc(1, sizeof(char)*len); ans=stread(sfd, *str, len); if(ans){ free(*str); *str=NULL; } }else{ *str=NULL; } return ans; }
int main(int argc, char *argv[]){ enum{ P_EXE, P_FRAC, P_NSTEP, P_TOT, }; if(argc!=P_TOT){ info2("Usage: \n\tenv MVM_CLIENT=hostname MVM_PORT=port MVM_SASTEP=sastep ./mvm_cpu fraction nstep\n"); _Exit(0); } int fraction=strtol(argv[P_FRAC], NULL, 10); int nstep=strtol(argv[P_NSTEP], NULL, 10); int nstep0=nstep>1?20:0;//warm up dmat *d_saind=dread("NFIRAOS_saind"); const int nsa=(d_saind->nx-1)/fraction; int *saind=mymalloc((1+nsa),int); for(int i=0; i<nsa+1; i++){ saind[i]=(int)d_saind->p[i]; } dfree(d_saind); const int totpix=saind[nsa]; const int nact=6981;//active subapertures. int ng=nsa*2; float FSMdelta=-0.2; smat *dm=snew(nact,1); smat *mvm=snew(nact, ng); smat *mtch=snew(totpix*2,1); smat *grad=snew(ng,1); smat *im0=snew(totpix,3); short *pix=mymalloc(totpix,short); short *pixbias=mymalloc(totpix,short); { rand_t rseed; seed_rand(&rseed, 1); srandu(mvm, 1e-7, &rseed); srandu(mtch, 1, &rseed); for(int i=0; i<totpix; i++){ pix[i]=(short)(randu(&rseed)*25565); pixbias[i]=(short)(randu(&rseed)*1000); } } smat *mvmt=strans(mvm); int sastep=200;//how many subapertures each time int nrep=1; if(getenv("MVM_NREP")){ nrep=strtol(getenv("MVM_NREP"), NULL, 10); } if(getenv("MVM_SECT")){ sastep=nsa/strtol(getenv("MVM_SECT"), NULL, 10); } if(getenv("MVM_TRANS")){ use_trans=strtol(getenv("MVM_TRANS"), NULL, 10); } if(getenv("MVM_SASTEP")){ sastep=strtol(getenv("MVM_SASTEP"), NULL, 10); } info2("use_trans=%d, nrep=%d, sastep=%d\n", use_trans, nrep, sastep); int sock=-1; char* MVM_CLIENT=getenv("MVM_CLIENT"); if(MVM_CLIENT){ short port=(short)strtol(getenv("MVM_PORT"), NULL, 10); sock=connect_port(MVM_CLIENT, port, 0 ,1); if(sock!=-1) { info2("Connected\n"); int cmd[7]; cmd[0]=nact; cmd[1]=nsa; cmd[2]=sastep; cmd[3]=totpix; cmd[4]=nstep; cmd[5]=nstep0; cmd[6]=2; if(stwriteintarr(sock, cmd, 7) || stwriteintarr(sock, saind, nsa+1) || stwrite(sock, pix, sizeof(short)*totpix)){ close(sock); sock=-1; warning("Failed: %s\n", strerror(errno)); } } } int ready=0; if(sock!=-1 && stwriteint(sock, ready)){ warning("error send ready signal: %s\n", strerror(errno)); close(sock); sock=-1; } smat *timing=snew(nstep, 1); TIC; float timtot=0, timmax=0, timmin=INFINITY; set_realtime(-1, -20); for(int jstep=-nstep0; jstep<nstep; jstep++){ int istep=jstep<0?0:jstep; tic; double theta=M_PI*0.5*istep+FSMdelta; float cd=cos(theta); float sd=cos(theta); szero(dm); for(int isa=0; isa<nsa; isa+=sastep){ int npixleft; int nsaleft; if(nsa<isa+sastep){//terminate npixleft=totpix-saind[isa]; nsaleft=nsa-isa; }else{ npixleft=saind[isa+sastep]-saind[isa]; nsaleft=sastep; } short *pcur=pix+saind[isa]; if(sock!=-1){ if(stread(sock, pcur, sizeof(short)*npixleft)){ warning("failed: %s\n", strerror(errno)); close(sock); sock=-1; _Exit(1); } if(isa==0) tic; } //Matched filter mtch_do(mtch->p, pix, pixbias, grad->p+isa*2, im0->p, im0->p+totpix, im0->p+totpix*2, saind+isa, nsaleft, cd, sd); //MVM for(int irep=0; irep<nrep; irep++){ if(use_trans){ mvmt_do(mvmt->p+isa*2, grad->p+isa*2,dm->p, nact, nsaleft*2, ng); }else{ mvm_do(mvm->p+isa*2*nact, grad->p+isa*2, dm->p, nact, nsaleft*2); } } }//for isa if(sock!=-1){ if(stwrite(sock, dm->p, sizeof(float)*nact)){ warning("error write dmres: %s\n", strerror(errno)); close(sock); sock=-1; _Exit(1); } if(streadint(sock, &ready)){//acknowledgement. warning("error read ack failed: %s\n", strerror(errno)); close(sock), sock=-1; _Exit(1); } timing->p[istep]=ready*1.e-6; }else{ timing->p[istep]=toc3;//do not tic. } if(jstep==istep){ timtot+=timing->p[istep]; if(timmax<timing->p[istep]){ timmax=timing->p[istep]; } if(timmin>timing->p[istep]){ timmin=timing->p[istep]; } } }//for istep float timmean=timtot/nstep; info2("Timing is mean %.3f, max %.3f min %.3f. BW is %.1f of 51.2GB/s\n", timmean*1e3, timmax*1e3, timmin*1e3, nrep*(nact*ng+nact+ng)*sizeof(float)/timmean/(1024*1024*1024)); writebin(timing, "cpu_timing_%s", HOST); if(nstep==1){ writearr("cpu_pix", 1, sizeof(short), M_INT16, NULL, pix, totpix, 1); writearr("cpu_pixbias", 1, sizeof(short), M_INT16, NULL, pixbias, totpix, 1); writebin(dm, "cpu_dm"); writebin(grad, "cpu_grad"); writebin(mvm, "cpu_mvm"); writebin(mtch, "cpu_mtch"); } }
int fsrestore(int argc, char *argv[]) { int i, j, status; int block, bytes; int blk, nblks, size, mark; struct partition *pp; printf("Current SCSI device = ID %d\n", scsi_device); getline("Is it sure ? (y/n) ", cons_buf); if ((cons_buf[0] != 'y') && (cons_buf[0] != 'Y')) return ST_ERROR; st_rewind(rst0); st_skip(rst0); status = stread(rst0, index, LABEL_SIZE); st_skip(rst0); for (i = 0; i < MAXPARTITIONS; i++) { pp = &(lp->d_partitions[i]); if (pp->p_size > 0) { printf("%c: ", i + 'A'); printf("size = %d(0x%s), ", pp->p_size, hexstr(pp->p_size, 8)); printf("offset = %d(0x%s)\n", pp->p_offset, hexstr(pp->p_offset, 8)); blk = pp->p_offset; nblks = pp->p_size; size = nblks << DEV_BSHIFT; block = BUF_BLOCK; bytes = BUF_BYTES; mark = nblks / block; if (nblks % block) mark++; for (j = 0; j < mark; j++) printf("-"); for (j = 0; j < mark; j++) printf("%c", '\x08'); while (nblks > 0) { if (nblks < block) { block = nblks; bytes = nblks << DEV_BSHIFT; } if (stread(rst0, dump_buf, bytes) != bytes) { printf("tape read failed !!!\n"); return ST_ERROR; } if (!scsi_write(blk, dump_buf, bytes)) { printf("disk write failed !!!\n"); return ST_ERROR; } blk += block; nblks -= block; size -= bytes; printf("#"); } st_skip(rst0); printf("\n\n"); } } return ST_NORMAL; }
//respond to scheduler static int respond(int sock){ int cmd[3]; //read fixed length header info. if(streadintarr(sock, cmd, 3)){ return -1;//failed } int ihost=host_from_sock(sock); if(ihost>=0){ htime[ihost]=myclockd(); } int pid=cmd[2]; switch(cmd[0]){ case -1:{//server request shutdown info("disconnect from %s\n", hosts[ihost]); return -1; } break; case MON_VERSION: break; case MON_STATUS: { if(ihost<0){ warning("Host not found\n"); return -1; } PROC_T *p=proc_get(ihost,pid); if(!p){ p=proc_add(ihost,pid); } if(stread(sock, &p->status, sizeof(STATUS_T))){ return -1; } if(p->status.info==S_REMOVE){ proc_remove(ihost, pid); }else{ if(cmd[1]!=ihost && cmd[1]!=cmd[2]){ /*A new mean to replace the ID of a job.*/ p->pid=cmd[1]; } gdk_threads_add_idle((GSourceFunc)refresh, p); } } break; case MON_PATH: { if(ihost<0){ warning("Host not found\n"); return -1; } PROC_T *p=proc_get(ihost,pid); if(!p){ p=proc_add(ihost,pid); } if(streadstr(sock, &p->path)){ return -1; } char *tmp=NULL; while((tmp=strchr(p->path, '\n'))){ tmp[0]=' '; } } break; case MON_LOAD: { if(ihost<0){ warning("Host not found\n"); return -1; } usage_cpu[ihost]=(double)((pid>>16) & 0xFFFF)/100.; usage_mem[ihost]=(double)(pid & 0xFFFF)/100.; usage_cpu[ihost]=MAX(MIN(1,usage_cpu[ihost]),0); usage_mem[ihost]=MAX(MIN(1,usage_mem[ihost]),0); gdk_threads_add_idle((GSourceFunc)update_progress, GINT_TO_POINTER(ihost)); } break; case MON_ADDHOST: if(cmd[1]>-1 && cmd[1]<nhost){ pthread_t tmp; pthread_create(&tmp, NULL, (void*(*)(void*))add_host, GINT_TO_POINTER(cmd[1])); }else if(cmd[1]==-2){ return -2; } break; default: warning_time("Invalid cmd %d\n",cmd[0]); return -1; } return 0; }
//server for mvmfull_real int mvm_server(int sock){ int cmd[7]; if(streadintarr(sock, cmd, 7)){ return -1; } int nact=cmd[0]; int nsa=cmd[1]; int sastep=cmd[2]; int totpix=cmd[3]; int pixpsa=totpix; int nstep=cmd[4]; int nstep0=cmd[5]; int type=cmd[6]; dbg("type=%d, nact=%d, nsa=%d, sastep=%d, %s=%d, nstep=%d\n",type, nact, nsa, sastep, type==1?"pixpsa":"totpix", totpix, nstep); int *saind=NULL; if(type==1){//mvmfull_iwfs totpix=pixpsa*nsa; }else{//mvmfull_real saind=mymalloc((nsa+1),int); if(streadintarr(sock, saind, nsa+1)){ return -1; } } short *pix=mymalloc(totpix,short); if(type==1){ rand_t rseed; seed_rand(&rseed, 1); for(int i=0; i<totpix; i++){ pix[i]=(short)randu(&rseed); } }else{ if(stread(sock, pix, totpix*sizeof(short))){ return -1; } } smat *dmres=snew(nact, 1); int ready; streadint(sock, &ready); //wait for client to be ready. #if __linux__ struct timespec ct; clock_gettime(CLOCK_MONOTONIC, &ct); int readtime_ns=500000;//500 micro-second read out time. int frametime_ns=1250000;//frame time. int nsend=((nsa+sastep-1)/sastep);//number of segments sent along read out int int1_ns=readtime_ns/nsend;//interval between segment sending int int2_ns=frametime_ns-readtime_ns;//interval after last segment. #endif TIC;tic; for(int istep=-nstep0; istep<nstep; istep++){ //info("\rSend trigger "); #if __linux__ //scheduled start time of the frame. double tk0=(double)ct.tv_sec+(double)ct.tv_nsec*1.e-9; #else tic; #endif for(int isa=0; isa<nsa; isa+=sastep){ #if __linux__ if(clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ct, NULL)){ warning("clock_nanosleep is interrupted\n"); } if(isa==0){ tic; } #endif int nleft; if(type==1){ nleft=((nsa-isa)<sastep?(nsa-isa):sastep)*pixpsa; }else{ if(nsa<isa+sastep){//terminate nleft=totpix-saind[isa]; }else{ nleft=saind[isa+sastep]-saind[isa]; } } if(stwrite(sock, pix+(type==1?pixpsa*isa:saind[isa]), 2*nleft)){//2 byte data. warning("failed: %s\n", strerror(errno)); return -1; } #if __linux__ ct.tv_nsec+=int1_ns; while(ct.tv_nsec>=1000000000){ ct.tv_nsec-=1000000000; ct.tv_sec++; } #endif } if(stread(sock, dmres->p, sizeof(float)*nact)){ warning("read dmres failed: %s\n", strerror(errno)); return -1; } ready=(int)(toc3*1e6);//mvm is finished. #if __linux__ if(nstep<100){ dbg("tk=%.6f tic=%.6f, toc=%.6f, ready=%.6f\n", tk0, tk, myclockd(), ready*1e-6); } #endif if(stwriteint(sock, ready)){ warning("write ready failed: %s\n", strerror(errno)); return -1; } //set next frame start time. #if __linux__ ct.tv_nsec+=int2_ns; while(ct.tv_nsec>=1000000000){ ct.tv_nsec-=1000000000; ct.tv_sec++; } #endif if((istep & 0xFF) == 0xFF){ info("%d %d us.\n", istep, ready); } } info("\n"); return -1; }