-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPI5Comm32.cpp
More file actions
88 lines (71 loc) · 2.06 KB
/
MPI5Comm32.cpp
File metadata and controls
88 lines (71 loc) · 2.06 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "pt4.h"
#include "mpi.h"
void Solve()
{
Task("MPI5Comm32");
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 n = size / 2;
int* sources = new int[n], * degrees = new int[n];
for (int i = 0; i < n; i++)
sources[i] = i;
for (int i = 0; i < n; i++)
{
if (i == n - 1 && size % 2 == 0)
degrees[i] = 1;
else
if (i == n - 1 && size % 2 == 1)
degrees[i] = 2;
else
degrees[i] = 2;
}
int x = 0;
for (int i = 0; i < n; i++)
x += degrees[i];
int* destinations = new int[x];
for (int i = 0; i < x; i++)
destinations[i] = i + 1;
/* for (int i = 0; i < n; i++)
{
Show(sources[i]);
}
ShowLine();
for (int i = 0; i < n; i++)
{
Show(degrees[i]);
}
ShowLine();
for (int i = 0; i < x; i++)
Show(destinations[i]);
ShowLine();*/
MPI_Comm distcomm;
if(rank == 0)
MPI_Dist_graph_create(MPI_COMM_WORLD, n, &sources[0], °rees[0], &destinations[0], MPI_UNWEIGHTED, MPI_INFO_NULL, 0, &distcomm);
else
MPI_Dist_graph_create(MPI_COMM_WORLD, 0, &sources[0], °rees[0], &destinations[0], MPI_UNWEIGHTED, MPI_INFO_NULL, 0, &distcomm);
int indegree, outdegree, w;
MPI_Dist_graph_neighbors_count(distcomm, &indegree, &outdegree, &w);
int* newsources = new int[indegree];
int* newdestinations = new int[outdegree];
MPI_Dist_graph_neighbors(distcomm, indegree, &newsources[0], MPI_UNWEIGHTED, outdegree, &newdestinations[0], MPI_UNWEIGHTED);
int a = 0, sum = 0;
pt >> a;
sum = a;
for (int i = 0; i < indegree; i++)
{
MPI_Status s;
int x;
MPI_Recv(&x, 1, MPI_INT, newsources[i], 0, distcomm, &s);
sum += x;
}
for (int i = 0; i < outdegree; i++)
{
MPI_Send(&sum, 1, MPI_INT, newdestinations[i], 0, distcomm);
}
pt << sum;
}