Commit 0a09169c authored by Taddeüs Kroes's avatar Taddeüs Kroes

Added test program to familiarize with MPI.

parent ae636604
...@@ -2,14 +2,17 @@ ...@@ -2,14 +2,17 @@
#include <stdio.h> #include <stdio.h>
#include <mpi.h> #include <mpi.h>
double *data = {.0}; double data[1] = {.0};
void initialize_ring(void) { void initialize_ring(int tasks) {
double received; double received;
MPI_Status status;
puts("Sending first data"); printf("Sending first data...");
MPI_Send(data, 1, MPI_DOUBLE, 2, MPI_ANY_TAG, MPI_COMM_WORLD); MPI_Send(data, 1, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD);
MPI_Recv(&received, 1, MPI_DOUBLE, tasks - 1, MPI_ANY_TAG, MPI_COMM_WORLD); puts("sent");
MPI_Recv(&received, 1, MPI_DOUBLE, tasks - 1, MPI_ANY_TAG, MPI_COMM_WORLD,
&status);
if( received != *data ) if( received != *data )
printf("Received data does not match (sent: %f, received: %f)\n", printf("Received data does not match (sent: %f, received: %f)\n",
...@@ -20,10 +23,15 @@ void initialize_ring(void) { ...@@ -20,10 +23,15 @@ void initialize_ring(void) {
void pass_data(int tasks, int rank) { void pass_data(int tasks, int rank) {
double received; double received;
MPI_Status status;
printf("Task %d sending data\n", task); int target = (rank + 1) % tasks, source = (rank - 1 + tasks) % tasks;
MPI_Recv(&received, 1, MPI_DOUBLE, (rank - 1 + tasks) % tasks, MPI_ANY_TAG, MPI_COMM_WORLD);
MPI_Send(data, 1, MPI_DOUBLE, (rank + 1) % tasks, MPI_ANY_TAG, MPI_COMM_WORLD); MPI_Recv(&received, 1, MPI_DOUBLE, source, MPI_ANY_TAG, MPI_COMM_WORLD,
&status);
printf("Task %d received data from %d, sending to %d...", rank, source,
target);
MPI_Send(data, 1, MPI_DOUBLE, target, 0, MPI_COMM_WORLD);
puts("sent");
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
...@@ -38,7 +46,7 @@ int main(int argc, char **argv) { ...@@ -38,7 +46,7 @@ int main(int argc, char **argv) {
MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if( !rank ) if( !rank )
initialize_ring(); initialize_ring(tasks);
else else
pass_data(tasks, rank); pass_data(tasks, rank);
......
#include <stdlib.h>
#include <stdio.h>
#include <mpi.h>
double data[1] = {.0};
void initialize_ring(int tasks) {
double received;
MPI_Status status;
printf("Sending first data...");
MPI_Send(data, 1, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD);
puts("sent");
MPI_Recv(&received, 1, MPI_DOUBLE, tasks - 1, MPI_ANY_TAG, MPI_COMM_WORLD,
&status);
if( received != *data )
printf("Received data does not match (sent: %f, received: %f)\n",
*data, received);
else
puts("Data received succesfully");
}
void pass_data(int tasks, int rank) {
double received;
MPI_Status status;
int target = (rank + 1) % tasks, source = (rank - 1 + tasks) % tasks;
MPI_Recv(&received, 1, MPI_DOUBLE, source, MPI_ANY_TAG, MPI_COMM_WORLD,
&status);
printf("Task %d received data from %d, sending to %d...", rank, source,
target);
MPI_Send(data, 1, MPI_DOUBLE, target, 0, MPI_COMM_WORLD);
puts("sent");
}
int main(int argc, char **argv) {
int tasks, rank, error;
if( (error = MPI_Init(&argc, &argv)) != MPI_SUCCESS ) {
printf("MPI_init failed (error %d)\n", error);
MPI_Abort(MPI_COMM_WORLD, error);
}
MPI_Comm_size(MPI_COMM_WORLD, &tasks);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if( !rank )
initialize_ring(tasks);
else
pass_data(tasks, rank);
MPI_Finalize();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment