communicator.hpp 62 KB

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