]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/mpi/src/environment.cpp
1 // Copyright (C) 2005-2006 Douglas Gregor <doug.gregor@gmail.com>
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // Message Passing Interface 1.1 -- 7.1.1. Environmental Inquiries
8 #include <boost/mpi/environment.hpp>
9 #include <boost/mpi/exception.hpp>
10 #include <boost/mpi/detail/mpi_datatype_cache.hpp>
17 namespace boost
{ namespace mpi
{
19 std::istream
& operator>>(std::istream
& in
, level
& l
)
26 } else if (tk
== "funneled") {
28 } else if (tk
== "serialized") {
30 } else if (tk
== "multiple") {
33 in
.setstate(std::ios::badbit
);
39 std::ostream
& operator<<(std::ostream
& out
, level l
)
55 out
<< "<level error>[" << int(l
) << ']';
56 out
.setstate(std::ios::badbit
);
62 } // namespace threading
64 #ifdef BOOST_MPI_HAS_NOARG_INITIALIZATION
65 environment::environment(bool abort_on_exception
)
66 : i_initialized(false),
67 abort_on_exception(abort_on_exception
)
70 BOOST_MPI_CHECK_RESULT(MPI_Init
, (0, 0));
74 #if (2 <= MPI_VERSION)
75 MPI_Comm_set_errhandler(MPI_COMM_WORLD
, MPI_ERRORS_RETURN
);
77 MPI_Errhandler_set(MPI_COMM_WORLD
, MPI_ERRORS_RETURN
);
81 environment::environment(threading::level mt_level
, bool abort_on_exception
)
82 : i_initialized(false),
83 abort_on_exception(abort_on_exception
)
85 // It is not clear that we can pass null in MPI_Init_thread.
86 int dummy_thread_level
= 0;
88 BOOST_MPI_CHECK_RESULT(MPI_Init_thread
,
89 (0, 0, int(mt_level
), &dummy_thread_level
));
93 #if (2 <= MPI_VERSION)
94 MPI_Comm_set_errhandler(MPI_COMM_WORLD
, MPI_ERRORS_RETURN
);
96 MPI_Errhandler_set(MPI_COMM_WORLD
, MPI_ERRORS_RETURN
);
101 environment::environment(int& argc
, char** &argv
, bool abort_on_exception
)
102 : i_initialized(false),
103 abort_on_exception(abort_on_exception
)
105 if (!initialized()) {
106 BOOST_MPI_CHECK_RESULT(MPI_Init
, (&argc
, &argv
));
107 i_initialized
= true;
110 #if (2 <= MPI_VERSION)
111 MPI_Comm_set_errhandler(MPI_COMM_WORLD
, MPI_ERRORS_RETURN
);
113 MPI_Errhandler_set(MPI_COMM_WORLD
, MPI_ERRORS_RETURN
);
117 environment::environment(int& argc
, char** &argv
, threading::level mt_level
,
118 bool abort_on_exception
)
119 : i_initialized(false),
120 abort_on_exception(abort_on_exception
)
122 // It is not clear that we can pass null in MPI_Init_thread.
123 int dummy_thread_level
= 0;
124 if (!initialized()) {
125 BOOST_MPI_CHECK_RESULT(MPI_Init_thread
,
126 (&argc
, &argv
, int(mt_level
), &dummy_thread_level
));
127 i_initialized
= true;
130 #if (2 <= MPI_VERSION)
131 MPI_Comm_set_errhandler(MPI_COMM_WORLD
, MPI_ERRORS_RETURN
);
133 MPI_Errhandler_set(MPI_COMM_WORLD
, MPI_ERRORS_RETURN
);
137 environment::~environment()
140 if (std::uncaught_exception() && abort_on_exception
) {
142 } else if (!finalized()) {
143 detail::mpi_datatype_cache().clear();
144 BOOST_MPI_CHECK_RESULT(MPI_Finalize
, ());
149 void environment::abort(int errcode
)
151 BOOST_MPI_CHECK_RESULT(MPI_Abort
, (MPI_COMM_WORLD
, errcode
));
154 bool environment::initialized()
157 BOOST_MPI_CHECK_RESULT(MPI_Initialized
, (&flag
));
161 bool environment::finalized()
164 BOOST_MPI_CHECK_RESULT(MPI_Finalized
, (&flag
));
168 int environment::max_tag()
173 #if (2 <= MPI_VERSION)
174 BOOST_MPI_CHECK_RESULT(MPI_Comm_get_attr
,
175 (MPI_COMM_WORLD
, MPI_TAG_UB
, &max_tag_value
, &found
));
177 BOOST_MPI_CHECK_RESULT(MPI_Attr_get
,
178 (MPI_COMM_WORLD
, MPI_TAG_UB
, &max_tag_value
, &found
));
181 return *max_tag_value
- num_reserved_tags
;
184 int environment::collectives_tag()
186 return max_tag() + 1;
189 optional
<int> environment::host_rank()
194 #if (2 <= MPI_VERSION)
195 BOOST_MPI_CHECK_RESULT(MPI_Comm_get_attr
,
196 (MPI_COMM_WORLD
, MPI_HOST
, &host
, &found
));
198 BOOST_MPI_CHECK_RESULT(MPI_Attr_get
,
199 (MPI_COMM_WORLD
, MPI_HOST
, &host
, &found
));
201 if (!found
|| *host
== MPI_PROC_NULL
)
202 return optional
<int>();
207 optional
<int> environment::io_rank()
212 #if (2 <= MPI_VERSION)
213 BOOST_MPI_CHECK_RESULT(MPI_Comm_get_attr
,
214 (MPI_COMM_WORLD
, MPI_IO
, &io
, &found
));
216 BOOST_MPI_CHECK_RESULT(MPI_Attr_get
,
217 (MPI_COMM_WORLD
, MPI_IO
, &io
, &found
));
219 if (!found
|| *io
== MPI_PROC_NULL
)
220 return optional
<int>();
225 std::string
environment::processor_name()
227 char name
[MPI_MAX_PROCESSOR_NAME
];
230 BOOST_MPI_CHECK_RESULT(MPI_Get_processor_name
, (name
, &len
));
231 return std::string(name
, len
);
234 threading::level
environment::thread_level()
238 BOOST_MPI_CHECK_RESULT(MPI_Query_thread
, (&level
));
239 return static_cast<threading::level
>(level
);
242 bool environment::is_main_thread()
246 BOOST_MPI_CHECK_RESULT(MPI_Is_thread_main
, (&isit
));
247 return static_cast<bool>(isit
);
250 } } // end namespace boost::mpi