42 #ifndef TPETRA_DETAILS_IALLREDUCE_HPP
43 #define TPETRA_DETAILS_IALLREDUCE_HPP
61 #include "TpetraCore_config.h"
62 #include "Teuchos_EReductionType.hpp"
63 #ifdef HAVE_TPETRACORE_MPI
66 #endif // HAVE_TPETRACORE_MPI
67 #include "Kokkos_Core.hpp"
70 #include <type_traits>
73 #ifndef DOXYGEN_SHOULD_SKIP_THIS
76 template<
class OrdinalType>
class Comm;
78 #endif // NOT DOXYGEN_SHOULD_SKIP_THIS
98 virtual void wait () = 0;
103 virtual void cancel () = 0;
113 std::shared_ptr<CommRequest>
116 #ifdef HAVE_TPETRACORE_MPI
131 class MpiCommRequest :
public CommRequest {
137 MpiCommRequest (
const MPI_Request req);
140 virtual ~MpiCommRequest ();
147 virtual void wait ();
155 virtual void waitWithStatus (MPI_Status& status);
158 virtual void cancel ();
163 #endif // HAVE_TPETRACORE_MPI
193 std::function<void(void) > action_;
224 template<
class PacketType,
225 class SendLayoutType,
226 class SendDeviceType,
227 class RecvLayoutType,
228 class RecvDeviceType,
233 template<
class PacketType,
234 class SendLayoutType,
235 class SendDeviceType,
236 class RecvLayoutType,
237 class RecvDeviceType>
251 typedef ::Kokkos::View<
const PacketType*, SendLayoutType,
254 typedef ::Kokkos::View<PacketType*, RecvLayoutType,
267 static_assert (std::is_same<SendLayoutType, ::Kokkos::LayoutLeft>::value ||
268 std::is_same<SendLayoutType, ::Kokkos::LayoutRight>::value,
269 "SendLayoutType must be either Kokkos::LayoutLeft or "
270 "Kokkos::LayoutRight.");
271 static_assert (std::is_same<RecvLayoutType, ::Kokkos::LayoutLeft>::value ||
272 std::is_same<RecvLayoutType, ::Kokkos::LayoutRight>::value,
273 "RecvLayoutType must be either Kokkos::LayoutLeft or "
274 "Kokkos::LayoutRight.");
279 if (req_.get () != NULL) {
288 req_ = std::shared_ptr<CommRequest> ();
301 if (req_.get () != NULL) {
304 req_ = std::shared_ptr<CommRequest> ();
318 if (req_.get () != NULL) {
321 req_ = std::shared_ptr<CommRequest> ();
331 std::shared_ptr<CommRequest> req_;
334 send_buffer_type sendbuf_;
337 recv_buffer_type recvbuf_;
341 template<
class PacketType,
342 class SendLayoutType,
343 class SendDeviceType,
344 class RecvLayoutType,
345 class RecvDeviceType>
359 typedef ::Kokkos::View<
const PacketType, SendLayoutType,
362 typedef ::Kokkos::View<PacketType, RecvLayoutType,
375 static_assert (std::is_same<SendLayoutType, ::Kokkos::LayoutLeft>::value ||
376 std::is_same<SendLayoutType, ::Kokkos::LayoutRight>::value,
377 "SendLayoutType must be either Kokkos::LayoutLeft or "
378 "Kokkos::LayoutRight.");
379 static_assert (std::is_same<RecvLayoutType, ::Kokkos::LayoutLeft>::value ||
380 std::is_same<RecvLayoutType, ::Kokkos::LayoutRight>::value,
381 "RecvLayoutType must be either Kokkos::LayoutLeft or "
382 "Kokkos::LayoutRight.");
387 if (req_.get () != NULL) {
396 req_ = std::shared_ptr<CommRequest> ();
409 if (req_.get () != NULL) {
412 req_ = std::shared_ptr<CommRequest> ();
426 if (req_.get () != NULL) {
429 req_ = std::shared_ptr<CommRequest> ();
439 std::shared_ptr<CommRequest> req_;
442 send_buffer_type sendbuf_;
445 recv_buffer_type recvbuf_;
480 template<
class PacketType,
481 class SendLayoutType,
482 class SendDeviceType,
483 class RecvLayoutType,
484 class RecvDeviceType>
485 std::shared_ptr<CommRequest>
486 wrapIallreduceCommRequest (
const std::shared_ptr<CommRequest>& req,
487 const ::Kokkos::View<
const PacketType*,
489 SendDeviceType>& sendbuf,
490 const ::Kokkos::View<PacketType*,
492 RecvDeviceType>& recvbuf)
494 static_assert (std::is_same<SendLayoutType, ::Kokkos::LayoutLeft>::value ||
495 std::is_same<SendLayoutType, ::Kokkos::LayoutRight>::value,
496 "SendLayoutType must be either Kokkos::LayoutLeft or "
497 "Kokkos::LayoutRight.");
498 static_assert (std::is_same<RecvLayoutType, ::Kokkos::LayoutLeft>::value ||
499 std::is_same<RecvLayoutType, ::Kokkos::LayoutRight>::value,
500 "RecvLayoutType must be either Kokkos::LayoutLeft or "
501 "Kokkos::LayoutRight.");
502 typedef IallreduceCommRequest<PacketType, SendLayoutType, SendDeviceType,
503 RecvLayoutType, RecvDeviceType, 1> req_type;
504 return std::shared_ptr<CommRequest> (
new req_type (req, sendbuf, recvbuf));
539 template<
class PacketType,
540 class SendLayoutType,
541 class SendDeviceType,
542 class RecvLayoutType,
543 class RecvDeviceType>
544 std::shared_ptr<CommRequest>
545 wrapIallreduceCommRequest (
const std::shared_ptr<CommRequest>& req,
546 const ::Kokkos::View<
const PacketType,
548 SendDeviceType>& sendbuf,
549 const ::Kokkos::View<PacketType,
551 RecvDeviceType>& recvbuf)
553 static_assert (std::is_same<SendLayoutType, ::Kokkos::LayoutLeft>::value ||
554 std::is_same<SendLayoutType, ::Kokkos::LayoutRight>::value,
555 "SendLayoutType must be either Kokkos::LayoutLeft or "
556 "Kokkos::LayoutRight.");
557 static_assert (std::is_same<RecvLayoutType, ::Kokkos::LayoutLeft>::value ||
558 std::is_same<RecvLayoutType, ::Kokkos::LayoutRight>::value,
559 "RecvLayoutType must be either Kokkos::LayoutLeft or "
560 "Kokkos::LayoutRight.");
561 typedef IallreduceCommRequest<PacketType, SendLayoutType, SendDeviceType,
562 RecvLayoutType, RecvDeviceType, 0> req_type;
563 return std::shared_ptr<CommRequest> (
new req_type (req, sendbuf, recvbuf));
566 #ifdef HAVE_TPETRACORE_MPI
578 std::shared_ptr<CommRequest>
579 iallreduceRawVoid (
const void* sendbuf,
582 MPI_Datatype mpiDatatype,
583 const bool mpiDatatypeNeedsFree,
584 const Teuchos::EReductionType op,
587 #endif // HAVE_TPETRACORE_MPI
597 template<
class Packet>
598 std::shared_ptr<CommRequest>
599 iallreduceRaw (
const Packet sendbuf[],
602 const ::Teuchos::EReductionType op,
603 const ::Teuchos::Comm<int>& comm)
605 #ifdef HAVE_TPETRACORE_MPI
606 using ::Tpetra::Details::MpiTypeTraits;
621 MPI_Datatype mpiDatatype = (count > 0) ?
622 MpiTypeTraits<Packet>::getType (sendbuf[0]) :
624 MPI_Comm rawComm = ::Tpetra::Details::extractMpiCommFromTeuchos (comm);
625 return iallreduceRawVoid (sendbuf, recvbuf, count, mpiDatatype,
626 MpiTypeTraits<Packet>::needsFree, op, rawComm);
627 #else // NOT HAVE_TPETRACORE_MPI
628 throw std::logic_error (
"Tpetra::Details::Impl::iallreduceRaw: This function "
629 "should never be called if MPI is not enabled. "
630 "Please report this bug to the Tpetra developers.");
631 #endif // HAVE_TPETRACORE_MPI
656 template<
class PacketType,
657 class SendLayoutType,
658 class SendDeviceType,
659 class RecvLayoutType,
660 class RecvDeviceType,
665 template<
class PacketType,
666 class SendLayoutType,
667 class SendDeviceType,
668 class RecvLayoutType,
669 class RecvDeviceType>
676 typedef ::Kokkos::View<
const PacketType*,
678 SendDeviceType> send_buffer_type;
679 typedef ::Kokkos::View<PacketType*,
681 RecvDeviceType> recv_buffer_type;
683 static std::shared_ptr<CommRequest>
684 iallreduce (
const send_buffer_type& sendbuf,
685 const recv_buffer_type& recvbuf,
686 const ::Teuchos::EReductionType op,
687 const ::Teuchos::Comm<int>& comm)
689 static_assert (! std::is_const<PacketType>::value,
690 "PacketType must be a nonconst type.");
691 static_assert (std::is_same<SendLayoutType, ::Kokkos::LayoutLeft>::value ||
692 std::is_same<SendLayoutType, ::Kokkos::LayoutRight>::value,
693 "SendLayoutType must be either Kokkos::LayoutLeft or "
694 "Kokkos::LayoutRight.");
695 static_assert (std::is_same<RecvLayoutType, ::Kokkos::LayoutLeft>::value ||
696 std::is_same<RecvLayoutType, ::Kokkos::LayoutRight>::value,
697 "RecvLayoutType must be either Kokkos::LayoutLeft or "
698 "Kokkos::LayoutRight.");
699 #ifdef HAVE_TPETRACORE_MPI
702 typedef typename std::remove_const<PacketType>::type packet_type;
704 std::shared_ptr<CommRequest> req =
705 Impl::iallreduceRaw<packet_type> (sendbuf.data (),
707 static_cast<int> (sendbuf.extent (0)),
709 return Impl::wrapIallreduceCommRequest (req, sendbuf, recvbuf);
710 #else // NOT HAVE_TPETRACORE_MPI
717 return Impl::emptyCommRequest ();
718 #endif // HAVE_TPETRACORE_MPI
723 template<
class PacketType,
724 class SendLayoutType,
725 class SendDeviceType,
726 class RecvLayoutType,
727 class RecvDeviceType>
738 typedef ::Kokkos::View<
const PacketType,
740 SendDeviceType> send_buffer_type;
741 typedef ::Kokkos::View<PacketType,
743 RecvDeviceType> recv_buffer_type;
745 static std::shared_ptr<CommRequest>
746 iallreduce (
const send_buffer_type& sendbuf,
747 const recv_buffer_type& recvbuf,
748 const ::Teuchos::EReductionType op,
749 const ::Teuchos::Comm<int>& comm)
751 static_assert (! std::is_const<PacketType>::value,
752 "PacketType must be a nonconst type.");
753 static_assert (std::is_same<SendLayoutType, ::Kokkos::LayoutLeft>::value ||
754 std::is_same<SendLayoutType, ::Kokkos::LayoutRight>::value,
755 "SendLayoutType must be either Kokkos::LayoutLeft or "
756 "Kokkos::LayoutRight.");
757 static_assert (std::is_same<RecvLayoutType, ::Kokkos::LayoutLeft>::value ||
758 std::is_same<RecvLayoutType, ::Kokkos::LayoutRight>::value,
759 "RecvLayoutType must be either Kokkos::LayoutLeft or "
760 "Kokkos::LayoutRight.");
761 #ifdef HAVE_TPETRACORE_MPI
764 typedef typename std::remove_const<PacketType>::type packet_type;
766 std::shared_ptr<CommRequest> req =
767 Impl::iallreduceRaw<packet_type> (sendbuf.data (),
769 static_cast<int> (1),
771 return Impl::wrapIallreduceCommRequest (req, sendbuf, recvbuf);
772 #else // NOT HAVE_TPETRACORE_MPI
779 return Impl::emptyCommRequest ();
780 #endif // HAVE_TPETRACORE_MPI
815 template<
class InputViewType,
class OutputViewType>
816 std::shared_ptr<CommRequest>
817 iallreduce (
const InputViewType& sendbuf,
818 const OutputViewType& recvbuf,
819 const ::Teuchos::EReductionType op,
820 const ::Teuchos::Comm<int>& comm)
822 static_assert (Kokkos::Impl::is_view<InputViewType>::value,
823 "InputViewType must be a Kokkos::View specialization.");
824 static_assert (Kokkos::Impl::is_view<OutputViewType>::value,
825 "OutputViewType must be a Kokkos::View specialization.");
826 constexpr
int rank =
static_cast<int> (OutputViewType::rank);
827 static_assert (static_cast<int> (InputViewType::rank) == rank,
828 "InputViewType and OutputViewType must have the same rank.");
829 static_assert (rank == 0 || rank == 1,
830 "InputViewType and OutputViewType must both have "
831 "rank 0 or rank 1.");
832 typedef typename OutputViewType::non_const_value_type packet_type;
833 static_assert (std::is_same<
typename OutputViewType::value_type,
835 "OutputViewType must be a nonconst Kokkos::View.");
836 static_assert (std::is_same<
typename InputViewType::non_const_value_type,
838 "InputViewType and OutputViewType must be Views "
839 "whose entries have the same type.");
840 typedef typename InputViewType::array_layout send_layout_type;
841 typedef typename OutputViewType::array_layout recv_layout_type;
842 typedef typename InputViewType::device_type send_device_type;
843 typedef typename OutputViewType::device_type recv_device_type;
846 recv_layout_type, recv_device_type, rank> impl_type;
850 std::shared_ptr<CommRequest>
851 iallreduce (
const int localValue,
853 const ::Teuchos::EReductionType op,
854 const ::Teuchos::Comm<int>& comm);
859 #endif // TPETRA_DETAILS_IALLREDUCE_HPP
IallreduceCommRequest(const std::shared_ptr< CommRequest > &req, const send_buffer_type &sendbuf, const recv_buffer_type &recvbuf)
Constructor that takes a wrapped request (representing the pending MPI_Iallreduce operation itself)...
Add specializations of Teuchos::Details::MpiTypeTraits for Kokkos::complex<float> and Kokkos::complex...
void cancel()
Cancel the pending communication request.
void wait()
Wait on this communication request to complete.
::Kokkos::View< const PacketType, SendLayoutType, SendDeviceType > send_buffer_type
Type of the send buffer.
void wait()
Wait on this communication request to complete.
Object representing a pending ::Tpetra::Details::iallreduce operation.
DeferredActionCommRequest()
Default constructor (take no action on wait).
::Kokkos::View< const PacketType *, SendLayoutType, SendDeviceType > send_buffer_type
Type of the send buffer.
void cancel()
Cancel the pending communication request, without taking the specified action.
void deep_copy(MultiVector< DS, DL, DG, DN > &dst, const MultiVector< SS, SL, SG, SN > &src)
Copy the contents of the MultiVector src into dst.
virtual ~IallreduceCommRequest()
Destructor (virtual for memory safety).
::Kokkos::View< PacketType *, RecvLayoutType, RecvDeviceType > recv_buffer_type
Type of the receive buffer.
virtual void wait()=0
Wait on this communication request to complete.
Implementation of ::Tpetra::Details::iallreduce.
Part of the Work-around for not having MPI >= 3.
void cancel()
Cancel the pending communication request.
IallreduceCommRequest()
Default constructor.
::Kokkos::View< PacketType, RecvLayoutType, RecvDeviceType > recv_buffer_type
Type of the receive buffer.
IallreduceCommRequest(const std::shared_ptr< CommRequest > &req, const send_buffer_type &sendbuf, const recv_buffer_type &recvbuf)
Constructor that takes a wrapped request (representing the pending MPI_Iallreduce operation itself)...
virtual ~IallreduceCommRequest()
Destructor (virtual for memory safety).
void wait()
Wait on this communication request to complete.
Base class for the request (more or less a future) representing a pending nonblocking MPI operation...
std::shared_ptr< CommRequest > iallreduce(const InputViewType &sendbuf, const OutputViewType &recvbuf, const ::Teuchos::EReductionType op, const ::Teuchos::Comm< int > &comm)
Nonblocking all-reduce, for either rank-1 or rank-0 Kokkos::View objects.
virtual void cancel()=0
Cancel the pending communication request.
IallreduceCommRequest()
Default constructor.
virtual ~CommRequest()
Destructor (virtual for memory safety of derived classes).