Skip to content

Instantly share code, notes, and snippets.

@Sutil
Created January 22, 2016 22:15
Show Gist options
  • Save Sutil/a7dc6a3ea33b22dbbc5a to your computer and use it in GitHub Desktop.
Save Sutil/a7dc6a3ea33b22dbbc5a to your computer and use it in GitHub Desktop.
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
MPI_Init(NULL, NULL);
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
int token;
if (rank != 0) {
MPI_Recv(&token, 1, MPI_INT, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("P%d recebeu token %d de P%d\n", rank, token, rank - 1);
} else {
token = -1;
}
MPI_Send(&token, 1, MPI_INT, (rank + 1) % size, 0, MPI_COMM_WORLD);
if (rank == 0) {
MPI_Recv(&token, 1, MPI_INT, size - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("P%d recebeu token %d de P%d\n", rank, token, size - 1);
}
MPI_Finalize();
}
// mpicc -o anel anel.c
// mpirun -n 2 ./anel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment