53#ifndef AMESOS2_TPETRA_MULTIVEC_ADAPTER_DEF_HPP
54#define AMESOS2_TPETRA_MULTIVEC_ADAPTER_DEF_HPP
63 using Tpetra::MultiVector;
65 template <
typename Scalar,
typename LocalOrdinal,
typename GlobalOrdinal,
class Node >
70 Node> >::MultiVecAdapter(
const Teuchos::RCP<multivec_t>& m )
74 template <
typename Scalar,
typename LocalOrdinal,
typename GlobalOrdinal,
class Node >
84 Node> >::clone()
const
86 using MV = MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
87 Teuchos::RCP<MV> Y (
new MV (mv_->getMap(), mv_->getNumVectors(),
false));
88 Y->setCopyOrView (Teuchos::View);
92 template <
typename Scalar,
typename LocalOrdinal,
typename GlobalOrdinal,
class Node >
93 typename MultiVecAdapter<
97 Node> >::multivec_t::impl_scalar_type *
102 Node> >::getMVPointer_impl()
const
104 TEUCHOS_TEST_FOR_EXCEPTION( this->getGlobalNumVectors() != 1,
105 std::invalid_argument,
106 "Amesos2_TpetraMultiVectorAdapter: getMVPointer_impl should only be called for case with a single vector and single MPI process" );
108 auto contig_local_view_2d = mv_->getLocalViewHost(Tpetra::Access::ReadWrite);
109 auto contig_local_view_1d = Kokkos::subview (contig_local_view_2d, Kokkos::ALL (), 0);
110 return contig_local_view_1d.data();
121 template <
typename Scalar,
typename LocalOrdinal,
typename GlobalOrdinal,
class Node >
127 Node> >::get1dCopy(
const Teuchos::ArrayView<scalar_t>& av,
130 const Tpetra::Map<LocalOrdinal,
132 Node> > distribution_map,
133 EDistribution distribution)
const
137 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
138 const size_t num_vecs = getGlobalNumVectors ();
140 TEUCHOS_TEST_FOR_EXCEPTION(
141 distribution_map.getRawPtr () == NULL, std::invalid_argument,
142 "Amesos2::MultiVecAdapter::get1dCopy: distribution_map argument is null.");
143 TEUCHOS_TEST_FOR_EXCEPTION(
144 mv_.is_null (), std::logic_error,
145 "Amesos2::MultiVecAdapter::get1dCopy: mv_ is null.");
147 TEUCHOS_TEST_FOR_EXCEPTION(
148 this->getMap ().is_null (), std::logic_error,
149 "Amesos2::MultiVecAdapter::get1dCopy: this->getMap() returns null.");
151#ifdef HAVE_AMESOS2_DEBUG
152 const size_t requested_vector_length = distribution_map->getLocalNumElements ();
153 TEUCHOS_TEST_FOR_EXCEPTION(
154 lda < requested_vector_length, std::invalid_argument,
155 "Amesos2::MultiVecAdapter::get1dCopy: On process " <<
156 distribution_map->getComm ()->getRank () <<
" of the distribution Map's "
157 "communicator, the given stride lda = " << lda <<
" is not large enough "
158 "for the local vector length " << requested_vector_length <<
".");
159 TEUCHOS_TEST_FOR_EXCEPTION(
160 as<size_t> (av.size ()) < as<size_t> ((num_vecs - 1) * lda + requested_vector_length),
161 std::invalid_argument,
"Amesos2::MultiVector::get1dCopy: MultiVector "
162 "storage not large enough given leading dimension and number of vectors." );
166 if ( num_vecs == 1 && this->getComm()->getRank() == 0 && this->getComm()->getSize() == 1 ) {
167 mv_->get1dCopy (av, lda);
174 RCP<const map_type> distMap;
175 if (exporter_.is_null () ||
176 ! exporter_->getSourceMap ()->isSameAs (* (this->getMap ())) ||
177 ! exporter_->getTargetMap ()->isSameAs (* distribution_map)) {
183 distMap = rcp(
new map_type(*distribution_map));
185 exporter_ = rcp (
new export_type (this->getMap (), distMap));
188 distMap = exporter_->getTargetMap ();
191 multivec_t redist_mv (distMap, num_vecs);
194 redist_mv.doExport (*mv_, *exporter_, Tpetra::REPLACE);
196 if ( distribution != CONTIGUOUS_AND_ROOTED ) {
199 redist_mv.get1dCopy (av, lda);
205 auto contig_local_view_2d = redist_mv.getLocalViewHost(Tpetra::Access::ReadOnly);
206 if ( redist_mv.isConstantStride() ) {
207 for (
size_t j = 0; j < num_vecs; ++j) {
208 auto av_j = av(lda*j, lda);
209 for (
size_t i = 0; i < lda; ++i ) {
210 av_j[i] = contig_local_view_2d(i,j);
219 const size_t lclNumRows = redist_mv.getLocalLength();
220 for (
size_t j = 0; j < redist_mv.getNumVectors(); ++j) {
221 auto av_j = av(lda*j, lclNumRows);
222 auto X_lcl_j_2d = redist_mv.getLocalViewHost(Tpetra::Access::ReadOnly);
223 auto X_lcl_j_1d = Kokkos::subview (X_lcl_j_2d, Kokkos::ALL (), j);
225 using val_type =
typename std::remove_const<
typename decltype( X_lcl_j_1d )::value_type>::type;
226 Kokkos::View<val_type*, Kokkos::HostSpace> umavj (
const_cast< val_type*
> (
reinterpret_cast<const val_type*
> ( av_j.getRawPtr () ) ), av_j.size () );
227 Kokkos::deep_copy (umavj, X_lcl_j_1d);
234 template <
typename Scalar,
typename LocalOrdinal,
typename GlobalOrdinal,
class Node >
235 template <
typename KV>
241 Node> >::get1dCopy_kokkos_view(
246 const Tpetra::Map<LocalOrdinal,
248 Node> > distribution_map,
249 EDistribution distribution)
const
253 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
254 const size_t num_vecs = getGlobalNumVectors ();
256 TEUCHOS_TEST_FOR_EXCEPTION(
257 distribution_map.getRawPtr () == NULL, std::invalid_argument,
258 "Amesos2::MultiVecAdapter::get1dCopy_kokkos_view: distribution_map argument is null.");
259 TEUCHOS_TEST_FOR_EXCEPTION(
260 mv_.is_null (), std::logic_error,
261 "Amesos2::MultiVecAdapter::get1dCopy_kokkos_view: mv_ is null.");
263 TEUCHOS_TEST_FOR_EXCEPTION(
264 this->getMap ().is_null (), std::logic_error,
265 "Amesos2::MultiVecAdapter::get1dCopy_kokkos_view: this->getMap() returns null.");
267#ifdef HAVE_AMESOS2_DEBUG
268 const size_t requested_vector_length = distribution_map->getLocalNumElements ();
269 TEUCHOS_TEST_FOR_EXCEPTION(
270 lda < requested_vector_length, std::invalid_argument,
271 "Amesos2::MultiVecAdapter::get1dCopy_kokkos_view: On process " <<
272 distribution_map->getComm ()->getRank () <<
" of the distribution Map's "
273 "communicator, the given stride lda = " << lda <<
" is not large enough "
274 "for the local vector length " << requested_vector_length <<
".");
281 if ( num_vecs == 1 && this->getComm()->getRank() == 0 && this->getComm()->getSize() == 1 ) {
282 if(mv_->isConstantStride()) {
285 deep_copy_only(bInitialize, kokkos_view, mv_->getLocalViewDevice(Tpetra::Access::ReadOnly), bAssigned);
289 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
"Resolve handling for non-constant stride.");
297 RCP<const map_type> distMap;
298 if (exporter_.is_null () ||
299 ! exporter_->getSourceMap ()->isSameAs (* (this->getMap ())) ||
300 ! exporter_->getTargetMap ()->isSameAs (* distribution_map)) {
306 distMap = rcp(
new map_type(*distribution_map));
308 exporter_ = rcp (
new export_type (this->getMap (), distMap));
311 distMap = exporter_->getTargetMap ();
314 multivec_t redist_mv (distMap, num_vecs);
317 redist_mv.doExport (*mv_, *exporter_, Tpetra::REPLACE);
319 if ( distribution != CONTIGUOUS_AND_ROOTED ) {
323 deep_copy_or_assign_view(bInitialize, kokkos_view, redist_mv.getLocalViewDevice(Tpetra::Access::ReadOnly), bAssigned);
327 if(redist_mv.isConstantStride()) {
329 deep_copy_or_assign_view(bInitialize, kokkos_view, redist_mv.getLocalViewDevice(Tpetra::Access::ReadOnly), bAssigned);
333 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
"Kokkos adapter non-constant stride not imlemented.");
339 template <
typename Scalar,
typename LocalOrdinal,
typename GlobalOrdinal,
class Node >
340 Teuchos::ArrayRCP<Scalar>
345 Node> >::get1dViewNonConst (
bool local)
351 TEUCHOS_TEST_FOR_EXCEPTION(
352 true, std::logic_error,
"Amesos2::MultiVecAdapter::get1dViewNonConst: "
406 template <
typename Scalar,
typename LocalOrdinal,
typename GlobalOrdinal,
class Node>
412 Node> >::put1dData(
const Teuchos::ArrayView<const scalar_t>& new_data,
415 const Tpetra::Map<LocalOrdinal,
418 EDistribution distribution )
421 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
423 TEUCHOS_TEST_FOR_EXCEPTION(
424 source_map.getRawPtr () == NULL, std::invalid_argument,
425 "Amesos2::MultiVecAdapter::put1dData: source_map argument is null.");
426 TEUCHOS_TEST_FOR_EXCEPTION(
427 mv_.is_null (), std::logic_error,
428 "Amesos2::MultiVecAdapter::put1dData: the internal MultiVector mv_ is null.");
430 TEUCHOS_TEST_FOR_EXCEPTION(
431 this->getMap ().is_null (), std::logic_error,
432 "Amesos2::MultiVecAdapter::put1dData: this->getMap() returns null.");
434 const size_t num_vecs = getGlobalNumVectors ();
437 if ( num_vecs == 1 && this->getComm()->getRank() == 0 && this->getComm()->getSize() == 1 ) {
439 auto mv_view_to_modify_2d = mv_->getLocalViewHost(Tpetra::Access::OverwriteAll);
440 for (
size_t i = 0; i < lda; ++i ) {
441 mv_view_to_modify_2d(i,0) = new_data[i];
449 RCP<const map_type> srcMap;
450 if (importer_.is_null () ||
451 ! importer_->getSourceMap ()->isSameAs (* source_map) ||
452 ! importer_->getTargetMap ()->isSameAs (* (this->getMap ()))) {
458 srcMap = rcp(
new map_type(*source_map));
459 importer_ = rcp (
new import_type (srcMap, this->getMap ()));
462 srcMap = importer_->getSourceMap ();
465 if ( distribution != CONTIGUOUS_AND_ROOTED ) {
468 const multivec_t source_mv (srcMap, new_data, lda, num_vecs);
469 mv_->doImport (source_mv, *importer_, Tpetra::REPLACE);
472 multivec_t redist_mv (srcMap, num_vecs);
473 if ( redist_mv.isConstantStride() ) {
474 auto contig_local_view_2d = redist_mv.getLocalViewHost(Tpetra::Access::OverwriteAll);
475 for (
size_t j = 0; j < num_vecs; ++j) {
476 auto av_j = new_data(lda*j, lda);
477 for (
size_t i = 0; i < lda; ++i ) {
478 contig_local_view_2d(i,j) = av_j[i];
487 const size_t lclNumRows = redist_mv.getLocalLength();
488 for (
size_t j = 0; j < redist_mv.getNumVectors(); ++j) {
489 auto av_j = new_data(lda*j, lclNumRows);
490 auto X_lcl_j_2d = redist_mv.getLocalViewHost(Tpetra::Access::ReadOnly);
491 auto X_lcl_j_1d = Kokkos::subview (X_lcl_j_2d, Kokkos::ALL (), j);
493 using val_type =
typename std::remove_const<
typename decltype( X_lcl_j_1d )::value_type>::type;
494 Kokkos::View<val_type*, Kokkos::HostSpace> umavj (
const_cast< val_type*
> (
reinterpret_cast<const val_type*
> ( av_j.getRawPtr () ) ), av_j.size () );
495 Kokkos::deep_copy (umavj, X_lcl_j_1d);
502 mv_->doImport (redist_mv, *importer_, Tpetra::REPLACE);
508 template <
typename Scalar,
typename LocalOrdinal,
typename GlobalOrdinal,
class Node>
509 template <
typename KV>
515 Node> >::put1dData_kokkos_view(KV& kokkos_new_data,
518 const Tpetra::Map<LocalOrdinal,
521 EDistribution distribution )
524 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
526 TEUCHOS_TEST_FOR_EXCEPTION(
527 source_map.getRawPtr () == NULL, std::invalid_argument,
528 "Amesos2::MultiVecAdapter::put1dData_kokkos_view: source_map argument is null.");
529 TEUCHOS_TEST_FOR_EXCEPTION(
530 mv_.is_null (), std::logic_error,
531 "Amesos2::MultiVecAdapter::put1dData_kokkos_view: the internal MultiVector mv_ is null.");
533 TEUCHOS_TEST_FOR_EXCEPTION(
534 this->getMap ().is_null (), std::logic_error,
535 "Amesos2::MultiVecAdapter::put1dData_kokkos_view: this->getMap() returns null.");
537 const size_t num_vecs = getGlobalNumVectors ();
540 if ( num_vecs == 1 && this->getComm()->getRank() == 0 && this->getComm()->getSize() == 1 ) {
545 auto mv_view_to_modify_2d = mv_->getLocalViewDevice(Tpetra::Access::OverwriteAll);
547 deep_copy_only(mv_view_to_modify_2d, kokkos_new_data);
554 RCP<const map_type> srcMap;
555 if (importer_.is_null () ||
556 ! importer_->getSourceMap ()->isSameAs (* source_map) ||
557 ! importer_->getTargetMap ()->isSameAs (* (this->getMap ()))) {
563 srcMap = rcp(
new map_type(*source_map));
564 importer_ = rcp (
new import_type (srcMap, this->getMap ()));
567 srcMap = importer_->getSourceMap ();
570 if ( distribution != CONTIGUOUS_AND_ROOTED ) {
575 typedef typename multivec_t::dual_view_type::t_host::value_type tpetra_mv_view_type;
576 Kokkos::View<tpetra_mv_view_type**,
typename KV::array_layout,
577 Kokkos::HostSpace> convert_kokkos_new_data;
578 deep_copy_or_assign_view(convert_kokkos_new_data, kokkos_new_data);
579#ifdef HAVE_TEUCHOS_COMPLEX
581 auto pData =
reinterpret_cast<Scalar*
>(convert_kokkos_new_data.data());
583 auto pData = convert_kokkos_new_data.data();
586 const multivec_t source_mv (srcMap, Teuchos::ArrayView<const scalar_t>(
587 pData, kokkos_new_data.size()), lda, num_vecs);
588 mv_->doImport (source_mv, *importer_, Tpetra::REPLACE);
591 multivec_t redist_mv (srcMap, num_vecs);
596 auto host_kokkos_new_data = Kokkos::create_mirror_view(kokkos_new_data);
597 Kokkos::deep_copy(host_kokkos_new_data, kokkos_new_data);
598 if ( redist_mv.isConstantStride() ) {
599 auto contig_local_view_2d = redist_mv.getLocalViewHost(Tpetra::Access::OverwriteAll);
600 for (
size_t j = 0; j < num_vecs; ++j) {
601 auto av_j = Kokkos::subview(host_kokkos_new_data, Kokkos::ALL, j);
602 for (
size_t i = 0; i < lda; ++i ) {
603 contig_local_view_2d(i,j) = av_j(i);
608 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
"Kokkos adapter "
609 "CONTIGUOUS_AND_ROOTED not implemented for put1dData_kokkos_view "
610 "with non constant stride.");
616 mv_->doImport (redist_mv, *importer_, Tpetra::REPLACE);
622 template <
typename Scalar,
typename LocalOrdinal,
typename GlobalOrdinal,
class Node >
628 Node> >::description()
const
630 std::ostringstream oss;
631 oss <<
"Amesos2 adapter wrapping: ";
632 oss << mv_->description();
637 template <
typename Scalar,
typename LocalOrdinal,
typename GlobalOrdinal,
class Node >
643 Node> >::describe (Teuchos::FancyOStream& os,
644 const Teuchos::EVerbosityLevel verbLevel)
const
646 mv_->describe (os, verbLevel);
650 template <
typename Scalar,
typename LocalOrdinal,
typename GlobalOrdinal,
class Node >
651 const char* MultiVecAdapter<
655 Node> >::name =
"Amesos2 adapter for Tpetra::MultiVector";
Copy or assign views based on memory spaces.
Amesos2::MultiVecAdapter specialization for the Tpetra::MultiVector class.