-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task_17.cpp
63 lines (56 loc) · 1.55 KB
/
Task_17.cpp
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 "mpi.h"
#include <iostream>
int main(int argc, char **argv)
{
int size, rank, position, i;
float a[10];
char b[10], buf[100];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
for (i = 0; i < 10; i++) {
a[i] = rank + 1.0;
if (rank == 0) b[i] = 'a';
else b[i] = 'b';
}
position = 0;
if (rank == 0) {
printf("rank = %d\n", rank);
for (int i = 0; i < 10; i++)
printf("a[%d]=%f ", i, a[i]);
printf("\n");
for (int i = 0; i < 10; i++)
printf("b[%i]=%c ", i, b[i]);
printf("\n-----------------------\n");
MPI_Pack(a, 10, MPI_FLOAT, buf, 100, &position, MPI_COMM_WORLD);
MPI_Pack(b, 10, MPI_CHAR, buf, 100, &position, MPI_COMM_WORLD);
MPI_Bcast(buf, 100, MPI_PACKED, 0, MPI_COMM_WORLD);
}
else {
if (rank == 1) {
printf("BEFORE UNPACK\n");
printf("rank = %d\n", rank);
for (int i = 0; i < 10; i++)
printf("a[%d]=%f ", i, a[i]);
printf("\n");
for (int i = 0; i < 10; i++)
printf("b[%i]=%c ", i, b[i]);
printf("\n-----------------------\n");
}
MPI_Bcast(buf, 100, MPI_PACKED, 0, MPI_COMM_WORLD);
position = 0;
MPI_Unpack(buf, 100, &position, a, 10, MPI_FLOAT, MPI_COMM_WORLD);
MPI_Unpack(buf, 100, &position, b, 10, MPI_CHAR, MPI_COMM_WORLD);
if (rank == 1) {
printf("AFTER UNPACK\n");
printf("rank = %d\n", rank);
for (int i = 0; i < 10; i++)
printf("a[%d]=%f ", i, a[i]);
printf("\n");
for (int i = 0; i < 10; i++)
printf("b[%i]=%c ", i, b[i]);
printf("\n-----------------------\n");
}
}
MPI_Finalize();
}