X-Git-Url: http://git.tremily.us/?p=parallel_computing.git;a=blobdiff_plain;f=src%2Fglobal_operations%2Fglobal_mpi_operations.c;h=a4f8a6445570837c776b5006fd669b18846fdbb7;hp=405e694beeb5667013d8ce6df1dbb2ee847d8e15;hb=352752e6934fb1cd0d00a840a48a816296c4e07a;hpb=2f428976c7fe04aa7483546291addeada1950b38 diff --git a/src/global_operations/global_mpi_operations.c b/src/global_operations/global_mpi_operations.c index 405e694..a4f8a64 100644 --- a/src/global_operations/global_mpi_operations.c +++ b/src/global_operations/global_mpi_operations.c @@ -6,61 +6,61 @@ #include #include - int main(int argc, char *argv[]) { - int rank, size; - double message, local_result, total_result_1, total_result_2; + int rank, size; + double message, local_result, total_result_1, total_result_2; - MPI_Init(&argc, &argv); - MPI_Comm_rank(MPI_COMM_WORLD, &rank); - MPI_Comm_size(MPI_COMM_WORLD, &size); + MPI_Init(&argc, &argv); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + MPI_Comm_size(MPI_COMM_WORLD, &size); - /* Synchronization. - * All process must reach here before continuing. */ - MPI_Barrier(MPI_COMM_WORLD); - printf("synchronized ( node %d )\n" , rank); - /* Synchronize again to ensure the "synchronized" messages are contiguous. */ - MPI_Barrier(MPI_COMM_WORLD); + /* Synchronization. + * All process must reach here before continuing. */ + MPI_Barrier(MPI_COMM_WORLD); + printf("synchronized ( node %d )\n", rank); + /* Synchronize again to ensure the "synchronized" messages are contiguous. */ + MPI_Barrier(MPI_COMM_WORLD); - /* An arbitrary message */ - message = 0; - if (rank == 0) - message = 5.6789; + /* An arbitrary message */ + message = 0; + if (rank == 0) + message = 5.6789; - /* Broadcast this message */ - MPI_Bcast(&message, 1, MPI_DOUBLE, 0 /* root */, MPI_COMM_WORLD); + /* Broadcast this message */ + MPI_Bcast(&message, 1, MPI_DOUBLE, 0 /* root */ , MPI_COMM_WORLD); - /* Check if message received */ - printf("node %d -- message %f\n", rank, message); + /* Check if message received */ + printf("node %d -- message %f\n", rank, message); - /* Process dependent result */ - local_result = 2.0 * rank; + /* Process dependent result */ + local_result = 2.0 * rank; - /* Reduce operations */ - MPI_Reduce(&local_result, &total_result_1, 1, MPI_DOUBLE, - MPI_MAX, 0 /* target_process */, MPI_COMM_WORLD); + /* Reduce operations */ + MPI_Reduce(&local_result, &total_result_1, 1, MPI_DOUBLE, + MPI_MAX, 0 /* target_process */ , MPI_COMM_WORLD); - MPI_Reduce(&local_result, &total_result_2, 1, MPI_DOUBLE, - MPI_SUM, 0 /* target_process */, MPI_COMM_WORLD); + MPI_Reduce(&local_result, &total_result_2, 1, MPI_DOUBLE, + MPI_SUM, 0 /* target_process */ , MPI_COMM_WORLD); - /* Only target node 0 has the global results */ - if ( rank == 0 ) - printf("results of global operations: %f %f <-- node 0 has results\n", - total_result_1, total_result_2 ); + /* Only target node 0 has the global results */ + if (rank == 0) + printf + ("results of global operations: %f %f <-- node 0 has results\n", + total_result_1, total_result_2); - /* Reduce operation followed by bcast. */ - MPI_Allreduce(&local_result, &total_result_1, 1, MPI_DOUBLE, - MPI_MAX, MPI_COMM_WORLD); + /* Reduce operation followed by bcast. */ + MPI_Allreduce(&local_result, &total_result_1, 1, MPI_DOUBLE, + MPI_MAX, MPI_COMM_WORLD); - MPI_Allreduce(&local_result, &total_result_2, 1, MPI_DOUBLE, - MPI_SUM, MPI_COMM_WORLD); + MPI_Allreduce(&local_result, &total_result_2, 1, MPI_DOUBLE, + MPI_SUM, MPI_COMM_WORLD); - /* All nodes have the results */ - printf("results of ALLREDUCE operations ( node %d ): %f %f\n", - rank, total_result_1, total_result_2 ); + /* All nodes have the results */ + printf("results of ALLREDUCE operations ( node %d ): %f %f\n", + rank, total_result_1, total_result_2); - /* Clean up and exit */ - MPI_Finalize(); - exit(1); + /* Clean up and exit */ + MPI_Finalize(); + exit(1); }