int gethyd(long *hydtime, long *hydstep) /* **----------------------------------------------------------- ** Input: none ** Output: hydtime = pointer to hydraulic solution time ** hydstep = pointer to hydraulic time step ** Returns: error code ** Purpose: retrieves hydraulic solution and hydraulic ** time step for next hydraulic event ** ** NOTE: when this function is called, WQ results have ** already been updated to the point in time when ** the next hydraulic event occurs. **----------------------------------------------------------- */ { int errcode = 0; /* Read hydraulic results from file */ if (!readhyd(hydtime)) return(307); if (!readhydstep(hydstep)) return(307); Htime = *hydtime; /* Save current results to output file */ if (Htime >= Rtime) { if (Saveflag) { errcode = saveoutput(); Nperiods++; } Rtime += Rstep; } /* If simulating WQ: */ if (Qualflag != NONE && Qtime < Dur) { /* Compute reaction rate coeffs. */ if (Reactflag && Qualflag != AGE) ratecoeffs(); /* Initialize pipe segments (at time 0) or */ /* else re-orient segments if flow reverses.*/ if (Qtime == 0) initsegs(); else reorientsegs(); } return(errcode); }
int main(int argc, char* argv[]) { int i,j,t; double sum; double d = 0.85; double c; double start, end, min_start, max_end; int rank, processes; MPI_Init( &argc, &argv ); MPI_Comm_rank( MPI_COMM_WORLD, &rank ); MPI_Comm_size( MPI_COMM_WORLD, &processes ); if (argc != 3) Usage(argv[0]); N = strtol(argv[1], NULL, 10); max_index=strtol(argv[2],NULL,10); l=calloc(N,sizeof(int)); r1=malloc(N*sizeof(double)); r2=malloc(N*sizeof(double)); map=calloc(max_index,sizeof(int)); D=malloc(N*sizeof(int*)); if(D == NULL) { fprintf(stderr, "Out of memory\n"); exit(0); } for (i= 0; i < N; i++) { D[i]=calloc(N,sizeof(int)); if(D[i] == NULL) { fprintf(stderr, "Out of memory\n"); exit(0); } } start = MPI_Wtime(); loadinput(processes); c = (1-d)/(double)N; for (i= 0; i < N; i++) { r2[i] = (double) 1/N; } int block_size; if ( rank == processes) { block_size = N - (N/processes)*(processes-1); } else { block_size = N/processes; } int recvcounts[processes]; int displs[processes]; for (t=0; t<processes; t++) { recvcounts[t]=block_size; displs[t]=t*block_size; } recvcounts[processes-1]=N-(N/processes)*(processes-1); displs[processes-1]=(processes-1)*N/processes; for (t=0; t<10; t++) { MPI_Allgatherv(r2+block_size*rank, block_size, MPI_DOUBLE, r1, recvcounts, displs, MPI_DOUBLE, MPI_COMM_WORLD); for (i=0; i<block_size; i++) { sum = 0; for (j=0; j<N; j++) { if (D[i+rank*block_size][j]) { sum+= r1[j]/(double)l[j]; } } r2[i+rank*block_size] = c + d*sum; } } end = MPI_Wtime(); MPI_Reduce(&start,&min_start,1,MPI_DOUBLE,MPI_MIN,0,MPI_COMM_WORLD); MPI_Reduce(&end,&max_end,1,MPI_DOUBLE,MPI_MAX,0,MPI_COMM_WORLD); if (rank == 0) { printf("Time taken:%f\n", end-start); saveoutput(r1,r2,map,N); } free(D); free(l); free(r1); free(r2); MPI_Finalize(); return 0; } /* main */