nonblocking.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. // Copyright (C) 2006 Douglas Gregor <doug.gregor -at- gmail.com>.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. /** @file nonblocking.hpp
  6. *
  7. * This header defines operations for completing non-blocking
  8. * communication requests.
  9. */
  10. #ifndef BOOST_MPI_NONBLOCKING_HPP
  11. #define BOOST_MPI_NONBLOCKING_HPP
  12. #include <boost/mpi/config.hpp>
  13. #include <vector>
  14. #include <iterator> // for std::iterator_traits
  15. #include <boost/optional.hpp>
  16. #include <utility> // for std::pair
  17. #include <algorithm> // for iter_swap, reverse
  18. #include <boost/static_assert.hpp>
  19. #include <boost/mpi/request.hpp>
  20. #include <boost/mpi/status.hpp>
  21. #include <boost/mpi/exception.hpp>
  22. namespace boost { namespace mpi {
  23. /**
  24. * @brief Wait until any non-blocking request has completed.
  25. *
  26. * This routine takes in a set of requests stored in the iterator
  27. * range @c [first,last) and waits until any of these requests has
  28. * been completed. It provides functionality equivalent to
  29. * @c MPI_Waitany.
  30. *
  31. * @param first The iterator that denotes the beginning of the
  32. * sequence of request objects.
  33. *
  34. * @param last The iterator that denotes the end of the sequence of
  35. * request objects. This may not be equal to @c first.
  36. *
  37. * @returns A pair containing the status object that corresponds to
  38. * the completed operation and the iterator referencing the completed
  39. * request.
  40. */
  41. template<typename ForwardIterator>
  42. std::pair<status, ForwardIterator>
  43. wait_any(ForwardIterator first, ForwardIterator last)
  44. {
  45. using std::advance;
  46. BOOST_ASSERT(first != last);
  47. typedef typename std::iterator_traits<ForwardIterator>::difference_type
  48. difference_type;
  49. bool all_trivial_requests = true;
  50. difference_type n = 0;
  51. ForwardIterator current = first;
  52. while (true) {
  53. // Check if we have found a completed request. If so, return it.
  54. if (current->active()) {
  55. optional<status> result = current->test();
  56. if (bool(result)) {
  57. return std::make_pair(*result, current);
  58. }
  59. }
  60. // Check if this request (and all others before it) are "trivial"
  61. // requests, e.g., they can be represented with a single
  62. // MPI_Request.
  63. // We could probably ignore non trivial request that are inactive,
  64. // but we can assume that a mix of trivial and non trivial requests
  65. // is unlikely enough not to care.
  66. all_trivial_requests = all_trivial_requests && current->trivial();
  67. // Move to the next request.
  68. ++n;
  69. if (++current == last) {
  70. // We have reached the end of the list. If all requests thus far
  71. // have been trivial, we can call MPI_Waitany directly, because
  72. // it may be more efficient than our busy-wait semantics.
  73. if (all_trivial_requests) {
  74. std::vector<MPI_Request> requests;
  75. requests.reserve(n);
  76. for (current = first; current != last; ++current) {
  77. requests.push_back(*current->trivial());
  78. }
  79. // Let MPI wait until one of these operations completes.
  80. int index;
  81. status stat;
  82. BOOST_MPI_CHECK_RESULT(MPI_Waitany,
  83. (n, &requests[0], &index, &stat.m_status));
  84. // We don't have a notion of empty requests or status objects,
  85. // so this is an error.
  86. if (index == MPI_UNDEFINED)
  87. boost::throw_exception(exception("MPI_Waitany", MPI_ERR_REQUEST));
  88. // Find the iterator corresponding to the completed request.
  89. current = first;
  90. advance(current, index);
  91. *current->trivial() = requests[index];
  92. return std::make_pair(stat, current);
  93. }
  94. // There are some nontrivial requests, so we must continue our
  95. // busy waiting loop.
  96. n = 0;
  97. current = first;
  98. all_trivial_requests = true;
  99. }
  100. }
  101. // We cannot ever get here
  102. BOOST_ASSERT(false);
  103. }
  104. /**
  105. * @brief Test whether any non-blocking request has completed.
  106. *
  107. * This routine takes in a set of requests stored in the iterator
  108. * range @c [first,last) and tests whether any of these requests has
  109. * been completed. This routine is similar to @c wait_any, but will
  110. * not block waiting for requests to completed. It provides
  111. * functionality equivalent to @c MPI_Testany.
  112. *
  113. * @param first The iterator that denotes the beginning of the
  114. * sequence of request objects.
  115. *
  116. * @param last The iterator that denotes the end of the sequence of
  117. * request objects.
  118. *
  119. * @returns If any outstanding requests have completed, a pair
  120. * containing the status object that corresponds to the completed
  121. * operation and the iterator referencing the completed
  122. * request. Otherwise, an empty @c optional<>.
  123. */
  124. template<typename ForwardIterator>
  125. optional<std::pair<status, ForwardIterator> >
  126. test_any(ForwardIterator first, ForwardIterator last)
  127. {
  128. while (first != last) {
  129. // Check if we have found a completed request. If so, return it.
  130. if (optional<status> result = first->test()) {
  131. return std::make_pair(*result, first);
  132. }
  133. ++first;
  134. }
  135. // We found nothing
  136. return optional<std::pair<status, ForwardIterator> >();
  137. }
  138. /**
  139. * @brief Wait until all non-blocking requests have completed.
  140. *
  141. * This routine takes in a set of requests stored in the iterator
  142. * range @c [first,last) and waits until all of these requests have
  143. * been completed. It provides functionality equivalent to
  144. * @c MPI_Waitall.
  145. *
  146. * @param first The iterator that denotes the beginning of the
  147. * sequence of request objects.
  148. *
  149. * @param last The iterator that denotes the end of the sequence of
  150. * request objects.
  151. *
  152. * @param out If provided, an output iterator through which the
  153. * status of each request will be emitted. The @c status objects are
  154. * emitted in the same order as the requests are retrieved from
  155. * @c [first,last).
  156. *
  157. * @returns If an @p out parameter was provided, the value @c out
  158. * after all of the @c status objects have been emitted.
  159. */
  160. template<typename ForwardIterator, typename OutputIterator>
  161. OutputIterator
  162. wait_all(ForwardIterator first, ForwardIterator last, OutputIterator out)
  163. {
  164. typedef typename std::iterator_traits<ForwardIterator>::difference_type
  165. difference_type;
  166. using std::distance;
  167. difference_type num_outstanding_requests = distance(first, last);
  168. std::vector<status> results(num_outstanding_requests);
  169. std::vector<bool> completed(num_outstanding_requests);
  170. while (num_outstanding_requests > 0) {
  171. bool all_trivial_requests = true;
  172. difference_type idx = 0;
  173. for (ForwardIterator current = first; current != last; ++current, ++idx) {
  174. if (!completed[idx]) {
  175. if (optional<status> stat = current->test()) {
  176. // This outstanding request has been completed. We're done.
  177. results[idx] = *stat;
  178. completed[idx] = true;
  179. --num_outstanding_requests;
  180. all_trivial_requests = false;
  181. } else {
  182. // Check if this request (and all others before it) are "trivial"
  183. // requests, e.g., they can be represented with a single
  184. // MPI_Request.
  185. all_trivial_requests = all_trivial_requests && current->trivial();
  186. }
  187. }
  188. }
  189. // If we have yet to fulfill any requests and all of the requests
  190. // are trivial (i.e., require only a single MPI_Request to be
  191. // fulfilled), call MPI_Waitall directly.
  192. if (all_trivial_requests
  193. && num_outstanding_requests == (difference_type)results.size()) {
  194. std::vector<MPI_Request> requests;
  195. requests.reserve(num_outstanding_requests);
  196. for (ForwardIterator current = first; current != last; ++current)
  197. requests.push_back(*current->trivial());
  198. // Let MPI wait until all of these operations completes.
  199. std::vector<MPI_Status> stats(num_outstanding_requests);
  200. BOOST_MPI_CHECK_RESULT(MPI_Waitall,
  201. (num_outstanding_requests, &requests[0],
  202. &stats[0]));
  203. for (std::vector<MPI_Status>::iterator i = stats.begin();
  204. i != stats.end(); ++i, ++out) {
  205. status stat;
  206. stat.m_status = *i;
  207. *out = stat;
  208. }
  209. return out;
  210. }
  211. all_trivial_requests = false;
  212. }
  213. return std::copy(results.begin(), results.end(), out);
  214. }
  215. /**
  216. * \overload
  217. */
  218. template<typename ForwardIterator>
  219. void
  220. wait_all(ForwardIterator first, ForwardIterator last)
  221. {
  222. typedef typename std::iterator_traits<ForwardIterator>::difference_type
  223. difference_type;
  224. using std::distance;
  225. difference_type num_outstanding_requests = distance(first, last);
  226. std::vector<bool> completed(num_outstanding_requests, false);
  227. while (num_outstanding_requests > 0) {
  228. bool all_trivial_requests = true;
  229. difference_type idx = 0;
  230. for (ForwardIterator current = first; current != last; ++current, ++idx) {
  231. if (!completed[idx]) {
  232. if (optional<status> stat = current->test()) {
  233. // This outstanding request has been completed.
  234. completed[idx] = true;
  235. --num_outstanding_requests;
  236. all_trivial_requests = false;
  237. } else {
  238. // Check if this request (and all others before it) are "trivial"
  239. // requests, e.g., they can be represented with a single
  240. // MPI_Request.
  241. all_trivial_requests = all_trivial_requests && current->trivial();
  242. }
  243. }
  244. }
  245. // If we have yet to fulfill any requests and all of the requests
  246. // are trivial (i.e., require only a single MPI_Request to be
  247. // fulfilled), call MPI_Waitall directly.
  248. if (all_trivial_requests
  249. && num_outstanding_requests == (difference_type)completed.size()) {
  250. std::vector<MPI_Request> requests;
  251. requests.reserve(num_outstanding_requests);
  252. for (ForwardIterator current = first; current != last; ++current)
  253. requests.push_back(*current->trivial());
  254. // Let MPI wait until all of these operations completes.
  255. BOOST_MPI_CHECK_RESULT(MPI_Waitall,
  256. (num_outstanding_requests, &requests[0],
  257. MPI_STATUSES_IGNORE));
  258. // Signal completion
  259. num_outstanding_requests = 0;
  260. }
  261. }
  262. }
  263. /**
  264. * @brief Tests whether all non-blocking requests have completed.
  265. *
  266. * This routine takes in a set of requests stored in the iterator
  267. * range @c [first,last) and determines whether all of these requests
  268. * have been completed. However, due to limitations of the underlying
  269. * MPI implementation, if any of the requests refers to a
  270. * non-blocking send or receive of a serialized data type, @c
  271. * test_all will always return the equivalent of @c false (i.e., the
  272. * requests cannot all be finished at this time). This routine
  273. * performs the same functionality as @c wait_all, except that this
  274. * routine will not block. This routine provides functionality
  275. * equivalent to @c MPI_Testall.
  276. *
  277. * @param first The iterator that denotes the beginning of the
  278. * sequence of request objects.
  279. *
  280. * @param last The iterator that denotes the end of the sequence of
  281. * request objects.
  282. *
  283. * @param out If provided and all requests hav been completed, an
  284. * output iterator through which the status of each request will be
  285. * emitted. The @c status objects are emitted in the same order as
  286. * the requests are retrieved from @c [first,last).
  287. *
  288. * @returns If an @p out parameter was provided, the value @c out
  289. * after all of the @c status objects have been emitted (if all
  290. * requests were completed) or an empty @c optional<>. If no @p out
  291. * parameter was provided, returns @c true if all requests have
  292. * completed or @c false otherwise.
  293. */
  294. template<typename ForwardIterator, typename OutputIterator>
  295. optional<OutputIterator>
  296. test_all(ForwardIterator first, ForwardIterator last, OutputIterator out)
  297. {
  298. std::vector<MPI_Request> requests;
  299. for (; first != last; ++first) {
  300. // If we have a non-trivial request, then no requests can be
  301. // completed.
  302. if (!first->trivial()) {
  303. return optional<OutputIterator>();
  304. }
  305. requests.push_back(*first->trivial());
  306. }
  307. int flag = 0;
  308. int n = requests.size();
  309. std::vector<MPI_Status> stats(n);
  310. BOOST_MPI_CHECK_RESULT(MPI_Testall, (n, &requests[0], &flag, &stats[0]));
  311. if (flag) {
  312. for (int i = 0; i < n; ++i, ++out) {
  313. status stat;
  314. stat.m_status = stats[i];
  315. *out = stat;
  316. }
  317. return out;
  318. } else {
  319. return optional<OutputIterator>();
  320. }
  321. }
  322. /**
  323. * \overload
  324. */
  325. template<typename ForwardIterator>
  326. bool
  327. test_all(ForwardIterator first, ForwardIterator last)
  328. {
  329. std::vector<MPI_Request> requests;
  330. for (; first != last; ++first) {
  331. // If we have a non-trivial request, then no requests can be
  332. // completed.
  333. if (!first->trivial()) {
  334. return false;
  335. }
  336. requests.push_back(*first->trivial());
  337. }
  338. int flag = 0;
  339. int n = requests.size();
  340. BOOST_MPI_CHECK_RESULT(MPI_Testall,
  341. (n, &requests[0], &flag, MPI_STATUSES_IGNORE));
  342. return flag != 0;
  343. }
  344. /**
  345. * @brief Wait until some non-blocking requests have completed.
  346. *
  347. * This routine takes in a set of requests stored in the iterator
  348. * range @c [first,last) and waits until at least one of the requests
  349. * has completed. It then completes all of the requests it can,
  350. * partitioning the input sequence into pending requests followed by
  351. * completed requests. If an output iterator is provided, @c status
  352. * objects will be emitted for each of the completed requests. This
  353. * routine provides functionality equivalent to @c MPI_Waitsome.
  354. *
  355. * @param first The iterator that denotes the beginning of the
  356. * sequence of request objects.
  357. *
  358. * @param last The iterator that denotes the end of the sequence of
  359. * request objects. This may not be equal to @c first.
  360. *
  361. * @param out If provided, the @c status objects corresponding to
  362. * completed requests will be emitted through this output iterator.
  363. * @returns If the @p out parameter was provided, a pair containing
  364. * the output iterator @p out after all of the @c status objects have
  365. * been written through it and an iterator referencing the first
  366. * completed request. If no @p out parameter was provided, only the
  367. * iterator referencing the first completed request will be emitted.
  368. */
  369. template<typename BidirectionalIterator, typename OutputIterator>
  370. std::pair<OutputIterator, BidirectionalIterator>
  371. wait_some(BidirectionalIterator first, BidirectionalIterator last,
  372. OutputIterator out)
  373. {
  374. using std::advance;
  375. if (first == last)
  376. return std::make_pair(out, first);
  377. typedef typename std::iterator_traits<BidirectionalIterator>::difference_type
  378. difference_type;
  379. bool all_trivial_requests = true;
  380. difference_type n = 0;
  381. BidirectionalIterator current = first;
  382. BidirectionalIterator start_of_completed = last;
  383. while (true) {
  384. // Check if we have found a completed request.
  385. if (optional<status> result = current->test()) {
  386. using std::iter_swap;
  387. // Emit the resulting status object
  388. *out++ = *result;
  389. // We're expanding the set of completed requests
  390. --start_of_completed;
  391. if (current == start_of_completed) {
  392. // If we have hit the end of the list of pending
  393. // requests. Finish up by fixing the order of the completed
  394. // set to match the order in which we emitted status objects,
  395. // then return.
  396. std::reverse(start_of_completed, last);
  397. return std::make_pair(out, start_of_completed);
  398. }
  399. // Swap the request we just completed with the last request that
  400. // has not yet been tested.
  401. iter_swap(current, start_of_completed);
  402. continue;
  403. }
  404. // Check if this request (and all others before it) are "trivial"
  405. // requests, e.g., they can be represented with a single
  406. // MPI_Request.
  407. all_trivial_requests = all_trivial_requests && current->trivial();
  408. // Move to the next request.
  409. ++n;
  410. if (++current == start_of_completed) {
  411. if (start_of_completed != last) {
  412. // We have satisfied some requests. Make the order of the
  413. // completed requests match that of the status objects we've
  414. // already emitted and we're done.
  415. std::reverse(start_of_completed, last);
  416. return std::make_pair(out, start_of_completed);
  417. }
  418. // We have reached the end of the list. If all requests thus far
  419. // have been trivial, we can call MPI_Waitsome directly, because
  420. // it may be more efficient than our busy-wait semantics.
  421. if (all_trivial_requests) {
  422. std::vector<MPI_Request> requests;
  423. std::vector<int> indices(n);
  424. std::vector<MPI_Status> stats(n);
  425. requests.reserve(n);
  426. for (current = first; current != last; ++current)
  427. requests.push_back(*current->trivial());
  428. // Let MPI wait until some of these operations complete.
  429. int num_completed;
  430. BOOST_MPI_CHECK_RESULT(MPI_Waitsome,
  431. (n, &requests[0], &num_completed, &indices[0],
  432. &stats[0]));
  433. // Translate the index-based result of MPI_Waitsome into a
  434. // partitioning on the requests.
  435. int current_offset = 0;
  436. current = first;
  437. for (int index = 0; index < num_completed; ++index, ++out) {
  438. using std::iter_swap;
  439. // Move "current" to the request object at this index
  440. advance(current, indices[index] - current_offset);
  441. current_offset = indices[index];
  442. // Emit the status object
  443. status stat;
  444. stat.m_status = stats[index];
  445. *out = stat;
  446. // Finish up the request and swap it into the "completed
  447. // requests" partition.
  448. *current->trivial() = requests[indices[index]];
  449. --start_of_completed;
  450. iter_swap(current, start_of_completed);
  451. }
  452. // We have satisfied some requests. Make the order of the
  453. // completed requests match that of the status objects we've
  454. // already emitted and we're done.
  455. std::reverse(start_of_completed, last);
  456. return std::make_pair(out, start_of_completed);
  457. }
  458. // There are some nontrivial requests, so we must continue our
  459. // busy waiting loop.
  460. n = 0;
  461. current = first;
  462. }
  463. }
  464. // We cannot ever get here
  465. BOOST_ASSERT(false);
  466. }
  467. /**
  468. * \overload
  469. */
  470. template<typename BidirectionalIterator>
  471. BidirectionalIterator
  472. wait_some(BidirectionalIterator first, BidirectionalIterator last)
  473. {
  474. using std::advance;
  475. if (first == last)
  476. return first;
  477. typedef typename std::iterator_traits<BidirectionalIterator>::difference_type
  478. difference_type;
  479. bool all_trivial_requests = true;
  480. difference_type n = 0;
  481. BidirectionalIterator current = first;
  482. BidirectionalIterator start_of_completed = last;
  483. while (true) {
  484. // Check if we have found a completed request.
  485. if (optional<status> result = current->test()) {
  486. using std::iter_swap;
  487. // We're expanding the set of completed requests
  488. --start_of_completed;
  489. // If we have hit the end of the list of pending requests, we're
  490. // done.
  491. if (current == start_of_completed)
  492. return start_of_completed;
  493. // Swap the request we just completed with the last request that
  494. // has not yet been tested.
  495. iter_swap(current, start_of_completed);
  496. continue;
  497. }
  498. // Check if this request (and all others before it) are "trivial"
  499. // requests, e.g., they can be represented with a single
  500. // MPI_Request.
  501. all_trivial_requests = all_trivial_requests && current->trivial();
  502. // Move to the next request.
  503. ++n;
  504. if (++current == start_of_completed) {
  505. // If we have satisfied some requests, we're done.
  506. if (start_of_completed != last)
  507. return start_of_completed;
  508. // We have reached the end of the list. If all requests thus far
  509. // have been trivial, we can call MPI_Waitsome directly, because
  510. // it may be more efficient than our busy-wait semantics.
  511. if (all_trivial_requests) {
  512. std::vector<MPI_Request> requests;
  513. std::vector<int> indices(n);
  514. requests.reserve(n);
  515. for (current = first; current != last; ++current)
  516. requests.push_back(*current->trivial());
  517. // Let MPI wait until some of these operations complete.
  518. int num_completed;
  519. BOOST_MPI_CHECK_RESULT(MPI_Waitsome,
  520. (n, &requests[0], &num_completed, &indices[0],
  521. MPI_STATUSES_IGNORE));
  522. // Translate the index-based result of MPI_Waitsome into a
  523. // partitioning on the requests.
  524. int current_offset = 0;
  525. current = first;
  526. for (int index = 0; index < num_completed; ++index) {
  527. using std::iter_swap;
  528. // Move "current" to the request object at this index
  529. advance(current, indices[index] - current_offset);
  530. current_offset = indices[index];
  531. // Finish up the request and swap it into the "completed
  532. // requests" partition.
  533. *current->trivial() = requests[indices[index]];
  534. --start_of_completed;
  535. iter_swap(current, start_of_completed);
  536. }
  537. // We have satisfied some requests, so we are done.
  538. return start_of_completed;
  539. }
  540. // There are some nontrivial requests, so we must continue our
  541. // busy waiting loop.
  542. n = 0;
  543. current = first;
  544. }
  545. }
  546. // We cannot ever get here
  547. BOOST_ASSERT(false);
  548. }
  549. /**
  550. * @brief Test whether some non-blocking requests have completed.
  551. *
  552. * This routine takes in a set of requests stored in the iterator
  553. * range @c [first,last) and tests to see if any of the requests has
  554. * completed. It completes all of the requests it can, partitioning
  555. * the input sequence into pending requests followed by completed
  556. * requests. If an output iterator is provided, @c status objects
  557. * will be emitted for each of the completed requests. This routine
  558. * is similar to @c wait_some, but does not wait until any requests
  559. * have completed. This routine provides functionality equivalent to
  560. * @c MPI_Testsome.
  561. *
  562. * @param first The iterator that denotes the beginning of the
  563. * sequence of request objects.
  564. *
  565. * @param last The iterator that denotes the end of the sequence of
  566. * request objects. This may not be equal to @c first.
  567. *
  568. * @param out If provided, the @c status objects corresponding to
  569. * completed requests will be emitted through this output iterator.
  570. * @returns If the @p out parameter was provided, a pair containing
  571. * the output iterator @p out after all of the @c status objects have
  572. * been written through it and an iterator referencing the first
  573. * completed request. If no @p out parameter was provided, only the
  574. * iterator referencing the first completed request will be emitted.
  575. */
  576. template<typename BidirectionalIterator, typename OutputIterator>
  577. std::pair<OutputIterator, BidirectionalIterator>
  578. test_some(BidirectionalIterator first, BidirectionalIterator last,
  579. OutputIterator out)
  580. {
  581. BidirectionalIterator current = first;
  582. BidirectionalIterator start_of_completed = last;
  583. while (current != start_of_completed) {
  584. // Check if we have found a completed request.
  585. if (optional<status> result = current->test()) {
  586. using std::iter_swap;
  587. // Emit the resulting status object
  588. *out++ = *result;
  589. // We're expanding the set of completed requests
  590. --start_of_completed;
  591. // Swap the request we just completed with the last request that
  592. // has not yet been tested.
  593. iter_swap(current, start_of_completed);
  594. continue;
  595. }
  596. // Move to the next request.
  597. ++current;
  598. }
  599. // Finish up by fixing the order of the completed set to match the
  600. // order in which we emitted status objects, then return.
  601. std::reverse(start_of_completed, last);
  602. return std::make_pair(out, start_of_completed);
  603. }
  604. /**
  605. * \overload
  606. */
  607. template<typename BidirectionalIterator>
  608. BidirectionalIterator
  609. test_some(BidirectionalIterator first, BidirectionalIterator last)
  610. {
  611. BidirectionalIterator current = first;
  612. BidirectionalIterator start_of_completed = last;
  613. while (current != start_of_completed) {
  614. // Check if we have found a completed request.
  615. if (optional<status> result = current->test()) {
  616. using std::iter_swap;
  617. // We're expanding the set of completed requests
  618. --start_of_completed;
  619. // Swap the request we just completed with the last request that
  620. // has not yet been tested.
  621. iter_swap(current, start_of_completed);
  622. continue;
  623. }
  624. // Move to the next request.
  625. ++current;
  626. }
  627. return start_of_completed;
  628. }
  629. } } // end namespace boost::mpi
  630. #endif // BOOST_MPI_NONBLOCKING_HPP