Debian Codegraph @

packages/teuchos/kokkoscomm/src/Kokkos_TeuchosCommAdapters.hpp

raw
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16
17 #ifndef KOKKOS_TEUCHOS_COMM_ADAPTERS_HPP
18 #define KOKKOS_TEUCHOS_COMM_ADAPTERS_HPP
19
20 #include <TeuchosKokkosComm_config.h>
21 #include <Teuchos_CommHelpers.hpp>
22 #include <Kokkos_Core.hpp>
23 #include <KokkosCompat_View.hpp>
24
25 /// \file Kokkos_TeuchosCommAdapters.hpp
26 /// \brief Adapters for Teuchos::Comm functions for Kokkos:View.
27 ///
28 /// Currently these just pass the Kokkos::View pointer along to the existing
29 /// Teuchos::Comm functions, but in the future could be more adapted
30 /// implementations.
31 ///
32 /// Not all comm functions have been overloaded.
33
34 namespace Teuchos {
35
36 //! Variant of send() that takes a tag (and restores the correct order of arguments).
37 template<typename Ordinal, class ViewType>
38 typename std::enable_if<(Kokkos::is_view<ViewType>::value)>::type
39 send (const ViewType& sendBuffer,
40 const Ordinal count,
41 const int destRank,
42 const int tag,
43 const Comm<Ordinal>& comm)
44 {
45 send(sendBuffer.data(), count, destRank, tag, comm);
46 }
47
48 //! Variant of ssend() that takes a tag (and restores the correct order of arguments).
49 template<typename Ordinal, class ViewType>
50 typename std::enable_if<(Kokkos::is_view<ViewType>::value)>::type
51 ssend (const ViewType& sendBuffer,
52 const Ordinal count,
53 const int destRank,
54 const int tag,
55 const Comm<Ordinal>& comm)
56 {
57 ssend(sendBuffer.data(), count, destRank, tag, comm);
58 }
59
60 //! Variant of readySend() that accepts a message tag.
61 template<typename Ordinal, class ViewType>
62 typename std::enable_if<(Kokkos::is_view<ViewType>::value)>::type
63 readySend (const ViewType& sendBuffer,
64 const Ordinal count,
65 const int destRank,
66 const int tag,
67 const Comm<Ordinal>& comm)
68 {
69 readySend(sendBuffer.data(), count, destRank, tag, comm);
70 }
71
72 //! Variant of isend() that takes a tag (and restores the correct order of arguments).
73 template<typename Ordinal, class ViewType>
74 typename std::enable_if<(Kokkos::is_view<ViewType>::value),RCP<CommRequest<Ordinal> >>::type
75 isend (const ViewType& sendBuffer,
76 const int destRank,
77 const int tag,
78 const Comm<Ordinal>& comm)
79 {
80 using Kokkos::Compat::persistingView;
81 // See Issue #1454: https://github.com/trilinos/Trilinos/issues/1454
82 typename ViewType::const_type sendBuffer_const = sendBuffer;
83 return isend (persistingView (sendBuffer_const), destRank, tag, comm);
84 }
85
86 //! Variant of ireceive that takes a tag argument (and restores the correct order of arguments).
87 template<typename Ordinal, class ViewType>
88 typename std::enable_if<(Kokkos::is_view<ViewType>::value),RCP<CommRequest<Ordinal> >>::type
89 ireceive (const ViewType& recvBuffer,
90 const int sourceRank,
91 const int tag,
92 const Comm<Ordinal>& comm)
93 {
94 using Kokkos::Compat::persistingView;
95 return ireceive(persistingView(recvBuffer), sourceRank, tag, comm);
96 }
97
98
99 template<typename Ordinal, typename SendViewType, typename RecvViewType>
100 typename std::enable_if<(Kokkos::is_view<SendViewType>::value && Kokkos::is_view<RecvViewType>::value)>::type
101 reduceAll (const SendViewType& sendBuf,
102 const RecvViewType& recvBuf,
103 const EReductionType reductionType,
104 const Comm<Ordinal>& comm)
105 {
106 // We can't use the array of intrinsic scalar type
107 // ((non_)const_array_intrinsic_type) here, because we're doing a
108 // reduction. That means we need to compute with the actual value
109 // type.
110 typedef typename SendViewType::value_type send_value_type;
111 typedef typename RecvViewType::value_type recv_value_type;
112
113 const bool typesDiffer =
114 ! std::is_same<send_value_type, recv_value_type>::value;
115 TEUCHOS_TEST_FOR_EXCEPTION(
116 typesDiffer, std::invalid_argument, "Teuchos::reduceAll: Send and receive "
117 "Views contain data of different types.");
118 TEUCHOS_TEST_FOR_EXCEPTION(
119 SendViewType::rank > 1 || RecvViewType::rank > 1, std::invalid_argument,
120 "Teuchos::reduceAll: Both send and receive Views must have rank 1. "
121 "The send View's rank is " << SendViewType::rank << " and the receive "
122 "View's rank is " << RecvViewType::rank << ".");
123 TEUCHOS_TEST_FOR_EXCEPTION(
124 sendBuf.extent (0) != recvBuf.extent (0), std::invalid_argument,
125 "Send and receive buffer lengths do not match. sendBuf.extent(0) = "
126 << sendBuf.extent (0) << " != recvBuf.extent(0) = "
127 << recvBuf.extent (0) << ".");
128
129 // mfh 04 Nov 2014: Don't let Teuchos::SerialComm do a deep copy;
130 // that always happens on the host, since SerialComm doesn't know
131 // about Kokkos.
132 if (comm.getSize () == 1) {
133 Kokkos::deep_copy (recvBuf, sendBuf);
134 }
135 else {
136 const Ordinal count = static_cast<Ordinal> (sendBuf.extent (0));
137 reduceAll (comm, reductionType, count,
138 sendBuf.data (),
139 recvBuf.data ());
140 }
141 }
142
143 template<typename Ordinal, typename Serializer,
144 class SendViewType,
145 class RecvViewType>
146 typename std::enable_if<(Kokkos::is_view<SendViewType>::value && Kokkos::is_view<RecvViewType>::value)>::type
147 reduceAll(const Comm<Ordinal>& comm,
148 const Serializer& serializer,
149 const EReductionType reductType,
150 const Ordinal count,
151 const SendViewType& sendBuffer,
152 const RecvViewType& recvBuffer)
153 {
154 // We can't use the array of intrinsic scalar type
155 // ((non_)const_array_intrinsic_type) here, because we're doing a
156 // reduction. That means we need to compute with the actual value
157 // type.
158 typedef typename SendViewType::value_type send_value_type;
159 typedef typename RecvViewType::value_type recv_value_type;
160
161 const bool typesDiffer =
162 ! std::is_same<send_value_type, recv_value_type>::value;
163 TEUCHOS_TEST_FOR_EXCEPTION(
164 typesDiffer, std::invalid_argument, "Teuchos::reduceAll: Send and receive "
165 "Views contain data of different types.");
166 TEUCHOS_TEST_FOR_EXCEPTION(
167 SendViewType::rank > 1 || RecvViewType::rank > 1, std::invalid_argument,
168 "Teuchos::reduceAll: Both send and receive Views must have rank 1. "
169 "The send View's rank is " << SendViewType::rank << " and the receive "
170 "View's rank is " << RecvViewType::rank << ".");
171
172 // mfh 04 Nov 2014: Don't let Teuchos::SerialComm do a deep copy;
173 // that always happens on the host, since SerialComm doesn't know
174 // about Kokkos.
175 if (comm.getSize () == 1) {
176 Kokkos::deep_copy (recvBuffer, sendBuffer);
177 }
178 else {
179 reduceAll (comm, serializer, reductType, count,
180 sendBuffer.data (),
181 recvBuffer.data ());
182 }
183 }
184
185 template<typename Ordinal,
186 class ViewType>
187 typename std::enable_if<(Kokkos::is_view<ViewType>::value)>::type
188 broadcast(const Comm<Ordinal>& comm,
189 const int rootRank,
190 const Ordinal count,
191 const ViewType& buffer)
192 {
193 broadcast( comm, rootRank, count, buffer.data() );
194 }
195
196 template<typename Ordinal,
197 class ViewType,
198 typename Serializer>
199 typename std::enable_if<(Kokkos::is_view<ViewType>::value)>::type
200 broadcast(const Comm<Ordinal>& comm,
201 const Serializer& serializer,
202 const int rootRank,
203 const Ordinal count,
204 const ViewType& buffer)
205 {
206 broadcast( comm, serializer, rootRank, count, buffer.data() );
207 }
208
209 } // namespace Teuchos
210
211 #endif // KOKKOS_TEUCHOS_COMM_ADAPTERS_HPP