I have a code snippet below in C ++ that basically calculates pi using the classic monte carlo technique.
srand48((unsigned)time(0) + my_rank); for(int i = 0 ; i < part_points; i++) { double x = drand48(); double y = drand48(); if( (pow(x,2)+pow(y,2)) < 1){ ++count; } } MPI_Reduce(&count, &total_hits, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); MPI_Barrier(MPI_COMM_WORLD); if(my_rank == root) { pi = 4*(total_hits/(double)total_points); cout << "Calculated pi: " << pi << " in " << end_time-start_time << endl; }
I'm just wondering if an MPI_Barrier call is needed. MPI_Reduce make sure that the body of the if statement is not executed before the reduction operation is completed? I hope I get it. Thanks
Cemre source share