c++ - Problems with MPI_Reduce -
first of all, new mpi. have program uses rectangle/midpoint rule calculate area of function equal pi. when use method involving sends , receives calculation gives value pi. however, when testing mpi_reduce achieve same thing pi 1 processor, using different numbers of processors value less pi. have code output local_sum each processor , global_sum mpi_reduce. local sums add global sum, isn't pi. can't figure out wrong. appreciated. here section of code relevant:
/* add areas calculated each process */ if (my_rank == 0) { total = my_area; (source = 1; source < p; source++) { mpi_recv(&my_area, 1, mpi_double, source, tag, mpi_comm_world, &status); total = total + my_area; } } else { mpi_send(&my_area, 1, mpi_double, dest, tag, mpi_comm_world); } //*********test begin using mpi_reduce********* // print local sums on each process printf("local sum process %d - %f\n", my_rank, my_area); double global_sum; // reduce of local sums global sum mpi_reduce( &my_area, // send data &global_sum, // receive data 1, // number of elements in send buffer mpi_double, // mpi datatype mpi_sum, // mpi reduce operation 0, // root process mpi_comm_world); // mpi communicator //**********test end using mpi_reduce********** /* print result */ if (my_rank == 0) { printf("with n = %d rectangles, our estimate\n", n); printf("of area %f %f = %.15f\n", a, b, total); cout << "the area calculated using mpi_reduce " << global_sum << "." << endl; }
the output of code single processor is:
enter limits of integration a, b, , number of bins n, separated spaces
0.0 1.0 10000000
local sum process 0 - 3.141593
with n = 10000000 rectangles, our estimate
of area 0.000000 1.000000 = 3.141592653473855
the area calculated using mpi_reduce 3.14159.
the output of code using (for example) 4 processors is:
local sum process 1 - 0.923739
local sum process 2 - 0.775058
local sum process 0 - 0.453312
local sum process 3 - 0.453312
with n = 10000000 rectangles, our estimate
of area 0.000000 1.000000 = 3.141591655190580
the area calculated using mpi_reduce 2.60542.
Comments
Post a Comment