void my_compute(int *pile1, int *pile2, int size, int opt) { int c; int ret; char *buf; int r[3][5]; r[0][4] = opt; my_init1(&c, r[0], size, &buf); while (++c && (r[0][3] = my_sort(pile1, r[0][1], size, -1))) { if (my_init2(pile1, pile2, r[0], r) && r[0][4] == 2) my_debug(pile1, pile2, r[0][1], r[0][2]); if ((ret = check_a(pile1, pile2, &buf, r)) == 0) return ; else if (ret == 1) continue ; if ((ret = check_b(pile1, pile2, &buf, r)) == 0) return ; } if (opt == 2) my_debug(pile1, pile2, r[0][1], r[0][2]); return (my_end(opt, c - 1, buf, **r - 1)); }
int main(int argc, char*argv[]) { // 6 config, each has three files to read char *files[] = { "../resources/config_32N32M_B.txt", "../resources/config_32N32M_A.txt", "../resources/config_32N32M_prior.txt", "../resources/config_64N64M_B.txt", "../resources/config_64N64M_A.txt", "../resources/config_64N64M_prior.txt", "../resources/config_128N128M_B.txt", "../resources/config_128N128M_A.txt", "../resources/config_128N128M_prior.txt", "../resources/config_256N256M_B.txt", "../resources/config_256N256M_A.txt", "../resources/config_256N256M_prior.txt", "../resources/config_512N512M_B.txt", "../resources/config_512N512M_A.txt", "../resources/config_512N512M_prior.txt", "../resources/config_1024N1024M_B.txt", "../resources/config_1024N1024M_A.txt", "../resources/config_1024N1024M_prior.txt", }; // variables int i,j,k; int Len; int debug=0; int job=0; // select job frome commmand line int argi; if (argc == 1) { puts("Please specify an option.\nUsage: \"./ocl_fo -job number(0-5) \"\n"); exit(1); } for (argi = 1; argi < argc; ++argi) { if (!strcmp(argv[argi], "-job")) { need_argument(argc, argv,argi); job = atoi(argv[++argi]) ; continue; } if (argv[argi][0] == '-') { fatal("'%s' is not a valid command-line option.\n",argv[argi]); } } //printf("job = %d\n", job); if( job > 5) { printf("Job number exceeds the limit 5! Exit Programm!\n"); exit(1); } HMM *word; word = (HMM*)malloc(sizeof(HMM)); Len = getLineNum(files[job*3+2]); printf("config_%dN_%dM\n",Len, Len); //read B,A,prior printf("Read the following files..."); //read_config(files,job,B,A,prior,Len); read_config(word,files,job,Len,Len); printf("Done!\n"); if( debug && job == 0 ) { puts("a"); check_a(word); puts("b"); check_b(word); puts("pri"); check_pri(word); } //---------------------- // run forward algorithm //---------------------- //--------------------------- // GPU Version //--------------------------- run_opencl_fo(word); //--------------------------- // CPU Version //--------------------------- puts("\n=>CPU"); struct timeval cpu_timer; int N = word->nstates; int T = word->len; float *B = word->b; float *A = word->a; float *prior = word->pri; double tmp, alpha_sum; double log_likelihood; float *alpha; // NxT alpha = (float*)malloc(sizeof(float)*N*T); float *A_t; // NxN A_t = (float*)malloc(sizeof(float)*N*N); log_likelihood = 0.0; // start timing tic(&cpu_timer); transpose(A, A_t, N, T); for(j=0;j<T;++j) { alpha_sum = 0.0; if(j==0){ // initialize for(i=0;i<N;++i){ alpha[i*T + 0] = B[i*T + 0] * prior[i]; alpha_sum += alpha[i*T + 0]; } }else{ // move forward for(i=0;i<N;++i) { // go through each state tmp = 0.0; for(k=0;k<N;++k){ tmp += A_t[i*N + k] * alpha[k*T + j-1]; } alpha[i*T + j] = (float)tmp * B[i*T + j]; alpha_sum += alpha[i*T + j]; } } // scaling for(i=0;i<N;++i){ alpha[i*T + j] /= alpha_sum; } log_likelihood += log(alpha_sum); } // end timing toc(&cpu_timer); printf("log_likelihood = %lf\n", log_likelihood); // free memory free_hmm(word); free(A_t); free(alpha); return 0; }
int main(void) { bool b_done=false; bool done=false; srand(time(NULL)); initXWindows(); init_opengl(); //declare game object Game game; game.n=0; //declare a box shape game.box[0].width = BOX_WIDTH; game.box[0].height = BOX_HEIGHT; game.box[0].center.x = WINDOW_WIDTH/7; game.box[0].center.y = WINDOW_HEIGHT - WINDOW_HEIGHT/7; game.box[1].width = BOX_WIDTH; game.box[1].height = BOX_HEIGHT; game.box[1].center.x = 2*WINDOW_WIDTH/7; game.box[1].center.y = WINDOW_HEIGHT - 2*(WINDOW_HEIGHT/7); game.box[2].width = BOX_WIDTH; game.box[2].height = BOX_HEIGHT; game.box[2].center.x = 3*WINDOW_WIDTH/7; game.box[2].center.y = WINDOW_HEIGHT - 3*(WINDOW_HEIGHT/7); game.box[3].width = BOX_WIDTH; game.box[3].height = BOX_HEIGHT; game.box[3].center.x = 4*WINDOW_WIDTH/7; game.box[3].center.y = WINDOW_HEIGHT - 4*(WINDOW_HEIGHT/7); game.box[4].width = BOX_WIDTH; game.box[4].height = BOX_HEIGHT; game.box[4].center.x = 5*WINDOW_WIDTH/7; game.box[4].center.y = WINDOW_HEIGHT - 5*(WINDOW_HEIGHT/7); game.circle.radius = CIRCLE_RADIUS; game.circle.center.x = WINDOW_WIDTH; game.circle.center.y = -80; while(XPending(dpy)) { XEvent e; XNextEvent(dpy, &e); b_done = check_b(&e); if (b_done){ break; } } //start animation while(!done) { while(XPending(dpy)) { XEvent e; XNextEvent(dpy, &e); done = check_escape(&e); } doMakeParticles(&game); movement(&game); render(&game); glXSwapBuffers(dpy, win); } cleanupXWindows(); return 0; }
int compute1(long double &a,string s,int angle=0)//算术综合运算 { if(!check_b(s)) return 0; Stack<long double> ss(20); Stack<char> sf(20); int i=0,j; string s1,s2; char c; long double b; while(IsUselessChar(s[i])) i++; if(s[i]=='-') { ss.PushStack(-1); sf.PushStack('*'); i++; } else if(s[i]=='+') i++; while(s[i]) { s2="\0"; if(IsUselessChar(s[i])) i++; else if(IsBracket(s[i])==1) { if(i>0&&!IsOperator1(s[i-1])) if(!sf.PushStack('*')) return 0; s2=GetBracketData(s,i); if(!compute1(a,s2,angle)||!ss.PushStack(a)) return 0; } else if(IsOperator1(s[i])) { if(s[i]=='!') { if(!ss.PopStack(b)||!factorial(a,(int)b)||!ss.PushStack(a)) return 0; i++; continue; } while(sf.GetTopData(c)) { if(PRI(s[i])>PRI(c)) break; if(!ss.PopStack(b)||!ss.PopStack(a)||!sf.PopStack(c)||!Account1(a,b,c)||!ss.PushStack(a)) return 0; } if(!sf.PushStack(s[i++])) return 0; } else if(IsData(s[i])==1) { while(IsData(s[i])==1) s2+=s[i++]; if(!ss.PushStack(string_double(s2))) return 0; } else if(s[i]=='S') { if(i>0&&!IsOperator1(s[i-1])) if(!sf.PushStack('*')) return 0; s2="\0"; for(j=0; j<4&&s[i]; j++,i++) s2+=s[i]; if(s2!="Sqrt"||IsBracket(s[i])!=1) return 0; s2=GetBracketData(s,i); s1="\0"; for(j=0; s2[j]; j++) { if(IsBracket(s2[j])==1) s1+="("+GetBracketData(s2,j)+")"; if(s2[j]==',') { j++; break; } else s1+=s2[j]; } if(!compute1(a,s1,angle)) return 0; s1="\0"; for(; s2[j]; j++) s1+=s2[j]; if(!compute1(b,s1,angle)||!Sqrt(a,b)||!ss.PushStack(a)) return 0; } else if(s[i]>='a'&&s[i]<='z'||s[i]=='L') { s1="\0"; if(i>0&&!IsOperator1(s[i-1])) if(!sf.PushStack('*')) return 0; while(s[i]>='a'&&s[i]<='z'||s[i]=='L'||s[i]=='^') { s1+=s[i++]; if(s[i-1]=='e'&&s[i]!='^') s1+='^'; } if(s[i-1]=='e') s2="1"; else if(IsBracket(s[i])==1) s2=GetBracketData(s,i); else if(IsData(s[i])==1) { while(s[i]) { if(IsData(s[i])==1) s2+=s[i++]; else break; } } else if(s[i]=='P'||s[i+1]=='i') s2="Pi"; else if(s[i]=='S') { for(j=0; j<4&&s[i]; j++,i++) s2+=s[i]; if(s2!="Sqrt"||IsBracket(s[i])!=1) return 0; s2+="("+GetBracketData(s,i)+")"; } else return 0; if(!compute1(a,s2,angle)||!compute3(b,s1,a,angle)||!ss.PushStack(b)) return 0; } else if(s[i]=='P') { if(i>0&&!IsOperator1(s[i-1])&&!sf.PushStack('*')||!ss.PushStack(P)) return 0; i+=2; } else return 0; } while(sf.PopStack(c)) if(!ss.PopStack(b)||!ss.PopStack(a)||!Account1(a,b,c)||!ss.PushStack(a)) return 0; if(!ss.PopStack(a)||ss.State()>=0) return 0; return 1; }