communicator.hpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  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. // We're sending a type that has an associated MPI datatype, so we
  1162. // map directly to that datatype.
  1163. template<typename T>
  1164. void
  1165. communicator::send_impl(int dest, int tag, const T& value, mpl::true_) const
  1166. {
  1167. // received by recv or trivial handler.
  1168. BOOST_MPI_CHECK_RESULT(MPI_Send,
  1169. (const_cast<T*>(&value), 1, get_mpi_datatype<T>(value),
  1170. dest, tag, MPI_Comm(*this)));
  1171. }
  1172. // We're sending a type that does not have an associated MPI
  1173. // datatype, so it must be serialized then sent as MPI_PACKED data,
  1174. // to be deserialized on the receiver side.
  1175. template<typename T>
  1176. void
  1177. communicator::send_impl(int dest, int tag, const T& value, mpl::false_) const
  1178. {
  1179. packed_oarchive oa(*this);
  1180. oa << value;
  1181. send(dest, tag, oa);
  1182. }
  1183. // Single-element receive may either send the element directly or
  1184. // serialize it via a buffer.
  1185. template<typename T>
  1186. void communicator::send(int dest, int tag, const T& value) const
  1187. {
  1188. this->send_impl(dest, tag, value, is_mpi_datatype<T>());
  1189. }
  1190. // We're sending an array of a type that has an associated MPI
  1191. // datatype, so we map directly to that datatype.
  1192. template<typename T>
  1193. void
  1194. communicator::array_send_impl(int dest, int tag, const T* values, int n,
  1195. mpl::true_) const
  1196. {
  1197. BOOST_MPI_CHECK_RESULT(MPI_Send,
  1198. (const_cast<T*>(values), n,
  1199. get_mpi_datatype<T>(*values),
  1200. dest, tag, MPI_Comm(*this)));
  1201. }
  1202. // We're sending an array of a type that does not have an associated
  1203. // MPI datatype, so it must be serialized then sent as MPI_PACKED
  1204. // data, to be deserialized on the receiver side.
  1205. template<typename T>
  1206. void
  1207. communicator::array_send_impl(int dest, int tag, const T* values, int n,
  1208. mpl::false_) const
  1209. {
  1210. packed_oarchive oa(*this);
  1211. T const* v = values;
  1212. while (v < values+n) {
  1213. oa << *v++;
  1214. }
  1215. send(dest, tag, oa);
  1216. }
  1217. template<typename T, typename A>
  1218. void communicator::send_vector(int dest, int tag,
  1219. const std::vector<T,A>& values, mpl::true_ primitive) const
  1220. {
  1221. #if defined(BOOST_MPI_USE_IMPROBE)
  1222. array_send_impl(dest, tag, values.data(), values.size(), primitive);
  1223. #else
  1224. {
  1225. // non blocking recv by legacy_dynamic_primitive_array_handler
  1226. // blocking recv by recv_vector(source,tag,value,primitive)
  1227. // send the vector size
  1228. typename std::vector<T,A>::size_type size = values.size();
  1229. send(dest, tag, size);
  1230. // send the data
  1231. this->array_send_impl(dest, tag, values.data(), size, primitive);
  1232. }
  1233. #endif
  1234. }
  1235. template<typename T, typename A>
  1236. void communicator::send_vector(int dest, int tag,
  1237. const std::vector<T,A>& value, mpl::false_ primitive) const
  1238. {
  1239. this->send_impl(dest, tag, value, primitive);
  1240. }
  1241. template<typename T, typename A>
  1242. void communicator::send(int dest, int tag, const std::vector<T,A>& value) const
  1243. {
  1244. send_vector(dest, tag, value, is_mpi_datatype<T>());
  1245. }
  1246. // Array send must send the elements directly
  1247. template<typename T>
  1248. void communicator::send(int dest, int tag, const T* values, int n) const
  1249. {
  1250. this->array_send_impl(dest, tag, values, n, is_mpi_datatype<T>());
  1251. }
  1252. // We're receiving a type that has an associated MPI datatype, so we
  1253. // map directly to that datatype.
  1254. template<typename T>
  1255. status communicator::recv_impl(int source, int tag, T& value, mpl::true_) const
  1256. {
  1257. status stat;
  1258. BOOST_MPI_CHECK_RESULT(MPI_Recv,
  1259. (const_cast<T*>(&value), 1,
  1260. get_mpi_datatype<T>(value),
  1261. source, tag, MPI_Comm(*this), &stat.m_status));
  1262. return stat;
  1263. }
  1264. template<typename T>
  1265. status
  1266. communicator::recv_impl(int source, int tag, T& value, mpl::false_) const
  1267. {
  1268. // Receive the message
  1269. packed_iarchive ia(*this);
  1270. status stat = recv(source, tag, ia);
  1271. // Deserialize the data in the message
  1272. ia >> value;
  1273. return stat;
  1274. }
  1275. // Single-element receive may either receive the element directly or
  1276. // deserialize it from a buffer.
  1277. template<typename T>
  1278. status communicator::recv(int source, int tag, T& value) const
  1279. {
  1280. return this->recv_impl(source, tag, value, is_mpi_datatype<T>());
  1281. }
  1282. template<typename T>
  1283. status
  1284. communicator::array_recv_impl(int source, int tag, T* values, int n,
  1285. mpl::true_) const
  1286. {
  1287. status stat;
  1288. BOOST_MPI_CHECK_RESULT(MPI_Recv,
  1289. (const_cast<T*>(values), n,
  1290. get_mpi_datatype<T>(*values),
  1291. source, tag, MPI_Comm(*this), &stat.m_status));
  1292. return stat;
  1293. }
  1294. template<typename T>
  1295. status
  1296. communicator::array_recv_impl(int source, int tag, T* values, int n,
  1297. mpl::false_) const
  1298. {
  1299. packed_iarchive ia(*this);
  1300. status stat = recv(source, tag, ia);
  1301. T* v = values;
  1302. while (v != values+n) {
  1303. ia >> *v++;
  1304. }
  1305. stat.m_count = n;
  1306. return stat;
  1307. }
  1308. template<typename T, typename A>
  1309. status communicator::recv_vector(int source, int tag,
  1310. std::vector<T,A>& values, mpl::true_ primitive) const
  1311. {
  1312. #if defined(BOOST_MPI_USE_IMPROBE)
  1313. {
  1314. MPI_Message msg;
  1315. status stat;
  1316. BOOST_MPI_CHECK_RESULT(MPI_Mprobe, (source,tag,*this,&msg,&stat.m_status));
  1317. int count;
  1318. BOOST_MPI_CHECK_RESULT(MPI_Get_count, (&stat.m_status,get_mpi_datatype<T>(),&count));
  1319. values.resize(count);
  1320. BOOST_MPI_CHECK_RESULT(MPI_Mrecv, (values.data(), count, get_mpi_datatype<T>(), &msg, &stat.m_status));
  1321. return stat;
  1322. }
  1323. #else
  1324. {
  1325. // receive the vector size
  1326. typename std::vector<T,A>::size_type size = 0;
  1327. recv(source, tag, size);
  1328. // size the vector
  1329. values.resize(size);
  1330. // receive the data
  1331. return this->array_recv_impl(source, tag, values.data(), size, primitive);
  1332. }
  1333. #endif
  1334. }
  1335. template<typename T, typename A>
  1336. status communicator::recv_vector(int source, int tag,
  1337. std::vector<T,A>& value, mpl::false_ false_type) const
  1338. {
  1339. return this->recv_impl(source, tag, value, false_type);
  1340. }
  1341. template<typename T, typename A>
  1342. status communicator::recv(int source, int tag, std::vector<T,A>& value) const
  1343. {
  1344. return recv_vector(source, tag, value, is_mpi_datatype<T>());
  1345. }
  1346. // Array receive must receive the elements directly into a buffer.
  1347. template<typename T>
  1348. status communicator::recv(int source, int tag, T* values, int n) const
  1349. {
  1350. return this->array_recv_impl(source, tag, values, n, is_mpi_datatype<T>());
  1351. }
  1352. template<typename T>
  1353. status communicator::sendrecv_impl(int dest, int stag, const T& sval, int src, int rtag, T& rval,
  1354. mpl::true_) const
  1355. {
  1356. status stat;
  1357. BOOST_MPI_CHECK_RESULT(MPI_Sendrecv,
  1358. (const_cast<T*>(&sval), 1,
  1359. get_mpi_datatype<T>(sval),
  1360. dest, stag,
  1361. &rval, 1,
  1362. get_mpi_datatype<T>(rval),
  1363. src, rtag,
  1364. MPI_Comm(*this), &stat.m_status));
  1365. return stat;
  1366. }
  1367. template<typename T>
  1368. status communicator::sendrecv_impl(int dest, int stag, const T& sval, int src, int rtag, T& rval,
  1369. mpl::false_) const
  1370. {
  1371. int const SEND = 0;
  1372. int const RECV = 1;
  1373. request srrequests[2];
  1374. srrequests[SEND] = this->isend_impl(dest, stag, sval, mpl::false_());
  1375. srrequests[RECV] = this->irecv_impl(src, rtag, rval, mpl::false_());
  1376. status srstatuses[2];
  1377. wait_all(srrequests, srrequests + 2, srstatuses);
  1378. return srstatuses[RECV];
  1379. }
  1380. template<typename T>
  1381. status communicator::sendrecv(int dest, int stag, const T& sval, int src, int rtag, T& rval) const
  1382. {
  1383. return this->sendrecv_impl(dest, stag, sval, src, rtag, rval, is_mpi_datatype<T>());
  1384. }
  1385. // We're sending a type that has an associated MPI datatype, so we
  1386. // map directly to that datatype.
  1387. template<typename T>
  1388. request
  1389. communicator::isend_impl(int dest, int tag, const T& value, mpl::true_) const
  1390. {
  1391. return request::make_trivial_send(*this, dest, tag, value);
  1392. }
  1393. // We're sending a type that does not have an associated MPI
  1394. // datatype, so it must be serialized then sent as MPI_PACKED data,
  1395. // to be deserialized on the receiver side.
  1396. template<typename T>
  1397. request
  1398. communicator::isend_impl(int dest, int tag, const T& value, mpl::false_) const
  1399. {
  1400. shared_ptr<packed_oarchive> archive(new packed_oarchive(*this));
  1401. *archive << value;
  1402. request result = isend(dest, tag, *archive);
  1403. result.preserve(archive);
  1404. return result;
  1405. }
  1406. // Single-element receive may either send the element directly or
  1407. // serialize it via a buffer.
  1408. template<typename T>
  1409. request communicator::isend(int dest, int tag, const T& value) const
  1410. {
  1411. return this->isend_impl(dest, tag, value, is_mpi_datatype<T>());
  1412. }
  1413. template<typename T, class A>
  1414. request communicator::isend(int dest, int tag, const std::vector<T,A>& values) const
  1415. {
  1416. return this->isend_vector(dest, tag, values, is_mpi_datatype<T>());
  1417. }
  1418. template<typename T, class A>
  1419. request
  1420. communicator::isend_vector(int dest, int tag, const std::vector<T,A>& values,
  1421. mpl::true_ primitive) const
  1422. {
  1423. return request::make_dynamic_primitive_array_send(*this, dest, tag, values);
  1424. }
  1425. template<typename T, class A>
  1426. request
  1427. communicator::isend_vector(int dest, int tag, const std::vector<T,A>& values,
  1428. mpl::false_ no) const
  1429. {
  1430. return this->isend_impl(dest, tag, values, no);
  1431. }
  1432. template<typename T>
  1433. request
  1434. communicator::array_isend_impl(int dest, int tag, const T* values, int n,
  1435. mpl::true_) const
  1436. {
  1437. return request::make_trivial_send(*this, dest, tag, values, n);
  1438. }
  1439. template<typename T>
  1440. request
  1441. communicator::array_isend_impl(int dest, int tag, const T* values, int n,
  1442. mpl::false_) const
  1443. {
  1444. shared_ptr<packed_oarchive> archive(new packed_oarchive(*this));
  1445. T const* v = values;
  1446. while (v < values+n) {
  1447. *archive << *v++;
  1448. }
  1449. request result = isend(dest, tag, *archive);
  1450. result.preserve(archive);
  1451. return result;
  1452. }
  1453. // Array isend must send the elements directly
  1454. template<typename T>
  1455. request communicator::isend(int dest, int tag, const T* values, int n) const
  1456. {
  1457. return array_isend_impl(dest, tag, values, n, is_mpi_datatype<T>());
  1458. }
  1459. // We're receiving a type that has an associated MPI datatype, so we
  1460. // map directly to that datatype.
  1461. template<typename T>
  1462. request
  1463. communicator::irecv_impl(int source, int tag, T& value, mpl::true_) const
  1464. {
  1465. return request::make_trivial_recv(*this, source, tag, value);
  1466. }
  1467. template<typename T>
  1468. request
  1469. communicator::irecv_impl(int source, int tag, T& value, mpl::false_) const
  1470. {
  1471. return request::make_serialized(*this, source, tag, value);
  1472. }
  1473. template<typename T>
  1474. request
  1475. communicator::irecv(int source, int tag, T& value) const
  1476. {
  1477. return this->irecv_impl(source, tag, value, is_mpi_datatype<T>());
  1478. }
  1479. template<typename T>
  1480. request
  1481. communicator::array_irecv_impl(int source, int tag, T* values, int n,
  1482. mpl::true_) const
  1483. {
  1484. return request::make_trivial_recv(*this, source, tag, values, n);
  1485. }
  1486. template<typename T>
  1487. request
  1488. communicator::array_irecv_impl(int source, int tag, T* values, int n,
  1489. mpl::false_) const
  1490. {
  1491. return request::make_serialized_array(*this, source, tag, values, n);
  1492. }
  1493. template<typename T, class A>
  1494. request
  1495. communicator::irecv_vector(int source, int tag, std::vector<T,A>& values,
  1496. mpl::true_ primitive) const
  1497. {
  1498. return request::make_dynamic_primitive_array_recv(*this, source, tag, values);
  1499. }
  1500. template<typename T, class A>
  1501. request
  1502. communicator::irecv_vector(int source, int tag, std::vector<T,A>& values,
  1503. mpl::false_ no) const
  1504. {
  1505. return irecv_impl(source, tag, values, no);
  1506. }
  1507. template<typename T, typename A>
  1508. request
  1509. communicator::irecv(int source, int tag, std::vector<T,A>& values) const
  1510. {
  1511. return irecv_vector(source, tag, values, is_mpi_datatype<T>());
  1512. }
  1513. // Array receive must receive the elements directly into a buffer.
  1514. template<typename T>
  1515. request communicator::irecv(int source, int tag, T* values, int n) const
  1516. {
  1517. return this->array_irecv_impl(source, tag, values, n, is_mpi_datatype<T>());
  1518. }
  1519. } } // end namespace boost::mpi
  1520. // If the user has already included skeleton_and_content.hpp, include
  1521. // the code to send/receive skeletons and content.
  1522. #ifdef BOOST_MPI_SKELETON_AND_CONTENT_HPP
  1523. # include <boost/mpi/detail/communicator_sc.hpp>
  1524. #endif
  1525. #ifdef BOOST_MSVC
  1526. # pragma warning(pop)
  1527. #endif
  1528. #endif // BOOST_MPI_COMMUNICATOR_HPP