-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommxx.cc
185 lines (167 loc) · 4.82 KB
/
commxx.cc
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "commxx.h"
#include "parallel_utils.h"
#include <stdexcept>
#include <climits>
// hash is a local function
static size_t
hash(const char * s)
{
size_t h = 37062913;
while (*s)
h = h * 101 + (unsigned char) *s++;
return h;
}
void
Commxx::construct(MPI_Comm const& parent_mpi_comm)
{
MPI_Comm temp_comm;
int error;
if (ranks.size() > 0) {
MPI_Group parent_group, group;
error = MPI_Comm_group(parent_mpi_comm, &parent_group);
if (error != MPI_SUCCESS) {
throw std::runtime_error("MPI error in Commxx(MPI_Comm_group)");
}
error = MPI_Group_incl(parent_group, ranks.size(), &ranks[0], &group);
if (error != MPI_SUCCESS) {
throw std::runtime_error("MPI error in Commxx(MPI_Group_incl)");
}
error = MPI_Comm_create(parent_mpi_comm, group, &temp_comm);
if (error != MPI_SUCCESS) {
throw std::runtime_error("MPI error in Commxx(MPI_Comm_create)");
}
error = MPI_Group_free(&parent_group);
if (error != MPI_SUCCESS) {
throw std::runtime_error(
"MPI error in Commxx(MPI_Group_free(parent))");
}
error = MPI_Group_free(&group);
if (error != MPI_SUCCESS) {
throw std::runtime_error("MPI error in Commxx(MPI_Group_free)");
}
has_this_rank_ = false;
const int no_rank = -1;
int this_rank = no_rank;
if (!parent_sptr) {
this_rank = Commxx().get_rank();
} else {
if (parent_sptr->has_this_rank()) {
this_rank = parent_sptr->get_rank();
}
}
if (this_rank != no_rank) {
for (std::vector<int >::const_iterator it = ranks.begin(); it
!= ranks.end(); ++it) {
if ((*it) == this_rank) {
has_this_rank_ = true;
}
}
}
} else {
temp_comm = parent_mpi_comm;
has_this_rank_ = true;
}
if (per_host && has_this_rank_) {
char name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(name, &name_len);
int color = hash(name) % INT_MAX;
int result = MPI_Comm_split(temp_comm, color, 0, &comm);
if (result != MPI_SUCCESS) throw std::runtime_error(
"MPI error in MPI_Comm_split");
if (ranks.size() > 0) {
error = MPI_Comm_free(&temp_comm);
if (error != MPI_SUCCESS) {
throw std::runtime_error(
"MPI error in Commxx(MPI_Comm_free(temp_comm))");
}
}
} else {
comm = temp_comm;
}
}
Commxx::Commxx() :
comm(MPI_COMM_WORLD ), per_host(false), ranks(0), parent_sptr(), has_this_rank_(
true)
{
}
Commxx::Commxx(bool per_host) :
per_host(per_host), ranks(0), parent_sptr()
{
construct(MPI_COMM_WORLD );
}
Commxx::Commxx(Commxx_sptr parent_sptr, bool per_host) :
per_host(per_host), ranks(0), parent_sptr()
{
construct(parent_sptr->get());
}
Commxx::Commxx(Commxx_sptr parent_sptr, std::vector<int > const& ranks,
bool per_host) :
per_host(per_host), ranks(ranks), parent_sptr(parent_sptr)
{
construct(parent_sptr->get());
}
Commxx_sptr
Commxx::get_parent_sptr() const
{
return parent_sptr;
}
int
Commxx::get_rank() const
{
int error, rank;
error = MPI_Comm_rank(comm, &rank);
if (error != MPI_SUCCESS) {
throw std::runtime_error("MPI error in MPI_Comm_rank");
}
return rank;
}
int
Commxx::get_size() const
{
int error, size;
error = MPI_Comm_size(comm, &size);
if (error != MPI_SUCCESS) {
throw std::runtime_error("MPI error in MPI_Comm_size");
}
return size;
}
bool
Commxx::has_this_rank() const
{
return has_this_rank_;
}
MPI_Comm
Commxx::get() const
{
return comm;
}
Commxx::~Commxx()
{
if (((ranks.size() > 0) || per_host) && has_this_rank_) {
int error = MPI_Comm_free(&comm);
if (error != MPI_SUCCESS) {
// throw std::runtime_error("MPI error in Commxx(MPI_Comm_free)");
}
}
}
Commxx_sptr
make_optimal_spc_comm(Commxx_sptr comm_sptr, int optimal_number, bool equally_spread)
{
int optimal_size=std::min(optimal_number, comm_sptr->get_size());
std::vector<int > on_ranks(optimal_size);
int start_rank;
if (equally_spread){
if ((comm_sptr->get_size() % optimal_size) !=0)
throw std::runtime_error("make_optimal_spc_comm, for equal_spread the subsize is not a divider of comm size");
start_rank=comm_sptr->get_rank()/optimal_size;
}
else{
start_rank=0;
}
for (int i=0; i<optimal_size;++i){
on_ranks[i]=i+start_rank*optimal_size;
}
Commxx_sptr ret_comm_sptr(new Commxx(comm_sptr,on_ranks));
return ret_comm_sptr;
}