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 count;
- if (world_rank == 0) {
- count = 0;
- for (int i = 0; i < 2; ++i) {
- count++;
- MPI_Send(&count, 1, MPI_INT, world_size - 1, 1, MPI_COMM_WORLD);
- printf("Process %d sends %d to proc %d\n", world_rank, count, world_size - 1);
- MPI_Recv(&count, 1, MPI_INT, world_rank + 1, 1, MPI_COMM_WORLD, &st);
- printf("Process %d got %d from proc %d\n", world_rank, count, world_size + 1);
- }
- }
- if (world_rank > 0 && world_rank < (world_size - 1)) {
- for (int i = 0; i < 2; ++i) {
- MPI_Recv(&count, 1, MPI_INT, world_rank + 1, 1, MPI_COMM_WORLD, &st);
- printf("Process %d got %d from proc %d\n", world_rank, count, world_rank + 1);
- count++;
- MPI_Send(&count, 1, MPI_INT, world_rank - 1, 1, MPI_COMM_WORLD);
- printf("Process %d sends %d to proc %d\n", world_rank, count, world_rank -1 );
- }
- }
- if (world_rank == world_size - 1) {
- for (int i = 0; i < 2; ++i) {
- MPI_Recv(&count, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &st);
- printf("Process %d got %d from proc %d\n", world_rank, count, 0);
- count++;
- MPI_Send(&count, 1, MPI_INT, world_rank - 1, 1, MPI_COMM_WORLD);
- printf("Process %d sends %d to proc %d\n", world_rank, count, world_rank - 1);
- }
- }
- // Finalize the MPI environment.
- MPI_Finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement