Commit ae636604 authored by Taddeüs Kroes's avatar Taddeüs Kroes

ModSIm ass4: Created a basic ring program.

parent 72bcddf6
...@@ -2,10 +2,45 @@ ...@@ -2,10 +2,45 @@
#include <stdio.h> #include <stdio.h>
#include <mpi.h> #include <mpi.h>
double *data = {.0};
void initialize_ring(void) {
double received;
puts("Sending first data");
MPI_Send(data, 1, MPI_DOUBLE, 2, MPI_ANY_TAG, MPI_COMM_WORLD);
MPI_Recv(&received, 1, MPI_DOUBLE, tasks - 1, MPI_ANY_TAG, MPI_COMM_WORLD);
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;
printf("Task %d sending data\n", task);
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);
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
MPI_Init(&argc, &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();
else
pass_data(tasks, rank);
MPI_Finalize(); MPI_Finalize();
} }
...@@ -4,5 +4,6 @@ RSH_AGENT=rsh ...@@ -4,5 +4,6 @@ RSH_AGENT=rsh
MPI_HOSTFILE=~/.mpirun.machines MPI_HOSTFILE=~/.mpirun.machines
PROGRAM_EXEC=ring PROGRAM_EXEC=ring
mpirun -np $NUM_PROCESSES --mca pls_rsh_agent $RSH_AGENT \ mpirun -np $NUM_PROCESSES $PROGRAM_EXEC
--hostfile $MPI_HOSTFILE $PROGRAM_EXEC #mpirun -np $NUM_PROCESSES --mca pls_rsh_agent $RSH_AGENT \
# --hostfile $MPI_HOSTFILE $PROGRAM_EXEC
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