Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <mpi.h>
- #include <stdio.h>
- #include <string>
- int main(int argc, char** argv) {
- // Initialize the MPI environment
- MPI_Init(&argc, &argv);
- // Get the number of processes
- int world_size;
- MPI_Comm_size(MPI_COMM_WORLD, &world_size);
- // Get the world_rank of the process
- int world_rank;
- MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
- MPI_Request request;
- MPI_Status st;
- int msg;
- if (world_rank == 0) {
- msg = 0;
- MPI_Send(&msg, 1, MPI_INT, world_size-1, 1, MPI_COMM_WORLD);
- printf("Process %d sends %d to proc %d\n", world_rank, msg, world_size - 1);
- MPI_Recv(&msg, 1, MPI_INT, world_rank+1, 1, MPI_COMM_WORLD, &st);
- printf("Process %d got %d from proc %d\n", world_rank, msg, world_size);
- msg++;
- MPI_Send(&msg, 1, MPI_INT, world_size - 1, 1, MPI_COMM_WORLD);
- printf("Process %d sends %d to proc %d\n", world_rank, msg, world_size - 1);
- MPI_Recv(&msg, 1, MPI_INT, world_rank + 1, 1, MPI_COMM_WORLD, &st);
- printf("Process %d got %d from proc %d\n", world_rank, msg, world_size);
- }
- if (world_rank > 0 && world_rank < (world_size - 1)) {
- MPI_Recv(&msg, 1, MPI_INT, world_rank + 1, 1, MPI_COMM_WORLD, &st);
- printf("Process %d got %d from proc %d\n", world_rank, msg, world_rank + 1);
- msg++;
- MPI_Send(&msg, 1, MPI_INT, world_rank - 1, 1, MPI_COMM_WORLD);
- printf("Process %d sends %d to proc %d\n", world_rank, msg, world_rank - 1);
- MPI_Recv(&msg, 1, MPI_INT, world_rank + 1, 1, MPI_COMM_WORLD, &st);
- printf("Process %d got %d from proc %d\n", world_rank, msg, world_rank + 1);
- msg++;
- MPI_Send(&msg, 1, MPI_INT, world_rank - 1, 1, MPI_COMM_WORLD);
- printf("Process %d sends %d to proc %d\n", world_rank, msg, world_rank - 1);
- }
- if (world_rank == world_size - 1) {
- MPI_Recv(&msg, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &st);
- printf("Process %d got %d from proc %d\n", world_rank, msg, 0);
- msg++;
- MPI_Send(&msg, 1, MPI_INT, world_rank - 1, 1, MPI_COMM_WORLD);
- printf("Process %d sends %d to proc %d\n", world_rank, msg, world_rank - 1);
- MPI_Recv(&msg, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &st);
- printf("Process %d got %d from proc %d\n", world_rank, msg, 0);
- msg++;
- MPI_Send(&msg, 1, MPI_INT, world_rank - 1, 1, MPI_COMM_WORLD);
- printf("Process %d sends %d to proc %d\n", world_rank, msg, world_rank - 1);
- }
- // Finalize the MPI environment.
- MPI_Finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement