From 352752e6934fb1cd0d00a840a48a816296c4e07a Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 30 Oct 2010 15:32:10 -0400 Subject: [PATCH] Ran `indent -linux` on src/global_operations/*.c. --- src/global_operations/broadcast.c | 166 +++++++++--------- src/global_operations/global_mpi_operations.c | 84 ++++----- src/global_operations/print_tree.c | 81 +++++---- 3 files changed, 161 insertions(+), 170 deletions(-) diff --git a/src/global_operations/broadcast.c b/src/global_operations/broadcast.c index 11204db..8fdd397 100644 --- a/src/global_operations/broadcast.c +++ b/src/global_operations/broadcast.c @@ -7,107 +7,99 @@ #include #include - #define TAG_BCAST 123 - void my_broadcast(int rank, int size, double *message); int main(int argc, char *argv[]) { - int rank, size; - double message; + int rank, size; + double message; - 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); - /* an arbitrary message */ - message = 0.0; - if (rank == 0) - message = 5.6789; + /* an arbitrary message */ + message = 0.0; + if (rank == 0) + message = 5.6789; - /* local broadcast */ - my_broadcast(rank, size, &message); + /* local broadcast */ + my_broadcast(rank, size, &message); - /* check that nodes received message */ - printf ("after broadcast -- node %d -- message %f\n", rank, message); + /* check that nodes received message */ + printf("after broadcast -- node %d -- message %f\n", rank, message); - /* end of code */ - MPI_Finalize(); - return EXIT_SUCCESS; + /* end of code */ + MPI_Finalize(); + return EXIT_SUCCESS; } - void my_broadcast(int rank, int size, double *message) { - int level, maxlevel; - int shift_ip, n_target; - int *target_process; - int from_process, target; - MPI_Status recv_status; - - /* build transmission tree */ - - /* size of binary tree */ - maxlevel = 0; - while (1 << maxlevel+1 < size) - maxlevel++; - /* '<<' is a bitwise shift left, so 1 << b == pow(2, b) */ - - /* make space for local branch of tree */ - target_process = (int *)malloc((unsigned) (size * sizeof(int))); - - /* build the branches */ - n_target = 0; - from_process = -1; - n_target = 0; - for (level=0; level <= maxlevel; level++) - { - shift_ip = 1 << level; - if (rank >= shift_ip) - from_process = rank - shift_ip; - if (rank < shift_ip) - { - target = rank + shift_ip; - if (target < size) - { - target_process[n_target] = target; - n_target++; - } + int level, maxlevel; + int shift_ip, n_target; + int *target_process; + int from_process, target; + MPI_Status recv_status; + + /* build transmission tree */ + + /* size of binary tree */ + maxlevel = 0; + while (1 << maxlevel + 1 < size) + maxlevel++; + /* '<<' is a bitwise shift left, so 1 << b == pow(2, b) */ + + /* make space for local branch of tree */ + target_process = (int *)malloc((unsigned)(size * sizeof(int))); + + /* build the branches */ + n_target = 0; + from_process = -1; + n_target = 0; + for (level = 0; level <= maxlevel; level++) { + shift_ip = 1 << level; + if (rank >= shift_ip) + from_process = rank - shift_ip; + if (rank < shift_ip) { + target = rank + shift_ip; + if (target < size) { + target_process[n_target] = target; + n_target++; + } + } } - } - - /* debugging output */ - fprintf(stderr, "process %d -- from_process %d -- %d targets\n", - rank, from_process, n_target); - if (n_target > 0) - { - for (target=0; target < n_target; target++) - fprintf(stderr, "process %d -- target %d\n", - rank, target_process[target]); - } - - /* message transmission */ - - /* receive message */ - if (rank > 0) - { - fprintf(stderr, "--- receiving %d %d \n", rank, from_process); - fflush(stderr); - MPI_Recv(message, 1, MPI_DOUBLE, from_process, TAG_BCAST, - MPI_COMM_WORLD, &recv_status); - } - - /* send message to all target processes */ - if (n_target > 0) - { - fprintf(stderr, "--- sending %d %d \n", rank, n_target); - for (target=0 ; target < n_target; target++) - MPI_Ssend(message, 1, MPI_DOUBLE, target_process[target], - TAG_BCAST, MPI_COMM_WORLD); - } - - /* free up space */ - free(target_process); + + /* debugging output */ + fprintf(stderr, "process %d -- from_process %d -- %d targets\n", + rank, from_process, n_target); + if (n_target > 0) { + for (target = 0; target < n_target; target++) + fprintf(stderr, "process %d -- target %d\n", + rank, target_process[target]); + } + + /* message transmission */ + + /* receive message */ + if (rank > 0) { + fprintf(stderr, "--- receiving %d %d \n", rank, from_process); + fflush(stderr); + MPI_Recv(message, 1, MPI_DOUBLE, from_process, TAG_BCAST, + MPI_COMM_WORLD, &recv_status); + } + + /* send message to all target processes */ + if (n_target > 0) { + fprintf(stderr, "--- sending %d %d \n", rank, n_target); + for (target = 0; target < n_target; target++) + MPI_Ssend(message, 1, MPI_DOUBLE, + target_process[target], TAG_BCAST, + MPI_COMM_WORLD); + } + + /* free up space */ + free(target_process); } 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); } diff --git a/src/global_operations/print_tree.c b/src/global_operations/print_tree.c index e08157b..f7383cd 100644 --- a/src/global_operations/print_tree.c +++ b/src/global_operations/print_tree.c @@ -1,41 +1,40 @@ -/* skeleton for a broadcast routine from node 0 */ - -#include -#include - -int main(int argc, char *argv[]) -{ - int two_to_generation; - int rank, size; - int to, from; - - /* scan over a hypothetical virtual machine of 15 nodes */ - size = 15; - for (rank=0; rank < size; rank++) - { - printf("rank %d", rank); - - /* two_to_generation reflects the steps in the tree broadcast */ - two_to_generation = 1; - while(two_to_generation < size) - { - /* receive message */ - if (rank >= two_to_generation && rank < two_to_generation*2) - { - from = rank - two_to_generation; - if ( from < size ) - printf(" -- from %d", from); - } - /* send message */ - if (rank < two_to_generation) - { - to = rank + two_to_generation; - if ( to < size ) - printf(" -- to %d", to); - } - two_to_generation = 2 * two_to_generation; - } - /* done for a given rank */ - printf("\n"); - } -} +/* skeleton for a broadcast routine from node 0 */ + +#include +#include +int main(int argc, char *argv[]) +{ + int two_to_generation; + int rank, size; + int to, from; + + /* scan over a hypothetical virtual machine of 15 nodes */ + size = 15; + for (rank = 0; rank < size; rank++) { + printf("rank %d", rank); + + /* two_to_generation reflects the steps in the tree broadcast */ + two_to_generation = 1; + while (two_to_generation < size) { + + /* receive message */ + if (rank >= two_to_generation + && rank < two_to_generation * 2) { + from = rank - two_to_generation; + if (from < size) + printf(" -- from %d", from); + } + + /* send message */ + if (rank < two_to_generation) { + to = rank + two_to_generation; + if (to < size) + printf(" -- to %d", to); + } + two_to_generation = 2 * two_to_generation; + } + + /* done for a given rank */ + printf("\n"); + } +} -- 2.26.2