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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
// Copyright (C) 2005-2006 Douglas Gregor <doug.gregor@gmail.com>
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Message Passing Interface 1.1 -- 7.1.1. Environmental Inquiries
#include <boost/mpi/environment.hpp>
#include <boost/mpi/exception.hpp>
#include <boost/mpi/detail/mpi_datatype_cache.hpp>
#include <cassert>
#include <string>
#include <exception>
#include <stdexcept>
#include <ostream>
namespace boost { namespace mpi {
namespace threading {
std::istream& operator>>(std::istream& in, level& l)
{
std::string tk;
in >> tk;
if (!in.bad()) {
if (tk == "single") {
l = single;
} else if (tk == "funneled") {
l = funneled;
} else if (tk == "serialized") {
l = serialized;
} else if (tk == "multiple") {
l = multiple;
} else {
in.setstate(std::ios::badbit);
}
}
return in;
}
std::ostream& operator<<(std::ostream& out, level l)
{
switch(l) {
case single:
out << "single";
break;
case funneled:
out << "funneled";
break;
case serialized:
out << "serialized";
break;
case multiple:
out << "multiple";
break;
default:
out << "<level error>[" << int(l) << ']';
out.setstate(std::ios::badbit);
break;
}
return out;
}
} // namespace threading
#ifdef BOOST_MPI_HAS_NOARG_INITIALIZATION
environment::environment(bool abort_on_exception)
: i_initialized(false),
abort_on_exception(abort_on_exception)
{
if (!initialized()) {
BOOST_MPI_CHECK_RESULT(MPI_Init, (0, 0));
i_initialized = true;
}
MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
}
environment::environment(threading::level mt_level, bool abort_on_exception)
: i_initialized(false),
abort_on_exception(abort_on_exception)
{
// It is not clear that we can pass null in MPI_Init_thread.
int dummy_thread_level = 0;
if (!initialized()) {
BOOST_MPI_CHECK_RESULT(MPI_Init_thread,
(0, 0, int(mt_level), &dummy_thread_level ));
i_initialized = true;
}
MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
}
#endif
environment::environment(int& argc, char** &argv, bool abort_on_exception)
: i_initialized(false),
abort_on_exception(abort_on_exception)
{
if (!initialized()) {
BOOST_MPI_CHECK_RESULT(MPI_Init, (&argc, &argv));
i_initialized = true;
}
MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
}
environment::environment(int& argc, char** &argv, threading::level mt_level,
bool abort_on_exception)
: i_initialized(false),
abort_on_exception(abort_on_exception)
{
// It is not clear that we can pass null in MPI_Init_thread.
int dummy_thread_level = 0;
if (!initialized()) {
BOOST_MPI_CHECK_RESULT(MPI_Init_thread,
(&argc, &argv, int(mt_level), &dummy_thread_level));
i_initialized = true;
}
MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
}
environment::~environment()
{
if (i_initialized) {
if (std::uncaught_exception() && abort_on_exception) {
abort(-1);
} else if (!finalized()) {
detail::mpi_datatype_cache().clear();
BOOST_MPI_CHECK_RESULT(MPI_Finalize, ());
}
}
}
void environment::abort(int errcode)
{
BOOST_MPI_CHECK_RESULT(MPI_Abort, (MPI_COMM_WORLD, errcode));
}
bool environment::initialized()
{
int flag;
BOOST_MPI_CHECK_RESULT(MPI_Initialized, (&flag));
return flag != 0;
}
bool environment::finalized()
{
int flag;
BOOST_MPI_CHECK_RESULT(MPI_Finalized, (&flag));
return flag != 0;
}
int environment::max_tag()
{
int* max_tag_value;
int found = 0;
BOOST_MPI_CHECK_RESULT(MPI_Attr_get,
(MPI_COMM_WORLD, MPI_TAG_UB, &max_tag_value, &found));
assert(found != 0);
return *max_tag_value - num_reserved_tags;
}
int environment::collectives_tag()
{
return max_tag() + 1;
}
optional<int> environment::host_rank()
{
int* host;
int found = 0;
BOOST_MPI_CHECK_RESULT(MPI_Attr_get,
(MPI_COMM_WORLD, MPI_HOST, &host, &found));
if (!found || *host == MPI_PROC_NULL)
return optional<int>();
else
return *host;
}
optional<int> environment::io_rank()
{
int* io;
int found = 0;
BOOST_MPI_CHECK_RESULT(MPI_Attr_get,
(MPI_COMM_WORLD, MPI_IO, &io, &found));
if (!found || *io == MPI_PROC_NULL)
return optional<int>();
else
return *io;
}
std::string environment::processor_name()
{
char name[MPI_MAX_PROCESSOR_NAME];
int len;
BOOST_MPI_CHECK_RESULT(MPI_Get_processor_name, (name, &len));
return std::string(name, len);
}
threading::level environment::thread_level()
{
int level;
BOOST_MPI_CHECK_RESULT(MPI_Query_thread, (&level));
return static_cast<threading::level>(level);
}
bool environment::is_main_thread()
{
int isit;
BOOST_MPI_CHECK_RESULT(MPI_Is_thread_main, (&isit));
return static_cast<bool>(isit);
}
} } // end namespace boost::mpi
|