-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPI2Send23.cpp
More file actions
66 lines (62 loc) · 1.29 KB
/
MPI2Send23.cpp
File metadata and controls
66 lines (62 loc) · 1.29 KB
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
64
65
66
#include "pt4.h"
#include "mpi.h"
template<class T>
void send(int t, MPI_Datatype d)
{
int n;
T x;
pt >> n;
T* a = new T[n];
for (int i = 0; i < n; i++)
{
pt >> x;
a[i] = x;
}
MPI_Send(a, n, d, 0, t, MPI_COMM_WORLD);
}
template<class T>
void recv(MPI_Datatype d, MPI_Status s)
{
int size = 0;
if (d == MPI_INT)
size = 4;
else
size = 8;
ShowLine(s.count / size);
T* a = new T[s.count/size];
MPI_Recv(a, s.count / size, d, s.MPI_SOURCE, s.MPI_TAG, MPI_COMM_WORLD, &s);
for(int i = 0; i < s.count / size; i++)
pt << a[i];
}
void Solve()
{
Task("MPI2Send23");
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);
if (rank > 0)
{
int t;
pt >> t;
if (t == 0)
send<int>(t, MPI_INT);
else
send<double>(t, MPI_DOUBLE);
}
else
{
MPI_Status s;
for (int i = 1; i < size; i++)
{
MPI_Probe(i, MPI_ANY_TAG, MPI_COMM_WORLD, &s);
if(s.MPI_TAG == 0)
recv<int>(MPI_INT, s);
else
recv<double>(MPI_DOUBLE, s);
}
}
}