communicator.hpp 57 KB

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