-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLab_5.c
More file actions
34 lines (26 loc) · 906 Bytes
/
Lab_5.c
File metadata and controls
34 lines (26 loc) · 906 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Write a MPI Program to demonstration of MPI_Send and MPI_Recv.
#include <mpi.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
int rank;
MPI_Init(&argc, &argv); // Initialize MPI
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Get process rank
if (rank == 0) {
int number = 42;
MPI_Send(&number, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); // Send to rank 1
printf("Rank 0 sent number %d to rank 1\n", number);
}
else if (rank == 1) {
int received;
MPI_Recv(&received, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); // Receive from rank 0
printf("Rank 1 received number %d from rank 0\n", received);
}
MPI_Finalize(); // Finalize MPI
return 0;
}
// Terminal Command
// Compile = mpicc Lab_5.c
// Execute = mpiexec -np 2 ./a.out
// OUTPUT
// Rank 0 sent number 42 to rank 1
// Rank 1 received number 42 from rank 0