/* ** main program: ** initialise, timestep loop, finalise */ int main(int argc, char* argv[]) { char* paramfile; /* name of the input parameter file */ char* obstaclefile; /* name of a the input obstacle file */ t_param params; /* struct to hold parameter values */ t_speed* cells = NULL; /* grid containing fluid densities */ t_speed* tmp_cells = NULL; /* scratch space */ int* obstacles = NULL; /* grid indicating which cells are blocked */ float* av_vels = NULL; /* a record of the av. velocity computed for each timestep */ int ii; /* generic counter */ struct timeval timstr; /* structure to hold elapsed time */ struct rusage ru; /* structure to hold CPU time--system and user */ double tic,toc; /* floating point numbers to calculate elapsed wallclock time */ double usrtim; /* floating point number to record elapsed user CPU time */ double systim; /* floating point number to record elapsed system CPU time */ /* parse the command line */ if(argc != 3) { usage(argv[0]); } else{ paramfile = argv[1]; obstaclefile = argv[2]; } /* initialise our data structures and load values from file */ initialise(paramfile, obstaclefile, ¶ms, &cells, &tmp_cells, &obstacles, &av_vels); /* iterate for maxIters timesteps */ gettimeofday(&timstr,NULL); tic=timstr.tv_sec+(timstr.tv_usec/1000000.0); for (ii=0;ii<params.maxIters;ii++) { timestep(params,cells,tmp_cells,obstacles); av_vels[ii] = av_velocity(params,cells,obstacles); #ifdef DEBUG printf("==timestep: %d==\n",ii); printf("av velocity: %.12E\n", av_vels[ii]); printf("tot density: %.12E\n",total_density(params,cells)); #endif } gettimeofday(&timstr,NULL); toc=timstr.tv_sec+(timstr.tv_usec/1000000.0); getrusage(RUSAGE_SELF, &ru); timstr=ru.ru_utime; usrtim=timstr.tv_sec+(timstr.tv_usec/1000000.0); timstr=ru.ru_stime; systim=timstr.tv_sec+(timstr.tv_usec/1000000.0); /* write final values and free memory */ printf("==done==\n"); printf("Reynolds number:\t\t%.12E\n",calc_reynolds(params,cells,obstacles)); printf("Elapsed time:\t\t\t%.6lf (s)\n", toc-tic); printf("Elapsed user CPU time:\t\t%.6lf (s)\n", usrtim); printf("Elapsed system CPU time:\t%.6lf (s)\n", systim); write_values(params,cells,obstacles,av_vels); finalise(¶ms, &cells, &tmp_cells, &obstacles, &av_vels); return EXIT_SUCCESS; }
/* ** main program: ** initialise, timestep loop, finalise */ int main(int argc, char* argv[]) { char * final_state_file = NULL; char * av_vels_file = NULL; char * param_file = NULL; accel_area_t accel_area; param_t params; /* struct to hold parameter values */ speed_t* cells = NULL; /* grid containing fluid densities */ speed_t* tmp_cells = NULL; /* scratch space */ int* obstacles = NULL; /* grid indicating which cells are blocked */ double* av_vels = NULL; /* a record of the av. velocity computed for each timestep */ int ii; /* generic counter */ struct timeval timstr; /* structure to hold elapsed time */ struct rusage ru; /* structure to hold CPU time--system and user */ double tic,toc; /* floating point numbers to calculate elapsed wallclock time */ double usrtim; /* floating point number to record elapsed user CPU time */ double systim; /* floating point number to record elapsed system CPU time */ parse_args(argc, argv, &final_state_file, &av_vels_file, ¶m_file); initialise(param_file, &accel_area, ¶ms, &cells, &tmp_cells, &obstacles, &av_vels); /* iterate for max_iters timesteps */ gettimeofday(&timstr,NULL); tic=timstr.tv_sec+(timstr.tv_usec/1000000.0); for (ii = 0; ii < params.max_iters; ii++) { timestep(params, accel_area, cells, tmp_cells, obstacles); av_vels[ii] = av_velocity(params, cells, obstacles); #ifdef DEBUG printf("==timestep: %d==\n", ii); printf("av velocity: %.12E\n", av_vels[ii]); printf("tot density: %.12E\n", total_density(params, cells)); #endif } gettimeofday(&timstr,NULL); toc=timstr.tv_sec+(timstr.tv_usec/1000000.0); getrusage(RUSAGE_SELF, &ru); timstr=ru.ru_utime; usrtim=timstr.tv_sec+(timstr.tv_usec/1000000.0); timstr=ru.ru_stime; systim=timstr.tv_sec+(timstr.tv_usec/1000000.0); printf("==done==\n"); printf("Reynolds number:\t\t%.12E\n", calc_reynolds(params,cells,obstacles)); printf("Elapsed time:\t\t\t%.6f (s)\n", toc-tic); printf("Elapsed user CPU time:\t\t%.6f (s)\n", usrtim); printf("Elapsed system CPU time:\t%.6f (s)\n", systim); write_values(final_state_file, av_vels_file, params, cells, obstacles, av_vels); finalise(&cells, &tmp_cells, &obstacles, &av_vels); return EXIT_SUCCESS; }
int main(int argc, char* argv[]) { char* paramfile; /* name of the input parameter file */ char* obstaclefile; /* name of a the input obstacle file */ t_param params; /* struct to hold parameter values */ t_speed* cells = NULL; /* grid containing fluid densities */ t_speed* tmp_cells = NULL; /* scratch space */ int* obstacles = NULL; /* grid indicating which cells are blocked */ float* av_vels = NULL; /* a record of the av. velocity computed for each timestep */ int ii; /* generic counter */ struct timeval timstr; /* structure to hold elapsed time */ struct rusage ru; /* structure to hold CPU time--system and user */ double tic,toc; /* floating point numbers to calculate elapsed wallclock time */ double usrtim; /* floating point number to record elapsed user CPU time */ double systim; /* floating point number to record elapsed system CPU time */ /* parse the command line */ if(argc != 3) { usage(argv[0]); } else{ paramfile = argv[1]; obstaclefile = argv[2]; } /* initialise our data structures and load values from file */ initialise(paramfile, obstaclefile, ¶ms, &cells, &tmp_cells, &obstacles, &av_vels); /* iterate for maxIters timesteps */ gettimeofday(&timstr,NULL); tic=timstr.tv_sec+(timstr.tv_usec/1000000.0); float tot_u_x=0; int tot_cells = 0; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); int start=0, end=0, ff, hh, gg; params.rest = params.ny%nprocs; for (ii=0;ii<params.maxIters;ii++) { timestep(params,cells,tmp_cells,obstacles); if(ii==1){ atyt = 0; } if(ii==2){ atyt = 1; } ///////////av_velocity................................................. int jj,kk, tt, source; float local_density; float l_tot_u_x=0; int l_tot_cells = 0; if(params.rest!=0){ if(rank>=params.rest){ start = params.rest*(params.ny/nprocs+1) + (rank-params.rest)*(params.ny/nprocs); end= start+params.ny/nprocs-1; } else{ start = rank*(params.ny/nprocs+1); end = start+params.ny/nprocs; } } for(kk=start;kk<=end;kk++) { for(jj=0;jj<params.nx;jj++) { if(!obstacles[kk*params.nx + jj]) { local_density= 0.0; for(tt=0;tt<NSPEEDS;tt++) { local_density += cells[kk*params.nx + jj].speeds[tt]; } l_tot_u_x += (cells[kk*params.nx + jj].speeds[1] + cells[kk*params.nx + jj].speeds[5] + cells[kk*params.nx + jj].speeds[8] - (cells[kk*params.nx + jj].speeds[3] + cells[kk*params.nx + jj].speeds[6] + cells[kk*params.nx + jj].speeds[7])) / local_density; l_tot_cells+=1; } } } if(rank!=MASTER){ MPI_Send(&l_tot_u_x, 1, MPI_FLOAT, dest, tag, MPI_COMM_WORLD); MPI_Send(&l_tot_cells, 1, MPI_INT, dest, tag, MPI_COMM_WORLD);} else{ tot_cells = l_tot_cells; for (source =1; source < nprocs; source++) { MPI_Recv(&l_tot_u_x, 1, MPI_FLOAT, source, tag, MPI_COMM_WORLD, &status); tot_u_x+=l_tot_u_x; MPI_Recv(&l_tot_cells, 1, MPI_INT, source, tag, MPI_COMM_WORLD, &status); tot_cells+=l_tot_cells; } av_vels[ii] = tot_u_x / (float)tot_cells; } } float mes[9*params.nx]; float mes2[9*params.nx]; if(rank!=MASTER){ if(params.rest!=0){ if(rank>=params.rest){ start = params.rest*(params.ny/nprocs+1) + (rank-params.rest)*(params.ny/nprocs); end= start+params.ny/nprocs-1; } else{ start = rank*(params.ny/nprocs+1); end = start+params.ny/nprocs; } } for(gg=start;gg<=end;gg++) { for(hh=0;hh<params.nx;hh++){ for(ff=0;ff<9;ff++){ mes[9*hh+ff] = cells[gg*params.nx + hh].speeds[ff]; } } } MPI_Isend(mes, 9*params.nx, MPI_FLOAT, MASTER, tag, MPI_COMM_WORLD, &request); } if(rank == MASTER){ for(source=1;source<nprocs;source++){ if(params.rest!=0){ if(source>=params.rest){ start = params.rest*(params.ny/nprocs+1) + (source-params.rest)*(params.ny/nprocs); end= start+params.ny/nprocs-1; } else{ start = source*(params.ny/nprocs+1); end = start+params.ny/nprocs; } } for(gg=start;gg<=end;gg++) { MPI_Irecv(mes2, 9*params.nx, MPI_FLOAT, source, tag, MPI_COMM_WORLD, &request); for(hh=0;hh<params.nx;hh++){ for(ff=0;ff<9;ff++){ cells[gg*params.nx + hh].speeds[ff]=mes2[9*hh+ff]; } } } } } ///////////av_velocity................................................. MPI_Finalize(); gettimeofday(&timstr,NULL); toc=timstr.tv_sec+(timstr.tv_usec/1000000.0); getrusage(RUSAGE_SELF, &ru); timstr=ru.ru_utime; usrtim=timstr.tv_sec+(timstr.tv_usec/1000000.0); timstr=ru.ru_stime; systim=timstr.tv_sec+(timstr.tv_usec/1000000.0); if(rank==0){ /* write final values and free memory */ printf("==done==\n"); printf("Reynolds number:\t\t%.12E\n",calc_reynolds(params,cells,obstacles)); printf("Elapsed time:\t\t\t%.6lf (s)\n", toc-tic); printf("Elapsed user CPU time:\t\t%.6lf (s)\n", usrtim); printf("Elapsed system CPU time:\t%.6lf (s)\n", systim); write_values(params,cells,obstacles,av_vels); finalise(¶ms, &cells, &tmp_cells, &obstacles, &av_vels); //openFile(); // OPEN THE MULTIPLE OUTPUT FILE //printf("\n\n%f\n", toc-tic); //printF(toc-tic); // fclose(ofp); } return EXIT_SUCCESS; }
int main(int argc, char* argv[]) { param_t params; /* struct to hold parameter values */ speed_t* cells = NULL; /* grid containing fluid densities */ speed_t* tmp_cells = NULL; /* scratch space */ int* obstacles = NULL; /* grid indicating which cells are blocked */ int* rowsSetup = NULL; int* accelgrid = NULL; float* av_vels = NULL; /* a record of the av. velocity computed for each timestep */ int ii, rank, size, tag=0, jj; /* generic counter */ struct timeval timstr; /* structure to hold elapsed time */ struct rusage ru; /* structure to hold CPU time--system and user */ double tic,toc; /* floating point numbers to calculate elapsed wallclock time */ double usrtim; /* floating point number to record elapsed user CPU time */ double systim; /* floating point number to record elapsed system CPU time */ int halorow; int buff; int extra; int start_row; int end_row; MPI_Status status; accel_area_t accel_area; /* initialise our data structures and load values from file */ initialise(argv[1], &accel_area, ¶ms, &cells, &tmp_cells, &obstacles, &av_vels, &accelgrid); // Initialize MPI environment. MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); extra = params.ny%size; halorow = (rank<extra) ? (params.ny/size + 1) * params.nx : (params.ny/size) * params.nx; calc_row_setup(&rowsSetup, rank, halorow, extra, params); buff = rowsSetup[0]; start_row = rowsSetup[1]; end_row = rowsSetup[2]; /* iterate for max_iters timesteps */ gettimeofday(&timstr,NULL); tic=timstr.tv_sec+(timstr.tv_usec/1000000.0); for(ii=0; ii<params.max_iters; ii++) { accelerate_flow(params,accel_area,cells,start_row, end_row, accelgrid); lattice(params,cells,tmp_cells,obstacles, av_vels, start_row, end_row, ii); } gettimeofday(&timstr,NULL); toc=timstr.tv_sec+(timstr.tv_usec/1000000.0); getrusage(RUSAGE_SELF, &ru); timstr=ru.ru_utime; usrtim=timstr.tv_sec+(timstr.tv_usec/1000000.0); timstr=ru.ru_stime; systim=timstr.tv_sec+(timstr.tv_usec/1000000.0); float* buffer = malloc(buff * 9 * sizeof(float)); if(rank != 0) { for(ii=0; ii<halorow; ii++) { buffer[9*ii] = cells[start_row+ii].speeds[0]; buffer[9*ii+1] = cells[start_row+ii].speeds[1]; buffer[9*ii+2] = cells[start_row+ii].speeds[2]; buffer[9*ii+3] = cells[start_row+ii].speeds[3]; buffer[9*ii+4] = cells[start_row+ii].speeds[4]; buffer[9*ii+5] = cells[start_row+ii].speeds[5]; buffer[9*ii+6] = cells[start_row+ii].speeds[6]; buffer[9*ii+7] = cells[start_row+ii].speeds[7]; buffer[9*ii+8] = cells[start_row+ii].speeds[8]; } MPI_Send(buffer, 9*buff, MPI_FLOAT, 0, tag, MPI_COMM_WORLD); } else { if(extra == 0) { for(ii=1; ii<size; ii++) { MPI_Recv(buffer, 9*buff, MPI_FLOAT, ii, tag, MPI_COMM_WORLD, &status); for(jj=0; jj<halorow; jj++) { cells[halorow*ii+jj].speeds[0] = buffer[9*jj]; cells[halorow*ii+jj].speeds[1] = buffer[9*jj+1]; cells[halorow*ii+jj].speeds[2] = buffer[9*jj+2]; cells[halorow*ii+jj].speeds[3] = buffer[9*jj+3]; cells[halorow*ii+jj].speeds[4] = buffer[9*jj+4]; cells[halorow*ii+jj].speeds[5] = buffer[9*jj+5]; cells[halorow*ii+jj].speeds[6] = buffer[9*jj+6]; cells[halorow*ii+jj].speeds[7] = buffer[9*jj+7]; cells[halorow*ii+jj].speeds[8] = buffer[9*jj+8]; } } } else { for(ii=1; ii<extra; ii++) { MPI_Recv(buffer, 9*buff, MPI_FLOAT, ii, tag, MPI_COMM_WORLD, &status); for(jj=0; jj<halorow; jj++) { cells[halorow*ii+jj].speeds[0] = buffer[9*jj]; cells[halorow*ii+jj].speeds[1] = buffer[9*jj+1]; cells[halorow*ii+jj].speeds[2] = buffer[9*jj+2]; cells[halorow*ii+jj].speeds[3] = buffer[9*jj+3]; cells[halorow*ii+jj].speeds[4] = buffer[9*jj+4]; cells[halorow*ii+jj].speeds[5] = buffer[9*jj+5]; cells[halorow*ii+jj].speeds[6] = buffer[9*jj+6]; cells[halorow*ii+jj].speeds[7] = buffer[9*jj+7]; cells[halorow*ii+jj].speeds[8] = buffer[9*jj+8]; } } for(ii=extra; ii<size; ii++) { MPI_Recv(buffer, 9*buff, MPI_FLOAT, ii, tag, MPI_COMM_WORLD, &status); for(jj=0; jj<(params.ny/size) * params.nx; jj++) { int local_extra = halorow * extra + (halorow-params.nx)* (ii-extra); cells[local_extra + jj].speeds[0] = buffer[9*jj]; cells[local_extra + jj].speeds[1] = buffer[9*jj+1]; cells[local_extra + jj].speeds[2] = buffer[9*jj+2]; cells[local_extra + jj].speeds[3] = buffer[9*jj+3]; cells[local_extra + jj].speeds[4] = buffer[9*jj+4]; cells[local_extra + jj].speeds[5] = buffer[9*jj+5]; cells[local_extra + jj].speeds[6] = buffer[9*jj+6]; cells[local_extra + jj].speeds[7] = buffer[9*jj+7]; cells[local_extra + jj].speeds[8] = buffer[9*jj+8]; } } } free(buffer); buffer = NULL; } MPI_Finalize(); /*Finilize MPI*/ if(rank == 0) { printf("==done==\n"); printf("Reynolds number:\t\t%.12E\n",calc_reynolds(params,cells,obstacles,av_vels[params.max_iters-1])); printf("Elapsed time:\t\t\t%.6lf (s)\n", toc-tic); printf("Elapsed user CPU time:\t\t%.6lf (s)\n", usrtim); printf("Elapsed system CPU time:\t%.6lf (s)\n", systim); write_values(params,cells,obstacles,av_vels); finalise(¶ms, &cells, &tmp_cells, &obstacles, &av_vels, &accelgrid, &rowsSetup); } return EXIT_SUCCESS; }