-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPI4Type12.cpp
More file actions
50 lines (42 loc) · 989 Bytes
/
MPI4Type12.cpp
File metadata and controls
50 lines (42 loc) · 989 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "pt4.h"
#include "mpi.h"
void Solve()
{
Task("MPI4Type12");
int flag;
MPI_Initialized(&flag);
if (flag == 0)
return;
int rank, size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int sz = sizeof(int) * 2 + sizeof(double);
char* rbuf = new char[sz];
int pos;
double d;
int a[2];
if (rank != 0)
{
for (int i = 0; i < 2; i++)
pt >> a[i];
pt >> d;
pos = 0;
MPI_Pack(a, 2, MPI_INT, rbuf, sz, &pos, MPI_COMM_WORLD);
MPI_Pack(&d, 1, MPI_DOUBLE, rbuf, sz, &pos, MPI_COMM_WORLD);
}
int all_sz = sz * (size - 1);
char* unpBUF = new char[all_sz];
MPI_Gather(rbuf, sz, MPI_PACKED, unpBUF, sz, MPI_PACKED, 0, MPI_COMM_WORLD);
if (rank == 0)
{
pos = sz;
for (int i = 1; i < size; i++)
{
MPI_Unpack(unpBUF, sz, &pos, a, 2, MPI_INT, MPI_COMM_WORLD);
MPI_Unpack(unpBUF, sz, &pos, &d, 1, MPI_DOUBLE, MPI_COMM_WORLD);
for (int i = 0; i < 2; i++)
pt << a[i];
pt << d;
}
}
}