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
51
52
53
54
55
56
57
58
59
60
61
62
63
| #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mpi.h"
#define MASTER 0
#define STRLEN 25
int main(int argc, char* argv[])
{
int rank;
int size;
int position;
char message[BUFSIZ];
float value; //VALUE TO SEND
char name[STRLEN]; //ASSIGNED NAME
int param; //ADDITIONAL PARAM
MPI_Init( &argc, &argv );
MPI_Comm_size( MPI_COMM_WORLD, &size );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
if (rank == MASTER) {
value = 10;
sprintf(name, "My Name");
param = 1;
position = 0;
/* now let's pack all those values into a single message */
MPI_Pack(&value, 1, MPI_FLOAT, message, BUFSIZ,
&position, MPI_COMM_WORLD);
/* position has been incremented to first free byte in the message.. */
MPI_Pack(name, STRLEN, MPI_CHAR, message, BUFSIZ,
&position, MPI_COMM_WORLD);
/* position has been incremented again.. */
MPI_Pack(¶m, 1, MPI_INT, message, BUFSIZ,
&position, MPI_COMM_WORLD);
MPI_Send(message, BUFSIZ, MPI_PACKED, 1, 1, MPI_COMM_WORLD);
}
else {
MPI_Recv(message, BUFSIZ, MPI_PACKED, 0, 1, MPI_COMM_WORLD, NULL);
position = 0;
MPI_Unpack(message, BUFSIZ, &position, &value, 1,
MPI_FLOAT, MPI_COMM_WORLD);
/* Note that we must know the length of string to expect here! */
MPI_Unpack(message, BUFSIZ, &position, name, STRLEN,
MPI_CHAR, MPI_COMM_WORLD);
MPI_Unpack(message, BUFSIZ, &position, ¶m, 1,
MPI_INT, MPI_COMM_WORLD);
printf("rank %d:\t%d %.1f %s\n", rank, param, value, name);
}
MPI_Finalize();
return EXIT_SUCCESS;
}
|