#include <stdlib.h>
#include <mpi.h>
-
#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);
}
#include <stdlib.h>
#include <mpi.h>
-
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);
}
-/* skeleton for a broadcast routine from node 0 */\r
-\r
-#include <stdio.h>\r
-#include <math.h>\r
-\r
-int main(int argc, char *argv[])\r
-{\r
- int two_to_generation;\r
- int rank, size;\r
- int to, from;\r
-\r
- /* scan over a hypothetical virtual machine of 15 nodes */\r
- size = 15;\r
- for (rank=0; rank < size; rank++)\r
- {\r
- printf("rank %d", rank);\r
-\r
- /* two_to_generation reflects the steps in the tree broadcast */\r
- two_to_generation = 1;\r
- while(two_to_generation < size)\r
- {\r
- /* receive message */\r
- if (rank >= two_to_generation && rank < two_to_generation*2)\r
- {\r
- from = rank - two_to_generation;\r
- if ( from < size )\r
- printf(" -- from %d", from);\r
- }\r
- /* send message */\r
- if (rank < two_to_generation)\r
- {\r
- to = rank + two_to_generation;\r
- if ( to < size )\r
- printf(" -- to %d", to);\r
- }\r
- two_to_generation = 2 * two_to_generation;\r
- }\r
- /* done for a given rank */\r
- printf("\n");\r
- }\r
-}\r
+/* skeleton for a broadcast routine from node 0 */
+
+#include <stdio.h>
+#include <math.h>
+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");
+ }
+}