Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <mpi.h>
- #include "l17.h"
- int main(int argc, char** argv) {
- int rc;
- rc = MPI_Init(&argc, &argv);
- L17_CHECK_ERROR(MPI_Init, rc);
- int size;
- rc = MPI_Comm_size(MPI_COMM_WORLD, &size);
- L17_CHECK_ERROR(MPI_Comm_size, rc);
- int rank;
- rc = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- L17_CHECK_ERROR(MPI_Comm_rank, rc);
- if (size == 1) {
- printf("No messages if there's only one process\n");
- exit(1);
- }
- if (rank == 0) {
- int i = 0;
- printf("Process %d: the number is %d\n", rank, i);
- rc = MPI_Send(&i, 1, MPI_INT, 1, 1, MPI_COMM_WORLD);
- L17_CHECK_ERROR(MPI_Send, rc);
- rc = MPI_Recv(&i, 1, MPI_INT, size - 1, 1, MPI_COMM_WORLD, NULL);
- L17_CHECK_ERROR(MPI_Recv, rc);
- printf("Process %d: the number is %d\n", rank, i);
- }
- else {
- int i;
- rc = MPI_Recv(&i, 1, MPI_INT, rank - 1, 1, MPI_COMM_WORLD, NULL);
- L17_CHECK_ERROR(MPI_Recv, rc);
- i += rank;
- printf("Process %d: the number is %d\n", rank, i);
- int torank = (rank == size - 1) ? 0 : rank + 1;
- rc = MPI_Send(&i, 1, MPI_INT, torank, 1, MPI_COMM_WORLD);
- L17_CHECK_ERROR(MPI_Send, rc);
- }
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment