communicator.hpp 68 KB

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