communicator.hpp 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  1. // Copyright (C) 2005, 2006 Douglas Gregor <doug.gregor -at- gmail.com>.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. /** @file communicator.hpp
  6. *
  7. * This header defines the @c communicator class, which is the basis
  8. * of all communication within Boost.MPI, and provides point-to-point
  9. * communication operations.
  10. */
  11. #ifndef BOOST_MPI_COMMUNICATOR_HPP
  12. #define BOOST_MPI_COMMUNICATOR_HPP
  13. #include <boost/assert.hpp>
  14. #include <boost/mpi/config.hpp>
  15. #include <boost/mpi/exception.hpp>
  16. #include <boost/optional.hpp>
  17. #include <boost/shared_ptr.hpp>
  18. #include <boost/mpi/datatype.hpp>
  19. #include <utility>
  20. #include <iterator>
  21. #include <stdexcept> // for std::range_error
  22. // For (de-)serializing sends and receives
  23. #include <boost/mpi/packed_oarchive.hpp>
  24. #include <boost/mpi/packed_iarchive.hpp>
  25. // For (de-)serializing skeletons and content
  26. #include <boost/mpi/skeleton_and_content_fwd.hpp>
  27. // For (de-)serializing arrays
  28. #include <boost/serialization/array.hpp>
  29. #include <boost/mpi/detail/point_to_point.hpp>
  30. #include <boost/mpi/status.hpp>
  31. #include <boost/mpi/request.hpp>
  32. #ifdef BOOST_MSVC
  33. # pragma warning(push)
  34. # pragma warning(disable : 4800) // forcing to bool 'true' or 'false'
  35. #endif
  36. namespace boost { namespace mpi {
  37. /**
  38. * @brief A constant representing "any process."
  39. *
  40. * This constant may be used for the @c source parameter of @c receive
  41. * operations to indicate that a message may be received from any
  42. * source.
  43. */
  44. const int any_source = MPI_ANY_SOURCE;
  45. /**
  46. * @brief A constant representing "any tag."
  47. *
  48. * This constant may be used for the @c tag parameter of @c receive
  49. * operations to indicate that a @c send with any tag will be matched
  50. * by the receive.
  51. */
  52. const int any_tag = MPI_ANY_TAG;
  53. /**
  54. * @brief Enumeration used to describe how to adopt a C @c MPI_Comm into
  55. * a Boost.MPI communicator.
  56. *
  57. * The values for this enumeration determine how a Boost.MPI
  58. * communicator will behave when constructed with an MPI
  59. * communicator. The options are:
  60. *
  61. * - @c comm_duplicate: Duplicate the MPI_Comm communicator to
  62. * create a new communicator (e.g., with MPI_Comm_dup). This new
  63. * MPI_Comm communicator will be automatically freed when the
  64. * Boost.MPI communicator (and all copies of it) is destroyed.
  65. *
  66. * - @c comm_take_ownership: Take ownership of the communicator. It
  67. * will be freed automatically when all of the Boost.MPI
  68. * communicators go out of scope. This option must not be used with
  69. * MPI_COMM_WORLD.
  70. *
  71. * - @c comm_attach: The Boost.MPI communicator will reference the
  72. * existing MPI communicator but will not free it when the Boost.MPI
  73. * communicator goes out of scope. This option should only be used
  74. * when the communicator is managed by the user or MPI library
  75. * (e.g., MPI_COMM_WORLD).
  76. */
  77. enum comm_create_kind { comm_duplicate, comm_take_ownership, comm_attach };
  78. /**
  79. * INTERNAL ONLY
  80. *
  81. * Forward declaration of @c group needed for the @c group
  82. * constructor and accessor.
  83. */
  84. class group;
  85. /**
  86. * INTERNAL ONLY
  87. *
  88. * Forward declaration of @c intercommunicator needed for the "cast"
  89. * from a communicator to an intercommunicator.
  90. */
  91. class intercommunicator;
  92. /**
  93. * INTERNAL ONLY
  94. *
  95. * Forward declaration of @c graph_communicator needed for the "cast"
  96. * from a communicator to a graph communicator.
  97. */
  98. class graph_communicator;
  99. /**
  100. * @brief A communicator that permits communication and
  101. * synchronization among a set of processes.
  102. *
  103. * The @c communicator class abstracts a set of communicating
  104. * processes in MPI. All of the processes that belong to a certain
  105. * communicator can determine the size of the communicator, their rank
  106. * within the communicator, and communicate with any other processes
  107. * in the communicator.
  108. */
  109. class BOOST_MPI_DECL communicator
  110. {
  111. public:
  112. /**
  113. * Build a new Boost.MPI communicator for @c MPI_COMM_WORLD.
  114. *
  115. * Constructs a Boost.MPI communicator that attaches to @c
  116. * MPI_COMM_WORLD. This is the equivalent of constructing with
  117. * @c (MPI_COMM_WORLD, comm_attach).
  118. */
  119. communicator();
  120. /**
  121. * Build a new Boost.MPI communicator based on the MPI communicator
  122. * @p comm.
  123. *
  124. * @p comm may be any valid MPI communicator. If @p comm is
  125. * MPI_COMM_NULL, an empty communicator (that cannot be used for
  126. * communication) is created and the @p kind parameter is
  127. * ignored. Otherwise, the @p kind parameters determines how the
  128. * Boost.MPI communicator will be related to @p comm:
  129. *
  130. * - If @p kind is @c comm_duplicate, duplicate @c comm to create
  131. * a new communicator. This new communicator will be freed when
  132. * the Boost.MPI communicator (and all copies of it) is destroyed.
  133. * This option is only permitted if @p comm is a valid MPI
  134. * intracommunicator or if the underlying MPI implementation
  135. * supports MPI 2.0 (which supports duplication of
  136. * intercommunicators).
  137. *
  138. * - If @p kind is @c comm_take_ownership, take ownership of @c
  139. * comm. It will be freed automatically when all of the Boost.MPI
  140. * communicators go out of scope. This option must not be used
  141. * when @c comm is MPI_COMM_WORLD.
  142. *
  143. * - If @p kind is @c comm_attach, this Boost.MPI communicator
  144. * will reference the existing MPI communicator @p comm but will
  145. * not free @p comm when the Boost.MPI communicator goes out of
  146. * scope. This option should only be used when the communicator is
  147. * managed by the user or MPI library (e.g., MPI_COMM_WORLD).
  148. */
  149. communicator(const MPI_Comm& comm, comm_create_kind kind);
  150. /**
  151. * Build a new Boost.MPI communicator based on a subgroup of another
  152. * MPI communicator.
  153. *
  154. * This routine will construct a new communicator containing all of
  155. * the processes from communicator @c comm that are listed within
  156. * the group @c subgroup. Equivalent to @c MPI_Comm_create.
  157. *
  158. * @param comm An MPI communicator.
  159. *
  160. * @param subgroup A subgroup of the MPI communicator, @p comm, for
  161. * which we will construct a new communicator.
  162. */
  163. communicator(const communicator& comm, const boost::mpi::group& subgroup);
  164. /**
  165. * @brief Determine the rank of the executing process in a
  166. * communicator.
  167. *
  168. * This routine is equivalent to @c MPI_Comm_rank.
  169. *
  170. * @returns The rank of the process in the communicator, which
  171. * will be a value in [0, size())
  172. */
  173. int rank() const;
  174. /**
  175. * @brief Determine the number of processes in a communicator.
  176. *
  177. * This routine is equivalent to @c MPI_Comm_size.
  178. *
  179. * @returns The number of processes in the communicator.
  180. */
  181. int size() const;
  182. /**
  183. * This routine constructs a new group whose members are the
  184. * processes within this communicator. Equivalent to
  185. * calling @c MPI_Comm_group.
  186. */
  187. boost::mpi::group group() const;
  188. // ----------------------------------------------------------------
  189. // Point-to-point communication
  190. // ----------------------------------------------------------------
  191. /**
  192. * @brief Send data to another process.
  193. *
  194. * This routine executes a potentially blocking send with tag @p tag
  195. * to the process with rank @p dest. It can be received by the
  196. * destination process with a matching @c recv call.
  197. *
  198. * The given @p value must be suitable for transmission over
  199. * MPI. There are several classes of types that meet these
  200. * requirements:
  201. *
  202. * - Types with mappings to MPI data types: If @c
  203. * is_mpi_datatype<T> is convertible to @c mpl::true_, then @p
  204. * value will be transmitted using the MPI data type
  205. * @c get_mpi_datatype<T>(). All primitive C++ data types that have
  206. * MPI equivalents, e.g., @c int, @c float, @c char, @c double,
  207. * etc., have built-in mappings to MPI data types. You may turn a
  208. * Serializable type with fixed structure into an MPI data type by
  209. * specializing @c is_mpi_datatype for your type.
  210. *
  211. * - Serializable types: Any type that provides the @c serialize()
  212. * functionality required by the Boost.Serialization library can be
  213. * transmitted and received.
  214. *
  215. * - Packed archives and skeletons: Data that has been packed into
  216. * an @c mpi::packed_oarchive or the skeletons of data that have
  217. * been backed into an @c mpi::packed_skeleton_oarchive can be
  218. * transmitted, but will be received as @c mpi::packed_iarchive and
  219. * @c mpi::packed_skeleton_iarchive, respectively, to allow the
  220. * values (or skeletons) to be extracted by the destination process.
  221. *
  222. * - Content: Content associated with a previously-transmitted
  223. * skeleton can be transmitted by @c send and received by @c
  224. * recv. The receiving process may only receive content into the
  225. * content of a value that has been constructed with the matching
  226. * skeleton.
  227. *
  228. * For types that have mappings to an MPI data type (including the
  229. * concent of a type), an invocation of this routine will result in
  230. * a single MPI_Send call. For variable-length data, e.g.,
  231. * serialized types and packed archives, two messages will be sent
  232. * via MPI_Send: one containing the length of the data and the
  233. * second containing the data itself. Note that the transmission
  234. * mode for variable-length data is an implementation detail that
  235. * is subject to change.
  236. *
  237. * @param dest The rank of the remote process to which the data
  238. * will be sent.
  239. *
  240. * @param tag The tag that will be associated with this message. Tags
  241. * may be any integer between zero and an implementation-defined
  242. * upper limit. This limit is accessible via @c environment::max_tag().
  243. *
  244. * @param value The value that will be transmitted to the
  245. * receiver. The type @c T of this value must meet the aforementioned
  246. * criteria for transmission.
  247. */
  248. template<typename T>
  249. void send(int dest, int tag, const T& value) const;
  250. /**
  251. * @brief Send the skeleton of an object.
  252. *
  253. * This routine executes a potentially blocking send with tag @p
  254. * tag to the process with rank @p dest. It can be received by the
  255. * destination process with a matching @c recv call. This variation
  256. * on @c send will be used when a send of a skeleton is explicitly
  257. * requested via code such as:
  258. *
  259. * @code
  260. * comm.send(dest, tag, skeleton(object));
  261. * @endcode
  262. *
  263. * The semantics of this routine are equivalent to that of sending
  264. * a @c packed_skeleton_oarchive storing the skeleton of the @c
  265. * object.
  266. *
  267. * @param dest The rank of the remote process to which the skeleton
  268. * will be sent.
  269. *
  270. * @param tag The tag that will be associated with this message. Tags
  271. * may be any integer between zero and an implementation-defined
  272. * upper limit. This limit is accessible via @c environment::max_tag().
  273. *
  274. * @param proxy The @c skeleton_proxy containing a reference to the
  275. * object whose skeleton will be transmitted.
  276. *
  277. */
  278. template<typename T>
  279. void send(int dest, int tag, const skeleton_proxy<T>& proxy) const;
  280. /**
  281. * @brief Send an array of values to another process.
  282. *
  283. * This routine executes a potentially blocking send of an array of
  284. * data with tag @p tag to the process with rank @p dest. It can be
  285. * received by the destination process with a matching array @c
  286. * recv call.
  287. *
  288. * If @c T is an MPI datatype, an invocation of this routine will
  289. * be mapped to a single call to MPI_Send, using the datatype @c
  290. * get_mpi_datatype<T>().
  291. *
  292. * @param dest The process rank of the remote process to which
  293. * the data will be sent.
  294. *
  295. * @param tag The tag that will be associated with this message. Tags
  296. * may be any integer between zero and an implementation-defined
  297. * upper limit. This limit is accessible via @c environment::max_tag().
  298. *
  299. * @param values The array of values that will be transmitted to the
  300. * receiver. The type @c T of these values must be mapped to an MPI
  301. * data type.
  302. *
  303. * @param n The number of values stored in the array. The destination
  304. * process must call receive with at least this many elements to
  305. * correctly receive the message.
  306. */
  307. template<typename T>
  308. void send(int dest, int tag, const T* values, int n) const;
  309. /**
  310. * @brief Send a message to another process without any data.
  311. *
  312. * This routine executes a potentially blocking send of a message
  313. * to another process. The message contains no extra data, and can
  314. * therefore only be received by a matching call to @c recv().
  315. *
  316. * @param dest The process rank of the remote process to which
  317. * the message will be sent.
  318. *
  319. * @param tag The tag that will be associated with this message. Tags
  320. * may be any integer between zero and an implementation-defined
  321. * upper limit. This limit is accessible via @c environment::max_tag().
  322. *
  323. */
  324. void send(int dest, int tag) const;
  325. /**
  326. * @brief Receive data from a remote process.
  327. *
  328. * This routine blocks until it receives a message from the process @p
  329. * source with the given @p tag. The type @c T of the @p value must be
  330. * suitable for transmission over MPI, which includes serializable
  331. * types, types that can be mapped to MPI data types (including most
  332. * built-in C++ types), packed MPI archives, skeletons, and content
  333. * associated with skeletons; see the documentation of @c send for a
  334. * complete description.
  335. *
  336. * @param source The process that will be sending data. This will
  337. * either be a process rank within the communicator or the
  338. * constant @c any_source, indicating that we can receive the
  339. * message from any process.
  340. *
  341. * @param tag The tag that matches a particular kind of message sent
  342. * by the source process. This may be any tag value permitted by @c
  343. * send. Alternatively, the argument may be the constant @c any_tag,
  344. * indicating that this receive matches a message with any tag.
  345. *
  346. * @param value Will contain the value of the message after a
  347. * successful receive. The type of this value must match the value
  348. * transmitted by the sender, unless the sender transmitted a packed
  349. * archive or skeleton: in these cases, the sender transmits a @c
  350. * packed_oarchive or @c packed_skeleton_oarchive and the
  351. * destination receives a @c packed_iarchive or @c
  352. * packed_skeleton_iarchive, respectively.
  353. *
  354. * @returns Information about the received message.
  355. */
  356. template<typename T>
  357. status recv(int source, int tag, T& value) const;
  358. /**
  359. * @brief Receive a skeleton from a remote process.
  360. *
  361. * This routine blocks until it receives a message from the process @p
  362. * source with the given @p tag containing a skeleton.
  363. *
  364. * @param source The process that will be sending data. This will
  365. * either be a process rank within the communicator or the constant
  366. * @c any_source, indicating that we can receive the message from
  367. * any process.
  368. *
  369. * @param tag The tag that matches a particular kind of message
  370. * sent by the source process. This may be any tag value permitted
  371. * by @c send. Alternatively, the argument may be the constant @c
  372. * any_tag, indicating that this receive matches a message with any
  373. * tag.
  374. *
  375. * @param proxy The @c skeleton_proxy containing a reference to the
  376. * object that will be reshaped to match the received skeleton.
  377. *
  378. * @returns Information about the received message.
  379. */
  380. template<typename T>
  381. status recv(int source, int tag, const skeleton_proxy<T>& proxy) const;
  382. /**
  383. * @brief Receive a skeleton from a remote process.
  384. *
  385. * This routine blocks until it receives a message from the process @p
  386. * source with the given @p tag containing a skeleton.
  387. *
  388. * @param source The process that will be sending data. This will
  389. * either be a process rank within the communicator or the constant
  390. * @c any_source, indicating that we can receive the message from
  391. * any process.
  392. *
  393. * @param tag The tag that matches a particular kind of message
  394. * sent by the source process. This may be any tag value permitted
  395. * by @c send. Alternatively, the argument may be the constant @c
  396. * any_tag, indicating that this receive matches a message with any
  397. * tag.
  398. *
  399. * @param proxy The @c skeleton_proxy containing a reference to the
  400. * object that will be reshaped to match the received skeleton.
  401. *
  402. * @returns Information about the received message.
  403. */
  404. template<typename T>
  405. status recv(int source, int tag, skeleton_proxy<T>& proxy) const;
  406. /**
  407. * @brief Receive an array of values from a remote process.
  408. *
  409. * This routine blocks until it receives an array of values from the
  410. * process @p source with the given @p tag. If the type @c T is
  411. *
  412. * @param source The process that will be sending data. This will
  413. * either be a process rank within the communicator or the
  414. * constant @c any_source, indicating that we can receive the
  415. * message from any process.
  416. *
  417. * @param tag The tag that matches a particular kind of message sent
  418. * by the source process. This may be any tag value permitted by @c
  419. * send. Alternatively, the argument may be the constant @c any_tag,
  420. * indicating that this receive matches a message with any tag.
  421. *
  422. * @param values Will contain the values in the message after a
  423. * successful receive. The type of these elements must match the
  424. * type of the elements transmitted by the sender.
  425. *
  426. * @param n The number of values that can be stored into the @p
  427. * values array. This shall not be smaller than the number of
  428. * elements transmitted by the sender.
  429. *
  430. * @throws std::range_error if the message to be received contains
  431. * more than @p n values.
  432. *
  433. * @returns Information about the received message.
  434. */
  435. template<typename T>
  436. status recv(int source, int tag, T* values, int n) const;
  437. /**
  438. * @brief Receive a message from a remote process without any data.
  439. *
  440. * This routine blocks until it receives a message from the process
  441. * @p source with the given @p tag.
  442. *
  443. * @param source The process that will be sending the message. This
  444. * will either be a process rank within the communicator or the
  445. * constant @c any_source, indicating that we can receive the
  446. * message from any process.
  447. *
  448. * @param tag The tag that matches a particular kind of message
  449. * sent by the source process. This may be any tag value permitted
  450. * by @c send. Alternatively, the argument may be the constant @c
  451. * any_tag, indicating that this receive matches a message with any
  452. * tag.
  453. *
  454. * @returns Information about the received message.
  455. */
  456. status recv(int source, int tag) const;
  457. /**
  458. * @brief Send a message to a remote process without blocking.
  459. *
  460. * The @c isend method is functionality identical to the @c send
  461. * method and transmits data in the same way, except that @c isend
  462. * will not block while waiting for the data to be
  463. * transmitted. Instead, a request object will be immediately
  464. * returned, allowing one to query the status of the communication
  465. * or wait until it has completed.
  466. *
  467. * @param dest The rank of the remote process to which the data
  468. * will be sent.
  469. *
  470. * @param tag The tag that will be associated with this message. Tags
  471. * may be any integer between zero and an implementation-defined
  472. * upper limit. This limit is accessible via @c environment::max_tag().
  473. *
  474. * @param value The value that will be transmitted to the
  475. * receiver. The type @c T of this value must meet the aforementioned
  476. * criteria for transmission.
  477. *
  478. * @returns a @c request object that describes this communication.
  479. */
  480. template<typename T>
  481. request isend(int dest, int tag, const T& value) const;
  482. /**
  483. * @brief Send the skeleton of an object without blocking.
  484. *
  485. * This routine is functionally identical to the @c send method for
  486. * @c skeleton_proxy objects except that @c isend will not block
  487. * while waiting for the data to be transmitted. Instead, a request
  488. * object will be immediately returned, allowing one to query the
  489. * status of the communication or wait until it has completed.
  490. *
  491. * The semantics of this routine are equivalent to a non-blocking
  492. * send of a @c packed_skeleton_oarchive storing the skeleton of
  493. * the @c object.
  494. *
  495. * @param dest The rank of the remote process to which the skeleton
  496. * will be sent.
  497. *
  498. * @param tag The tag that will be associated with this message. Tags
  499. * may be any integer between zero and an implementation-defined
  500. * upper limit. This limit is accessible via @c environment::max_tag().
  501. *
  502. * @param proxy The @c skeleton_proxy containing a reference to the
  503. * object whose skeleton will be transmitted.
  504. *
  505. * @returns a @c request object that describes this communication.
  506. */
  507. template<typename T>
  508. request isend(int dest, int tag, const skeleton_proxy<T>& proxy) const;
  509. /**
  510. * @brief Send an array of values to another process without
  511. * blocking.
  512. *
  513. * This routine is functionally identical to the @c send method for
  514. * arrays except that @c isend will not block while waiting for the
  515. * data to be transmitted. Instead, a request object will be
  516. * immediately returned, allowing one to query the status of the
  517. * communication or wait until it has completed.
  518. *
  519. * @param dest The process rank of the remote process to which
  520. * the data will be sent.
  521. *
  522. * @param tag The tag that will be associated with this message. Tags
  523. * may be any integer between zero and an implementation-defined
  524. * upper limit. This limit is accessible via @c environment::max_tag().
  525. *
  526. * @param values The array of values that will be transmitted to the
  527. * receiver. The type @c T of these values must be mapped to an MPI
  528. * data type.
  529. *
  530. * @param n The number of values stored in the array. The destination
  531. * process must call receive with at least this many elements to
  532. * correctly receive the message.
  533. *
  534. * @returns a @c request object that describes this communication.
  535. */
  536. template<typename T>
  537. request isend(int dest, int tag, const T* values, int n) const;
  538. /**
  539. * @brief Send a message to another process without any data
  540. * without blocking.
  541. *
  542. * This routine is functionally identical to the @c send method for
  543. * sends with no data, except that @c isend will not block while
  544. * waiting for the message to be transmitted. Instead, a request
  545. * object will be immediately returned, allowing one to query the
  546. * status of the communication or wait until it has completed.
  547. *
  548. * @param dest The process rank of the remote process to which
  549. * the message will be sent.
  550. *
  551. * @param tag The tag that will be associated with this message. Tags
  552. * may be any integer between zero and an implementation-defined
  553. * upper limit. This limit is accessible via @c environment::max_tag().
  554. *
  555. *
  556. * @returns a @c request object that describes this communication.
  557. */
  558. request isend(int dest, int tag) const;
  559. /**
  560. * @brief Prepare to receive a message from a remote process.
  561. *
  562. * The @c irecv method is functionally identical to the @c recv
  563. * method and receive data in the same way, except that @c irecv
  564. * will not block while waiting for data to be
  565. * transmitted. Instead, it immediately returns a request object
  566. * that allows one to query the status of the receive or wait until
  567. * it has completed.
  568. *
  569. * @param source The process that will be sending data. This will
  570. * either be a process rank within the communicator or the
  571. * constant @c any_source, indicating that we can receive the
  572. * message from any process.
  573. *
  574. * @param tag The tag that matches a particular kind of message sent
  575. * by the source process. This may be any tag value permitted by @c
  576. * send. Alternatively, the argument may be the constant @c any_tag,
  577. * indicating that this receive matches a message with any tag.
  578. *
  579. * @param value Will contain the value of the message after a
  580. * successful receive. The type of this value must match the value
  581. * transmitted by the sender, unless the sender transmitted a packed
  582. * archive or skeleton: in these cases, the sender transmits a @c
  583. * packed_oarchive or @c packed_skeleton_oarchive and the
  584. * destination receives a @c packed_iarchive or @c
  585. * packed_skeleton_iarchive, respectively.
  586. *
  587. * @returns a @c request object that describes this communication.
  588. */
  589. template<typename T>
  590. request irecv(int source, int tag, T& value) const;
  591. /**
  592. * @brief Initiate receipt of an array of values from a remote process.
  593. *
  594. * This routine initiates a receive operation for an array of values
  595. * transmitted by process @p source with the given @p tag.
  596. *
  597. * @param source The process that will be sending data. This will
  598. * either be a process rank within the communicator or the
  599. * constant @c any_source, indicating that we can receive the
  600. * message from any process.
  601. *
  602. * @param tag The tag that matches a particular kind of message sent
  603. * by the source process. This may be any tag value permitted by @c
  604. * send. Alternatively, the argument may be the constant @c any_tag,
  605. * indicating that this receive matches a message with any tag.
  606. *
  607. * @param values Will contain the values in the message after a
  608. * successful receive. The type of these elements must match the
  609. * type of the elements transmitted by the sender.
  610. *
  611. * @param n The number of values that can be stored into the @p
  612. * values array. This shall not be smaller than the number of
  613. * elements transmitted by the sender.
  614. *
  615. * @returns a @c request object that describes this communication.
  616. */
  617. template<typename T>
  618. request irecv(int source, int tag, T* values, int n) const;
  619. /**
  620. * @brief Initiate receipt of a message from a remote process that
  621. * carries no data.
  622. *
  623. * This routine initiates a receive operation for a message from
  624. * process @p source with the given @p tag that carries no data.
  625. *
  626. * @param source The process that will be sending the message. This
  627. * will either be a process rank within the communicator or the
  628. * constant @c any_source, indicating that we can receive the
  629. * message from any process.
  630. *
  631. * @param tag The tag that matches a particular kind of message
  632. * sent by the source process. This may be any tag value permitted
  633. * by @c send. Alternatively, the argument may be the constant @c
  634. * any_tag, indicating that this receive matches a message with any
  635. * tag.
  636. *
  637. * @returns a @c request object that describes this communication.
  638. */
  639. request irecv(int source, int tag) const;
  640. /**
  641. * @brief Waits until a message is available to be received.
  642. *
  643. * This operation waits until a message matching (@p source, @p tag)
  644. * is available to be received. It then returns information about
  645. * that message. The functionality is equivalent to @c MPI_Probe. To
  646. * check if a message is available without blocking, use @c iprobe.
  647. *
  648. * @param source Determine if there is a message available from
  649. * this rank. If @c any_source, then the message returned may come
  650. * from any source.
  651. *
  652. * @param tag Determine if there is a message available with the
  653. * given tag. If @c any_tag, then the message returned may have any
  654. * tag.
  655. *
  656. * @returns Returns information about the first message that
  657. * matches the given criteria.
  658. */
  659. status probe(int source = any_source, int tag = any_tag) const;
  660. /**
  661. * @brief Determine if a message is available to be received.
  662. *
  663. * This operation determines if a message matching (@p source, @p
  664. * tag) is available to be received. If so, it returns information
  665. * about that message; otherwise, it returns immediately with an
  666. * empty optional. The functionality is equivalent to @c
  667. * MPI_Iprobe. To wait until a message is available, use @c wait.
  668. *
  669. * @param source Determine if there is a message available from
  670. * this rank. If @c any_source, then the message returned may come
  671. * from any source.
  672. *
  673. * @param tag Determine if there is a message available with the
  674. * given tag. If @c any_tag, then the message returned may have any
  675. * tag.
  676. *
  677. * @returns If a matching message is available, returns
  678. * information about that message. Otherwise, returns an empty
  679. * @c boost::optional.
  680. */
  681. optional<status>
  682. iprobe(int source = any_source, int tag = any_tag) const;
  683. #ifdef barrier
  684. // Linux defines a function-like macro named "barrier". So, we need
  685. // to avoid expanding the macro when we define our barrier()
  686. // function. However, some C++ parsers (Doxygen, for instance) can't
  687. // handle this syntax, so we only use it when necessary.
  688. void (barrier)() const;
  689. #else
  690. /**
  691. * @brief Wait for all processes within a communicator to reach the
  692. * barrier.
  693. *
  694. * This routine is a collective operation that blocks each process
  695. * until all processes have entered it, then releases all of the
  696. * processes "simultaneously". It is equivalent to @c MPI_Barrier.
  697. */
  698. void barrier() const;
  699. #endif
  700. /** @brief Determine if this communicator is valid for
  701. * communication.
  702. *
  703. * Evaluates @c true in a boolean context if this communicator is
  704. * valid for communication, i.e., does not represent
  705. * MPI_COMM_NULL. Otherwise, evaluates @c false.
  706. */
  707. operator bool() const { return (bool)comm_ptr; }
  708. /**
  709. * @brief Access the MPI communicator associated with a Boost.MPI
  710. * communicator.
  711. *
  712. * This routine permits the implicit conversion from a Boost.MPI
  713. * communicator to an MPI communicator.
  714. *
  715. * @returns The associated MPI communicator.
  716. */
  717. operator MPI_Comm() const;
  718. /**
  719. * Split the communicator into multiple, disjoint communicators
  720. * each of which is based on a particular color. This is a
  721. * collective operation that returns a new communicator that is a
  722. * subgroup of @p this. This routine is functionally equivalent to
  723. * @c MPI_Comm_split.
  724. *
  725. * @param color The color of this process. All processes with the
  726. * same @p color value will be placed into the same group.
  727. *
  728. * @returns A new communicator containing all of the processes in
  729. * @p this that have the same @p color.
  730. */
  731. communicator split(int color) const;
  732. /**
  733. * Split the communicator into multiple, disjoint communicators
  734. * each of which is based on a particular color. This is a
  735. * collective operation that returns a new communicator that is a
  736. * subgroup of @p this. This routine is functionally equivalent to
  737. * @c MPI_Comm_split.
  738. *
  739. * @param color The color of this process. All processes with the
  740. * same @p color value will be placed into the same group.
  741. *
  742. * @param key A key value that will be used to determine the
  743. * ordering of processes with the same color in the resulting
  744. * communicator. If omitted, the rank of the processes in @p this
  745. * will determine the ordering of processes in the resulting
  746. * group.
  747. *
  748. * @returns A new communicator containing all of the processes in
  749. * @p this that have the same @p color.
  750. */
  751. communicator split(int color, int key) const;
  752. /**
  753. * Determine if the communicator is in fact an intercommunicator
  754. * and, if so, return that intercommunicator.
  755. *
  756. * @returns an @c optional containing the intercommunicator, if this
  757. * communicator is in fact an intercommunicator. Otherwise, returns
  758. * an empty @c optional.
  759. */
  760. optional<intercommunicator> as_intercommunicator() const;
  761. /**
  762. * Determine if the communicator has a graph topology and, if so,
  763. * return that @c graph_communicator. Even though the communicators
  764. * have different types, they refer to the same underlying
  765. * communication space and can be used interchangeably for
  766. * communication.
  767. *
  768. * @returns an @c optional containing the graph communicator, if this
  769. * communicator does in fact have a graph topology. Otherwise, returns
  770. * an empty @c optional.
  771. */
  772. optional<graph_communicator> as_graph_communicator() const;
  773. /**
  774. * Determines whether this communicator has a Cartesian topology.
  775. */
  776. bool has_cartesian_topology() const;
  777. #if 0
  778. template<typename Extents>
  779. communicator
  780. with_cartesian_topology(const Extents& extents,
  781. bool periodic = false,
  782. bool reorder = false) const;
  783. template<typename DimInputIterator, typename PeriodicInputIterator>
  784. communicator
  785. with_cartesian_topology(DimInputIterator first_dim,
  786. DimInputIterator last_dim,
  787. PeriodicInputIterator first_periodic,
  788. bool reorder = false);
  789. template<typename Allocator, std::size_t NumDims>
  790. communicator
  791. with_cartesian_topology(const multi_array<bool, NumDims, Allocator>& periods,
  792. bool reorder = false);
  793. #endif
  794. /** Abort all tasks in the group of this communicator.
  795. *
  796. * Makes a "best attempt" to abort all of the tasks in the group of
  797. * this communicator. Depending on the underlying MPI
  798. * implementation, this may either abort the entire program (and
  799. * possibly return @p errcode to the environment) or only abort
  800. * some processes, allowing the others to continue. Consult the
  801. * documentation for your MPI implementation. This is equivalent to
  802. * a call to @c MPI_Abort
  803. *
  804. * @param errcode The error code to return from aborted processes.
  805. * @returns Will not return.
  806. */
  807. void abort(int errcode) const;
  808. protected:
  809. /**
  810. * INTERNAL ONLY
  811. *
  812. * Function object that frees an MPI communicator and deletes the
  813. * memory associated with it. Intended to be used as a deleter with
  814. * shared_ptr.
  815. */
  816. struct comm_free
  817. {
  818. void operator()(MPI_Comm* comm) const
  819. {
  820. BOOST_ASSERT( comm != 0 );
  821. BOOST_ASSERT(*comm != MPI_COMM_NULL);
  822. int finalized;
  823. BOOST_MPI_CHECK_RESULT(MPI_Finalized, (&finalized));
  824. if (!finalized)
  825. BOOST_MPI_CHECK_RESULT(MPI_Comm_free, (comm));
  826. delete comm;
  827. }
  828. };
  829. /**
  830. * INTERNAL ONLY
  831. *
  832. * We're sending a type that has an associated MPI datatype, so we
  833. * map directly to that datatype.
  834. */
  835. template<typename T>
  836. void send_impl(int dest, int tag, const T& value, mpl::true_) const;
  837. /**
  838. * INTERNAL ONLY
  839. *
  840. * We're sending a type that does not have an associated MPI
  841. * datatype, so it must be serialized then sent as MPI_PACKED data,
  842. * to be deserialized on the receiver side.
  843. */
  844. template<typename T>
  845. void send_impl(int dest, int tag, const T& value, mpl::false_) const;
  846. /**
  847. * INTERNAL ONLY
  848. *
  849. * We're sending an array of a type that has an associated MPI
  850. * datatype, so we map directly to that datatype.
  851. */
  852. template<typename T>
  853. void
  854. array_send_impl(int dest, int tag, const T* values, int n, mpl::true_) const;
  855. /**
  856. * INTERNAL ONLY
  857. *
  858. * We're sending an array of a type that does not have an associated
  859. * MPI datatype, so it must be serialized then sent as MPI_PACKED
  860. * data, to be deserialized on the receiver side.
  861. */
  862. template<typename T>
  863. void
  864. array_send_impl(int dest, int tag, const T* values, int n,
  865. mpl::false_) const;
  866. /**
  867. * INTERNAL ONLY
  868. *
  869. * We're sending a type that has an associated MPI datatype, so we
  870. * map directly to that datatype.
  871. */
  872. template<typename T>
  873. request isend_impl(int dest, int tag, const T& value, mpl::true_) const;
  874. /**
  875. * INTERNAL ONLY
  876. *
  877. * We're sending a type that does not have an associated MPI
  878. * datatype, so it must be serialized then sent as MPI_PACKED data,
  879. * to be deserialized on the receiver side.
  880. */
  881. template<typename T>
  882. request isend_impl(int dest, int tag, const T& value, mpl::false_) const;
  883. /**
  884. * INTERNAL ONLY
  885. *
  886. * We're sending an array of a type that has an associated MPI
  887. * datatype, so we map directly to that datatype.
  888. */
  889. template<typename T>
  890. request
  891. array_isend_impl(int dest, int tag, const T* values, int n,
  892. mpl::true_) const;
  893. /**
  894. * INTERNAL ONLY
  895. *
  896. * We're sending an array of a type that does not have an associated
  897. * MPI datatype, so it must be serialized then sent as MPI_PACKED
  898. * data, to be deserialized on the receiver side.
  899. */
  900. template<typename T>
  901. request
  902. array_isend_impl(int dest, int tag, const T* values, int n,
  903. mpl::false_) const;
  904. /**
  905. * INTERNAL ONLY
  906. *
  907. * We're receiving a type that has an associated MPI datatype, so we
  908. * map directly to that datatype.
  909. */
  910. template<typename T>
  911. status recv_impl(int source, int tag, T& value, mpl::true_) const;
  912. /**
  913. * INTERNAL ONLY
  914. *
  915. * We're receiving a type that does not have an associated MPI
  916. * datatype, so it must have been serialized then sent as
  917. * MPI_PACKED. We'll receive it and then deserialize.
  918. */
  919. template<typename T>
  920. status recv_impl(int source, int tag, T& value, mpl::false_) const;
  921. /**
  922. * INTERNAL ONLY
  923. *
  924. * We're receiving an array of a type that has an associated MPI
  925. * datatype, so we map directly to that datatype.
  926. */
  927. template<typename T>
  928. status
  929. array_recv_impl(int source, int tag, T* values, int n, mpl::true_) const;
  930. /**
  931. * INTERNAL ONLY
  932. *
  933. * We're receiving a type that does not have an associated MPI
  934. * datatype, so it must have been serialized then sent as
  935. * MPI_PACKED. We'll receive it and then deserialize.
  936. */
  937. template<typename T>
  938. status
  939. array_recv_impl(int source, int tag, T* values, int n, mpl::false_) const;
  940. /**
  941. * INTERNAL ONLY
  942. *
  943. * We're receiving a type that has an associated MPI datatype, so we
  944. * map directly to that datatype.
  945. */
  946. template<typename T>
  947. request irecv_impl(int source, int tag, T& value, mpl::true_) const;
  948. /**
  949. * INTERNAL ONLY
  950. *
  951. * We're receiving a type that does not have an associated MPI
  952. * datatype, so it must have been serialized then sent as
  953. * MPI_PACKED. We'll receive it and then deserialize.
  954. */
  955. template<typename T>
  956. request irecv_impl(int source, int tag, T& value, mpl::false_) const;
  957. /**
  958. * INTERNAL ONLY
  959. *
  960. * We're receiving a type that has an associated MPI datatype, so we
  961. * map directly to that datatype.
  962. */
  963. template<typename T>
  964. request
  965. array_irecv_impl(int source, int tag, T* values, int n, mpl::true_) const;
  966. /**
  967. * INTERNAL ONLY
  968. *
  969. * We're receiving a type that does not have an associated MPI
  970. * datatype, so it must have been serialized then sent as
  971. * MPI_PACKED. We'll receive it and then deserialize.
  972. */
  973. template<typename T>
  974. request
  975. array_irecv_impl(int source, int tag, T* values, int n, mpl::false_) const;
  976. shared_ptr<MPI_Comm> comm_ptr;
  977. };
  978. /**
  979. * @brief Determines whether two communicators are identical.
  980. *
  981. * Equivalent to calling @c MPI_Comm_compare and checking whether the
  982. * result is @c MPI_IDENT.
  983. *
  984. * @returns True when the two communicators refer to the same
  985. * underlying MPI communicator.
  986. */
  987. BOOST_MPI_DECL bool operator==(const communicator& comm1, const communicator& comm2);
  988. /**
  989. * @brief Determines whether two communicators are different.
  990. *
  991. * @returns @c !(comm1 == comm2)
  992. */
  993. inline bool operator!=(const communicator& comm1, const communicator& comm2)
  994. {
  995. return !(comm1 == comm2);
  996. }
  997. /************************************************************************
  998. * Implementation details *
  999. ************************************************************************/
  1000. // Count elements in a message
  1001. template<typename T>
  1002. inline optional<int> status::count() const
  1003. {
  1004. return count_impl<T>(is_mpi_datatype<T>());
  1005. }
  1006. template<typename T>
  1007. optional<int> status::count_impl(mpl::true_) const
  1008. {
  1009. if (m_count != -1)
  1010. return m_count;
  1011. int return_value;
  1012. BOOST_MPI_CHECK_RESULT(MPI_Get_count,
  1013. (&m_status, get_mpi_datatype<T>(T()), &return_value));
  1014. if (return_value == MPI_UNDEFINED)
  1015. return optional<int>();
  1016. else
  1017. /* Cache the result. */
  1018. return m_count = return_value;
  1019. }
  1020. template<typename T>
  1021. inline optional<int> status::count_impl(mpl::false_) const
  1022. {
  1023. if (m_count == -1)
  1024. return optional<int>();
  1025. else
  1026. return m_count;
  1027. }
  1028. // We're sending a type that has an associated MPI datatype, so we
  1029. // map directly to that datatype.
  1030. template<typename T>
  1031. void
  1032. communicator::send_impl(int dest, int tag, const T& value, mpl::true_) const
  1033. {
  1034. BOOST_MPI_CHECK_RESULT(MPI_Send,
  1035. (const_cast<T*>(&value), 1, get_mpi_datatype<T>(value),
  1036. dest, tag, MPI_Comm(*this)));
  1037. }
  1038. // We're sending a type that does not have an associated MPI
  1039. // datatype, so it must be serialized then sent as MPI_PACKED data,
  1040. // to be deserialized on the receiver side.
  1041. template<typename T>
  1042. void
  1043. communicator::send_impl(int dest, int tag, const T& value, mpl::false_) const
  1044. {
  1045. packed_oarchive oa(*this);
  1046. oa << value;
  1047. send(dest, tag, oa);
  1048. }
  1049. // Single-element receive may either send the element directly or
  1050. // serialize it via a buffer.
  1051. template<typename T>
  1052. void communicator::send(int dest, int tag, const T& value) const
  1053. {
  1054. this->send_impl(dest, tag, value, is_mpi_datatype<T>());
  1055. }
  1056. // We're sending an array of a type that has an associated MPI
  1057. // datatype, so we map directly to that datatype.
  1058. template<typename T>
  1059. void
  1060. communicator::array_send_impl(int dest, int tag, const T* values, int n,
  1061. mpl::true_) const
  1062. {
  1063. BOOST_MPI_CHECK_RESULT(MPI_Send,
  1064. (const_cast<T*>(values), n,
  1065. get_mpi_datatype<T>(*values),
  1066. dest, tag, MPI_Comm(*this)));
  1067. }
  1068. // We're sending an array of a type that does not have an associated
  1069. // MPI datatype, so it must be serialized then sent as MPI_PACKED
  1070. // data, to be deserialized on the receiver side.
  1071. template<typename T>
  1072. void
  1073. communicator::array_send_impl(int dest, int tag, const T* values, int n,
  1074. mpl::false_) const
  1075. {
  1076. packed_oarchive oa(*this);
  1077. oa << n << boost::serialization::make_array(values, n);
  1078. send(dest, tag, oa);
  1079. }
  1080. // Array send must send the elements directly
  1081. template<typename T>
  1082. void communicator::send(int dest, int tag, const T* values, int n) const
  1083. {
  1084. this->array_send_impl(dest, tag, values, n, is_mpi_datatype<T>());
  1085. }
  1086. // We're receiving a type that has an associated MPI datatype, so we
  1087. // map directly to that datatype.
  1088. template<typename T>
  1089. status communicator::recv_impl(int source, int tag, T& value, mpl::true_) const
  1090. {
  1091. status stat;
  1092. BOOST_MPI_CHECK_RESULT(MPI_Recv,
  1093. (const_cast<T*>(&value), 1,
  1094. get_mpi_datatype<T>(value),
  1095. source, tag, MPI_Comm(*this), &stat.m_status));
  1096. return stat;
  1097. }
  1098. template<typename T>
  1099. status
  1100. communicator::recv_impl(int source, int tag, T& value, mpl::false_) const
  1101. {
  1102. // Receive the message
  1103. packed_iarchive ia(*this);
  1104. status stat = recv(source, tag, ia);
  1105. // Deserialize the data in the message
  1106. ia >> value;
  1107. return stat;
  1108. }
  1109. // Single-element receive may either receive the element directly or
  1110. // deserialize it from a buffer.
  1111. template<typename T>
  1112. status communicator::recv(int source, int tag, T& value) const
  1113. {
  1114. return this->recv_impl(source, tag, value, is_mpi_datatype<T>());
  1115. }
  1116. template<typename T>
  1117. status
  1118. communicator::array_recv_impl(int source, int tag, T* values, int n,
  1119. mpl::true_) const
  1120. {
  1121. status stat;
  1122. BOOST_MPI_CHECK_RESULT(MPI_Recv,
  1123. (const_cast<T*>(values), n,
  1124. get_mpi_datatype<T>(*values),
  1125. source, tag, MPI_Comm(*this), &stat.m_status));
  1126. return stat;
  1127. }
  1128. template<typename T>
  1129. status
  1130. communicator::array_recv_impl(int source, int tag, T* values, int n,
  1131. mpl::false_) const
  1132. {
  1133. // Receive the message
  1134. packed_iarchive ia(*this);
  1135. status stat = recv(source, tag, ia);
  1136. // Determine how much data we are going to receive
  1137. int count;
  1138. ia >> count;
  1139. // Deserialize the data in the message
  1140. boost::serialization::array<T> arr(values, count > n? n : count);
  1141. ia >> arr;
  1142. if (count > n) {
  1143. boost::throw_exception(
  1144. std::range_error("communicator::recv: message receive overflow"));
  1145. }
  1146. stat.m_count = count;
  1147. return stat;
  1148. }
  1149. // Array receive must receive the elements directly into a buffer.
  1150. template<typename T>
  1151. status communicator::recv(int source, int tag, T* values, int n) const
  1152. {
  1153. return this->array_recv_impl(source, tag, values, n, is_mpi_datatype<T>());
  1154. }
  1155. // We're sending a type that has an associated MPI datatype, so we
  1156. // map directly to that datatype.
  1157. template<typename T>
  1158. request
  1159. communicator::isend_impl(int dest, int tag, const T& value, mpl::true_) const
  1160. {
  1161. request req;
  1162. BOOST_MPI_CHECK_RESULT(MPI_Isend,
  1163. (const_cast<T*>(&value), 1,
  1164. get_mpi_datatype<T>(value),
  1165. dest, tag, MPI_Comm(*this), &req.m_requests[0]));
  1166. return req;
  1167. }
  1168. // We're sending a type that does not have an associated MPI
  1169. // datatype, so it must be serialized then sent as MPI_PACKED data,
  1170. // to be deserialized on the receiver side.
  1171. template<typename T>
  1172. request
  1173. communicator::isend_impl(int dest, int tag, const T& value, mpl::false_) const
  1174. {
  1175. shared_ptr<packed_oarchive> archive(new packed_oarchive(*this));
  1176. *archive << value;
  1177. request result = isend(dest, tag, *archive);
  1178. result.m_data = archive;
  1179. return result;
  1180. }
  1181. // Single-element receive may either send the element directly or
  1182. // serialize it via a buffer.
  1183. template<typename T>
  1184. request communicator::isend(int dest, int tag, const T& value) const
  1185. {
  1186. return this->isend_impl(dest, tag, value, is_mpi_datatype<T>());
  1187. }
  1188. template<typename T>
  1189. request
  1190. communicator::array_isend_impl(int dest, int tag, const T* values, int n,
  1191. mpl::true_) const
  1192. {
  1193. request req;
  1194. BOOST_MPI_CHECK_RESULT(MPI_Isend,
  1195. (const_cast<T*>(values), n,
  1196. get_mpi_datatype<T>(*values),
  1197. dest, tag, MPI_Comm(*this), &req.m_requests[0]));
  1198. return req;
  1199. }
  1200. template<typename T>
  1201. request
  1202. communicator::array_isend_impl(int dest, int tag, const T* values, int n,
  1203. mpl::false_) const
  1204. {
  1205. shared_ptr<packed_oarchive> archive(new packed_oarchive(*this));
  1206. *archive << n << boost::serialization::make_array(values, n);
  1207. request result = isend(dest, tag, *archive);
  1208. result.m_data = archive;
  1209. return result;
  1210. }
  1211. // Array isend must send the elements directly
  1212. template<typename T>
  1213. request communicator::isend(int dest, int tag, const T* values, int n) const
  1214. {
  1215. return array_isend_impl(dest, tag, values, n, is_mpi_datatype<T>());
  1216. }
  1217. namespace detail {
  1218. /**
  1219. * Internal data structure that stores everything required to manage
  1220. * the receipt of serialized data via a request object.
  1221. */
  1222. template<typename T>
  1223. struct serialized_irecv_data
  1224. {
  1225. serialized_irecv_data(const communicator& comm, int source, int tag,
  1226. T& value)
  1227. : comm(comm), source(source), tag(tag), ia(comm), value(value)
  1228. {
  1229. }
  1230. void deserialize(status& stat)
  1231. {
  1232. ia >> value;
  1233. stat.m_count = 1;
  1234. }
  1235. communicator comm;
  1236. int source;
  1237. int tag;
  1238. std::size_t count;
  1239. packed_iarchive ia;
  1240. T& value;
  1241. };
  1242. template<>
  1243. struct serialized_irecv_data<packed_iarchive>
  1244. {
  1245. serialized_irecv_data(const communicator& comm, int source, int tag,
  1246. packed_iarchive& ia)
  1247. : comm(comm), source(source), tag(tag), ia(ia) { }
  1248. void deserialize(status&) { /* Do nothing. */ }
  1249. communicator comm;
  1250. int source;
  1251. int tag;
  1252. std::size_t count;
  1253. packed_iarchive& ia;
  1254. };
  1255. /**
  1256. * Internal data structure that stores everything required to manage
  1257. * the receipt of an array of serialized data via a request object.
  1258. */
  1259. template<typename T>
  1260. struct serialized_array_irecv_data
  1261. {
  1262. serialized_array_irecv_data(const communicator& comm, int source, int tag,
  1263. T* values, int n)
  1264. : comm(comm), source(source), tag(tag), ia(comm), values(values), n(n)
  1265. {
  1266. }
  1267. void deserialize(status& stat);
  1268. communicator comm;
  1269. int source;
  1270. int tag;
  1271. std::size_t count;
  1272. packed_iarchive ia;
  1273. T* values;
  1274. int n;
  1275. };
  1276. template<typename T>
  1277. void serialized_array_irecv_data<T>::deserialize(status& stat)
  1278. {
  1279. // Determine how much data we are going to receive
  1280. int count;
  1281. ia >> count;
  1282. // Deserialize the data in the message
  1283. boost::serialization::array<T> arr(values, count > n? n : count);
  1284. ia >> arr;
  1285. if (count > n) {
  1286. boost::throw_exception(
  1287. std::range_error("communicator::recv: message receive overflow"));
  1288. }
  1289. stat.m_count = count;
  1290. }
  1291. }
  1292. template<typename T>
  1293. optional<status>
  1294. request::handle_serialized_irecv(request* self, request_action action)
  1295. {
  1296. typedef detail::serialized_irecv_data<T> data_t;
  1297. shared_ptr<data_t> data = static_pointer_cast<data_t>(self->m_data);
  1298. if (action == ra_wait) {
  1299. status stat;
  1300. if (self->m_requests[1] == MPI_REQUEST_NULL) {
  1301. // Wait for the count message to complete
  1302. BOOST_MPI_CHECK_RESULT(MPI_Wait,
  1303. (self->m_requests, &stat.m_status));
  1304. // Resize our buffer and get ready to receive its data
  1305. data->ia.resize(data->count);
  1306. BOOST_MPI_CHECK_RESULT(MPI_Irecv,
  1307. (data->ia.address(), data->ia.size(), MPI_PACKED,
  1308. stat.source(), stat.tag(),
  1309. MPI_Comm(data->comm), self->m_requests + 1));
  1310. }
  1311. // Wait until we have received the entire message
  1312. BOOST_MPI_CHECK_RESULT(MPI_Wait,
  1313. (self->m_requests + 1, &stat.m_status));
  1314. data->deserialize(stat);
  1315. return stat;
  1316. } else if (action == ra_test) {
  1317. status stat;
  1318. int flag = 0;
  1319. if (self->m_requests[1] == MPI_REQUEST_NULL) {
  1320. // Check if the count message has completed
  1321. BOOST_MPI_CHECK_RESULT(MPI_Test,
  1322. (self->m_requests, &flag, &stat.m_status));
  1323. if (flag) {
  1324. // Resize our buffer and get ready to receive its data
  1325. data->ia.resize(data->count);
  1326. BOOST_MPI_CHECK_RESULT(MPI_Irecv,
  1327. (data->ia.address(), data->ia.size(),MPI_PACKED,
  1328. stat.source(), stat.tag(),
  1329. MPI_Comm(data->comm), self->m_requests + 1));
  1330. } else
  1331. return optional<status>(); // We have not finished yet
  1332. }
  1333. // Check if we have received the message data
  1334. BOOST_MPI_CHECK_RESULT(MPI_Test,
  1335. (self->m_requests + 1, &flag, &stat.m_status));
  1336. if (flag) {
  1337. data->deserialize(stat);
  1338. return stat;
  1339. } else
  1340. return optional<status>();
  1341. } else {
  1342. return optional<status>();
  1343. }
  1344. }
  1345. template<typename T>
  1346. optional<status>
  1347. request::handle_serialized_array_irecv(request* self, request_action action)
  1348. {
  1349. typedef detail::serialized_array_irecv_data<T> data_t;
  1350. shared_ptr<data_t> data = static_pointer_cast<data_t>(self->m_data);
  1351. if (action == ra_wait) {
  1352. status stat;
  1353. if (self->m_requests[1] == MPI_REQUEST_NULL) {
  1354. // Wait for the count message to complete
  1355. BOOST_MPI_CHECK_RESULT(MPI_Wait,
  1356. (self->m_requests, &stat.m_status));
  1357. // Resize our buffer and get ready to receive its data
  1358. data->ia.resize(data->count);
  1359. BOOST_MPI_CHECK_RESULT(MPI_Irecv,
  1360. (data->ia.address(), data->ia.size(), MPI_PACKED,
  1361. stat.source(), stat.tag(),
  1362. MPI_Comm(data->comm), self->m_requests + 1));
  1363. }
  1364. // Wait until we have received the entire message
  1365. BOOST_MPI_CHECK_RESULT(MPI_Wait,
  1366. (self->m_requests + 1, &stat.m_status));
  1367. data->deserialize(stat);
  1368. return stat;
  1369. } else if (action == ra_test) {
  1370. status stat;
  1371. int flag = 0;
  1372. if (self->m_requests[1] == MPI_REQUEST_NULL) {
  1373. // Check if the count message has completed
  1374. BOOST_MPI_CHECK_RESULT(MPI_Test,
  1375. (self->m_requests, &flag, &stat.m_status));
  1376. if (flag) {
  1377. // Resize our buffer and get ready to receive its data
  1378. data->ia.resize(data->count);
  1379. BOOST_MPI_CHECK_RESULT(MPI_Irecv,
  1380. (data->ia.address(), data->ia.size(),MPI_PACKED,
  1381. stat.source(), stat.tag(),
  1382. MPI_Comm(data->comm), self->m_requests + 1));
  1383. } else
  1384. return optional<status>(); // We have not finished yet
  1385. }
  1386. // Check if we have received the message data
  1387. BOOST_MPI_CHECK_RESULT(MPI_Test,
  1388. (self->m_requests + 1, &flag, &stat.m_status));
  1389. if (flag) {
  1390. data->deserialize(stat);
  1391. return stat;
  1392. } else
  1393. return optional<status>();
  1394. } else {
  1395. return optional<status>();
  1396. }
  1397. }
  1398. // We're receiving a type that has an associated MPI datatype, so we
  1399. // map directly to that datatype.
  1400. template<typename T>
  1401. request
  1402. communicator::irecv_impl(int source, int tag, T& value, mpl::true_) const
  1403. {
  1404. request req;
  1405. BOOST_MPI_CHECK_RESULT(MPI_Irecv,
  1406. (const_cast<T*>(&value), 1,
  1407. get_mpi_datatype<T>(value),
  1408. source, tag, MPI_Comm(*this), &req.m_requests[0]));
  1409. return req;
  1410. }
  1411. template<typename T>
  1412. request
  1413. communicator::irecv_impl(int source, int tag, T& value, mpl::false_) const
  1414. {
  1415. typedef detail::serialized_irecv_data<T> data_t;
  1416. shared_ptr<data_t> data(new data_t(*this, source, tag, value));
  1417. request req;
  1418. req.m_data = data;
  1419. req.m_handler = request::handle_serialized_irecv<T>;
  1420. BOOST_MPI_CHECK_RESULT(MPI_Irecv,
  1421. (&data->count, 1,
  1422. get_mpi_datatype<std::size_t>(data->count),
  1423. source, tag, MPI_Comm(*this), &req.m_requests[0]));
  1424. return req;
  1425. }
  1426. template<typename T>
  1427. request
  1428. communicator::irecv(int source, int tag, T& value) const
  1429. {
  1430. return this->irecv_impl(source, tag, value, is_mpi_datatype<T>());
  1431. }
  1432. template<typename T>
  1433. request
  1434. communicator::array_irecv_impl(int source, int tag, T* values, int n,
  1435. mpl::true_) const
  1436. {
  1437. request req;
  1438. BOOST_MPI_CHECK_RESULT(MPI_Irecv,
  1439. (const_cast<T*>(values), n,
  1440. get_mpi_datatype<T>(*values),
  1441. source, tag, MPI_Comm(*this), &req.m_requests[0]));
  1442. return req;
  1443. }
  1444. template<typename T>
  1445. request
  1446. communicator::array_irecv_impl(int source, int tag, T* values, int n,
  1447. mpl::false_) const
  1448. {
  1449. typedef detail::serialized_array_irecv_data<T> data_t;
  1450. shared_ptr<data_t> data(new data_t(*this, source, tag, values, n));
  1451. request req;
  1452. req.m_data = data;
  1453. req.m_handler = request::handle_serialized_array_irecv<T>;
  1454. BOOST_MPI_CHECK_RESULT(MPI_Irecv,
  1455. (&data->count, 1,
  1456. get_mpi_datatype<std::size_t>(data->count),
  1457. source, tag, MPI_Comm(*this), &req.m_requests[0]));
  1458. return req;
  1459. }
  1460. // Array receive must receive the elements directly into a buffer.
  1461. template<typename T>
  1462. request communicator::irecv(int source, int tag, T* values, int n) const
  1463. {
  1464. return this->array_irecv_impl(source, tag, values, n, is_mpi_datatype<T>());
  1465. }
  1466. /**
  1467. * INTERNAL ONLY
  1468. */
  1469. template<>
  1470. BOOST_MPI_DECL void
  1471. communicator::send<packed_oarchive>(int dest, int tag,
  1472. const packed_oarchive& ar) const;
  1473. /**
  1474. * INTERNAL ONLY
  1475. */
  1476. template<>
  1477. BOOST_MPI_DECL void
  1478. communicator::send<packed_skeleton_oarchive>
  1479. (int dest, int tag, const packed_skeleton_oarchive& ar) const;
  1480. /**
  1481. * INTERNAL ONLY
  1482. */
  1483. template<>
  1484. BOOST_MPI_DECL void
  1485. communicator::send<content>(int dest, int tag, const content& c) const;
  1486. /**
  1487. * INTERNAL ONLY
  1488. */
  1489. template<>
  1490. BOOST_MPI_DECL status
  1491. communicator::recv<packed_iarchive>(int source, int tag,
  1492. packed_iarchive& ar) const;
  1493. /**
  1494. * INTERNAL ONLY
  1495. */
  1496. template<>
  1497. BOOST_MPI_DECL status
  1498. communicator::recv<packed_skeleton_iarchive>
  1499. (int source, int tag, packed_skeleton_iarchive& ar) const;
  1500. /**
  1501. * INTERNAL ONLY
  1502. */
  1503. template<>
  1504. BOOST_MPI_DECL status
  1505. communicator::recv<const content>(int source, int tag,
  1506. const content& c) const;
  1507. /**
  1508. * INTERNAL ONLY
  1509. */
  1510. template<>
  1511. inline status
  1512. communicator::recv<content>(int source, int tag,
  1513. content& c) const
  1514. {
  1515. return recv<const content>(source,tag,c);
  1516. }
  1517. /**
  1518. * INTERNAL ONLY
  1519. */
  1520. template<>
  1521. BOOST_MPI_DECL request
  1522. communicator::isend<packed_oarchive>(int dest, int tag,
  1523. const packed_oarchive& ar) const;
  1524. /**
  1525. * INTERNAL ONLY
  1526. */
  1527. template<>
  1528. BOOST_MPI_DECL request
  1529. communicator::isend<packed_skeleton_oarchive>
  1530. (int dest, int tag, const packed_skeleton_oarchive& ar) const;
  1531. /**
  1532. * INTERNAL ONLY
  1533. */
  1534. template<>
  1535. BOOST_MPI_DECL request
  1536. communicator::isend<content>(int dest, int tag, const content& c) const;
  1537. /**
  1538. * INTERNAL ONLY
  1539. */
  1540. template<>
  1541. BOOST_MPI_DECL request
  1542. communicator::irecv<packed_skeleton_iarchive>
  1543. (int source, int tag, packed_skeleton_iarchive& ar) const;
  1544. /**
  1545. * INTERNAL ONLY
  1546. */
  1547. template<>
  1548. BOOST_MPI_DECL request
  1549. communicator::irecv<const content>(int source, int tag,
  1550. const content& c) const;
  1551. /**
  1552. * INTERNAL ONLY
  1553. */
  1554. template<>
  1555. inline request
  1556. communicator::irecv<content>(int source, int tag,
  1557. content& c) const
  1558. {
  1559. return irecv<const content>(source, tag, c);
  1560. }
  1561. } } // end namespace boost::mpi
  1562. // If the user has already included skeleton_and_content.hpp, include
  1563. // the code to send/receive skeletons and content.
  1564. #ifdef BOOST_MPI_SKELETON_AND_CONTENT_HPP
  1565. # include <boost/mpi/detail/communicator_sc.hpp>
  1566. #endif
  1567. #ifdef BOOST_MSVC
  1568. # pragma warning(pop)
  1569. #endif
  1570. #endif // BOOST_MPI_COMMUNICATOR_HPP