44 #ifndef TPETRA_FOR_EACH_MULTIVECTOR_HPP
45 #define TPETRA_FOR_EACH_MULTIVECTOR_HPP
50 #include "Tpetra_Map.hpp"
51 #include "Teuchos_Comm.hpp"
52 #include "Teuchos_TypeNameTraits.hpp"
53 #include "Kokkos_Core.hpp"
79 template<
class ViewType,
80 class UserFunctionType,
82 class MultiVectorForEachLoopBody {
84 static_assert (
static_cast<int> (ViewType::Rank) == 2,
85 "ViewType must be a rank-2 Kokkos::View.");
87 MultiVectorForEachLoopBody (
const ViewType& X_lcl,
89 X_lcl_ (X_lcl), f_ (f)
91 KOKKOS_INLINE_FUNCTION
void
92 operator () (
const LocalIndexType i)
const {
93 const LocalIndexType numCols =
94 static_cast<LocalIndexType
> (X_lcl_.extent (1));
95 for (LocalIndexType j = 0; j < numCols; ++j) {
108 template<
class ViewType,
109 class UserFunctionType,
110 class LocalIndexType>
111 class VectorForEachLoopBody {
113 static_assert (
static_cast<int> (ViewType::Rank) == 1,
114 "ViewType must be a rank-1 Kokkos::View.");
116 VectorForEachLoopBody (
const ViewType& X_lcl,
117 UserFunctionType f) :
118 X_lcl_ (X_lcl), f_ (f)
120 KOKKOS_INLINE_FUNCTION
void
121 operator () (
const LocalIndexType i)
const {
131 template<
class ExecutionSpace,
132 class SC,
class LO,
class GO,
class NT,
133 class UserFunctionType>
143 template<
class MemorySpace>
144 using for_each_memory_space =
145 typename std::conditional<
146 Kokkos::SpaceAccessibility<
148 typename MemorySpace::memory_space>::accessible,
149 typename MemorySpace::memory_space,
150 typename ExecutionSpace::memory_space>::type;
154 for_each (
const char kernelLabel[],
155 ExecutionSpace execSpace,
159 using Teuchos::TypeNameTraits;
162 using preferred_memory_space =
163 typename MV::device_type::memory_space;
164 using memory_space = for_each_memory_space<preferred_memory_space>;
165 using range_type = Kokkos::RangePolicy<ExecutionSpace, LO>;
167 const int myRank = X.
getMap ()->getComm ()->getRank ();
170 std::ostringstream os;
171 os <<
"Proc " << myRank <<
": Tpetra::for_each:" << endl
172 <<
" kernelLabel: " << kernelLabel << endl
173 <<
" ExecutionSpace: "
174 << TypeNameTraits<ExecutionSpace>::name () << endl
176 << TypeNameTraits<memory_space>::name () << endl;
177 std::cerr << os.str ();
185 memory_space memSpace;
188 for (
size_t j = 0; j < numVecs; ++j) {
194 using read_write_view_type =
196 decltype (
readWrite (X_j_ref).on (memSpace))>;
198 ([=] (
const read_write_view_type& X_j_lcl) {
199 using functor_type = VectorForEachLoopBody<
200 read_write_view_type, UserFunctionType, LO>;
201 Kokkos::parallel_for (kernelLabel, range,
202 functor_type (X_j_lcl, f));
208 using read_write_view_type =
212 ([=] (
const read_write_view_type& X_lcl) {
213 using functor_type = MultiVectorForEachLoopBody<
214 read_write_view_type, UserFunctionType, LO>;
215 Kokkos::parallel_for (kernelLabel, range,
216 functor_type (X_lcl, f));
228 template<
class ExecutionSpace,
229 class SC,
class LO,
class GO,
class NT,
230 class UserFunctionType>
236 for_each (
const char kernelLabel[],
237 ExecutionSpace execSpace,
251 #endif // TPETRA_FOR_EACH_MULTIVECTOR_HPP