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). at(execSpace))>;
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));
203 },
readWrite (X_j_ref).on (memSpace). at(execSpace));
208 using read_write_view_type =
210 decltype (
readWrite (X).on (memSpace). at(execSpace))>;
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));
217 },
readWrite (X).on (memSpace). at(execSpace));
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
Include this file to make Tpetra::MultiVector and Tpetra::Vector work with Tpetra::withLocalAccess.
Declaration and definition of Tpetra::for_each; declaration of helper classes for users to specialize...
size_t getNumVectors() const
Number of columns in the multivector.
size_t getLocalLength() const
Local number of rows on the calling process.
bool isConstantStride() const
Whether this multivector has constant stride between columns.
One or more distributed dense vectors.
Teuchos::RCP< Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getVectorNonConst(const size_t j)
Return a Vector which is a nonconst view of column j.
Details::LocalAccess< GlobalObjectType, Details::read_write > readWrite(GlobalObjectType &)
Declare that you want to access the given global object's local data in read-and-write mode...
static bool verbose()
Whether Tpetra is in verbose mode.
void withLocalAccess(typename Details::ArgsToFunction< LocalAccessTypes...>::type userFunction, LocalAccessTypes...localAccesses)
Get access to a Tpetra global object's local data.
A distributed dense vector.
Specialize this class to implement Tpetra::for_each for specific GlobalDataStructure types...
virtual Teuchos::RCP< const map_type > getMap() const
The Map describing the parallel distribution of this object.
typename Details::GetNonowningLocalObject< LocalAccessType >::nonowning_local_object_type with_local_access_function_argument_type
Type of the local object, that is an argument to the function the user gives to withLocalAccess.
void for_each(const char kernelLabel[], ExecutionSpace execSpace, GlobalDataStructure &X, UserFunctionType f)
Apply a function entrywise to each local entry of a Tpetra global data structure, analogously to std:...
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra's behavior.