-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBsend.c
50 lines (44 loc) · 1.46 KB
/
Bsend.c
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int main(int argc, char* argv[])
{
printf("Done by Maitreyee Paliwal \n\n");
MPI_Init(&argc, &argv);
int size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
if(size != 2)
{
printf("This application is meant to be run with 2 processes.\n");
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
enum role_ranks { SENDER, RECEIVER };
int my_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
switch(my_rank)
{
case SENDER:
{
int buffer_attached_size = MPI_BSEND_OVERHEAD + sizeof(int);
int buffer_sent;
scanf("%d", &buffer_sent);
char* buffer_attached = (char*)malloc(buffer_attached_size);
MPI_Buffer_attach(buffer_attached, buffer_attached_size);
printf("\n[MPI process %d] I send value %d.\n", my_rank, buffer_sent);
MPI_Bsend(&buffer_sent, 1, MPI_INT, RECEIVER, 0, MPI_COMM_WORLD);
MPI_Buffer_detach(&buffer_attached, &buffer_attached_size);
free(buffer_attached);
break;
}
case RECEIVER:
{
// Receive the message and print it.
int received;
MPI_Recv(&received, 1, MPI_INT, SENDER, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("\n[MPI process %d] I received value: %d.\n", my_rank, received);
break;
}
}
MPI_Finalize();
return EXIT_SUCCESS;
}