/* * Initialize cells based on input file, otherwise all cells * are DEAD. */ void init_grids (struct life_t * life) { FILE * fd; int i,j; if (life->infile != NULL) { if ((fd = fopen(life->infile, "r")) == NULL) { perror("Failed to open file for input"); exit(EXIT_FAILURE); } if (fscanf(fd, "%d %d\n", &life->nrows, &life->tcols) == EOF) { printf("File must at least define grid dimensions!\nExiting.\n"); exit(EXIT_FAILURE); } } // resize so each process is in charge of a vertical slice of the whole board life->ubound = (((life->rank + 1) * life->tcols / life->size) - 1); // we want 1 col of (overlap?) life->lbound = life->rank * life->tcols / life->size; life->ncols = (life->ubound - life->lbound) + 1; printf("[Process %d] lower bound is %d upper bound is %d width is %d random seed is %d.\n", life->rank, life->lbound, life->ubound, life->ncols, life->randseed); allocate_grids(life); for (i = 0; i < life->nrows+2; i++) { for (j = 0; j < life->ncols+2; j++) { life->grid[i][j] = DEAD; life->next_grid[i][j] = DEAD; } } if (life->infile != NULL) { while (fscanf(fd, "%d %d\n", &i, &j) != EOF) { if (j <= life->ubound && j >= life->lbound){ fprintf(stderr, "[Process %d] %d %d -> %d %d.\n", life->rank, i, j, i, j - life->lbound); life->grid[i+1][j - life->lbound + 1] = ALIVE; life->next_grid[i+1][j- life->lbound + 1] = ALIVE; } } fclose(fd); } else { randomize_grid(life, INIT_PROB); } if (life->print){ printf("[Host %d] printing initial slice.\n", life->rank); print_grid (life); } }
/* Pointless Idea, it'd be cool to run a '###' across the grid, in such a way that it moved one block at a time, or something. */ int main(int argc, char **argv){ srandom(time(NULL)); //add options later ncurses_init(); atexit((void(*)(void))endwin); sigaction(SIGTERM, &interupt_act, NULL); world *w = init_world(term_rows, term_cols); randomize_grid(w); draw_world(w, stdscr); while(1){ wmove(stdscr, 0, 0); setup_initial_conditons(w, stdscr); run_life(w, stdscr); } }
int main() { int i = 0; struct tms before, after; int start = (950 / sizeof(int)) * 1000; int end = (1050 / sizeof(int)) * 1000; int step = (end - start) / 50; FILE *fp; fp = fopen("datas", "w"); for (i = start; i < end; i += step) { int a, b, k, j, runtime, overall; int** testgrid; int** neighbor; a = b = (int) sqrt(i); testgrid = make_grid(a, b); neighbor = make_grid(a, b); for(k = 0; k < 10; k++) { randomize_grid(testgrid, &a, &b); zero_grid(neighbor, a, b); times(&before); for(j = 0; j < 30; j++) { next(testgrid, neighbor, a, b); } times(&after); runtime = (int) (after.tms_utime + after.tms_stime) - (before.tms_utime + before.tms_stime); overall += runtime; } free_grid(testgrid, a); free_grid(neighbor, a); fprintf(fp, "%d \t %d \n", i, overall); fflush(fp); } return 0; }
// Randomize the current grid in a perfectly parallel fashion. void gol_randomize(void) { randomize_grid(grids[cur_grid]); }
/*----------------------------------------------------------- * Programme principal *----------------------------------------------------------*/ int main (int argc, char **argv) { struct ctx_s ctx; int i; int *recopy; double timer_start, timer_end; double runtime; char output[256]; /* Initialisation MPI */ initalize_mpi(argc, argv, &ctx); /* Initialisation de la taille du domain (grille carree) */ ctx.X = GRIDSZ / ctx.size; ctx.Y = GRIDSZ; ctx.grid = init_grid(&ctx); randomize_grid(&ctx, ctx.grid); ctx.grid_next = init_grid(&ctx); fprintf(stderr, "Starting task %d (%dx%d)\n", ctx.rank, ctx.X, ctx.Y); /* Initialisation affichage */ sprintf(output, "output/Rank%d", ctx.rank); display_init(&ctx, output, GRIDSZ,GRIDSZ,0 ); // taille 0 pour la 3ieme dimension signifie une grille 2D /* Mettre la case au milieu du sous-domaine de la tache 0 en feu ! */ if (ctx.rank == 0) { ctx.grid[(ctx.X+2)/2][(ctx.Y+2)/2] = burning; } recopy = recopy_grid(&ctx, ctx.grid); gather_grid(&ctx, recopy); timer_start = get_timer(); for (i=0; i < MAX_STEPS; ++i) { int **swap; /* Nombre de cellules mises à jour depuis le pas * de temps precedent */ int nb_updated_cells; /* Si la simulation est terminee */ int leave; exchange_ghost_cells(&ctx); nb_updated_cells = update_cells(&ctx); leave = check_final_conditions(&ctx, nb_updated_cells); if (leave) break; swap = ctx.grid; ctx.grid = ctx.grid_next; ctx.grid_next = swap; recopy = recopy_grid(&ctx, ctx.grid); gather_grid(&ctx, recopy); if (ctx.rank == 0 && i%10 == 0) { fprintf(stderr, "..[%d]", i); } } timer_end = get_timer(); if (ctx.rank == 0) { fprintf(stderr, "\n"); } display_finalize(&ctx); runtime = diff_time(timer_end, timer_start); print_time (&ctx, runtime); /* Fin de la simulation */ finalize_mpi(&ctx); exit(0); }
int main( int argc, char* args[] ) { //get a random seed. srand(time(NULL)); //mouse variables and cell types int x, y, sleepTime = 0, countVar = 0; //mouse is held variables int mouseStatusLeft = 0, mouseStatusRight = 0; //make sure the program waits for a quit int quit = false; //Initialize if( init() == false ) return 1; //Load the files if( load_files() == false ) return 2; //Update the screen //if( SDL_Flip( screen ) == -1 ) return 3; //if(SDL_RenderPresent( screen ) == -1 ) return 3; //initialize the cell stuff. This gets the cell system up and running. This also sets all cells to m_air and all the saturation to m_no_saturaion init_cell_stuff(); /* CELL_SIZE = 4; int i;// //putting test materials into grid for(i=0; i<GRID_WIDTH; i++){ if(get_rand(1,4)==1) grid[i+camera_x][GRID_HEIGHT-1-get_rand(7,15)+camera_y].mat = m_plant_root; if(get_rand(1,10)==10) grid[i+camera_x][camera_y+get_rand(20,35)].mat = m_spring; grid[i+camera_x][camera_y+get_rand(30,34)+30].mat = m_earth; grid[i+camera_x][camera_y+get_rand(30,34)+30].mat = m_earth; grid[i+camera_x][camera_y+get_rand(30,34)+30].mat = m_earth; } */ CELL_SIZE = 8; sleepTime = 0; //int i;// //putting test materials into grid /* for(i=7 ; i<GRID_WIDTH ; i+=15){ for(j=26 ; j<GRID_HEIGHT ; j+=20){ grid[i][j].mat = m_anti_scurge; } } */ //-------------------------------------- //for(i=0 ; i<GRID_WIDTH*GRID_HEIGHT / 2 ; i++) // grid[get_rand(0,GRID_WIDTH-1)][get_rand(0,GRID_HEIGHT-1)].mat = m_plant; //int keyw=0, keya=0, keys=0, keyd=0; //unsigned lastPanTime = 0; bool alt = 0; // this keeps track of the state of the alt key //these variables store the position of the user's cursor at the moment that the user decided to pan the screen int mouse_x_when_pan=0; int mouse_y_when_pan=0; // these are for keeping track of the motion of the mouse when the user is panning. these keep the panning from being really jumpy. // without them, there is a truncation error when adjusting the camera_x and camera_y materials and re-setting the mouse coordinates to its original state. // these keep track of the truncation error and add it to the next operation so that the mouse motion feels fluid. int remainder_panning_motion_x=0; int remainder_panning_motion_y=0; //last minute verification that the initial start up values for the grid size and the camera positions are valid. verify_grid_and_cell_size(); verify_camera(); //While the user hasn't quit while(1){ //While there's an event to handle while( SDL_PollEvent( &event ) ){ //If the user has Xed out the window if( event.type == SDL_QUIT || quit == true ){ //Quit the program clean_up(); return 0; } if( event.type == SDL_MOUSEBUTTONDOWN ){ /// mouse down x = event.motion.x; y = event.motion.y; if( event.button.button == SDL_BUTTON_LEFT ){ mouseStatusLeft = 1; } else if( event.button.button == SDL_BUTTON_RIGHT ){ mouseStatusRight = 1; } } else if(event.type == SDL_MOUSEWHEEL){ if(event.wheel.y > 0) zoom_in(x,y); if(event.wheel.y < 0) zoom_out(x,y); } else if(event.type == SDL_MOUSEBUTTONUP){ /// mouse up x = event.motion.x; y = event.motion.y; if( event.button.button == SDL_BUTTON_LEFT ){ mouseStatusLeft = 0; } else if( event.button.button == SDL_BUTTON_RIGHT ){ mouseStatusRight = 0; } } else if( event.type == SDL_MOUSEMOTION ){ /// mouse motion x = event.motion.x; y = event.motion.y; // if the alt key (camera panning key) is down and the coordinates have changed, then let the screen be panned! if(alt && x != mouse_x_when_pan && y != mouse_y_when_pan){ // this adjusts the x-axis camera (this scales with the CELL_SIZE) camera_x += (x-mouse_x_when_pan+remainder_panning_motion_x)/CELL_SIZE; camera_y += (y-mouse_y_when_pan+remainder_panning_motion_y)/CELL_SIZE; //calculate the remainders of the mouse motion. // these values represent the motion of the mouse that is not utilized by the discrete nature of the operation on the camera_x and camera_y values. remainder_panning_motion_x = (x-mouse_x_when_pan+remainder_panning_motion_x) - (int)((x-mouse_x_when_pan+remainder_panning_motion_x)/CELL_SIZE)*CELL_SIZE; remainder_panning_motion_y = (y-mouse_y_when_pan+remainder_panning_motion_y) - (int)((y-mouse_y_when_pan+remainder_panning_motion_y)/CELL_SIZE)*CELL_SIZE; // make sure the camera is not out of bounds. verify_camera(); //reset the user's curcor position to the original position the curcor was in when the user started panning the camera SDL_WarpMouseInWindow(window, mouse_x_when_pan, mouse_y_when_pan); } } else if(event.type == SDL_WINDOWEVENT_RESIZED ){ /// window resize //float new_cell_size = CELL_SIZE * event.resize.h/((float)SCREEN_HEIGHT); // adjust the pixel size. float new_cell_size = CELL_SIZE * event.window.data1/((float)SCREEN_HEIGHT); // adjust the pixel size. if(new_cell_size - ((int)new_cell_size) >= 0.5f) CELL_SIZE = new_cell_size + 1; else CELL_SIZE = new_cell_size; //SCREEN_WIDTH = event.resize.w; //SCREEN_HEIGHT = event.resize.h; SCREEN_WIDTH = event.window.data1; SCREEN_HEIGHT = event.window.data2; verify_grid_and_cell_size(); // make sure the window isn't too big for the cell size //set_window_size(event.resize.w, event.resize.h); set_window_size(event.window.data1, event.window.data2); verify_camera(); } if( event.type == SDL_KEYDOWN ){ ///keyboard event switch( event.key.keysym.sym ){ case SDLK_UP: break; //change block type up case SDLK_DOWN: break; // change block type down case SDLK_c: reset_cells(); break;//clear the screen case SDLK_p: print_saturation_data(); break; // prints the cellSat[][] array to stdout. this is for debuggin purposes. case SDLK_r: randomize_grid(); break; // randomize grid case SDLK_LEFT: if(paused != 1) {sleepTime /= 2;} break; //speeds up the game case SDLK_RIGHT: if(paused != 1) {if(sleepTime == 0){sleepTime = 1;} {sleepTime *= 2;} if(sleepTime > 2000) {sleepTime = 2000;}} break; //slows down the game case SDLK_SPACE: if(paused == 0) {paused = 1;} else if(paused == 1) {paused = 0;} break; //pause the game case SDLK_ESCAPE: quit = true; // quit with escape case SDLK_w: gen_world(w_normal,0); break; // generate a world /* case SDLK_w: keyw=1; break; // store key state case SDLK_a: keya=1; break; case SDLK_s: keys=1; break; case SDLK_d: keyd=1; break; */ case SDLK_LALT: case SDLK_RALT: // store the state of the alt key. alt = 1; // turn off the cursor SDL_ShowCursor(SDL_DISABLE); // store the position of the mouse. this will allow the program to make the cursor stay stationary mouse_x_when_pan = x; mouse_y_when_pan = y; //reset the remainder mouse motion variables for x and y. remainder_panning_motion_x = 0; remainder_panning_motion_y = 0; break; default: break; } } if( event.type == SDL_KEYUP ){ ///keyboard event switch( event.key.keysym.sym ){ /* case SDLK_w: keyw=0; break;//lastPanTime=0; break; // store key state case SDLK_a: keya=0; break;//lastPanTime=0; break; case SDLK_s: keys=0; break;//lastPanTime=0; break; case SDLK_d: keyd=0; break;//lastPanTime=0; break; */ case SDLK_LALT: case SDLK_RALT: // show the cursor again. SDL_ShowCursor(SDL_ENABLE); alt = 0; break; default: break; } } } // end while(event) //no more events to handle at the moment. /* //this handles time-controlled panning if(SDL_GetTicks() - MIN_PAN_INTERVAL > lastPanTime){ if(keyw) pan(D_UP); if(keya) pan(D_LEFT); if(keys) pan(D_DOWN); if(keyd) pan(D_RIGHT); lastPanTime = SDL_GetTicks(); //#if (DEBUG_GRIDSIM) // printf("\nlastPanTime = %d\n", lastPanTime); //#endif } */ //checks if the mouse is held or not if(mouseStatusLeft == 1 && mouseModifier == 0){ //make sure the mouse isn't inside either of the two GUIs. if(y >= 50 && x < SCREEN_WIDTH - 200) setcell(x, y, currentMat); } else if(mouseStatusRight == 1 && mouseModifier == 0){ deletecell(x, y, currentMat); } //speed of gameplay countVar++; if(sleepTime <= 0) { sleepTime = 0; } //evealuate cells if(countVar >= sleepTime && paused != 1){ evaluate_grid(); countVar = 0; } //updates screen with cells print_cells(); //displays selection gui selectionGUI(x, y, mouseStatusLeft); //displays brushes and misc gui brushesGUI(x, y, mouseStatusLeft); // If the user is not panning, then it is fine to show the cursor. if(!alt){ //displays cursor cursorDisplay(x, y); } //draw_line(screen, 300, 300, x, y, 1, mats[currentMat].color); // test the line drawing function //updates the screen //SDL_Flip( screen ); //SDL_RenderPresent( screen ); SDL_UpdateWindowSurface(window); }// end while(quit == false) //Free the surface and quit SDL clean_up(); return 0; }
//interactively read initial conditons from the user void setup_initial_conditons(world *w, WINDOW *win){ keypad(win, 1); int c,y,x,ymax,xmax; getyx(win, y ,x); getmaxyx(win, ymax, xmax); wmove(win, 0, 0); while(c = wgetch(win)){ switch(c){ case KEY_UP: // case KEY_SUP: y = y > 0 ? y-1 : ymax; break; case KEY_DOWN: // case KEY_SDOWN: y = y < ymax ? y+1 : 0; break; case KEY_LEFT: // case KEY_SLEFT: x = x > 0 ? x-1 : xmax; break; case KEY_RIGHT: // case KEY_SRIGHT: x = x < xmax ? x+1 : 0; break; case KEY_BACKSPACE: reset_grid(w); draw_world(w, win); x = y = 0; break; case '+': wait_time += 0.05; break; case '-': wait_time -= 0.05; break; case 'q': exit(0); case 'r': randomize_grid(w); draw_world(w, win); x = y = 0; break; case 's': step_world(w); draw_world(w, win); break; case '\r': case '\n': waddch(win, '#'); w->grid[y*w->cols + x] = 1; break; case ' ': return; } wmove(win, y ,x); // if(c == KEY_SUP || c == KEY_SDOWN | c == KEY_SLEFT || c = KEY_SRIGHT){ // waddch(win, '#'); // wmove(win, y ,x); // } } }