10#ifndef IFPACK2_BLOCKTRIDICONTAINER_IMPL_HPP
11#define IFPACK2_BLOCKTRIDICONTAINER_IMPL_HPP
16#include <Teuchos_Details_MpiTypeTraits.hpp>
18#include <Tpetra_Details_extractMpiCommFromTeuchos.hpp>
19#include <Tpetra_Distributor.hpp>
20#include <Tpetra_BlockMultiVector.hpp>
22#include <KokkosKernels_ArithTraits.hpp>
23#include <KokkosBatched_Util.hpp>
24#include <KokkosBatched_Vector.hpp>
25#include <KokkosBatched_Copy_Decl.hpp>
26#include <KokkosBatched_Copy_Impl.hpp>
27#include <KokkosBatched_AddRadial_Decl.hpp>
28#include <KokkosBatched_AddRadial_Impl.hpp>
29#include <KokkosBatched_SetIdentity_Decl.hpp>
30#include <KokkosBatched_SetIdentity_Impl.hpp>
31#include <KokkosBatched_Gemm_Decl.hpp>
32#include <KokkosBatched_Gemm_Serial_Impl.hpp>
33#include <KokkosBatched_Gemm_Team_Impl.hpp>
34#include <KokkosBatched_Gemv_Decl.hpp>
35#include <KokkosBatched_Gemv_Team_Impl.hpp>
36#include <KokkosBatched_Trsm_Decl.hpp>
37#include <KokkosBatched_Trsm_Serial_Impl.hpp>
38#include <KokkosBatched_Trsm_Team_Impl.hpp>
39#include <KokkosBatched_Trsv_Decl.hpp>
40#include <KokkosBatched_Trsv_Serial_Impl.hpp>
41#include <KokkosBatched_Trsv_Team_Impl.hpp>
42#include <KokkosBatched_LU_Decl.hpp>
43#include <KokkosBatched_LU_Serial_Impl.hpp>
44#include <KokkosBatched_LU_Team_Impl.hpp>
46#include <KokkosBlas1_nrm1.hpp>
47#include <KokkosBlas1_nrm2.hpp>
52#include "Ifpack2_BlockHelper.hpp"
53#include "Ifpack2_BlockComputeResidualVector.hpp"
54#include "Ifpack2_BlockComputeResidualAndSolve.hpp"
59#if defined(KOKKOS_ENABLE_CUDA) && defined(IFPACK2_BLOCKTRIDICONTAINER_ENABLE_PROFILE)
60#include "cuda_profiler_api.h"
65#define IFPACK2_BLOCKTRIDICONTAINER_USE_MPI_3
73#define IFPACK2_BLOCKTRIDICONTAINER_USE_PINNED_MEMORY_FOR_MPI
77#define IFPACK2_BLOCKTRIDICONTAINER_USE_CUDA_SPACE
80#if defined(HAVE_IFPACK2_BLOCKTRIDICONTAINER_SMALL_SCALAR)
81#define IFPACK2_BLOCKTRIDICONTAINER_USE_SMALL_SCALAR_FOR_BLOCKTRIDIAG
85#define IFPACK2_BLOCKTRIDICONTAINER_USE_EXEC_SPACE_INSTANCES
89namespace BlockTriDiContainerDetails {
91namespace KB = KokkosBatched;
96using do_not_initialize_tag = Kokkos::ViewAllocateWithoutInitializing;
98template <
typename MemoryTraitsType, Kokkos::MemoryTraitsFlags flag>
99using MemoryTraits = Kokkos::MemoryTraits<MemoryTraitsType::is_unmanaged |
100 MemoryTraitsType::is_random_access |
103template <
typename ViewType>
104using Unmanaged = Kokkos::View<
typename ViewType::data_type,
105 typename ViewType::array_layout,
106 typename ViewType::device_type,
107 MemoryTraits<typename ViewType::memory_traits, Kokkos::Unmanaged>>;
108template <
typename ViewType>
109using Atomic = Kokkos::View<
typename ViewType::data_type,
110 typename ViewType::array_layout,
111 typename ViewType::device_type,
112 MemoryTraits<typename ViewType::memory_traits, Kokkos::Atomic>>;
113template <
typename ViewType>
114using Const = Kokkos::View<
typename ViewType::const_data_type,
115 typename ViewType::array_layout,
116 typename ViewType::device_type,
117 typename ViewType::memory_traits>;
118template <
typename ViewType>
119using ConstUnmanaged = Const<Unmanaged<ViewType>>;
121template <
typename ViewType>
122using AtomicUnmanaged = Atomic<Unmanaged<ViewType>>;
124template <
typename ViewType>
125using Unmanaged = Kokkos::View<
typename ViewType::data_type,
126 typename ViewType::array_layout,
127 typename ViewType::device_type,
128 MemoryTraits<typename ViewType::memory_traits, Kokkos::Unmanaged>>;
130template <
typename ViewType>
131using Scratch = Kokkos::View<
typename ViewType::data_type,
132 typename ViewType::array_layout,
133 typename ViewType::execution_space::scratch_memory_space,
134 MemoryTraits<typename ViewType::memory_traits, Kokkos::Unmanaged>>;
143#if defined(IFPACK2_BLOCKTRIDICONTAINER_USE_SMALL_SCALAR_FOR_BLOCKTRIDIAG)
151#if defined(KOKKOS_ENABLE_CUDA) && defined(IFPACK2_BLOCKTRIDICONTAINER_ENABLE_PROFILE)
152#define IFPACK2_BLOCKTRIDICONTAINER_PROFILER_REGION_BEGIN \
155#define IFPACK2_BLOCKTRIDICONTAINER_PROFILER_REGION_END \
156 { cudaProfilerStop(); }
159#define IFPACK2_BLOCKTRIDICONTAINER_PROFILER_REGION_BEGIN
160#define IFPACK2_BLOCKTRIDICONTAINER_PROFILER_REGION_END
166template <
typename MatrixType>
167typename Teuchos::RCP<const typename BlockHelperDetails::ImplType<MatrixType>::tpetra_import_type>
168createBlockCrsTpetraImporter(
const Teuchos::RCP<
const typename BlockHelperDetails::ImplType<MatrixType>::tpetra_row_matrix_type> &A) {
169 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::CreateBlockCrsTpetraImporter", CreateBlockCrsTpetraImporter);
171 using tpetra_map_type =
typename impl_type::tpetra_map_type;
172 using tpetra_mv_type =
typename impl_type::tpetra_block_multivector_type;
173 using tpetra_import_type =
typename impl_type::tpetra_import_type;
174 using crs_matrix_type =
typename impl_type::tpetra_crs_matrix_type;
175 using block_crs_matrix_type =
typename impl_type::tpetra_block_crs_matrix_type;
177 auto A_crs = Teuchos::rcp_dynamic_cast<const crs_matrix_type>(A);
178 auto A_bcrs = Teuchos::rcp_dynamic_cast<const block_crs_matrix_type>(A);
180 bool hasBlockCrsMatrix = !A_bcrs.is_null();
183 const auto g = hasBlockCrsMatrix ? A_bcrs->getCrsGraph() : *(A_crs->getCrsGraph());
185 const auto blocksize = hasBlockCrsMatrix ? A_bcrs->getBlockSize() : 1;
186 const auto src = Teuchos::rcp(
new tpetra_map_type(tpetra_mv_type::makePointMap(*g.getDomainMap(), blocksize)));
187 const auto tgt = Teuchos::rcp(
new tpetra_map_type(tpetra_mv_type::makePointMap(*g.getColMap(), blocksize)));
188 IFPACK2_BLOCKHELPER_TIMER_FENCE(
typename BlockHelperDetails::ImplType<MatrixType>::execution_space)
189 return Teuchos::rcp(
new tpetra_import_type(src, tgt));
197template <
typename MatrixType>
198struct AsyncableImport {
200 using impl_type = BlockHelperDetails::ImplType<MatrixType>;
206#if !defined(HAVE_IFPACK2_MPI)
207 typedef int MPI_Request;
208 typedef int MPI_Comm;
212 using scalar_type =
typename impl_type::scalar_type;
214 static int isend(
const MPI_Comm comm,
const char *buf,
int count,
int dest,
int tag, MPI_Request *ireq) {
215#ifdef HAVE_IFPACK2_MPI
217 int ret = MPI_Isend(
const_cast<char *
>(buf), count, MPI_CHAR, dest, tag, comm, ireq == NULL ? &ureq : ireq);
218 if (ireq == NULL) MPI_Request_free(&ureq);
225 static int irecv(
const MPI_Comm comm,
char *buf,
int count,
int src,
int tag, MPI_Request *ireq) {
226#ifdef HAVE_IFPACK2_MPI
228 int ret = MPI_Irecv(buf, count, MPI_CHAR, src, tag, comm, ireq == NULL ? &ureq : ireq);
229 if (ireq == NULL) MPI_Request_free(&ureq);
236 static int waitany(
int count, MPI_Request *reqs,
int *index) {
237#ifdef HAVE_IFPACK2_MPI
238 return MPI_Waitany(count, reqs, index, MPI_STATUS_IGNORE);
244 static int waitall(
int count, MPI_Request *reqs) {
245#ifdef HAVE_IFPACK2_MPI
246 return MPI_Waitall(count, reqs, MPI_STATUS_IGNORE);
253 using tpetra_map_type =
typename impl_type::tpetra_map_type;
254 using tpetra_import_type =
typename impl_type::tpetra_import_type;
256 using local_ordinal_type =
typename impl_type::local_ordinal_type;
257 using global_ordinal_type =
typename impl_type::global_ordinal_type;
261 using int_1d_view_host = Kokkos::View<int *, Kokkos::HostSpace>;
262 using local_ordinal_type_1d_view_host = Kokkos::View<local_ordinal_type *, Kokkos::HostSpace>;
264 using execution_space =
typename impl_type::execution_space;
265 using memory_space =
typename impl_type::memory_space;
266 using local_ordinal_type_1d_view =
typename impl_type::local_ordinal_type_1d_view;
268 using size_type_1d_view_host = Kokkos::View<size_type *, Kokkos::HostSpace>;
270#if defined(KOKKOS_ENABLE_CUDA)
271 using impl_scalar_type_1d_view =
272 typename std::conditional<std::is_same<execution_space, Kokkos::Cuda>::value,
273#if defined(IFPACK2_BLOCKTRIDICONTAINER_USE_PINNED_MEMORY_FOR_MPI)
274 Kokkos::View<impl_scalar_type *, Kokkos::CudaHostPinnedSpace>,
275#elif defined(IFPACK2_BLOCKTRIDICONTAINER_USE_CUDA_MEMORY_FOR_MPI)
276 Kokkos::View<impl_scalar_type *, Kokkos::CudaSpace>,
278 typename impl_type::impl_scalar_type_1d_view,
280 typename impl_type::impl_scalar_type_1d_view>::type;
282 using impl_scalar_type_1d_view =
typename impl_type::impl_scalar_type_1d_view;
284 using impl_scalar_type_1d_view_host = Kokkos::View<impl_scalar_type *, Kokkos::HostSpace>;
285 using impl_scalar_type_2d_view =
typename impl_type::impl_scalar_type_2d_view;
286 using impl_scalar_type_2d_view_tpetra =
typename impl_type::impl_scalar_type_2d_view_tpetra;
288#ifdef HAVE_IFPACK2_MPI
292 impl_scalar_type_2d_view_tpetra remote_multivector;
293 local_ordinal_type blocksize;
295 template <
typename T>
296 struct SendRecvPair {
301 SendRecvPair<int_1d_view_host> pids;
302 SendRecvPair<std::vector<MPI_Request>> reqs;
303 SendRecvPair<size_type_1d_view> offset;
304 SendRecvPair<size_type_1d_view_host> offset_host;
305 SendRecvPair<local_ordinal_type_1d_view> lids;
306 SendRecvPair<impl_scalar_type_1d_view> buffer;
307 SendRecvPair<impl_scalar_type_1d_view_host> buffer_host;
309 local_ordinal_type_1d_view dm2cm;
311#if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP) || defined(KOKKOS_ENABLE_SYCL)
312 using exec_instance_1d_std_vector = std::vector<execution_space>;
313 exec_instance_1d_std_vector exec_instances;
318 void setOffsetValues(
const Teuchos::ArrayView<const size_t> &lens,
319 const size_type_1d_view &offs) {
321 Kokkos::View<size_t *, Kokkos::HostSpace> lens_host(
const_cast<size_t *
>(lens.getRawPtr()), lens.size());
322 const auto lens_device = Kokkos::create_mirror_view_and_copy(memory_space(), lens_host);
325 const Kokkos::RangePolicy<execution_space> policy(0, offs.extent(0));
326 const local_ordinal_type lens_size = lens_device.extent(0);
327 Kokkos::parallel_scan(
328 "AsyncableImport::RangePolicy::setOffsetValues",
329 policy, KOKKOS_LAMBDA(
const local_ordinal_type &i, size_type &update,
const bool &
final) {
332 update += (i < lens_size ? lens_device[i] : 0);
336 void setOffsetValuesHost(
const Teuchos::ArrayView<const size_t> &lens,
337 const size_type_1d_view_host &offs) {
339 Kokkos::View<size_t *, Kokkos::HostSpace> lens_host(
const_cast<size_t *
>(lens.getRawPtr()), lens.size());
340 const auto lens_device = Kokkos::create_mirror_view_and_copy(memory_space(), lens_host);
344 for (local_ordinal_type i = 1, iend = offs.extent(0); i < iend; ++i) {
345 offs(i) = offs(i - 1) + lens[i - 1];
350 void createMpiRequests(
const tpetra_import_type &
import) {
351 Tpetra::Distributor &distributor =
import.getDistributor();
354 const auto pids_from = distributor.getProcsFrom();
355 pids.recv = int_1d_view_host(do_not_initialize_tag(
"pids recv"), pids_from.size());
356 memcpy(pids.recv.data(), pids_from.getRawPtr(),
sizeof(
int) * pids.recv.extent(0));
358 const auto pids_to = distributor.getProcsTo();
359 pids.send = int_1d_view_host(do_not_initialize_tag(
"pids send"), pids_to.size());
360 memcpy(pids.send.data(), pids_to.getRawPtr(),
sizeof(
int) * pids.send.extent(0));
363 reqs.recv.resize(pids.recv.extent(0));
364 memset(reqs.recv.data(), 0, reqs.recv.size() *
sizeof(MPI_Request));
365 reqs.send.resize(pids.send.extent(0));
366 memset(reqs.send.data(), 0, reqs.send.size() *
sizeof(MPI_Request));
370 const auto lengths_to = distributor.getLengthsTo();
371 offset.send = size_type_1d_view(do_not_initialize_tag(
"offset send"), lengths_to.size() + 1);
373 const auto lengths_from = distributor.getLengthsFrom();
374 offset.recv = size_type_1d_view(do_not_initialize_tag(
"offset recv"), lengths_from.size() + 1);
376 setOffsetValues(lengths_to, offset.send);
377 offset_host.send = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), offset.send);
379 setOffsetValues(lengths_from, offset.recv);
380 offset_host.recv = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), offset.recv);
382 const auto lengths_to = distributor.getLengthsTo();
383 offset_host.send = size_type_1d_view_host(do_not_initialize_tag(
"offset send"), lengths_to.size() + 1);
385 const auto lengths_from = distributor.getLengthsFrom();
386 offset_host.recv = size_type_1d_view_host(do_not_initialize_tag(
"offset recv"), lengths_from.size() + 1);
388 setOffsetValuesHost(lengths_to, offset_host.send);
391 setOffsetValuesHost(lengths_from, offset_host.recv);
396 void createSendRecvIDs(
const tpetra_import_type &
import) {
398 const auto remote_lids =
import.getRemoteLIDs();
399 const local_ordinal_type_1d_view_host
400 remote_lids_view_host(
const_cast<local_ordinal_type *
>(remote_lids.getRawPtr()), remote_lids.size());
401 lids.recv = local_ordinal_type_1d_view(do_not_initialize_tag(
"lids recv"), remote_lids.size());
402 Kokkos::deep_copy(lids.recv, remote_lids_view_host);
405 auto epids =
import.getExportPIDs();
406 auto elids =
import.getExportLIDs();
407 TEUCHOS_ASSERT(epids.size() == elids.size());
408 lids.send = local_ordinal_type_1d_view(do_not_initialize_tag(
"lids send"), elids.size());
409 auto lids_send_host = Kokkos::create_mirror_view(lids.send);
412 for (local_ordinal_type cnt = 0, i = 0, iend = pids.send.extent(0); i < iend; ++i) {
413 const auto pid_send_value = pids.send[i];
414 for (local_ordinal_type j = 0, jend = epids.size(); j < jend; ++j)
415 if (epids[j] == pid_send_value) lids_send_host[cnt++] = elids[j];
416 TEUCHOS_ASSERT(
static_cast<size_t>(cnt) == offset_host.send[i + 1]);
418 Kokkos::deep_copy(lids.send, lids_send_host);
421 void createExecutionSpaceInstances() {
422#if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP) || defined(KOKKOS_ENABLE_SYCL)
425 Kokkos::Experimental::partition_space(execution_space(), std::vector<int>(8, 1));
432 struct ToMultiVector {};
434 AsyncableImport(
const Teuchos::RCP<const tpetra_map_type> &src_map,
435 const Teuchos::RCP<const tpetra_map_type> &tgt_map,
436 const local_ordinal_type blocksize_,
437 const local_ordinal_type_1d_view dm2cm_) {
438 blocksize = blocksize_;
441#ifdef HAVE_IFPACK2_MPI
442 comm = Tpetra::Details::extractMpiCommFromTeuchos(*tgt_map->getComm());
444 const tpetra_import_type
import(src_map, tgt_map);
446 createMpiRequests(
import);
447 createSendRecvIDs(
import);
448 createExecutionSpaceInstances();
451 void createDataBuffer(
const local_ordinal_type &num_vectors) {
452 const size_type extent_0 = lids.recv.extent(0) * blocksize;
453 const size_type extent_1 = num_vectors;
454 if (remote_multivector.extent(0) == extent_0 &&
455 remote_multivector.extent(1) == extent_1) {
459 impl_scalar_type_2d_view_tpetra(do_not_initialize_tag(
"remote multivector"), extent_0, extent_1);
461 const auto send_buffer_size = offset_host.send[offset_host.send.extent(0) - 1] * blocksize * num_vectors;
462 const auto recv_buffer_size = offset_host.recv[offset_host.recv.extent(0) - 1] * blocksize * num_vectors;
464 buffer.send = impl_scalar_type_1d_view(do_not_initialize_tag(
"buffer send"), send_buffer_size);
465 buffer.recv = impl_scalar_type_1d_view(do_not_initialize_tag(
"buffer recv"), recv_buffer_size);
467 if (!Tpetra::Details::Behavior::assumeMpiIsGPUAware()) {
468 buffer_host.send = impl_scalar_type_1d_view_host(do_not_initialize_tag(
"buffer send"), send_buffer_size);
469 buffer_host.recv = impl_scalar_type_1d_view_host(do_not_initialize_tag(
"buffer recv"), recv_buffer_size);
475#ifdef HAVE_IFPACK2_MPI
476 waitall(reqs.recv.size(), reqs.recv.data());
477 waitall(reqs.send.size(), reqs.send.data());
485#if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP) || defined(KOKKOS_ENABLE_SYCL)
486 template <
typename PackTag>
487 static void copy(
const local_ordinal_type_1d_view &lids_,
488 const impl_scalar_type_1d_view &buffer_,
489 const local_ordinal_type ibeg_,
490 const local_ordinal_type iend_,
491 const impl_scalar_type_2d_view_tpetra &multivector_,
492 const local_ordinal_type blocksize_,
493 const execution_space &exec_instance_) {
494 const local_ordinal_type num_vectors = multivector_.extent(1);
495 const local_ordinal_type mv_blocksize = blocksize_ * num_vectors;
496 const local_ordinal_type idiff = iend_ - ibeg_;
497 const auto abase = buffer_.data() + mv_blocksize * ibeg_;
499 using team_policy_type = Kokkos::TeamPolicy<execution_space>;
500 local_ordinal_type vector_size(0);
503 else if (blocksize_ <= 8)
505 else if (blocksize_ <= 16)
510 const auto work_item_property = Kokkos::Experimental::WorkItemProperty::HintLightWeight;
511 const team_policy_type policy(exec_instance_, idiff, 1, vector_size);
512 Kokkos::parallel_for(
513 Kokkos::Experimental::require(policy, work_item_property),
514 KOKKOS_LAMBDA(
const typename team_policy_type::member_type &member) {
515 const local_ordinal_type i = member.league_rank();
516 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, num_vectors), [&](
const local_ordinal_type &j) {
517 auto aptr = abase + blocksize_ * (i + idiff * j);
518 auto bptr = &multivector_(blocksize_ * lids_(i + ibeg_), j);
519 if (std::is_same<PackTag, ToBuffer>::value)
520 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, blocksize_), [&](
const local_ordinal_type &k) {
524 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, blocksize_), [&](
const local_ordinal_type &k) {
531 void asyncSendRecvVar1(
const impl_scalar_type_2d_view_tpetra &mv) {
532 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::AsyncableImport::AsyncSendRecv", AsyncSendRecv);
534#ifdef HAVE_IFPACK2_MPI
536 const local_ordinal_type num_vectors = mv.extent(1);
537 const local_ordinal_type mv_blocksize = blocksize * num_vectors;
540 for (local_ordinal_type i = 0, iend = pids.recv.extent(0); i < iend; ++i) {
541 if (Tpetra::Details::Behavior::assumeMpiIsGPUAware()) {
543 reinterpret_cast<char *
>(buffer.recv.data() + offset_host.recv[i] * mv_blocksize),
544 (offset_host.recv[i + 1] - offset_host.recv[i]) * mv_blocksize *
sizeof(impl_scalar_type),
550 reinterpret_cast<char *
>(buffer_host.recv.data() + offset_host.recv[i] * mv_blocksize),
551 (offset_host.recv[i + 1] - offset_host.recv[i]) * mv_blocksize *
sizeof(impl_scalar_type),
559 execution_space().fence();
562 for (local_ordinal_type i = 0; i < static_cast<local_ordinal_type>(pids.send.extent(0)); ++i) {
564 if (i < 8) exec_instances[i % 8].fence();
565 copy<ToBuffer>(lids.send, buffer.send,
566 offset_host.send(i), offset_host.send(i + 1),
569 exec_instances[i % 8]);
570 if (!Tpetra::Details::Behavior::assumeMpiIsGPUAware()) {
572 const local_ordinal_type num_vectors = mv.extent(1);
573 const local_ordinal_type mv_blocksize = blocksize * num_vectors;
575 Kokkos::deep_copy(exec_instances[i % 8],
576 Kokkos::subview(buffer_host.send,
577 Kokkos::pair<local_ordinal_type, local_ordinal_type>(
578 offset_host.send(i) * mv_blocksize,
579 offset_host.send(i + 1) * mv_blocksize)),
580 Kokkos::subview(buffer.send,
581 Kokkos::pair<local_ordinal_type, local_ordinal_type>(
582 offset_host.send(i) * mv_blocksize,
583 offset_host.send(i + 1) * mv_blocksize)));
588 for (local_ordinal_type i = 0; i < static_cast<local_ordinal_type>(pids.send.extent(0)); ++i) {
590 if (i < 8) exec_instances[i % 8].fence();
591 if (Tpetra::Details::Behavior::assumeMpiIsGPUAware()) {
593 reinterpret_cast<const char *
>(buffer.send.data() + offset_host.send[i] * mv_blocksize),
594 (offset_host.send[i + 1] - offset_host.send[i]) * mv_blocksize *
sizeof(impl_scalar_type),
600 reinterpret_cast<const char *
>(buffer_host.send.data() + offset_host.send[i] * mv_blocksize),
601 (offset_host.send[i + 1] - offset_host.send[i]) * mv_blocksize *
sizeof(impl_scalar_type),
609 for (local_ordinal_type i = 0, iend = pids.recv.extent(0); i < iend; ++i) {
612 MPI_Iprobe(pids.recv[i], 42, comm, &flag, &stat);
615 IFPACK2_BLOCKHELPER_TIMER_FENCE(execution_space)
618 void syncRecvVar1() {
619 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::AsyncableImport::SyncRecv", SyncRecv);
620#ifdef HAVE_IFPACK2_MPI
622 for (local_ordinal_type i = 0; i < static_cast<local_ordinal_type>(pids.recv.extent(0)); ++i) {
623 local_ordinal_type idx = i;
626 waitany(pids.recv.extent(0), reqs.recv.data(), &idx);
628 if (!Tpetra::Details::Behavior::assumeMpiIsGPUAware()) {
629 const local_ordinal_type num_vectors = remote_multivector.extent(1);
630 const local_ordinal_type mv_blocksize = blocksize * num_vectors;
633 Kokkos::subview(buffer.recv,
634 Kokkos::pair<local_ordinal_type, local_ordinal_type>(
635 offset_host.recv(idx) * mv_blocksize,
636 offset_host.recv(idx + 1) * mv_blocksize)),
637 Kokkos::subview(buffer_host.recv,
638 Kokkos::pair<local_ordinal_type, local_ordinal_type>(
639 offset_host.recv(idx) * mv_blocksize,
640 offset_host.recv(idx + 1) * mv_blocksize)));
644 copy<ToMultiVector>(lids.recv, buffer.recv,
645 offset_host.recv(idx), offset_host.recv(idx + 1),
646 remote_multivector, blocksize,
647 exec_instances[idx % 8]);
654 waitall(reqs.send.size(), reqs.send.data());
656 IFPACK2_BLOCKHELPER_TIMER_FENCE(execution_space)
665 template <
typename PackTag>
666 static void copy(
const local_ordinal_type_1d_view &lids_,
667 const impl_scalar_type_1d_view &buffer_,
668 const local_ordinal_type &ibeg_,
669 const local_ordinal_type &iend_,
670 const impl_scalar_type_2d_view_tpetra &multivector_,
671 const local_ordinal_type blocksize_) {
672 const local_ordinal_type num_vectors = multivector_.extent(1);
673 const local_ordinal_type mv_blocksize = blocksize_ * num_vectors;
674 const local_ordinal_type idiff = iend_ - ibeg_;
675 const auto abase = buffer_.data() + mv_blocksize * ibeg_;
676 if constexpr (BlockHelperDetails::is_device<execution_space>::value) {
677 using team_policy_type = Kokkos::TeamPolicy<execution_space>;
678 local_ordinal_type vector_size(0);
681 else if (blocksize_ <= 8)
683 else if (blocksize_ <= 16)
687 const team_policy_type policy(idiff, 1, vector_size);
688 Kokkos::parallel_for(
689 "AsyncableImport::TeamPolicy::copy",
690 policy, KOKKOS_LAMBDA(
const typename team_policy_type::member_type &member) {
691 const local_ordinal_type i = member.league_rank();
692 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, num_vectors), [&](
const local_ordinal_type &j) {
693 auto aptr = abase + blocksize_ * (i + idiff * j);
694 auto bptr = &multivector_(blocksize_ * lids_(i + ibeg_), j);
695 if (std::is_same<PackTag, ToBuffer>::value)
696 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, blocksize_), [&](
const local_ordinal_type &k) {
700 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, blocksize_), [&](
const local_ordinal_type &k) {
706 const Kokkos::RangePolicy<execution_space> policy(0, idiff * num_vectors);
707 Kokkos::parallel_for(
708 "AsyncableImport::RangePolicy::copy",
709 policy, KOKKOS_LAMBDA(
const local_ordinal_type &ij) {
710 const local_ordinal_type i = ij % idiff;
711 const local_ordinal_type j = ij / idiff;
712 auto aptr = abase + blocksize_ * (i + idiff * j);
713 auto bptr = &multivector_(blocksize_ * lids_(i + ibeg_), j);
714 auto from = std::is_same<PackTag, ToBuffer>::value ? bptr : aptr;
715 auto to = std::is_same<PackTag, ToBuffer>::value ? aptr : bptr;
716 memcpy(to, from,
sizeof(impl_scalar_type) * blocksize_);
724 void asyncSendRecvVar0(
const impl_scalar_type_2d_view_tpetra &mv) {
725 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::AsyncableImport::AsyncSendRecv", AsyncSendRecv);
727#ifdef HAVE_IFPACK2_MPI
729 const local_ordinal_type num_vectors = mv.extent(1);
730 const local_ordinal_type mv_blocksize = blocksize * num_vectors;
733 for (local_ordinal_type i = 0, iend = pids.recv.extent(0); i < iend; ++i) {
734 if (Tpetra::Details::Behavior::assumeMpiIsGPUAware()) {
736 reinterpret_cast<char *
>(buffer.recv.data() + offset_host.recv[i] * mv_blocksize),
737 (offset_host.recv[i + 1] - offset_host.recv[i]) * mv_blocksize *
sizeof(impl_scalar_type),
743 reinterpret_cast<char *
>(buffer_host.recv.data() + offset_host.recv[i] * mv_blocksize),
744 (offset_host.recv[i + 1] - offset_host.recv[i]) * mv_blocksize *
sizeof(impl_scalar_type),
752 for (local_ordinal_type i = 0, iend = pids.send.extent(0); i < iend; ++i) {
753 copy<ToBuffer>(lids.send, buffer.send, offset_host.send(i), offset_host.send(i + 1),
756 if (Tpetra::Details::Behavior::assumeMpiIsGPUAware()) {
758 reinterpret_cast<const char *
>(buffer.send.data() + offset_host.send[i] * mv_blocksize),
759 (offset_host.send[i + 1] - offset_host.send[i]) * mv_blocksize *
sizeof(impl_scalar_type),
765 Kokkos::subview(buffer_host.send,
766 Kokkos::pair<local_ordinal_type, local_ordinal_type>(
767 offset_host.send(i) * mv_blocksize,
768 offset_host.send(i + 1) * mv_blocksize)),
769 Kokkos::subview(buffer.send,
770 Kokkos::pair<local_ordinal_type, local_ordinal_type>(
771 offset_host.send(i) * mv_blocksize,
772 offset_host.send(i + 1) * mv_blocksize)));
774 reinterpret_cast<const char *
>(buffer_host.send.data() + offset_host.send[i] * mv_blocksize),
775 (offset_host.send[i + 1] - offset_host.send[i]) * mv_blocksize *
sizeof(impl_scalar_type),
784 for (local_ordinal_type i = 0, iend = pids.recv.extent(0); i < iend; ++i) {
787 MPI_Iprobe(pids.recv[i], 42, comm, &flag, &stat);
790 IFPACK2_BLOCKHELPER_TIMER_FENCE(execution_space)
793 void syncRecvVar0() {
794 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::AsyncableImport::SyncRecv", SyncRecv);
795#ifdef HAVE_IFPACK2_MPI
797 for (local_ordinal_type i = 0, iend = pids.recv.extent(0); i < iend; ++i) {
798 local_ordinal_type idx = i;
799 waitany(pids.recv.extent(0), reqs.recv.data(), &idx);
800 if (!Tpetra::Details::Behavior::assumeMpiIsGPUAware()) {
801 const local_ordinal_type num_vectors = remote_multivector.extent(1);
802 const local_ordinal_type mv_blocksize = blocksize * num_vectors;
804 Kokkos::subview(buffer.recv,
805 Kokkos::pair<local_ordinal_type, local_ordinal_type>(
806 offset_host.recv(idx) * mv_blocksize,
807 offset_host.recv(idx + 1) * mv_blocksize)),
808 Kokkos::subview(buffer_host.recv,
809 Kokkos::pair<local_ordinal_type, local_ordinal_type>(
810 offset_host.recv(idx) * mv_blocksize,
811 offset_host.recv(idx + 1) * mv_blocksize)));
813 copy<ToMultiVector>(lids.recv, buffer.recv, offset_host.recv(idx), offset_host.recv(idx + 1),
814 remote_multivector, blocksize);
817 waitall(reqs.send.size(), reqs.send.data());
819 IFPACK2_BLOCKHELPER_TIMER_FENCE(execution_space)
825 void asyncSendRecv(
const impl_scalar_type_2d_view_tpetra &mv) {
826#if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP) || defined(KOKKOS_ENABLE_SYCL)
827#if defined(IFPACK2_BLOCKTRIDICONTAINER_USE_EXEC_SPACE_INSTANCES)
828 asyncSendRecvVar1(mv);
830 asyncSendRecvVar0(mv);
833 asyncSendRecvVar0(mv);
837#if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP) || defined(KOKKOS_ENABLE_SYCL)
838#if defined(IFPACK2_BLOCKTRIDICONTAINER_USE_EXEC_SPACE_INSTANCES)
848 void syncExchange(
const impl_scalar_type_2d_view_tpetra &mv) {
849 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::AsyncableImport::SyncExchange", SyncExchange);
852 IFPACK2_BLOCKHELPER_TIMER_FENCE(execution_space)
855 impl_scalar_type_2d_view_tpetra getRemoteMultiVectorLocalView()
const {
return remote_multivector; }
858template <
typename ViewType1,
typename ViewType2>
859struct are_same_struct {
863 are_same_struct(ViewType1 keys1_, ViewType2 keys2_)
866 KOKKOS_INLINE_FUNCTION
867 void operator()(
int i,
unsigned int &count)
const {
868 if (keys1(i) != keys2(i)) count++;
872template <
typename ViewType1,
typename ViewType2>
873bool are_same(ViewType1 keys1, ViewType2 keys2) {
874 unsigned int are_same_ = 0;
876 Kokkos::parallel_reduce(Kokkos::RangePolicy<typename ViewType1::execution_space>(0, keys1.extent(0)),
877 are_same_struct(keys1, keys2),
879 return are_same_ == 0;
885template <
typename MatrixType>
886Teuchos::RCP<AsyncableImport<MatrixType>>
887createBlockCrsAsyncImporter(
const Teuchos::RCP<
const typename BlockHelperDetails::ImplType<MatrixType>::tpetra_row_matrix_type> &A) {
888 IFPACK2_BLOCKHELPER_TIMER(
"createBlockCrsAsyncImporter", createBlockCrsAsyncImporter);
889 using impl_type = BlockHelperDetails::ImplType<MatrixType>;
890 using tpetra_map_type =
typename impl_type::tpetra_map_type;
891 using local_ordinal_type =
typename impl_type::local_ordinal_type;
892 using global_ordinal_type =
typename impl_type::global_ordinal_type;
893 using local_ordinal_type_1d_view =
typename impl_type::local_ordinal_type_1d_view;
894 using crs_matrix_type =
typename impl_type::tpetra_crs_matrix_type;
895 using block_crs_matrix_type =
typename impl_type::tpetra_block_crs_matrix_type;
896 using global_indices_array_device_type = Kokkos::View<const global_ordinal_type *, typename tpetra_map_type::device_type>;
898 auto A_crs = Teuchos::rcp_dynamic_cast<const crs_matrix_type>(A);
899 auto A_bcrs = Teuchos::rcp_dynamic_cast<const block_crs_matrix_type>(A);
901 bool hasBlockCrsMatrix = !A_bcrs.is_null();
904 const auto g = hasBlockCrsMatrix ? A_bcrs->getCrsGraph() : *(A_crs->getCrsGraph());
906 const auto blocksize = hasBlockCrsMatrix ? A_bcrs->getBlockSize() : 1;
907 const auto domain_map = g.getDomainMap();
908 const auto column_map = g.getColMap();
910 std::vector<global_ordinal_type> gids;
912 Kokkos::Subview<global_indices_array_device_type, std::pair<int, int>> column_map_global_iD_last;
914 bool separate_remotes =
true, found_first =
false, need_owned_permutation =
false;
916 IFPACK2_BLOCKHELPER_TIMER(
"createBlockCrsAsyncImporter::loop_over_local_elements", loop_over_local_elements);
918 global_indices_array_device_type column_map_global_iD = column_map->getMyGlobalIndicesDevice();
919 global_indices_array_device_type domain_map_global_iD = domain_map->getMyGlobalIndicesDevice();
921 if (are_same(domain_map_global_iD, column_map_global_iD)) {
923 separate_remotes =
true;
924 need_owned_permutation =
false;
926 column_map_global_iD_last = Kokkos::subview(column_map_global_iD,
927 std::pair<int, int>(domain_map_global_iD.extent(0), column_map_global_iD.extent(0)));
930 for (
size_t i = 0; i < column_map->getLocalNumElements(); ++i) {
931 const global_ordinal_type gid = column_map->getGlobalElement(i);
932 if (!domain_map->isNodeGlobalElement(gid)) {
935 }
else if (found_first) {
936 separate_remotes =
false;
939 if (!found_first && !need_owned_permutation &&
940 domain_map->getLocalElement(gid) !=
static_cast<local_ordinal_type
>(i)) {
949 need_owned_permutation =
true;
953 IFPACK2_BLOCKHELPER_TIMER_FENCE(
typename BlockHelperDetails::ImplType<MatrixType>::execution_space)
956 if (separate_remotes) {
957 IFPACK2_BLOCKHELPER_TIMER(
"createBlockCrsAsyncImporter::separate_remotes", separate_remotes);
958 const auto invalid = Teuchos::OrdinalTraits<global_ordinal_type>::invalid();
959 const auto parsimonious_col_map = need_owned_permutation ? Teuchos::rcp(
new tpetra_map_type(invalid, gids.data(), gids.size(), 0, domain_map->getComm())) : Teuchos::rcp(new tpetra_map_type(invalid, column_map_global_iD_last, 0, domain_map->getComm()));
960 if (parsimonious_col_map->getGlobalNumElements() > 0) {
962 local_ordinal_type_1d_view dm2cm;
963 if (need_owned_permutation) {
964 dm2cm = local_ordinal_type_1d_view(do_not_initialize_tag(
"dm2cm"), domain_map->getLocalNumElements());
965 const auto dm2cm_host = Kokkos::create_mirror_view(dm2cm);
966 for (size_t i = 0; i < domain_map->getLocalNumElements(); ++i)
967 dm2cm_host(i) = domain_map->getLocalElement(column_map->getGlobalElement(i));
968 Kokkos::deep_copy(dm2cm, dm2cm_host);
970 IFPACK2_BLOCKHELPER_TIMER_FENCE(
typename BlockHelperDetails::ImplType<MatrixType>::execution_space)
971 return Teuchos::rcp(
new AsyncableImport<MatrixType>(domain_map, parsimonious_col_map, blocksize, dm2cm));
974 IFPACK2_BLOCKHELPER_TIMER_FENCE(
typename BlockHelperDetails::ImplType<MatrixType>::execution_space)
975 return Teuchos::null;
978template <
typename local_ordinal_type>
980 return block_size * block_size;
983template <
typename local_ordinal_type>
985 return 2 * block_size * block_size;
988template <
typename local_ordinal_type>
990 return 2 * subline_length * costTRSM(block_size) + 2 * (subline_length - 1) * costGEMV(block_size);
993template <
typename local_ordinal_type>
999 const local_ordinal_type subline_length = ceil(
double(line_length - (n_subparts_per_part - 1) * 2) / n_subparts_per_part);
1000 if (subline_length < 1) {
1005 const local_ordinal_type p_n_sublines = ceil(
double(n_subparts_per_part) * num_parts / num_teams);
1006 const local_ordinal_type p_n_sublines_2 = ceil(
double(n_subparts_per_part - 1) * num_parts / num_teams);
1008 const local_ordinal_type p_costApplyE = p_n_sublines_2 * subline_length * 2 * costGEMV(block_size);
1009 const local_ordinal_type p_costApplyS = p_n_lines * costTriDiagSolve((n_subparts_per_part - 1) * 2, block_size);
1010 const local_ordinal_type p_costApplyAinv = p_n_sublines * costTriDiagSolve(subline_length, block_size);
1013 if (n_subparts_per_part == 1) {
1014 return p_costApplyAinv;
1016 return p_costApplyE + p_costApplyS + p_costApplyAinv + p_costApplyC;
1019template <
typename local_ordinal_type>
1030 double parallelismSurplus = Kokkos::sqrt((
double)num_teams / num_parts);
1031 double logLineLength = Kokkos::log2((
double)line_length);
1032 (void)logLineLength;
1034#if defined(KOKKOS_ARCH_AMD_GFX942) || defined(KOKKOS_ARCH_AMD_GFX942_APU)
1036 double modeled = -9.2312 + 4.6946 * parallelismSurplus + 0.4095 * block_size + 0.966 * logLineLength;
1038 if (parallelismSurplus < 0.3)
1040#elif defined(KOKKOS_ARCH_HOPPER) || defined(KOKKOS_ARCH_BLACKWELL)
1042 double modeled = -9.6053 + 4.7477 * parallelismSurplus + 0.2338 * block_size + 1.0794 * logLineLength;
1044 double maxSplit = (double)line_length / 8;
1045 if (modeled > maxSplit)
1047#elif defined(KOKKOS_ENABLE_CUDA)
1051 if (parallelismSurplus > 1 && line_length > 64)
1053#elif defined(KOKKOS_ENABLE_HIP)
1055 double modeled = -8.6214 + 7.3468 * parallelismSurplus + 0.3596 * block_size + 0.6673 * logLineLength;
1059 if (parallelismSurplus > 1 && line_length > 64)
1066 if (parallelismSurplus < 0.3)
1067 n_subparts_per_part = 1;
1077 if (max_subparts_per_part > 16)
1078 max_subparts_per_part = 16;
1079 if (n_subparts_per_part < min_subparts_per_part)
1080 n_subparts_per_part = min_subparts_per_part;
1081 if (n_subparts_per_part > max_subparts_per_part)
1082 n_subparts_per_part = max_subparts_per_part;
1083 return n_subparts_per_part;
1086template <
typename ArgActiveExecutionMemorySpace>
1087struct SolveTridiagsDefaultModeAndAlgo;
1092template <
typename MatrixType>
1093BlockHelperDetails::PartInterface<MatrixType>
1094createPartInterface(
const Teuchos::RCP<
const typename BlockHelperDetails::ImplType<MatrixType>::tpetra_row_matrix_type> &A,
1095 const Teuchos::RCP<
const typename BlockHelperDetails::ImplType<MatrixType>::tpetra_crs_graph_type> &G,
1096 const Teuchos::Array<Teuchos::Array<
typename BlockHelperDetails::ImplType<MatrixType>::local_ordinal_type>> &partitions,
1097 const typename BlockHelperDetails::ImplType<MatrixType>::local_ordinal_type n_subparts_per_part_in) {
1098 IFPACK2_BLOCKHELPER_TIMER(
"createPartInterface", createPartInterface);
1101 using local_ordinal_type_1d_view =
typename impl_type::local_ordinal_type_1d_view;
1102 using local_ordinal_type_2d_view =
typename impl_type::local_ordinal_type_2d_view;
1103 using size_type =
typename impl_type::size_type;
1105 auto bA = Teuchos::rcp_dynamic_cast<const typename BlockHelperDetails::ImplType<MatrixType>::tpetra_block_crs_matrix_type>(A);
1107 TEUCHOS_ASSERT(!bA.is_null() || G->getLocalNumRows() != 0);
1108 const local_ordinal_type blocksize = bA.is_null() ? A->getLocalNumRows() / G->getLocalNumRows() : A->getBlockSize();
1109 constexpr int vector_length = impl_type::vector_length;
1110 constexpr int internal_vector_length = impl_type::internal_vector_length;
1112 const auto comm = A->getRowMap()->getComm();
1114 BlockHelperDetails::PartInterface<MatrixType> interf;
1117 const bool jacobi = partitions.size() == 0 || partitions.size() == A_n_lclrows;
1120 typedef std::pair<local_ordinal_type, local_ordinal_type> size_idx_pair_type;
1121 std::vector<size_idx_pair_type> partsz(nparts);
1125 partsz[i] = size_idx_pair_type(partitions[i].size(), i);
1126 std::sort(partsz.begin(), partsz.end(),
1127 [](
const size_idx_pair_type &x,
const size_idx_pair_type &y) {
1128 return x.first > y.first;
1134 n_subparts_per_part = 1;
1136 if (n_subparts_per_part_in == -1) {
1139 using execution_space =
typename impl_type::execution_space;
1142 if constexpr (impl_type::node_type::is_gpu) {
1143 const int line_length = partsz[0].first;
1146 SolveTridiagsDefaultModeAndAlgo<typename execution_space::memory_space>::
1147 recommended_team_size(blocksize, vector_length, internal_vector_length);
1149 const local_ordinal_type num_teams = std::max(1, execution_space().concurrency() / (team_size * vector_length));
1150 n_subparts_per_part = getAutomaticNSubparts(nparts, num_teams, line_length, blocksize);
1151#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
1152 printf(
"Automatically chosen n_subparts_per_part = %d for nparts = %d, num_teams = %d, team_size = %d, line_length = %d, and blocksize = %d;\n", n_subparts_per_part, nparts, num_teams, team_size, line_length, blocksize);
1155 n_subparts_per_part = 1;
1156#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
1157 printf(
"Automatically chosen n_subparts_per_part = 1 for CPU backend\n");
1161 n_subparts_per_part = n_subparts_per_part_in;
1169 const local_ordinal_type n_sub_parts_and_schur = n_sub_parts + nparts * (n_subparts_per_part - 1);
1171#if defined(BLOCKTRIDICONTAINER_DEBUG)
1178 TEUCHOS_TEST_FOR_EXCEPT_MSG(nrows != A_n_lclrows, BlockHelperDetails::get_msg_prefix(comm) <<
"The #rows implied by the local partition is not "
1179 <<
"the same as getLocalNumRows: " << nrows <<
" vs " << A_n_lclrows);
1183 std::vector<local_ordinal_type> p;
1185 interf.max_partsz = 1;
1186 interf.max_subpartsz = 0;
1187 interf.n_subparts_per_part = 1;
1188 interf.nparts = nparts;
1194 p[i] = partsz[i].second;
1196 interf.max_partsz = partsz[0].first;
1199 const local_ordinal_type sub_line_length = (interf.max_partsz - (n_subparts_per_part - 1) * connection_length) / n_subparts_per_part;
1200 const local_ordinal_type last_sub_line_length = interf.max_partsz - (n_subparts_per_part - 1) * (connection_length + sub_line_length);
1202 interf.max_subpartsz = (sub_line_length > last_sub_line_length) ? sub_line_length : last_sub_line_length;
1203 interf.n_subparts_per_part = n_subparts_per_part;
1204 interf.nparts = nparts;
1208 interf.partptr = local_ordinal_type_1d_view(do_not_initialize_tag(
"partptr"), nparts + 1);
1209 interf.lclrow = local_ordinal_type_1d_view(do_not_initialize_tag(
"lclrow"), A_n_lclrows);
1210 interf.part2rowidx0 = local_ordinal_type_1d_view(do_not_initialize_tag(
"part2rowidx0"), nparts + 1);
1211 interf.part2packrowidx0 = local_ordinal_type_1d_view(do_not_initialize_tag(
"part2packrowidx0"), nparts + 1);
1212 interf.rowidx2part = local_ordinal_type_1d_view(do_not_initialize_tag(
"rowidx2part"), A_n_lclrows);
1214 interf.part2rowidx0_sub = local_ordinal_type_1d_view(do_not_initialize_tag(
"part2rowidx0_sub"), n_sub_parts_and_schur + 1);
1215 interf.part2packrowidx0_sub = local_ordinal_type_2d_view(do_not_initialize_tag(
"part2packrowidx0_sub"), nparts, 2 * n_subparts_per_part);
1216 interf.rowidx2part_sub = local_ordinal_type_1d_view(do_not_initialize_tag(
"rowidx2part"), A_n_lclrows);
1218 interf.partptr_sub = local_ordinal_type_2d_view(do_not_initialize_tag(
"partptr_sub"), n_sub_parts_and_schur, 2);
1221 const auto partptr = Kokkos::create_mirror_view(interf.partptr);
1222 const auto partptr_sub = Kokkos::create_mirror_view(interf.partptr_sub);
1224 const auto lclrow = Kokkos::create_mirror_view(interf.lclrow);
1225 const auto part2rowidx0 = Kokkos::create_mirror_view(interf.part2rowidx0);
1226 const auto part2packrowidx0 = Kokkos::create_mirror_view(interf.part2packrowidx0);
1227 const auto rowidx2part = Kokkos::create_mirror_view(interf.rowidx2part);
1229 const auto part2rowidx0_sub = Kokkos::create_mirror_view(interf.part2rowidx0_sub);
1230 const auto part2packrowidx0_sub = Kokkos::create_mirror_view(Kokkos::HostSpace(), interf.part2packrowidx0_sub);
1231 const auto rowidx2part_sub = Kokkos::create_mirror_view(interf.rowidx2part_sub);
1234 interf.row_contiguous =
true;
1236 part2rowidx0(0) = 0;
1237 part2packrowidx0(0) = 0;
1241 IFPACK2_BLOCKHELPER_TIMER(
"compute part indices (Jacobi)", Jacobi);
1246 part2rowidx0(i) = i;
1255 if (ip % vector_length == 0) pack_nrows = 1;
1256 part2packrowidx0(ip + 1) = part2packrowidx0(ip) + ((ip + 1) % vector_length == 0 || ip + 1 == nparts ? pack_nrows : 0);
1258 part2rowidx0_sub(0) = 0;
1259 partptr_sub(0, 0) = 0;
1265 TEUCHOS_TEST_FOR_EXCEPTION(full_line_length != ipnrows, std::logic_error,
1266 "In the part " << ip);
1270 if (full_line_length < n_subparts_per_part + (n_subparts_per_part - 1) * connection_length)
1271 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
1272 "The part " << ip <<
" is too short to use " << n_subparts_per_part <<
" sub parts.");
1274 const local_ordinal_type sub_line_length = (full_line_length - (n_subparts_per_part - 1) * connection_length) / n_subparts_per_part;
1275 const local_ordinal_type last_sub_line_length = full_line_length - (n_subparts_per_part - 1) * (connection_length + sub_line_length);
1277 if (ip % vector_length == 0) pack_nrows_sub = ipnrows;
1279 for (
local_ordinal_type local_sub_ip = 0; local_sub_ip < n_subparts_per_part; ++local_sub_ip) {
1282 if (local_sub_ip != n_subparts_per_part - 1) {
1283 if (local_sub_ip != 0) {
1284 partptr_sub(sub_ip, 0) = partptr_sub(nparts * (2 * local_sub_ip - 1) + ip, 1);
1285 }
else if (ip != 0) {
1286 partptr_sub(sub_ip, 0) = partptr_sub(nparts * 2 * (n_subparts_per_part - 1) + ip - 1, 1);
1288 partptr_sub(sub_ip, 1) = sub_line_length + partptr_sub(sub_ip, 0);
1289 partptr_sub(schur_ip, 0) = partptr_sub(sub_ip, 1);
1290 partptr_sub(schur_ip, 1) = connection_length + partptr_sub(schur_ip, 0);
1292 part2rowidx0_sub(sub_ip + 1) = part2rowidx0_sub(sub_ip) + sub_line_length;
1293 part2rowidx0_sub(sub_ip + 2) = part2rowidx0_sub(sub_ip + 1) + connection_length;
1295#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
1296 printf(
"Sub Part index = %d, first LID associated to the sub part = %d, sub part size = %d;\n", sub_ip, partptr_sub(ip, 2 * local_sub_ip), sub_line_length);
1297 printf(
"Sub Part index Schur = %d, first LID associated to the sub part = %d, sub part size = %d;\n", sub_ip + 1, partptr_sub(ip, 2 * local_sub_ip + 1), connection_length);
1300 if (local_sub_ip != 0) {
1301 partptr_sub(sub_ip, 0) = partptr_sub(nparts * (2 * local_sub_ip - 1) + ip, 1);
1302 }
else if (ip != 0) {
1303 partptr_sub(sub_ip, 0) = partptr_sub(nparts * 2 * (n_subparts_per_part - 1) + ip - 1, 1);
1305 partptr_sub(sub_ip, 1) = last_sub_line_length + partptr_sub(sub_ip, 0);
1307 part2rowidx0_sub(sub_ip + 1) = part2rowidx0_sub(sub_ip) + last_sub_line_length;
1309#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
1310 printf(
"Sub Part index = %d, first LID associated to the sub part = %d, sub part size = %d;\n", sub_ip, partptr_sub(ip, 2 * local_sub_ip), last_sub_line_length);
1316#ifdef IFPACK2_BLOCKTRIDICONTAINER_WRITE_MM
1317 std::cout <<
"partptr_sub = " << std::endl;
1318 for (size_type i = 0; i < partptr_sub.extent(0); ++i) {
1319 for (size_type j = 0; j < partptr_sub.extent(1); ++j) {
1320 std::cout << partptr_sub(i, j) <<
" ";
1322 std::cout << std::endl;
1324 std::cout <<
"partptr_sub end" << std::endl;
1332 part2packrowidx0_sub(ip, 0) = 0;
1337 ip_max = nparts > (ipack + 1) * vector_length ? (ipack + 1) * vector_length : nparts;
1339 part2packrowidx0_sub(ip, 0) = part2packrowidx0_sub(ip - vector_length, part2packrowidx0_sub.extent(1) - 1);
1343 for (size_type local_sub_ip = 0; local_sub_ip < part2packrowidx0_sub.extent(1) - 1; ++local_sub_ip) {
1345 ip_max = nparts > (ipack + 1) * vector_length ? (ipack + 1) * vector_length : nparts;
1351 const local_ordinal_type sub_line_length = (full_line_length - (n_subparts_per_part - 1) * connection_length) / n_subparts_per_part;
1352 const local_ordinal_type last_sub_line_length = full_line_length - (n_subparts_per_part - 1) * (connection_length + sub_line_length);
1354 if (local_sub_ip % 2 == 0) pack_nrows_sub = sub_line_length;
1355 if (local_sub_ip % 2 == 1) pack_nrows_sub = connection_length;
1356 if (local_sub_ip == part2packrowidx0_sub.extent(1) - 2) pack_nrows_sub = last_sub_line_length;
1358 part2packrowidx0_sub(ip_min, local_sub_ip + 1) = part2packrowidx0_sub(ip_min, local_sub_ip) + pack_nrows_sub;
1361 part2packrowidx0_sub(ip, local_sub_ip + 1) = part2packrowidx0_sub(ip_min, local_sub_ip + 1);
1366 Kokkos::deep_copy(interf.part2packrowidx0_sub, part2packrowidx0_sub);
1368 IFPACK2_BLOCKHELPER_TIMER_FENCE(
typename BlockHelperDetails::ImplType<MatrixType>::execution_space)
1370 IFPACK2_BLOCKHELPER_TIMER(
"compute part indices", indices);
1372 const auto *part = &partitions[p[ip]];
1374 TEUCHOS_ASSERT(ip == 0 || (ipnrows <=
static_cast<local_ordinal_type>(partitions[p[ip - 1]].size())));
1375 TEUCHOS_TEST_FOR_EXCEPT_MSG(ipnrows == 0,
1376 BlockHelperDetails::get_msg_prefix(comm)
1377 <<
"partition " << p[ip]
1378 <<
" is empty, which is not allowed.");
1380 part2rowidx0(ip + 1) = part2rowidx0(ip) + ipnrows;
1383 if (ip % vector_length == 0) pack_nrows = ipnrows;
1384 part2packrowidx0(ip + 1) = part2packrowidx0(ip) + ((ip + 1) % vector_length == 0 || ip + 1 == nparts ? pack_nrows : 0);
1387 const auto lcl_row = (*part)[i];
1388 TEUCHOS_TEST_FOR_EXCEPT_MSG(lcl_row < 0 || lcl_row >= A_n_lclrows,
1389 BlockHelperDetails::get_msg_prefix(comm)
1390 <<
"partitions[" << p[ip] <<
"]["
1391 << i <<
"] = " << lcl_row
1392 <<
" but input matrix implies limits of [0, " << A_n_lclrows - 1
1394 lclrow(offset + i) = lcl_row;
1395 rowidx2part(offset + i) = ip;
1396 if (interf.row_contiguous && offset + i > 0 && lclrow((offset + i) - 1) + 1 != lcl_row)
1397 interf.row_contiguous =
false;
1399 partptr(ip + 1) = offset + ipnrows;
1401#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
1402 printf(
"Part index = ip = %d, first LID associated to the part = partptr(ip) = offset = %d, part->size() = ipnrows = %d;\n", ip, offset, ipnrows);
1403 printf(
"partptr(%d+1) = %d\n", ip, partptr(ip + 1));
1407 part2rowidx0_sub(0) = 0;
1408 partptr_sub(0, 0) = 0;
1412 const auto *part = &partitions[p[ip]];
1416 TEUCHOS_TEST_FOR_EXCEPTION(full_line_length != ipnrows, std::logic_error,
1417 "In the part " << ip);
1421 if (full_line_length < n_subparts_per_part + (n_subparts_per_part - 1) * connection_length)
1422 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
1423 "The part " << ip <<
" is too short to use " << n_subparts_per_part <<
" sub parts.");
1425 const local_ordinal_type sub_line_length = (full_line_length - (n_subparts_per_part - 1) * connection_length) / n_subparts_per_part;
1426 const local_ordinal_type last_sub_line_length = full_line_length - (n_subparts_per_part - 1) * (connection_length + sub_line_length);
1428 if (ip % vector_length == 0) pack_nrows_sub = ipnrows;
1430 for (
local_ordinal_type local_sub_ip = 0; local_sub_ip < n_subparts_per_part; ++local_sub_ip) {
1433 if (local_sub_ip != n_subparts_per_part - 1) {
1434 if (local_sub_ip != 0) {
1435 partptr_sub(sub_ip, 0) = partptr_sub(nparts * (2 * local_sub_ip - 1) + ip, 1);
1436 }
else if (ip != 0) {
1437 partptr_sub(sub_ip, 0) = partptr_sub(nparts * 2 * (n_subparts_per_part - 1) + ip - 1, 1);
1439 partptr_sub(sub_ip, 1) = sub_line_length + partptr_sub(sub_ip, 0);
1440 partptr_sub(schur_ip, 0) = partptr_sub(sub_ip, 1);
1441 partptr_sub(schur_ip, 1) = connection_length + partptr_sub(schur_ip, 0);
1443 part2rowidx0_sub(sub_ip + 1) = part2rowidx0_sub(sub_ip) + sub_line_length;
1444 part2rowidx0_sub(sub_ip + 2) = part2rowidx0_sub(sub_ip + 1) + connection_length;
1446#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
1447 printf(
"Sub Part index = %d, first LID associated to the sub part = %d, sub part size = %d;\n", sub_ip, partptr_sub(sub_ip, 0), sub_line_length);
1448 printf(
"Sub Part index Schur = %d, first LID associated to the sub part = %d, sub part size = %d;\n", sub_ip + 1, partptr_sub(ip, 2 * local_sub_ip + 1), connection_length);
1451 if (local_sub_ip != 0) {
1452 partptr_sub(sub_ip, 0) = partptr_sub(nparts * (2 * local_sub_ip - 1) + ip, 1);
1453 }
else if (ip != 0) {
1454 partptr_sub(sub_ip, 0) = partptr_sub(nparts * 2 * (n_subparts_per_part - 1) + ip - 1, 1);
1456 partptr_sub(sub_ip, 1) = last_sub_line_length + partptr_sub(sub_ip, 0);
1458 part2rowidx0_sub(sub_ip + 1) = part2rowidx0_sub(sub_ip) + last_sub_line_length;
1460#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
1461 printf(
"Sub Part index = %d, first LID associated to the sub part = %d, sub part size = %d;\n", sub_ip, partptr_sub(sub_ip, 0), last_sub_line_length);
1472 part2packrowidx0_sub(ip, 0) = 0;
1477 ip_max = nparts > (ipack + 1) * vector_length ? (ipack + 1) * vector_length : nparts;
1479 part2packrowidx0_sub(ip, 0) = part2packrowidx0_sub(ip - vector_length, part2packrowidx0_sub.extent(1) - 1);
1483 for (size_type local_sub_ip = 0; local_sub_ip < part2packrowidx0_sub.extent(1) - 1; ++local_sub_ip) {
1485 ip_max = nparts > (ipack + 1) * vector_length ? (ipack + 1) * vector_length : nparts;
1491 const local_ordinal_type sub_line_length = (full_line_length - (n_subparts_per_part - 1) * connection_length) / n_subparts_per_part;
1492 const local_ordinal_type last_sub_line_length = full_line_length - (n_subparts_per_part - 1) * (connection_length + sub_line_length);
1494 if (local_sub_ip % 2 == 0) pack_nrows_sub = sub_line_length;
1495 if (local_sub_ip % 2 == 1) pack_nrows_sub = connection_length;
1496 if (local_sub_ip == part2packrowidx0_sub.extent(1) - 2) pack_nrows_sub = last_sub_line_length;
1498 part2packrowidx0_sub(ip_min, local_sub_ip + 1) = part2packrowidx0_sub(ip_min, local_sub_ip) + pack_nrows_sub;
1501 part2packrowidx0_sub(ip, local_sub_ip + 1) = part2packrowidx0_sub(ip_min, local_sub_ip + 1);
1506 Kokkos::deep_copy(interf.part2packrowidx0_sub, part2packrowidx0_sub);
1508 IFPACK2_BLOCKHELPER_TIMER_FENCE(
typename BlockHelperDetails::ImplType<MatrixType>::execution_space)
1510#if defined(BLOCKTRIDICONTAINER_DEBUG)
1511 TEUCHOS_ASSERT(partptr(nparts) == nrows);
1513 if (lclrow(0) != 0) interf.row_contiguous =
false;
1515 Kokkos::deep_copy(interf.partptr, partptr);
1516 Kokkos::deep_copy(interf.lclrow, lclrow);
1518 Kokkos::deep_copy(interf.partptr_sub, partptr_sub);
1521 interf.part2rowidx0 = interf.partptr;
1522 Kokkos::deep_copy(interf.part2packrowidx0, part2packrowidx0);
1524 interf.part2packrowidx0_back = part2packrowidx0_sub(part2packrowidx0_sub.extent(0) - 1, part2packrowidx0_sub.extent(1) - 1);
1525 Kokkos::deep_copy(interf.rowidx2part, rowidx2part);
1528 IFPACK2_BLOCKHELPER_TIMER(
"Fill packptr", packptr0);
1529 local_ordinal_type npacks = ceil(
float(nparts) / vector_length) * (part2packrowidx0_sub.extent(1) - 1);
1532 if (part2packrowidx0(ip) != part2packrowidx0(ip - 1))
1535 interf.packptr = local_ordinal_type_1d_view(do_not_initialize_tag(
"packptr"), npacks + 1);
1536 const auto packptr = Kokkos::create_mirror_view(interf.packptr);
1539 if (part2packrowidx0(ip) != part2packrowidx0(ip - 1))
1542 Kokkos::deep_copy(interf.packptr, packptr);
1545 npacks = ceil(
float(nparts) / vector_length) * (part2packrowidx0_sub.extent(1) - 1);
1547 interf.packindices_sub = local_ordinal_type_1d_view(do_not_initialize_tag(
"packindices_sub"), npacks_per_subpart * n_subparts_per_part);
1548 interf.packindices_schur = local_ordinal_type_2d_view(do_not_initialize_tag(
"packindices_schur"), npacks_per_subpart, n_subparts_per_part - 1);
1550 const auto packindices_sub = Kokkos::create_mirror_view(interf.packindices_sub);
1551 const auto packindices_schur = Kokkos::create_mirror_view(interf.packindices_schur);
1554 for (
local_ordinal_type local_sub_ip = 0; local_sub_ip < n_subparts_per_part - 1; ++local_sub_ip) {
1555 for (
local_ordinal_type local_pack_ip = 0; local_pack_ip < npacks_per_subpart; ++local_pack_ip) {
1556 packindices_sub(local_sub_ip * npacks_per_subpart + local_pack_ip) = 2 * local_sub_ip * npacks_per_subpart + local_pack_ip;
1557 packindices_schur(local_pack_ip, local_sub_ip) = 2 * local_sub_ip * npacks_per_subpart + local_pack_ip + npacks_per_subpart;
1561 for (
local_ordinal_type local_pack_ip = 0; local_pack_ip < npacks_per_subpart; ++local_pack_ip) {
1562 packindices_sub((n_subparts_per_part - 1) * npacks_per_subpart + local_pack_ip) = 2 * (n_subparts_per_part - 1) * npacks_per_subpart + local_pack_ip;
1565#ifdef IFPACK2_BLOCKTRIDICONTAINER_WRITE_MM
1566 std::cout <<
"packindices_sub = " << std::endl;
1567 for (size_type i = 0; i < packindices_sub.extent(0); ++i) {
1568 std::cout << packindices_sub(i) <<
" ";
1570 std::cout << std::endl;
1571 std::cout <<
"packindices_sub end" << std::endl;
1573 std::cout <<
"packindices_schur = " << std::endl;
1574 for (size_type i = 0; i < packindices_schur.extent(0); ++i) {
1575 for (size_type j = 0; j < packindices_schur.extent(1); ++j) {
1576 std::cout << packindices_schur(i, j) <<
" ";
1578 std::cout << std::endl;
1581 std::cout <<
"packindices_schur end" << std::endl;
1584 Kokkos::deep_copy(interf.packindices_sub, packindices_sub);
1585 Kokkos::deep_copy(interf.packindices_schur, packindices_schur);
1587 interf.packptr_sub = local_ordinal_type_1d_view(do_not_initialize_tag(
"packptr"), npacks + 1);
1588 const auto packptr_sub = Kokkos::create_mirror_view(interf.packptr_sub);
1591 packptr_sub(k) = packptr(k % npacks_per_subpart) + (k / npacks_per_subpart) * packptr(npacks_per_subpart);
1593 Kokkos::deep_copy(interf.packptr_sub, packptr_sub);
1594 IFPACK2_BLOCKHELPER_TIMER_FENCE(
typename BlockHelperDetails::ImplType<MatrixType>::execution_space)
1596 IFPACK2_BLOCKHELPER_TIMER_FENCE(
typename BlockHelperDetails::ImplType<MatrixType>::execution_space)
1604template <
typename MatrixType>
1605struct BlockTridiags {
1607 using local_ordinal_type_1d_view =
typename impl_type::local_ordinal_type_1d_view;
1609 using size_type_2d_view =
typename impl_type::size_type_2d_view;
1610 using vector_type_3d_view =
typename impl_type::vector_type_3d_view;
1611 using vector_type_4d_view =
typename impl_type::vector_type_4d_view;
1612 using btdm_scalar_type_3d_view =
typename impl_type::btdm_scalar_type_3d_view;
1613 using internal_vector_type_3d_view =
typename impl_type::internal_vector_type_3d_view;
1619 size_type_2d_view flat_td_ptr, pack_td_ptr, pack_td_ptr_schur;
1622 local_ordinal_type_1d_view A_colindsub;
1625 vector_type_3d_view values;
1628 vector_type_3d_view values_schur;
1630 vector_type_4d_view e_values;
1634 internal_vector_type_3d_view X_internal_vector_values_schur;
1639 size_type_1d_view diag_offsets;
1643 btdm_scalar_type_3d_view d_inv;
1645 bool is_diagonal_only;
1647 BlockTridiags() =
default;
1648 BlockTridiags(
const BlockTridiags &b) =
default;
1651 template <
typename idx_type>
1652 static KOKKOS_FORCEINLINE_FUNCTION
1654 IndexToRow(
const idx_type &ind) {
return (ind + 1) / 3; }
1657 template <
typename idx_type>
1658 static KOKKOS_FORCEINLINE_FUNCTION
1660 RowToIndex(
const idx_type &row) {
return row > 0 ? 3 * row - 1 : 0; }
1662 template <
typename idx_type>
1663 static KOKKOS_FORCEINLINE_FUNCTION
1665 NumBlocks(
const idx_type &nrows) {
return nrows > 0 ? 3 * nrows - 2 : 0; }
1667 template <
typename idx_type>
1668 static KOKKOS_FORCEINLINE_FUNCTION
1670 NumBlocksSchur(
const idx_type &nrows) {
return nrows > 0 ? 3 * nrows + 2 : 0; }
1676template <
typename MatrixType>
1678createBlockTridiags(
const BlockHelperDetails::PartInterface<MatrixType> &interf) {
1679 IFPACK2_BLOCKHELPER_TIMER(
"createBlockTridiags", createBlockTridiags0);
1681 using execution_space =
typename impl_type::execution_space;
1683 using size_type =
typename impl_type::size_type;
1684 using size_type_2d_view =
typename impl_type::size_type_2d_view;
1686 constexpr int vector_length = impl_type::vector_length;
1693 btdm.flat_td_ptr = size_type_2d_view(do_not_initialize_tag(
"btdm.flat_td_ptr"), interf.nparts, 2 * interf.n_subparts_per_part);
1694 const Kokkos::RangePolicy<execution_space> policy(0, 2 * interf.nparts * interf.n_subparts_per_part);
1695 Kokkos::parallel_scan(
1696 "createBlockTridiags::RangePolicy::flat_td_ptr",
1697 policy, KOKKOS_LAMBDA(
const local_ordinal_type &i, size_type &update,
const bool &
final) {
1702 btdm.flat_td_ptr(partidx, local_subpartidx) = update;
1704 if (local_subpartidx != (2 * interf.n_subparts_per_part - 1)) {
1705 const local_ordinal_type nrows = interf.partptr_sub(interf.nparts * local_subpartidx + partidx, 1) - interf.partptr_sub(interf.nparts * local_subpartidx + partidx, 0);
1706 if (local_subpartidx % 2 == 0)
1707 update += btdm.NumBlocks(nrows);
1709 update += btdm.NumBlocksSchur(nrows);
1713 const auto nblocks = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Kokkos::subview(btdm.flat_td_ptr, interf.nparts - 1, 2 * interf.n_subparts_per_part - 1));
1718 if (vector_length == 1) {
1719 btdm.pack_td_ptr = btdm.flat_td_ptr;
1724 const auto part2packrowidx0 = Kokkos::create_mirror_view(interf.part2packrowidx0);
1725 Kokkos::deep_copy(part2packrowidx0, interf.part2packrowidx0);
1727 if (part2packrowidx0(ip) != part2packrowidx0(ip - 1))
1728 ++npacks_per_subpart;
1730 btdm.pack_td_ptr = size_type_2d_view(do_not_initialize_tag(
"btdm.pack_td_ptr"), interf.nparts, 2 * interf.n_subparts_per_part);
1731 const Kokkos::RangePolicy<execution_space> policy(0, npacks_per_subpart);
1733 Kokkos::parallel_for(
1734 "createBlockTridiags::RangePolicy::pack_td_ptr",
1737 const local_ordinal_type pack_id = (j == 2 * interf.n_subparts_per_part - 1) ? i + (j - 1) * npacks_per_subpart : i + j * npacks_per_subpart;
1738 const local_ordinal_type nparts_in_pack = interf.packptr_sub(pack_id + 1) - interf.packptr_sub(pack_id);
1744 btdm.pack_td_ptr(partidx + pti, j) = btdm.flat_td_ptr(i, j);
1750 btdm.pack_td_ptr_schur = size_type_2d_view(do_not_initialize_tag(
"btdm.pack_td_ptr_schur"), interf.nparts, interf.n_subparts_per_part);
1752 const auto host_pack_td_ptr_schur = Kokkos::create_mirror_view(btdm.pack_td_ptr_schur);
1755 host_pack_td_ptr_schur(0, 0) = 0;
1757 if (i % vector_length == 0) {
1759 host_pack_td_ptr_schur(i, 0) = host_pack_td_ptr_schur(i - 1, host_pack_td_ptr_schur.extent(1) - 1);
1761 host_pack_td_ptr_schur(i, j + 1) = host_pack_td_ptr_schur(i, j) + btdm.NumBlocks(connection_length) + (j != 0 ? 1 : 0) + (j != interf.n_subparts_per_part - 2 ? 1 : 0);
1765 host_pack_td_ptr_schur(i, j) = host_pack_td_ptr_schur(i - 1, j);
1770 Kokkos::deep_copy(btdm.pack_td_ptr_schur, host_pack_td_ptr_schur);
1772#ifdef IFPACK2_BLOCKTRIDICONTAINER_WRITE_MM
1773 const auto host_flat_td_ptr = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), btdm.flat_td_ptr);
1774 std::cout <<
"flat_td_ptr = " << std::endl;
1775 for (size_type i = 0; i < host_flat_td_ptr.extent(0); ++i) {
1776 for (size_type j = 0; j < host_flat_td_ptr.extent(1); ++j) {
1777 std::cout << host_flat_td_ptr(i, j) <<
" ";
1779 std::cout << std::endl;
1781 std::cout <<
"flat_td_ptr end" << std::endl;
1783 const auto host_pack_td_ptr = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), btdm.pack_td_ptr);
1785 std::cout <<
"pack_td_ptr = " << std::endl;
1786 for (size_type i = 0; i < host_pack_td_ptr.extent(0); ++i) {
1787 for (size_type j = 0; j < host_pack_td_ptr.extent(1); ++j) {
1788 std::cout << host_pack_td_ptr(i, j) <<
" ";
1790 std::cout << std::endl;
1792 std::cout <<
"pack_td_ptr end" << std::endl;
1794 std::cout <<
"pack_td_ptr_schur = " << std::endl;
1795 for (size_type i = 0; i < host_pack_td_ptr_schur.extent(0); ++i) {
1796 for (size_type j = 0; j < host_pack_td_ptr_schur.extent(1); ++j) {
1797 std::cout << host_pack_td_ptr_schur(i, j) <<
" ";
1799 std::cout << std::endl;
1801 std::cout <<
"pack_td_ptr_schur end" << std::endl;
1805 IFPACK2_BLOCKHELPER_TIMER_FENCE(
typename BlockHelperDetails::ImplType<MatrixType>::execution_space)
1820template <
typename MatrixType>
1821void setTridiagsToIdentity(
const BlockTridiags<MatrixType> &btdm,
1822 const typename BlockHelperDetails::ImplType<MatrixType>::local_ordinal_type_1d_view &packptr) {
1824 using execution_space =
typename impl_type::execution_space;
1826 using size_type_2d_view =
typename impl_type::size_type_2d_view;
1828 const ConstUnmanaged<size_type_2d_view> pack_td_ptr(btdm.pack_td_ptr);
1832 const int vector_length = impl_type::vector_length;
1833 const int internal_vector_length = impl_type::internal_vector_length;
1835 using btdm_scalar_type =
typename impl_type::btdm_scalar_type;
1836 using internal_vector_type =
typename impl_type::internal_vector_type;
1837 using internal_vector_type_4d_view =
1838 typename impl_type::internal_vector_type_4d_view;
1840 using team_policy_type = Kokkos::TeamPolicy<execution_space>;
1841 const internal_vector_type_4d_view values(
reinterpret_cast<internal_vector_type *
>(btdm.values.data()),
1842 btdm.values.extent(0),
1843 btdm.values.extent(1),
1844 btdm.values.extent(2),
1845 vector_length / internal_vector_length);
1847#if defined(KOKKOS_ENABLE_CUDA) && defined(__CUDA_ARCH__)
1850 total_team_size = 32;
1851 else if (blocksize <= 9)
1852 total_team_size = 64;
1853 else if (blocksize <= 12)
1854 total_team_size = 96;
1855 else if (blocksize <= 16)
1856 total_team_size = 128;
1857 else if (blocksize <= 20)
1858 total_team_size = 160;
1860 total_team_size = 160;
1862 const team_policy_type policy(packptr.extent(0) - 1, team_size, vector_loop_size);
1863#elif defined(KOKKOS_ENABLE_HIP)
1870 total_team_size = 32;
1871 else if (blocksize <= 9)
1872 total_team_size = 64;
1873 else if (blocksize <= 12)
1874 total_team_size = 96;
1875 else if (blocksize <= 16)
1876 total_team_size = 128;
1877 else if (blocksize <= 20)
1878 total_team_size = 160;
1880 total_team_size = 160;
1882 const team_policy_type policy(packptr.extent(0) - 1, team_size, vector_loop_size);
1883#elif defined(KOKKOS_ENABLE_SYCL)
1887 total_team_size = 32;
1888 else if (blocksize <= 9)
1889 total_team_size = 64;
1890 else if (blocksize <= 12)
1891 total_team_size = 96;
1892 else if (blocksize <= 16)
1893 total_team_size = 128;
1894 else if (blocksize <= 20)
1895 total_team_size = 160;
1897 total_team_size = 160;
1899 const team_policy_type policy(packptr.extent(0) - 1, team_size, vector_loop_size);
1902 const team_policy_type policy(packptr.extent(0) - 1, 1, 1);
1904 Kokkos::parallel_for(
1905 "setTridiagsToIdentity::TeamPolicy",
1906 policy, KOKKOS_LAMBDA(
const typename team_policy_type::member_type &member) {
1913 const btdm_scalar_type one(1);
1914 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size), [&](
const int &v) {
1915 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, icount), [&](
const local_ordinal_type &ii) {
1918 values(i, j, j, v) = one;
1929template <
typename MatrixType>
1930void performSymbolicPhase(
const Teuchos::RCP<
const typename BlockHelperDetails::ImplType<MatrixType>::tpetra_row_matrix_type> &A,
1931 const Teuchos::RCP<
const typename BlockHelperDetails::ImplType<MatrixType>::tpetra_crs_graph_type> &g,
1932 const BlockHelperDetails::PartInterface<MatrixType> &interf,
1933 BlockTridiags<MatrixType> &btdm,
1935 const bool overlap_communication_and_computation,
1936 const Teuchos::RCP<AsyncableImport<MatrixType>> &async_importer,
1938 bool use_fused_jacobi) {
1939 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::SymbolicPhase", SymbolicPhase);
1943 using execution_space =
typename impl_type::execution_space;
1947 using size_type =
typename impl_type::size_type;
1948 using local_ordinal_type_1d_view =
typename impl_type::local_ordinal_type_1d_view;
1949 using size_type_1d_view =
typename impl_type::size_type_1d_view;
1950 using vector_type_3d_view =
typename impl_type::vector_type_3d_view;
1951 using vector_type_4d_view =
typename impl_type::vector_type_4d_view;
1952 using crs_matrix_type =
typename impl_type::tpetra_crs_matrix_type;
1953 using block_crs_matrix_type =
typename impl_type::tpetra_block_crs_matrix_type;
1954 using btdm_scalar_type_3d_view =
typename impl_type::btdm_scalar_type_3d_view;
1955 using internal_vector_type_3d_view =
typename impl_type::internal_vector_type_3d_view;
1956 using lo_traits = Tpetra::Details::OrdinalTraits<local_ordinal_type>;
1958 constexpr int vector_length = impl_type::vector_length;
1959 constexpr int internal_vector_length = impl_type::internal_vector_length;
1961 const auto comm = A->getRowMap()->getComm();
1963 auto A_crs = Teuchos::rcp_dynamic_cast<const crs_matrix_type>(A);
1964 auto A_bcrs = Teuchos::rcp_dynamic_cast<const block_crs_matrix_type>(A);
1966 bool hasBlockCrsMatrix = !A_bcrs.is_null();
1967 TEUCHOS_ASSERT(hasBlockCrsMatrix || g->getLocalNumRows() != 0);
1968 const local_ordinal_type blocksize = hasBlockCrsMatrix ? A->getBlockSize() : A->getLocalNumRows() / g->getLocalNumRows();
1970 const auto partptr = interf.partptr;
1971 const auto lclrow = interf.lclrow;
1972 const auto rowidx2part = interf.rowidx2part;
1973 const auto part2rowidx0 = interf.part2rowidx0;
1974 const auto packptr = interf.packptr;
1978 Kokkos::HostSpace(), Kokkos::subview(partptr, partptr.extent(0) - 1))();
1980 Kokkos::View<local_ordinal_type *, execution_space> col2row(
"col2row", A->getLocalNumCols());
1984 Kokkos::deep_copy(execution_space(), col2row, Teuchos::OrdinalTraits<local_ordinal_type>::invalid());
1986 TEUCHOS_ASSERT(!(g->getRowMap().is_null() || g->getColMap().is_null() || g->getDomainMap().is_null()));
1987#if defined(BLOCKTRIDICONTAINER_DEBUG)
1990 auto rowmapHost = g->getRowMap();
1991 auto colmapHost = g->getColMap();
1992 auto dommapHost = g->getDomainMap();
1995 TEUCHOS_ASSERT(gid != Teuchos::OrdinalTraits<global_ordinal_type>::invalid());
1996 if (dommapHost->isNodeGlobalElement(gid)) {
1998 TEUCHOS_TEST_FOR_EXCEPT_MSG(lc == Teuchos::OrdinalTraits<local_ordinal_type>::invalid(),
1999 BlockHelperDetails::get_msg_prefix(comm) <<
"GID " << gid
2000 <<
" gives an invalid local column.");
2005 auto rowmap = g->getRowMap()->getLocalMap();
2006 auto colmap = g->getColMap()->getLocalMap();
2007 auto dommap = g->getDomainMap()->getLocalMap();
2009 const Kokkos::RangePolicy<execution_space> policy(0, nrows);
2010 Kokkos::parallel_for(
2011 "performSymbolicPhase::RangePolicy::col2row",
2014 if (dommap.getLocalElement(gid) != lo_traits::invalid()) {
2023 const auto local_graph = g->getLocalGraphDevice();
2024 const auto local_graph_rowptr = local_graph.row_map;
2025 TEUCHOS_ASSERT(local_graph_rowptr.size() ==
static_cast<size_t>(nrows + 1));
2026 const auto local_graph_colidx = local_graph.entries;
2030 Kokkos::View<local_ordinal_type *, execution_space> lclrow2idx(
"lclrow2idx", nrows);
2032 const Kokkos::RangePolicy<execution_space> policy(0, nrows);
2033 Kokkos::parallel_for(
2034 "performSymbolicPhase::RangePolicy::lclrow2idx",
2036 lclrow2idx(lclrow(i)) = i;
2041 size_type D_nnz, R_nnz_owned, R_nnz_remote;
2043 const Kokkos::RangePolicy<execution_space> policy(0, nrows);
2044 Kokkos::parallel_reduce
2047 policy, KOKKOS_LAMBDA(
const local_ordinal_type &lr, size_type &update_D_nnz, size_type &update_R_nnz_owned, size_type &update_R_nnz_remote) {
2051 for (size_type j = local_graph_rowptr(lr); j < local_graph_rowptr(lr + 1); ++j) {
2054 bool incr_R =
false;
2069 if (ri0 + 1 >= ri && ri0 <= ri + 1)
2076 ++update_R_nnz_owned;
2078 ++update_R_nnz_remote;
2082 D_nnz, R_nnz_owned, R_nnz_remote);
2085 if (!overlap_communication_and_computation) {
2086 R_nnz_owned += R_nnz_remote;
2092 const auto flat_td_ptr = btdm.flat_td_ptr;
2094 btdm.A_colindsub = local_ordinal_type_1d_view(
"btdm.A_colindsub", D_nnz);
2095 const auto D_A_colindsub = btdm.A_colindsub;
2097#if defined(BLOCKTRIDICONTAINER_DEBUG)
2098 Kokkos::deep_copy(D_A_colindsub, Teuchos::OrdinalTraits<local_ordinal_type>::invalid());
2104 const Kokkos::RangePolicy<execution_space> policy(0, nparts);
2105 Kokkos::parallel_for(
2106 "performSymbolicPhase::RangePolicy<execution_space>::D_graph",
2114 const size_type j0 = local_graph_rowptr(lr0);
2115 for (size_type j = j0; j < local_graph_rowptr(lr0 + 1); ++j) {
2121 if (pi != pi0)
continue;
2122 if (ri + 1 < ri0 || ri > ri0 + 1)
continue;
2124 D_A_colindsub(flat_td_ptr(pi0, 0) + ((td_row_os + ri) - ri0)) = row_entry;
2129#if defined(BLOCKTRIDICONTAINER_DEBUG)
2131 auto D_A_colindsub_host = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), D_A_colindsub);
2132 for (
size_t i = 0; i < D_A_colindsub_host.extent(0); ++i)
2133 TEUCHOS_ASSERT(D_A_colindsub_host(i) != Teuchos::OrdinalTraits<local_ordinal_type>::invalid());
2139 const auto pack_td_ptr_last = Kokkos::subview(btdm.pack_td_ptr, btdm.pack_td_ptr.extent(0) - 1, btdm.pack_td_ptr.extent(1) - 1);
2140 const auto num_packed_blocks = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), pack_td_ptr_last);
2141 btdm.values = vector_type_3d_view(
"btdm.values", num_packed_blocks(), blocksize, blocksize);
2143 if (interf.n_subparts_per_part > 1) {
2144 const auto pack_td_ptr_schur_last = Kokkos::subview(btdm.pack_td_ptr_schur, btdm.pack_td_ptr_schur.extent(0) - 1, btdm.pack_td_ptr_schur.extent(1) - 1);
2145 const auto num_packed_blocks_schur = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), pack_td_ptr_schur_last);
2146 btdm.values_schur = vector_type_3d_view(
"btdm.values_schur", num_packed_blocks_schur(), blocksize, blocksize);
2149 if (vector_length > 1) setTridiagsToIdentity(btdm, interf.packptr);
2155 amd.rowptr = size_type_1d_view(
"amd.rowptr", nrows + 1);
2156 amd.A_colindsub = local_ordinal_type_1d_view(do_not_initialize_tag(
"amd.A_colindsub"), R_nnz_owned);
2158 const auto R_rowptr = amd.rowptr;
2159 const auto R_A_colindsub = amd.A_colindsub;
2161 amd.rowptr_remote = size_type_1d_view(
"amd.rowptr_remote", overlap_communication_and_computation ? nrows + 1 : 0);
2162 amd.A_colindsub_remote = local_ordinal_type_1d_view(do_not_initialize_tag(
"amd.A_colindsub_remote"), R_nnz_remote);
2164 const auto R_rowptr_remote = amd.rowptr_remote;
2165 const auto R_A_colindsub_remote = amd.A_colindsub_remote;
2168 const Kokkos::RangePolicy<execution_space> policy(0, nrows);
2169 Kokkos::parallel_for(
2170 "performSymbolicPhase::RangePolicy<execution_space>::R_graph_count",
2174 const size_type j0 = local_graph_rowptr(lr);
2175 for (size_type j = j0; j < local_graph_rowptr(lr + 1); ++j) {
2181 if (pi == pi0 && ri + 1 >= ri0 && ri <= ri0 + 1) {
2186 if (!overlap_communication_and_computation || lc < nrows) {
2189 ++R_rowptr_remote(lr);
2198 size_type R_rowptr_final;
2199#if KOKKOSKERNELS_VERSION >= 50299
2200 KokkosKernels::exclusive_parallel_prefix_sum(execution_space(), R_rowptr, R_rowptr_final);
2202 KokkosKernels::Impl::kk_exclusive_parallel_prefix_sum<execution_space>(nrows + 1, R_rowptr, R_rowptr_final);
2204 TEUCHOS_ASSERT(R_rowptr_final == R_nnz_owned);
2205 if (overlap_communication_and_computation) {
2206 size_type R_rowptr_remote_final;
2207#if KOKKOSKERNELS_VERSION >= 50299
2208 KokkosKernels::exclusive_parallel_prefix_sum(execution_space(), R_rowptr_remote, R_rowptr_remote_final);
2210 KokkosKernels::Impl::kk_exclusive_parallel_prefix_sum<execution_space>(nrows + 1, R_rowptr_remote, R_rowptr_remote_final);
2212 TEUCHOS_ASSERT(R_rowptr_remote_final == R_nnz_remote);
2217 Kokkos::RangePolicy<execution_space> policy(0, nrows);
2218 Kokkos::parallel_for(
2219 "performSymbolicPhase::RangePolicy<execution_space>::R_graph_fill",
2224 size_type cnt_rowptr = R_rowptr(lr);
2225 size_type cnt_rowptr_remote = overlap_communication_and_computation ? R_rowptr_remote(lr) : 0;
2227 const size_type j0 = local_graph_rowptr(lr);
2228 for (size_type j = j0; j < local_graph_rowptr(lr + 1); ++j) {
2234 if (pi == pi0 && ri + 1 >= ri0 && ri <= ri0 + 1)
2238 if (!overlap_communication_and_computation || lc < nrows)
2239 R_A_colindsub(cnt_rowptr++) = row_entry;
2241 R_A_colindsub_remote(cnt_rowptr_remote++) = row_entry;
2247 if (hasBlockCrsMatrix)
2248 amd.tpetra_values = (
const_cast<block_crs_matrix_type *
>(A_bcrs.get())->getValuesDeviceNonConst());
2250 amd.tpetra_values = (
const_cast<crs_matrix_type *
>(A_crs.get()))->getLocalValuesDevice(Tpetra::Access::ReadWrite);
2254 if (interf.n_subparts_per_part > 1) {
2256 btdm.e_values = vector_type_4d_view(
"btdm.e_values", 2, interf.part2packrowidx0_back, blocksize, blocksize);
2257 btdm.X_internal_vector_values_schur = internal_vector_type_3d_view(
2258 do_not_initialize_tag(
"X_internal_vector_values_schur"),
2259 2 * (interf.n_subparts_per_part - 1) * interf.part2packrowidx0_sub.extent(0),
2261 vector_length / internal_vector_length);
2272 if (BlockHelperDetails::is_device<execution_space>::value && !useSeqMethod && hasBlockCrsMatrix) {
2273 bool is_async_importer_active = !async_importer.is_null();
2274 local_ordinal_type_1d_view dm2cm = is_async_importer_active ? async_importer->dm2cm : local_ordinal_type_1d_view();
2275 bool ownedRemoteSeparate = overlap_communication_and_computation || !is_async_importer_active;
2276 BlockHelperDetails::ComputeResidualVector<MatrixType>::precompute_A_x_offsets(amd, interf, g, dm2cm, blocksize, ownedRemoteSeparate);
2280 if (use_fused_jacobi) {
2281 btdm.d_inv = btdm_scalar_type_3d_view(do_not_initialize_tag(
"btdm.d_inv"), interf.nparts, blocksize, blocksize);
2282 auto rowptrs = A_bcrs->getCrsGraph().getLocalRowPtrsDevice();
2283 auto entries = A_bcrs->getCrsGraph().getLocalIndicesDevice();
2284 btdm.diag_offsets = BlockHelperDetails::findDiagOffsets<execution_space, size_type_1d_view>(rowptrs, entries, interf.nparts, blocksize);
2286 IFPACK2_BLOCKHELPER_TIMER_FENCE(
typename BlockHelperDetails::ImplType<MatrixType>::execution_space)
2292template <
typename ArgActiveExecutionMemorySpace>
2297 typedef KB::Mode::Serial mode_type;
2298#if defined(__KOKKOSBATCHED_INTEL_MKL_COMPACT_BATCHED__)
2299 typedef KB::Algo::Level3::CompactMKL algo_type;
2301 typedef KB::Algo::Level3::Blocked algo_type;
2303 static int recommended_team_size(
const int ,
2310#if defined(KOKKOS_ENABLE_CUDA)
2311static inline int ExtractAndFactorizeRecommendedCudaTeamSize(
const int blksize,
2312 const int vector_length,
2313 const int internal_vector_length) {
2314 const int vector_size = vector_length / internal_vector_length;
2315 int total_team_size(0);
2317 total_team_size = 32;
2318 else if (blksize <= 9)
2319 total_team_size = 32;
2320 else if (blksize <= 12)
2321 total_team_size = 96;
2322 else if (blksize <= 16)
2323 total_team_size = 128;
2324 else if (blksize <= 20)
2325 total_team_size = 160;
2327 total_team_size = 160;
2328 return 2 * total_team_size / vector_size;
2331struct ExtractAndFactorizeTridiagsDefaultModeAndAlgo<Kokkos::CudaSpace> {
2332 typedef KB::Mode::Team mode_type;
2333 typedef KB::Algo::Level3::Unblocked algo_type;
2334 static int recommended_team_size(
const int blksize,
2335 const int vector_length,
2336 const int internal_vector_length) {
2337 return ExtractAndFactorizeRecommendedCudaTeamSize(blksize, vector_length, internal_vector_length);
2341struct ExtractAndFactorizeTridiagsDefaultModeAndAlgo<Kokkos::CudaUVMSpace> {
2342 typedef KB::Mode::Team mode_type;
2343 typedef KB::Algo::Level3::Unblocked algo_type;
2344 static int recommended_team_size(
const int blksize,
2345 const int vector_length,
2346 const int internal_vector_length) {
2347 return ExtractAndFactorizeRecommendedCudaTeamSize(blksize, vector_length, internal_vector_length);
2352#if defined(KOKKOS_ENABLE_HIP)
2353static inline int ExtractAndFactorizeRecommendedHIPTeamSize(
const int blksize,
2354 const int vector_length,
2355 const int internal_vector_length) {
2356 const int vector_size = vector_length / internal_vector_length;
2357 int total_team_size(0);
2359 total_team_size = 32;
2360 else if (blksize <= 9)
2361 total_team_size = 32;
2362 else if (blksize <= 12)
2363 total_team_size = 96;
2364 else if (blksize <= 16)
2365 total_team_size = 128;
2366 else if (blksize <= 20)
2367 total_team_size = 160;
2369 total_team_size = 160;
2370 return 2 * total_team_size / vector_size;
2373struct ExtractAndFactorizeTridiagsDefaultModeAndAlgo<Kokkos::HIPSpace> {
2374 typedef KB::Mode::Team mode_type;
2375 typedef KB::Algo::Level3::Unblocked algo_type;
2376 static int recommended_team_size(
const int blksize,
2377 const int vector_length,
2378 const int internal_vector_length) {
2379 return ExtractAndFactorizeRecommendedHIPTeamSize(blksize, vector_length, internal_vector_length);
2383struct ExtractAndFactorizeTridiagsDefaultModeAndAlgo<Kokkos::HIPHostPinnedSpace> {
2384 typedef KB::Mode::Team mode_type;
2385 typedef KB::Algo::Level3::Unblocked algo_type;
2386 static int recommended_team_size(
const int blksize,
2387 const int vector_length,
2388 const int internal_vector_length) {
2389 return ExtractAndFactorizeRecommendedHIPTeamSize(blksize, vector_length, internal_vector_length);
2394#if defined(KOKKOS_ENABLE_SYCL)
2395static inline int ExtractAndFactorizeRecommendedSYCLTeamSize(
const int blksize,
2396 const int vector_length,
2397 const int internal_vector_length) {
2398 const int vector_size = vector_length / internal_vector_length;
2399 int total_team_size(0);
2401 total_team_size = 32;
2402 else if (blksize <= 9)
2403 total_team_size = 32;
2404 else if (blksize <= 12)
2405 total_team_size = 96;
2406 else if (blksize <= 16)
2407 total_team_size = 128;
2408 else if (blksize <= 20)
2409 total_team_size = 160;
2411 total_team_size = 160;
2412 return 2 * total_team_size / vector_size;
2415struct ExtractAndFactorizeTridiagsDefaultModeAndAlgo<Kokkos::SYCLDeviceUSMSpace> {
2416 typedef KB::Mode::Team mode_type;
2417 typedef KB::Algo::Level3::Unblocked algo_type;
2418 static int recommended_team_size(
const int blksize,
2419 const int vector_length,
2420 const int internal_vector_length) {
2421 return ExtractAndFactorizeRecommendedSYCLTeamSize(blksize, vector_length, internal_vector_length);
2425struct ExtractAndFactorizeTridiagsDefaultModeAndAlgo<Kokkos::SYCLSharedUSMSpace> {
2426 typedef KB::Mode::Team mode_type;
2427 typedef KB::Algo::Level3::Unblocked algo_type;
2428 static int recommended_team_size(
const int blksize,
2429 const int vector_length,
2430 const int internal_vector_length) {
2431 return ExtractAndFactorizeRecommendedSYCLTeamSize(blksize, vector_length, internal_vector_length);
2436template <
typename impl_type,
typename WWViewType>
2437KOKKOS_INLINE_FUNCTION
void
2438solveMultiVector(
const typename Kokkos::TeamPolicy<typename impl_type::execution_space>::member_type &member,
2439 const typename impl_type::local_ordinal_type & ,
2440 const typename impl_type::local_ordinal_type &i0,
2441 const typename impl_type::local_ordinal_type &r0,
2442 const typename impl_type::local_ordinal_type &nrows,
2443 const typename impl_type::local_ordinal_type &v,
2444 const ConstUnmanaged<typename impl_type::internal_vector_type_4d_view> D_internal_vector_values,
2445 const Unmanaged<typename impl_type::internal_vector_type_4d_view> X_internal_vector_values,
2446 const WWViewType &WW,
2447 const bool skip_first_pass =
false) {
2448 using execution_space =
typename impl_type::execution_space;
2449 using team_policy_type = Kokkos::TeamPolicy<execution_space>;
2450 using member_type =
typename team_policy_type::member_type;
2453 typedef SolveTridiagsDefaultModeAndAlgo<typename execution_space::memory_space> default_mode_and_algo_type;
2455 typedef typename default_mode_and_algo_type::mode_type default_mode_type;
2456 typedef typename default_mode_and_algo_type::multi_vector_algo_type default_algo_type;
2458 using btdm_magnitude_type =
typename impl_type::btdm_magnitude_type;
2461 const auto one = KokkosKernels::ArithTraits<btdm_magnitude_type>::one();
2462 const auto zero = KokkosKernels::ArithTraits<btdm_magnitude_type>::zero();
2465 auto A = Kokkos::subview(D_internal_vector_values, i0, Kokkos::ALL(), Kokkos::ALL(), v);
2466 auto X1 = Kokkos::subview(X_internal_vector_values, r0, Kokkos::ALL(), Kokkos::ALL(), v);
2473 if (skip_first_pass) {
2474 i += (nrows - 2) * 3;
2476 A.assign_data(&D_internal_vector_values(i + 2, 0, 0, v));
2477 X2.assign_data(&X_internal_vector_values(++r, 0, 0, v));
2478 A.assign_data(&D_internal_vector_values(i + 3, 0, 0, v));
2479 KB::Trsm<member_type,
2480 KB::Side::Left, KB::Uplo::Lower, KB::Trans::NoTranspose, KB::Diag::Unit,
2481 default_mode_type, default_algo_type>::invoke(member, one, A, X2);
2482 X1.assign_data(X2.data());
2485 KB::Trsm<member_type,
2486 KB::Side::Left, KB::Uplo::Lower, KB::Trans::NoTranspose, KB::Diag::Unit,
2487 default_mode_type, default_algo_type>::invoke(member, one, A, X1);
2489 A.assign_data(&D_internal_vector_values(i + 2, 0, 0, v));
2490 X2.assign_data(&X_internal_vector_values(++r, 0, 0, v));
2491 member.team_barrier();
2492 KB::Gemm<member_type,
2493 KB::Trans::NoTranspose, KB::Trans::NoTranspose,
2494 default_mode_type, default_algo_type>::invoke(member, -one, A, X1, one, X2);
2495 A.assign_data(&D_internal_vector_values(i + 3, 0, 0, v));
2496 KB::Trsm<member_type,
2497 KB::Side::Left, KB::Uplo::Lower, KB::Trans::NoTranspose, KB::Diag::Unit,
2498 default_mode_type, default_algo_type>::invoke(member, one, A, X2);
2499 X1.assign_data(X2.data());
2504 KB::Trsm<member_type,
2505 KB::Side::Left, KB::Uplo::Upper, KB::Trans::NoTranspose, KB::Diag::NonUnit,
2506 default_mode_type, default_algo_type>::invoke(member, one, A, X1);
2509 A.assign_data(&D_internal_vector_values(i + 1, 0, 0, v));
2510 X2.assign_data(&X_internal_vector_values(--r, 0, 0, v));
2511 member.team_barrier();
2512 KB::Gemm<member_type,
2513 KB::Trans::NoTranspose, KB::Trans::NoTranspose,
2514 default_mode_type, default_algo_type>::invoke(member, -one, A, X1, one, X2);
2516 A.assign_data(&D_internal_vector_values(i, 0, 0, v));
2517 KB::Trsm<member_type,
2518 KB::Side::Left, KB::Uplo::Upper, KB::Trans::NoTranspose, KB::Diag::NonUnit,
2519 default_mode_type, default_algo_type>::invoke(member, one, A, X2);
2520 X1.assign_data(X2.data());
2524 auto W = Kokkos::subview(WW, Kokkos::ALL(), Kokkos::ALL(), v);
2525 KB::Copy<member_type, KB::Trans::NoTranspose, default_mode_type>::invoke(member, X1, W);
2526 member.team_barrier();
2527 KB::Gemm<member_type,
2528 KB::Trans::NoTranspose, KB::Trans::NoTranspose,
2529 default_mode_type, default_algo_type>::invoke(member, one, A, W, zero, X1);
2533template <
typename impl_type,
typename WWViewType,
typename XViewType>
2534KOKKOS_INLINE_FUNCTION
void
2535solveSingleVectorNew(
const typename Kokkos::TeamPolicy<typename impl_type::execution_space>::member_type &member,
2536 const typename impl_type::local_ordinal_type &blocksize,
2537 const typename impl_type::local_ordinal_type &i0,
2538 const typename impl_type::local_ordinal_type &r0,
2539 const typename impl_type::local_ordinal_type &nrows,
2540 const typename impl_type::local_ordinal_type &v,
2541 const ConstUnmanaged<typename impl_type::internal_vector_type_4d_view> D_internal_vector_values,
2542 const XViewType &X_internal_vector_values,
2543 const WWViewType &WW) {
2544 using execution_space =
typename impl_type::execution_space;
2549 typedef SolveTridiagsDefaultModeAndAlgo<typename execution_space::memory_space> default_mode_and_algo_type;
2551 typedef typename default_mode_and_algo_type::mode_type default_mode_type;
2552 typedef typename default_mode_and_algo_type::single_vector_algo_type default_algo_type;
2554 using btdm_magnitude_type =
typename impl_type::btdm_magnitude_type;
2557 auto A = D_internal_vector_values.data();
2558 auto X = X_internal_vector_values.data();
2561 const auto one = KokkosKernels::ArithTraits<btdm_magnitude_type>::one();
2562 const auto zero = KokkosKernels::ArithTraits<btdm_magnitude_type>::zero();
2573 A += i0 * astep + v;
2574 X += r0 * xstep + v;
2579 KOKKOSBATCHED_TRSV_LOWER_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
2582 blocksize, blocksize,
2588 member.team_barrier();
2589 KOKKOSBATCHED_GEMV_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
2591 blocksize, blocksize,
2593 A + 2 * astep, as0, as1,
2596 X + 1 * xstep, xs0);
2597 KOKKOSBATCHED_TRSV_LOWER_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
2600 blocksize, blocksize,
2602 A + 3 * astep, as0, as1,
2603 X + 1 * xstep, xs0);
2610 KOKKOSBATCHED_TRSV_UPPER_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
2613 blocksize, blocksize,
2620 member.team_barrier();
2621 KOKKOSBATCHED_GEMV_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
2623 blocksize, blocksize,
2625 A + 1 * astep, as0, as1,
2628 X - 1 * xstep, xs0);
2629 KOKKOSBATCHED_TRSV_UPPER_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
2632 blocksize, blocksize,
2635 X - 1 * xstep, xs0);
2642 auto W = WW.data() + v;
2643 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, blocksize), [&](
int i) { W[i * ws0] = X[i * xs0]; });
2644 member.team_barrier();
2645 KOKKOSBATCHED_GEMV_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
2647 blocksize, blocksize,
2656template <
typename local_ordinal_type,
typename ViewType>
2657void writeBTDValuesToFile(
const local_ordinal_type &n_parts,
const ViewType &scalar_values_device, std::string fileName) {
2658#ifdef IFPACK2_BLOCKTRIDICONTAINER_WRITE_MM
2659 auto scalar_values = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), scalar_values_device);
2660 std::ofstream myfile;
2661 myfile.open(fileName);
2664 local_ordinal_type nnz = scalar_values.extent(0) * scalar_values.extent(1) * scalar_values.extent(2) * n_parts_per_pack;
2675 myfile <<
"%%MatrixMarket matrix coordinate real general" << std::endl;
2676 myfile <<
"%%nnz = " << nnz;
2677 myfile <<
" block size = " << block_size;
2678 myfile <<
" number of blocks = " << n_blocks;
2679 myfile <<
" number of parts = " << n_parts;
2680 myfile <<
" number of blocks per part = " << n_blocks_per_part;
2681 myfile <<
" number of rows = " << n_rows;
2682 myfile <<
" number of cols = " << n_rows;
2683 myfile <<
" number of packs = " << n_packs << std::endl;
2685 myfile << n_rows <<
" " << n_rows <<
" " << nnz << std::setprecision(9) << std::endl;
2687 local_ordinal_type current_part_idx, current_block_idx, current_row_offset, current_col_offset, current_row, current_col;
2689 for (
local_ordinal_type i_part_in_pack = 0; i_part_in_pack < n_parts_per_pack; ++i_part_in_pack) {
2690 current_part_idx = i_part_in_pack + i_pack * n_parts_per_pack;
2691 for (
local_ordinal_type i_block_in_part = 0; i_block_in_part < n_blocks_per_part; ++i_block_in_part) {
2692 current_block_idx = i_block_in_part + i_pack * n_blocks_per_part;
2695 if (i_block_in_part % 3 == 0) {
2696 current_row_offset = i_block_in_part / 3 * block_size;
2697 current_col_offset = i_block_in_part / 3 * block_size;
2698 }
else if (i_block_in_part % 3 == 1) {
2699 current_row_offset = (i_block_in_part - 1) / 3 * block_size;
2700 current_col_offset = ((i_block_in_part - 1) / 3 + 1) * block_size;
2701 }
else if (i_block_in_part % 3 == 2) {
2702 current_row_offset = ((i_block_in_part - 2) / 3 + 1) * block_size;
2703 current_col_offset = (i_block_in_part - 2) / 3 * block_size;
2705 current_row_offset += current_part_idx * n_rows_per_part;
2706 current_col_offset += current_part_idx * n_rows_per_part;
2709 current_row = current_row_offset + i_in_block + 1;
2710 current_col = current_col_offset + j_in_block + 1;
2711 myfile << current_row <<
" " << current_col <<
" " << scalar_values(current_block_idx, i_in_block, j_in_block, i_part_in_pack) << std::endl;
2722template <
typename local_ordinal_type,
typename ViewType>
2723void write4DMultiVectorValuesToFile(
const local_ordinal_type &n_parts,
const ViewType &scalar_values_device, std::string fileName) {
2724#ifdef IFPACK2_BLOCKTRIDICONTAINER_WRITE_MM
2725 auto scalar_values = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), scalar_values_device);
2726 std::ofstream myfile;
2727 myfile.open(fileName);
2729 const local_ordinal_type n_parts_per_pack = n_parts < scalar_values.extent(3) ? n_parts : scalar_values.extent(3);
2741 myfile <<
"%%MatrixMarket matrix array real general" << std::endl;
2742 myfile <<
"%%block size = " << block_size;
2743 myfile <<
" number of blocks = " << n_blocks;
2744 myfile <<
" number of parts = " << n_parts;
2745 myfile <<
" number of blocks per part = " << n_blocks_per_part;
2746 myfile <<
" number of rows = " << n_rows;
2747 myfile <<
" number of cols = " << n_cols;
2748 myfile <<
" number of packs = " << n_packs << std::endl;
2750 myfile << n_rows <<
" " << n_cols << std::setprecision(9) << std::endl;
2753 (void)current_row_offset;
2754 (void)current_part_idx;
2757 for (
local_ordinal_type i_part_in_pack = 0; i_part_in_pack < n_parts_per_pack; ++i_part_in_pack) {
2758 current_part_idx = i_part_in_pack + i_pack * n_parts_per_pack;
2759 for (
local_ordinal_type i_block_in_part = 0; i_block_in_part < n_blocks_per_part; ++i_block_in_part) {
2760 current_block_idx = i_block_in_part + i_pack * n_blocks_per_part;
2765 myfile << scalar_values(current_block_idx, i_in_block, j_in_block, i_part_in_pack) << std::endl;
2775template <
typename local_ordinal_type,
typename ViewType>
2776void write5DMultiVectorValuesToFile(
const local_ordinal_type &n_parts,
const ViewType &scalar_values_device, std::string fileName) {
2777#ifdef IFPACK2_BLOCKTRIDICONTAINER_WRITE_MM
2778 auto scalar_values = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), scalar_values_device);
2779 std::ofstream myfile;
2780 myfile.open(fileName);
2782 const local_ordinal_type n_parts_per_pack = n_parts < scalar_values.extent(4) ? n_parts : scalar_values.extent(4);
2795 myfile <<
"%%MatrixMarket matrix array real general" << std::endl;
2796 myfile <<
"%%block size = " << block_size;
2797 myfile <<
" number of blocks = " << n_blocks;
2798 myfile <<
" number of parts = " << n_parts;
2799 myfile <<
" number of blocks per part = " << n_blocks_per_part;
2800 myfile <<
" number of rows = " << n_rows;
2801 myfile <<
" number of cols = " << n_cols;
2802 myfile <<
" number of packs = " << n_packs << std::endl;
2804 myfile << n_rows <<
" " << n_cols << std::setprecision(9) << std::endl;
2807 (void)current_row_offset;
2808 (void)current_part_idx;
2809 for (
local_ordinal_type i_block_col = 0; i_block_col < n_blocks_cols; ++i_block_col) {
2812 for (
local_ordinal_type i_part_in_pack = 0; i_part_in_pack < n_parts_per_pack; ++i_part_in_pack) {
2813 current_part_idx = i_part_in_pack + i_pack * n_parts_per_pack;
2814 for (
local_ordinal_type i_block_in_part = 0; i_block_in_part < n_blocks_per_part; ++i_block_in_part) {
2815 current_block_idx = i_block_in_part + i_pack * n_blocks_per_part;
2820 myfile << scalar_values(i_block_col, current_block_idx, i_in_block, j_in_block, i_part_in_pack) << std::endl;
2831template <
typename local_ordinal_type,
typename member_type,
typename ViewType1,
typename ViewType2>
2832KOKKOS_INLINE_FUNCTION
void
2833copy3DView(
const member_type &member,
const ViewType1 &view1,
const ViewType2 &view2) {
2846 Kokkos::Experimental::local_deep_copy(member, view1, view2);
2848template <
typename MatrixType,
int ScratchLevel>
2849struct ExtractAndFactorizeTridiags {
2851 using impl_type = BlockHelperDetails::ImplType<MatrixType>;
2853 using execution_space =
typename impl_type::execution_space;
2854 using memory_space =
typename impl_type::memory_space;
2856 using local_ordinal_type =
typename impl_type::local_ordinal_type;
2857 using size_type =
typename impl_type::size_type;
2858 using impl_scalar_type =
typename impl_type::impl_scalar_type;
2859 using magnitude_type =
typename impl_type::magnitude_type;
2861 using row_matrix_type =
typename impl_type::tpetra_row_matrix_type;
2862 using crs_graph_type =
typename impl_type::tpetra_crs_graph_type;
2864 using local_ordinal_type_1d_view =
typename impl_type::local_ordinal_type_1d_view;
2865 using local_ordinal_type_2d_view =
typename impl_type::local_ordinal_type_2d_view;
2866 using size_type_1d_view =
typename impl_type::size_type_1d_view;
2867 using size_type_2d_view =
typename impl_type::size_type_2d_view;
2868 using impl_scalar_type_1d_view_tpetra =
typename impl_type::impl_scalar_type_1d_view_tpetra;
2870 using btdm_scalar_type =
typename impl_type::btdm_scalar_type;
2871 using btdm_magnitude_type =
typename impl_type::btdm_magnitude_type;
2872 using vector_type_3d_view =
typename impl_type::vector_type_3d_view;
2873 using vector_type_4d_view =
typename impl_type::vector_type_4d_view;
2874 using internal_vector_type_4d_view =
typename impl_type::internal_vector_type_4d_view;
2875 using internal_vector_type_5d_view =
typename impl_type::internal_vector_type_5d_view;
2876 using btdm_scalar_type_2d_view =
typename impl_type::btdm_scalar_type_2d_view;
2877 using btdm_scalar_type_3d_view =
typename impl_type::btdm_scalar_type_3d_view;
2878 using btdm_scalar_type_4d_view =
typename impl_type::btdm_scalar_type_4d_view;
2879 using btdm_scalar_type_5d_view =
typename impl_type::btdm_scalar_type_5d_view;
2880 using internal_vector_scratch_type_3d_view = Scratch<typename impl_type::internal_vector_type_3d_view>;
2881 using btdm_scalar_scratch_type_3d_view = Scratch<typename impl_type::btdm_scalar_type_3d_view>;
2882 using tpetra_block_access_view_type =
typename impl_type::tpetra_block_access_view_type;
2883 using local_crs_graph_type =
typename impl_type::local_crs_graph_type;
2884 using colinds_view =
typename local_crs_graph_type::entries_type;
2886 using internal_vector_type =
typename impl_type::internal_vector_type;
2887 static constexpr int vector_length = impl_type::vector_length;
2888 static constexpr int internal_vector_length = impl_type::internal_vector_length;
2889 static_assert(vector_length >= internal_vector_length,
"Ifpack2 BlockTriDi Numeric: vector_length must be at least as large as internal_vector_length");
2890 static_assert(vector_length % internal_vector_length == 0,
"Ifpack2 BlockTriDi Numeric: vector_length must be divisible by internal_vector_length");
2895 static constexpr int half_vector_length = impl_type::half_vector_length;
2898 using team_policy_type = Kokkos::TeamPolicy<execution_space>;
2899 using member_type =
typename team_policy_type::member_type;
2903 const ConstUnmanaged<local_ordinal_type_1d_view> partptr, lclrow, packptr, packindices_sub, packptr_sub;
2904 const ConstUnmanaged<local_ordinal_type_2d_view> partptr_sub, part2packrowidx0_sub, packindices_schur;
2905 const local_ordinal_type max_partsz;
2907 using size_type_1d_view_tpetra = Kokkos::View<size_t *, typename impl_type::node_device_type>;
2908 ConstUnmanaged<size_type_1d_view_tpetra> A_block_rowptr;
2909 ConstUnmanaged<size_type_1d_view_tpetra> A_point_rowptr;
2910 ConstUnmanaged<impl_scalar_type_1d_view_tpetra> A_values;
2912 const ConstUnmanaged<size_type_2d_view> pack_td_ptr, flat_td_ptr, pack_td_ptr_schur;
2913 const ConstUnmanaged<local_ordinal_type_1d_view> A_colindsub;
2914 const Unmanaged<internal_vector_type_4d_view> internal_vector_values, internal_vector_values_schur;
2915 const Unmanaged<internal_vector_type_5d_view> e_internal_vector_values;
2916 const Unmanaged<btdm_scalar_type_4d_view> scalar_values, scalar_values_schur;
2917 const Unmanaged<btdm_scalar_type_5d_view> e_scalar_values;
2918 const Unmanaged<btdm_scalar_type_3d_view> d_inv;
2919 const Unmanaged<size_type_1d_view> diag_offsets;
2921 const local_ordinal_type blocksize, blocksize_square;
2923 const magnitude_type tiny;
2924 const local_ordinal_type vector_loop_size;
2926 bool hasBlockCrsMatrix;
2929 ExtractAndFactorizeTridiags(
const BlockTridiags<MatrixType> &btdm_,
2930 const BlockHelperDetails::PartInterface<MatrixType> &interf_,
2931 const Teuchos::RCP<const row_matrix_type> &A_,
2932 const Teuchos::RCP<const crs_graph_type> &G_,
2933 const magnitude_type &tiny_)
2935 partptr(interf_.partptr)
2936 , lclrow(interf_.lclrow)
2937 , packptr(interf_.packptr)
2938 , packindices_sub(interf_.packindices_sub)
2939 , packptr_sub(interf_.packptr_sub)
2940 , partptr_sub(interf_.partptr_sub)
2941 , part2packrowidx0_sub(interf_.part2packrowidx0_sub)
2942 , packindices_schur(interf_.packindices_schur)
2943 , max_partsz(interf_.max_partsz)
2946 pack_td_ptr(btdm_.pack_td_ptr)
2947 , flat_td_ptr(btdm_.flat_td_ptr)
2948 , pack_td_ptr_schur(btdm_.pack_td_ptr_schur)
2949 , A_colindsub(btdm_.A_colindsub)
2950 , internal_vector_values((internal_vector_type *)btdm_.values.data(),
2951 btdm_.values.extent(0),
2952 btdm_.values.extent(1),
2953 btdm_.values.extent(2),
2954 vector_length / internal_vector_length)
2955 , internal_vector_values_schur((internal_vector_type *)btdm_.values_schur.data(),
2956 btdm_.values_schur.extent(0),
2957 btdm_.values_schur.extent(1),
2958 btdm_.values_schur.extent(2),
2959 vector_length / internal_vector_length)
2960 , e_internal_vector_values((internal_vector_type *)btdm_.e_values.data(),
2961 btdm_.e_values.extent(0),
2962 btdm_.e_values.extent(1),
2963 btdm_.e_values.extent(2),
2964 btdm_.e_values.extent(3),
2965 vector_length / internal_vector_length)
2966 , scalar_values((btdm_scalar_type *)btdm_.values.data(),
2967 btdm_.values.extent(0),
2968 btdm_.values.extent(1),
2969 btdm_.values.extent(2),
2971 , scalar_values_schur((btdm_scalar_type *)btdm_.values_schur.data(),
2972 btdm_.values_schur.extent(0),
2973 btdm_.values_schur.extent(1),
2974 btdm_.values_schur.extent(2),
2976 , e_scalar_values((btdm_scalar_type *)btdm_.e_values.data(),
2977 btdm_.e_values.extent(0),
2978 btdm_.e_values.extent(1),
2979 btdm_.e_values.extent(2),
2980 btdm_.e_values.extent(3),
2982 , d_inv(btdm_.d_inv)
2983 , diag_offsets(btdm_.diag_offsets)
2984 , blocksize(btdm_.values.extent(1))
2985 , blocksize_square(blocksize * blocksize)
2989 , vector_loop_size(vector_length / internal_vector_length) {
2990 using crs_matrix_type =
typename impl_type::tpetra_crs_matrix_type;
2991 using block_crs_matrix_type =
typename impl_type::tpetra_block_crs_matrix_type;
2993 auto A_crs = Teuchos::rcp_dynamic_cast<const crs_matrix_type>(A_);
2994 auto A_bcrs = Teuchos::rcp_dynamic_cast<const block_crs_matrix_type>(A_);
2996 hasBlockCrsMatrix = !A_bcrs.is_null();
2998 A_block_rowptr = G_->getLocalGraphDevice().row_map;
2999 if (hasBlockCrsMatrix) {
3000 A_values =
const_cast<block_crs_matrix_type *
>(A_bcrs.get())->getValuesDeviceNonConst();
3002 A_point_rowptr = A_crs->getCrsGraph()->getLocalGraphDevice().row_map;
3003 A_values = A_crs->getLocalValuesDevice(Tpetra::Access::ReadOnly);
3008 KOKKOS_INLINE_FUNCTION
3010 extract(local_ordinal_type partidx,
3011 local_ordinal_type local_subpartidx,
3012 local_ordinal_type npacks)
const {
3013#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3014 printf(
"extract partidx = %d, local_subpartidx = %d, npacks = %d;\n", partidx, local_subpartidx, npacks);
3016 using tlb = BlockHelperDetails::TpetraLittleBlock<Tpetra::Impl::BlockCrsMatrixLittleBlockArrayLayout>;
3017 const size_type kps = pack_td_ptr(partidx, local_subpartidx);
3018 local_ordinal_type kfs[vector_length] = {};
3019 local_ordinal_type ri0[vector_length] = {};
3020 local_ordinal_type nrows[vector_length] = {};
3022 for (local_ordinal_type vi = 0; vi < npacks; ++vi, ++partidx) {
3023 kfs[vi] = flat_td_ptr(partidx, local_subpartidx);
3024 ri0[vi] = partptr_sub(pack_td_ptr.extent(0) * local_subpartidx + partidx, 0);
3025 nrows[vi] = partptr_sub(pack_td_ptr.extent(0) * local_subpartidx + partidx, 1) - ri0[vi];
3026#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3027 printf(
"kfs[%d] = %d;\n", vi, kfs[vi]);
3028 printf(
"ri0[%d] = %d;\n", vi, ri0[vi]);
3029 printf(
"nrows[%d] = %d;\n", vi, nrows[vi]);
3032 local_ordinal_type tr_min = 0;
3033 local_ordinal_type tr_max = nrows[0];
3034 if (local_subpartidx % 2 == 1) {
3038#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3039 printf(
"tr_min = %d and tr_max = %d;\n", tr_min, tr_max);
3041 for (local_ordinal_type tr = tr_min, j = 0; tr < tr_max; ++tr) {
3042 for (local_ordinal_type e = 0; e < 3; ++e) {
3043 if (hasBlockCrsMatrix) {
3044 const impl_scalar_type *block[vector_length] = {};
3045 for (local_ordinal_type vi = 0; vi < npacks; ++vi) {
3046 const size_type Aj = A_block_rowptr(lclrow(ri0[vi] + tr)) + A_colindsub(kfs[vi] + j);
3048 block[vi] = &A_values(Aj * blocksize_square);
3050 const size_type pi = kps + j;
3051#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3052 printf(
"Extract pi = %ld, ri0 + tr = %d, kfs + j = %d\n", pi, ri0[0] + tr, kfs[0] + j);
3055 for (local_ordinal_type ii = 0; ii < blocksize; ++ii) {
3056 for (local_ordinal_type jj = 0; jj < blocksize; ++jj) {
3057 const auto idx = tlb::getFlatIndex(ii, jj, blocksize);
3058 auto &v = internal_vector_values(pi, ii, jj, 0);
3059 for (local_ordinal_type vi = 0; vi < npacks; ++vi) {
3060 v[vi] =
static_cast<btdm_scalar_type
>(block[vi][idx]);
3065 const size_type pi = kps + j;
3067 for (local_ordinal_type vi = 0; vi < npacks; ++vi) {
3068 const size_type Aj_c = A_colindsub(kfs[vi] + j);
3070 for (local_ordinal_type ii = 0; ii < blocksize; ++ii) {
3071 auto point_row_offset = A_point_rowptr(lclrow(ri0[vi] + tr) * blocksize + ii);
3073 for (local_ordinal_type jj = 0; jj < blocksize; ++jj) {
3074 scalar_values(pi, ii, jj, vi) = A_values(point_row_offset + Aj_c * blocksize + jj);
3080 if (nrows[0] == 1)
break;
3081 if (local_subpartidx % 2 == 0) {
3082 if (e == 1 && (tr == 0 || tr + 1 == nrows[0]))
break;
3083 for (local_ordinal_type vi = 1; vi < npacks; ++vi) {
3084 if ((e == 0 && nrows[vi] == 1) || (e == 1 && tr + 1 == nrows[vi])) {
3090 if (e == 0 && (tr == -1 || tr == nrows[0]))
break;
3091 for (local_ordinal_type vi = 1; vi < npacks; ++vi) {
3092 if ((e == 0 && nrows[vi] == 1) || (e == 0 && tr == nrows[vi])) {
3102 KOKKOS_INLINE_FUNCTION
3104 extract(
const member_type &member,
3105 const local_ordinal_type &partidxbeg,
3106 local_ordinal_type local_subpartidx,
3107 const local_ordinal_type &npacks,
3108 const local_ordinal_type &vbeg)
const {
3109#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3110 printf(
"extract partidxbeg = %d, local_subpartidx = %d, npacks = %d, vbeg = %d;\n", partidxbeg, local_subpartidx, npacks, vbeg);
3112 using tlb = BlockHelperDetails::TpetraLittleBlock<Tpetra::Impl::BlockCrsMatrixLittleBlockArrayLayout>;
3113 local_ordinal_type kfs_vals[internal_vector_length] = {};
3114 local_ordinal_type ri0_vals[internal_vector_length] = {};
3115 local_ordinal_type nrows_vals[internal_vector_length] = {};
3117 const size_type kps = pack_td_ptr(partidxbeg, local_subpartidx);
3118 for (local_ordinal_type v = vbeg, vi = 0; v < npacks && vi < internal_vector_length; ++v, ++vi) {
3119 kfs_vals[vi] = flat_td_ptr(partidxbeg + vi, local_subpartidx);
3120 ri0_vals[vi] = partptr_sub(pack_td_ptr.extent(0) * local_subpartidx + partidxbeg + vi, 0);
3121 nrows_vals[vi] = partptr_sub(pack_td_ptr.extent(0) * local_subpartidx + partidxbeg + vi, 1) - ri0_vals[vi];
3122#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3123 printf(
"kfs_vals[%d] = %d;\n", vi, kfs_vals[vi]);
3124 printf(
"ri0_vals[%d] = %d;\n", vi, ri0_vals[vi]);
3125 printf(
"nrows_vals[%d] = %d;\n", vi, nrows_vals[vi]);
3129 local_ordinal_type j_vals[internal_vector_length] = {};
3131 local_ordinal_type tr_min = 0;
3132 local_ordinal_type tr_max = nrows_vals[0];
3133 if (local_subpartidx % 2 == 1) {
3137#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3138 printf(
"tr_min = %d and tr_max = %d;\n", tr_min, tr_max);
3140 for (local_ordinal_type tr = tr_min; tr < tr_max; ++tr) {
3141 for (local_ordinal_type v = vbeg, vi = 0; v < npacks && vi < internal_vector_length; ++v, ++vi) {
3142 const local_ordinal_type nrows = (local_subpartidx % 2 == 0 ? nrows_vals[vi] : nrows_vals[vi]);
3143 if ((local_subpartidx % 2 == 0 && tr < nrows) || (local_subpartidx % 2 == 1 && tr < nrows + 1)) {
3144 auto &j = j_vals[vi];
3145 const local_ordinal_type kfs = kfs_vals[vi];
3146 const local_ordinal_type ri0 = ri0_vals[vi];
3147 local_ordinal_type lbeg, lend;
3148 if (local_subpartidx % 2 == 0) {
3149 lbeg = (tr == tr_min ? 1 : 0);
3150 lend = (tr == nrows - 1 ? 2 : 3);
3157 }
else if (tr == nrows) {
3162 if (hasBlockCrsMatrix) {
3163 for (local_ordinal_type l = lbeg; l < lend; ++l, ++j) {
3164 const size_type Aj = A_block_rowptr(lclrow(ri0 + tr)) + A_colindsub(kfs + j);
3165 const impl_scalar_type *block = &A_values(Aj * blocksize_square);
3166 const size_type pi = kps + j;
3167#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3168 printf(
"Extract pi = %ld, ri0 + tr = %d, kfs + j = %d, tr = %d, lbeg = %d, lend = %d, l = %d\n", pi, ri0 + tr, kfs + j, tr, lbeg, lend, l);
3170 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, blocksize),
3171 [&](
const local_ordinal_type &ii) {
3172 for (local_ordinal_type jj = 0; jj < blocksize; ++jj) {
3173 scalar_values(pi, ii, jj, v) =
static_cast<btdm_scalar_type
>(block[tlb::getFlatIndex(ii, jj, blocksize)]);
3178 for (local_ordinal_type l = lbeg; l < lend; ++l, ++j) {
3179 const size_type Aj_c = A_colindsub(kfs + j);
3180 const size_type pi = kps + j;
3181 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, blocksize),
3182 [&](
const local_ordinal_type &ii) {
3183 auto point_row_offset = A_point_rowptr(lclrow(ri0 + tr) * blocksize + ii);
3184 for (local_ordinal_type jj = 0; jj < blocksize; ++jj) {
3185 scalar_values(pi, ii, jj, v) = A_values(point_row_offset + Aj_c * blocksize + jj);
3195 template <
typename AAViewType,
3196 typename WWViewType>
3197 KOKKOS_INLINE_FUNCTION
void
3198 factorize_subline(
const member_type &member,
3199 const local_ordinal_type &i0,
3200 const local_ordinal_type &nrows,
3201 const local_ordinal_type &v,
3202 const AAViewType &AA,
3203 const WWViewType &WW)
const {
3204 typedef ExtractAndFactorizeTridiagsDefaultModeAndAlgo<typename execution_space::memory_space> default_mode_and_algo_type;
3206 typedef typename default_mode_and_algo_type::mode_type default_mode_type;
3207 typedef typename default_mode_and_algo_type::algo_type default_algo_type;
3210 const auto one = KokkosKernels::ArithTraits<btdm_magnitude_type>::one();
3212#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3213 printf(
"i0 = %d, nrows = %d, v = %d, AA.extent(0) = %ld;\n", i0, nrows, v, AA.extent(0));
3217 auto A = Kokkos::subview(AA, i0, Kokkos::ALL(), Kokkos::ALL(), v);
3219 default_mode_type, KB::Algo::LU::Unblocked>::invoke(member, A, tiny);
3224 local_ordinal_type i = i0;
3225 for (local_ordinal_type tr = 1; tr < nrows; ++tr, i += 3) {
3226#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3227 printf(
"tr = %d, i = %d;\n", tr, i);
3229 B.assign_data(&AA(i + 1, 0, 0, v));
3230 KB::Trsm<member_type,
3231 KB::Side::Left, KB::Uplo::Lower, KB::Trans::NoTranspose, KB::Diag::Unit,
3232 default_mode_type, default_algo_type>::invoke(member, one, A, B);
3233 C.assign_data(&AA(i + 2, 0, 0, v));
3234 KB::Trsm<member_type,
3235 KB::Side::Right, KB::Uplo::Upper, KB::Trans::NoTranspose, KB::Diag::NonUnit,
3236 default_mode_type, default_algo_type>::invoke(member, one, A, C);
3237 A.assign_data(&AA(i + 3, 0, 0, v));
3239 member.team_barrier();
3240 KB::Gemm<member_type,
3241 KB::Trans::NoTranspose, KB::Trans::NoTranspose,
3242 default_mode_type, default_algo_type>::invoke(member, -one, C, B, one, A);
3244 default_mode_type, KB::Algo::LU::Unblocked>::invoke(member, A, tiny);
3248 auto W = Kokkos::subview(WW, Kokkos::ALL(), Kokkos::ALL(), v);
3249 KB::Copy<member_type, KB::Trans::NoTranspose, default_mode_type>::invoke(member, A, W);
3250 KB::SetIdentity<member_type, default_mode_type>::invoke(member, A);
3251 member.team_barrier();
3252 KB::Trsm<member_type,
3253 KB::Side::Left, KB::Uplo::Lower, KB::Trans::NoTranspose, KB::Diag::Unit,
3254 default_mode_type, default_algo_type>::invoke(member, one, W, A);
3255 KB::Trsm<member_type,
3256 KB::Side::Left, KB::Uplo::Upper, KB::Trans::NoTranspose, KB::Diag::NonUnit,
3257 default_mode_type, default_algo_type>::invoke(member, one, W, A);
3262 struct ExtractAndFactorizeSubLineTag {};
3263 struct ExtractAndFactorizeFusedJacobiTag {};
3264 struct ExtractBCDTag {};
3265 struct ComputeETag {};
3266 struct ComputeSchurTag {};
3267 struct FactorizeSchurTag {};
3269 KOKKOS_INLINE_FUNCTION
3271 operator()(
const ExtractAndFactorizeSubLineTag &,
const member_type &member)
const {
3273 const local_ordinal_type packidx = packindices_sub(member.league_rank());
3275 const local_ordinal_type subpartidx = packptr_sub(packidx);
3276 const local_ordinal_type n_parts = part2packrowidx0_sub.extent(0);
3277 const local_ordinal_type local_subpartidx = subpartidx / n_parts;
3278 const local_ordinal_type partidx = subpartidx % n_parts;
3280 const local_ordinal_type npacks = packptr_sub(packidx + 1) - subpartidx;
3281 const local_ordinal_type i0 = pack_td_ptr(partidx, local_subpartidx);
3282 const local_ordinal_type nrows = partptr_sub(subpartidx, 1) - partptr_sub(subpartidx, 0);
3284 internal_vector_scratch_type_3d_view
3285 WW(member.team_scratch(ScratchLevel), blocksize, blocksize, vector_loop_size);
3287#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3288 printf(
"rank = %d, i0 = %d, npacks = %d, nrows = %d, packidx = %d, subpartidx = %d, partidx = %d, local_subpartidx = %d;\n", member.league_rank(), i0, npacks, nrows, packidx, subpartidx, partidx, local_subpartidx);
3289 printf(
"vector_loop_size = %d\n", vector_loop_size);
3292 if (vector_loop_size == 1) {
3293 extract(partidx, local_subpartidx, npacks);
3294 factorize_subline(member, i0, nrows, 0, internal_vector_values, WW);
3296 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size),
3297 [&](
const local_ordinal_type &v) {
3298 const local_ordinal_type vbeg = v * internal_vector_length;
3299#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3300 printf(
"i0 = %d, npacks = %d, vbeg = %d;\n", i0, npacks, vbeg);
3303 extract(member, partidx + vbeg, local_subpartidx, npacks, vbeg);
3306 member.team_barrier();
3307 factorize_subline(member, i0, nrows, v, internal_vector_values, WW);
3312 KOKKOS_INLINE_FUNCTION
3314 operator()(
const ExtractAndFactorizeFusedJacobiTag &,
const member_type &member)
const {
3315 using default_mode_and_algo_type = ExtractAndFactorizeTridiagsDefaultModeAndAlgo<typename execution_space::memory_space>;
3316 using default_mode_type =
typename default_mode_and_algo_type::mode_type;
3317 using default_algo_type =
typename default_mode_and_algo_type::algo_type;
3320 btdm_scalar_scratch_type_3d_view WW1(member.team_scratch(ScratchLevel), half_vector_length, blocksize, blocksize);
3321 btdm_scalar_scratch_type_3d_view WW2(member.team_scratch(ScratchLevel), half_vector_length, blocksize, blocksize);
3322 const auto one = KokkosKernels::ArithTraits<btdm_magnitude_type>::one();
3323 const local_ordinal_type nrows = lclrow.extent(0);
3324 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, half_vector_length),
3325 [&](
const local_ordinal_type &v) {
3326 local_ordinal_type row = member.league_rank() * half_vector_length + v;
3328 auto W1 = Kokkos::subview(WW1, v, Kokkos::ALL(), Kokkos::ALL());
3329 auto W2 = Kokkos::subview(WW2, v, Kokkos::ALL(), Kokkos::ALL());
3332 const impl_scalar_type *A_diag = A_values.data() + diag_offsets(row);
3335 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, blocksize * blocksize),
3337 W1.data()[i] = A_diag[i];
3340 KB::SetIdentity<member_type, default_mode_type>::invoke(member, W2);
3345 KB::SetIdentity<member_type, default_mode_type>::invoke(member, W1);
3347 member.team_barrier();
3349 KB::LU<member_type, default_mode_type, KB::Algo::LU::Unblocked>::invoke(member, W1, tiny);
3350 member.team_barrier();
3351 KB::Trsm<member_type,
3352 KB::Side::Left, KB::Uplo::Lower, KB::Trans::NoTranspose, KB::Diag::Unit,
3353 default_mode_type, default_algo_type>::invoke(member, one, W1, W2);
3354 KB::Trsm<member_type,
3355 KB::Side::Left, KB::Uplo::Upper, KB::Trans::NoTranspose, KB::Diag::NonUnit,
3356 default_mode_type, default_algo_type>::invoke(member, one, W1, W2);
3357 member.team_barrier();
3359 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, blocksize * blocksize),
3361 auto d_inv_block = &d_inv(row, 0, 0);
3362 d_inv_block[i] = W2.data()[i];
3368 KOKKOS_INLINE_FUNCTION
3370 operator()(
const ExtractBCDTag &,
const member_type &member)
const {
3372 const local_ordinal_type packindices_schur_i = member.league_rank() % packindices_schur.extent(0);
3373 const local_ordinal_type packindices_schur_j = member.league_rank() / packindices_schur.extent(0);
3374 const local_ordinal_type packidx = packindices_schur(packindices_schur_i, packindices_schur_j);
3376 const local_ordinal_type subpartidx = packptr_sub(packidx);
3377 const local_ordinal_type n_parts = part2packrowidx0_sub.extent(0);
3378 const local_ordinal_type local_subpartidx = subpartidx / n_parts;
3379 const local_ordinal_type partidx = subpartidx % n_parts;
3381 const local_ordinal_type npacks = packptr_sub(packidx + 1) - subpartidx;
3385 if (vector_loop_size == 1) {
3386 extract(partidx, local_subpartidx, npacks);
3388 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size),
3389 [&](
const local_ordinal_type &v) {
3390 const local_ordinal_type vbeg = v * internal_vector_length;
3391#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3392 const local_ordinal_type i0 = pack_td_ptr(partidx, local_subpartidx);
3393 printf(
"i0 = %d, npacks = %d, vbeg = %d;\n", i0, npacks, vbeg);
3396 extract(member, partidx + vbeg, local_subpartidx, npacks, vbeg);
3400 member.team_barrier();
3402 const size_type kps1 = pack_td_ptr(partidx, local_subpartidx);
3403 const size_type kps2 = pack_td_ptr(partidx, local_subpartidx + 1) - 1;
3405 const local_ordinal_type r1 = part2packrowidx0_sub(partidx, local_subpartidx) - 1;
3406 const local_ordinal_type r2 = part2packrowidx0_sub(partidx, local_subpartidx) + 2;
3408#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3409 printf(
"Copy for Schur complement part id = %d from kps1 = %ld to r1 = %d and from kps2 = %ld to r2 = %d partidx = %d local_subpartidx = %d;\n", packidx, kps1, r1, kps2, r2, partidx, local_subpartidx);
3413 copy3DView<local_ordinal_type>(member, Kokkos::subview(e_internal_vector_values, 0, r1, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL()),
3414 Kokkos::subview(internal_vector_values, kps1, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL()));
3416 copy3DView<local_ordinal_type>(member, Kokkos::subview(e_internal_vector_values, 1, r2, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL()),
3417 Kokkos::subview(internal_vector_values, kps2, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL()));
3420 KOKKOS_INLINE_FUNCTION
3422 operator()(
const ComputeETag &,
const member_type &member)
const {
3424 const local_ordinal_type packidx = packindices_sub(member.league_rank());
3426 const local_ordinal_type subpartidx = packptr_sub(packidx);
3427 const local_ordinal_type n_parts = part2packrowidx0_sub.extent(0);
3428 const local_ordinal_type local_subpartidx = subpartidx / n_parts;
3429 const local_ordinal_type partidx = subpartidx % n_parts;
3431 const local_ordinal_type npacks = packptr_sub(packidx + 1) - subpartidx;
3432 const local_ordinal_type i0 = pack_td_ptr(partidx, local_subpartidx);
3433 const local_ordinal_type r0 = part2packrowidx0_sub(partidx, local_subpartidx);
3434 const local_ordinal_type nrows = partptr_sub(subpartidx, 1) - partptr_sub(subpartidx, 0);
3435 const local_ordinal_type num_vectors = blocksize;
3439 internal_vector_scratch_type_3d_view
3440 WW(member.team_scratch(ScratchLevel), blocksize, num_vectors, vector_loop_size);
3441 if (local_subpartidx == 0) {
3442 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size), [&](
const int &v) {
3443 solveMultiVector<impl_type, internal_vector_scratch_type_3d_view>(member, blocksize, i0, r0, nrows, v, internal_vector_values, Kokkos::subview(e_internal_vector_values, 0, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL()), WW,
true);
3445 }
else if (local_subpartidx == (local_ordinal_type)part2packrowidx0_sub.extent(1) - 2) {
3446 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size), [&](
const int &v) {
3447 solveMultiVector<impl_type, internal_vector_scratch_type_3d_view>(member, blocksize, i0, r0, nrows, v, internal_vector_values, Kokkos::subview(e_internal_vector_values, 1, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL()), WW);
3450 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size), [&](
const int &v) {
3451 solveMultiVector<impl_type, internal_vector_scratch_type_3d_view>(member, blocksize, i0, r0, nrows, v, internal_vector_values, Kokkos::subview(e_internal_vector_values, 0, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL()), WW,
true);
3452 solveMultiVector<impl_type, internal_vector_scratch_type_3d_view>(member, blocksize, i0, r0, nrows, v, internal_vector_values, Kokkos::subview(e_internal_vector_values, 1, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL()), WW);
3457 KOKKOS_INLINE_FUNCTION
3459 operator()(
const ComputeSchurTag &,
const member_type &member)
const {
3461 const local_ordinal_type packindices_schur_i = member.league_rank() % packindices_schur.extent(0);
3462 const local_ordinal_type packindices_schur_j = member.league_rank() / packindices_schur.extent(0);
3463 const local_ordinal_type packidx = packindices_schur(packindices_schur_i, packindices_schur_j);
3465 const local_ordinal_type subpartidx = packptr_sub(packidx);
3466 const local_ordinal_type n_parts = part2packrowidx0_sub.extent(0);
3467 const local_ordinal_type local_subpartidx = subpartidx / n_parts;
3468 const local_ordinal_type partidx = subpartidx % n_parts;
3471 const local_ordinal_type i0 = pack_td_ptr(partidx, local_subpartidx);
3477 const local_ordinal_type local_subpartidx_schur = (local_subpartidx - 1) / 2;
3478 const local_ordinal_type i0_schur = local_subpartidx_schur == 0 ? pack_td_ptr_schur(partidx, local_subpartidx_schur) : pack_td_ptr_schur(partidx, local_subpartidx_schur) + 1;
3479 const local_ordinal_type i0_offset = local_subpartidx_schur == 0 ? i0 + 2 : i0 + 2;
3481 for (local_ordinal_type i = 0; i < 4; ++i) {
3482 copy3DView<local_ordinal_type>(member, Kokkos::subview(internal_vector_values_schur, i0_schur + i, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL()),
3483 Kokkos::subview(internal_vector_values, i0_offset + i, Kokkos::ALL(), Kokkos::ALL(), Kokkos::ALL()));
3486 member.team_barrier();
3488 const auto one = KokkosKernels::ArithTraits<btdm_magnitude_type>::one();
3490 const size_type c_kps1 = pack_td_ptr(partidx, local_subpartidx) + 1;
3491 const size_type c_kps2 = pack_td_ptr(partidx, local_subpartidx + 1) - 2;
3493 const local_ordinal_type e_r1 = part2packrowidx0_sub(partidx, local_subpartidx) - 1;
3494 const local_ordinal_type e_r2 = part2packrowidx0_sub(partidx, local_subpartidx) + 2;
3496 typedef ExtractAndFactorizeTridiagsDefaultModeAndAlgo<typename execution_space::memory_space> default_mode_and_algo_type;
3498 typedef typename default_mode_and_algo_type::mode_type default_mode_type;
3499 typedef typename default_mode_and_algo_type::algo_type default_algo_type;
3501 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size), [&](
const int &v) {
3502 for (size_type i = 0; i < pack_td_ptr_schur(partidx, local_subpartidx_schur + 1) - pack_td_ptr_schur(partidx, local_subpartidx_schur); ++i) {
3503 local_ordinal_type e_r, e_c, c_kps;
3505 if (local_subpartidx_schur == 0) {
3510 }
else if (i == 3) {
3514 }
else if (i == 4) {
3526 }
else if (i == 1) {
3530 }
else if (i == 4) {
3534 }
else if (i == 5) {
3543 auto S = Kokkos::subview(internal_vector_values_schur, pack_td_ptr_schur(partidx, local_subpartidx_schur) + i, Kokkos::ALL(), Kokkos::ALL(), v);
3544 auto C = Kokkos::subview(internal_vector_values, c_kps, Kokkos::ALL(), Kokkos::ALL(), v);
3545 auto E = Kokkos::subview(e_internal_vector_values, e_c, e_r, Kokkos::ALL(), Kokkos::ALL(), v);
3546 KB::Gemm<member_type,
3547 KB::Trans::NoTranspose, KB::Trans::NoTranspose,
3548 default_mode_type, default_algo_type>::invoke(member, -one, C, E, one, S);
3553 KOKKOS_INLINE_FUNCTION
3555 operator()(
const FactorizeSchurTag &,
const member_type &member)
const {
3556 const local_ordinal_type packidx = packindices_schur(member.league_rank(), 0);
3558 const local_ordinal_type subpartidx = packptr_sub(packidx);
3560 const local_ordinal_type n_parts = part2packrowidx0_sub.extent(0);
3561 const local_ordinal_type partidx = subpartidx % n_parts;
3563 const local_ordinal_type i0 = pack_td_ptr_schur(partidx, 0);
3564 const local_ordinal_type nrows = 2 * (pack_td_ptr_schur.extent(1) - 1);
3566 internal_vector_scratch_type_3d_view
3567 WW(member.team_scratch(ScratchLevel), blocksize, blocksize, vector_loop_size);
3569#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3570 printf(
"FactorizeSchurTag rank = %d, i0 = %d, nrows = %d, vector_loop_size = %d;\n", member.league_rank(), i0, nrows, vector_loop_size);
3573 if (vector_loop_size == 1) {
3574 factorize_subline(member, i0, nrows, 0, internal_vector_values_schur, WW);
3576 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size),
3577 [&](
const local_ordinal_type &v) {
3578 factorize_subline(member, i0, nrows, v, internal_vector_values_schur, WW);
3584 IFPACK2_BLOCKTRIDICONTAINER_PROFILER_REGION_BEGIN;
3585 const local_ordinal_type team_size =
3586 ExtractAndFactorizeTridiagsDefaultModeAndAlgo<typename execution_space::memory_space>::
3587 recommended_team_size(blocksize, vector_length, internal_vector_length);
3588 const local_ordinal_type per_team_scratch = internal_vector_scratch_type_3d_view::
3589 shmem_size(blocksize, blocksize, vector_loop_size);
3592#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3593 printf(
"Start ExtractAndFactorizeSubLineTag\n");
3595 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::NumericPhase::ExtractAndFactorizeSubLineTag", ExtractAndFactorizeSubLineTag0);
3596 Kokkos::TeamPolicy<execution_space, ExtractAndFactorizeSubLineTag>
3597 policy(packindices_sub.extent(0), team_size, vector_loop_size);
3599 const local_ordinal_type n_parts = part2packrowidx0_sub.extent(0);
3600 writeBTDValuesToFile(n_parts, scalar_values,
"before.mm");
3602 policy.set_scratch_size(ScratchLevel, Kokkos::PerTeam(per_team_scratch));
3603 Kokkos::parallel_for(
"ExtractAndFactorize::TeamPolicy::run<ExtractAndFactorizeSubLineTag>",
3605 execution_space().fence();
3607 writeBTDValuesToFile(n_parts, scalar_values,
"after.mm");
3608#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3609 printf(
"End ExtractAndFactorizeSubLineTag\n");
3613 if (packindices_schur.extent(1) > 0) {
3615#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3616 printf(
"Start ExtractBCDTag\n");
3618 Kokkos::deep_copy(e_scalar_values, KokkosKernels::ArithTraits<btdm_magnitude_type>::zero());
3619 Kokkos::deep_copy(scalar_values_schur, KokkosKernels::ArithTraits<btdm_magnitude_type>::zero());
3621 write5DMultiVectorValuesToFile(part2packrowidx0_sub.extent(0), e_scalar_values,
"e_scalar_values_before_extract.mm");
3624 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::NumericPhase::ExtractBCDTag", ExtractBCDTag0);
3625 Kokkos::TeamPolicy<execution_space, ExtractBCDTag>
3626 policy(packindices_schur.extent(0) * packindices_schur.extent(1), team_size, vector_loop_size);
3628 policy.set_scratch_size(ScratchLevel, Kokkos::PerTeam(per_team_scratch));
3629 Kokkos::parallel_for(
"ExtractAndFactorize::TeamPolicy::run<ExtractBCDTag>",
3631 execution_space().fence();
3634#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3635 printf(
"End ExtractBCDTag\n");
3637 writeBTDValuesToFile(part2packrowidx0_sub.extent(0), scalar_values,
"after_extraction_of_BCD.mm");
3638#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3639 printf(
"Start ComputeETag\n");
3641 write5DMultiVectorValuesToFile(part2packrowidx0_sub.extent(0), e_scalar_values,
"e_scalar_values_after_extract.mm");
3643 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::NumericPhase::ComputeETag", ComputeETag0);
3644 Kokkos::TeamPolicy<execution_space, ComputeETag>
3645 policy(packindices_sub.extent(0), team_size, vector_loop_size);
3647 policy.set_scratch_size(ScratchLevel, Kokkos::PerTeam(per_team_scratch));
3648 Kokkos::parallel_for(
"ExtractAndFactorize::TeamPolicy::run<ComputeETag>",
3650 execution_space().fence();
3652 write5DMultiVectorValuesToFile(part2packrowidx0_sub.extent(0), e_scalar_values,
"e_scalar_values_after_compute.mm");
3654#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3655 printf(
"End ComputeETag\n");
3660#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3661 printf(
"Start ComputeSchurTag\n");
3663 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::NumericPhase::ComputeSchurTag", ComputeSchurTag0);
3664 writeBTDValuesToFile(part2packrowidx0_sub.extent(0), scalar_values_schur,
"before_schur.mm");
3665 Kokkos::TeamPolicy<execution_space, ComputeSchurTag>
3666 policy(packindices_schur.extent(0) * packindices_schur.extent(1), team_size, vector_loop_size);
3668 Kokkos::parallel_for(
"ExtractAndFactorize::TeamPolicy::run<ComputeSchurTag>",
3670 writeBTDValuesToFile(part2packrowidx0_sub.extent(0), scalar_values_schur,
"after_schur.mm");
3671 execution_space().fence();
3672#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3673 printf(
"End ComputeSchurTag\n");
3678#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3679 printf(
"Start FactorizeSchurTag\n");
3681 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::NumericPhase::FactorizeSchurTag", FactorizeSchurTag0);
3682 Kokkos::TeamPolicy<execution_space, FactorizeSchurTag>
3683 policy(packindices_schur.extent(0), team_size, vector_loop_size);
3684 policy.set_scratch_size(ScratchLevel, Kokkos::PerTeam(per_team_scratch));
3685 Kokkos::parallel_for(
"ExtractAndFactorize::TeamPolicy::run<FactorizeSchurTag>",
3687 execution_space().fence();
3688 writeBTDValuesToFile(part2packrowidx0_sub.extent(0), scalar_values_schur,
"after_factor_schur.mm");
3689#ifdef IFPACK2_BLOCKTRIDICONTAINER_USE_PRINTF
3690 printf(
"End FactorizeSchurTag\n");
3695 IFPACK2_BLOCKTRIDICONTAINER_PROFILER_REGION_END;
3698 void run_fused_jacobi() {
3699 IFPACK2_BLOCKTRIDICONTAINER_PROFILER_REGION_BEGIN;
3700 const local_ordinal_type team_size =
3701 ExtractAndFactorizeTridiagsDefaultModeAndAlgo<typename execution_space::memory_space>::
3702 recommended_team_size(blocksize, half_vector_length, 1);
3703 const local_ordinal_type per_team_scratch =
3704 btdm_scalar_scratch_type_3d_view::shmem_size(blocksize, blocksize, 2 * half_vector_length);
3706 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::NumericPhase::ExtractAndFactorizeFusedJacobi", ExtractAndFactorizeFusedJacobiTag);
3707 Kokkos::TeamPolicy<execution_space, ExtractAndFactorizeFusedJacobiTag>
3708 policy((lclrow.extent(0) + half_vector_length - 1) / half_vector_length, team_size, half_vector_length);
3710 policy.set_scratch_size(ScratchLevel, Kokkos::PerTeam(per_team_scratch));
3711 Kokkos::parallel_for(
"ExtractAndFactorize::TeamPolicy::run<ExtractAndFactorizeFusedJacobiTag>",
3714 IFPACK2_BLOCKTRIDICONTAINER_PROFILER_REGION_END;
3721template <
typename MatrixType>
3722void performNumericPhase(
const Teuchos::RCP<
const typename BlockHelperDetails::ImplType<MatrixType>::tpetra_row_matrix_type> &A,
3723 const Teuchos::RCP<
const typename BlockHelperDetails::ImplType<MatrixType>::tpetra_crs_graph_type> &G,
3724 const BlockHelperDetails::PartInterface<MatrixType> &interf,
3725 BlockTridiags<MatrixType> &btdm,
3726 const typename BlockHelperDetails::ImplType<MatrixType>::magnitude_type tiny,
3727 bool use_fused_jacobi) {
3729 using execution_space =
typename impl_type::execution_space;
3730 using team_policy_type = Kokkos::TeamPolicy<execution_space>;
3731 using internal_vector_scratch_type_3d_view = Scratch<typename impl_type::internal_vector_type_3d_view>;
3732 using btdm_scalar_scratch_type_3d_view = Scratch<typename impl_type::btdm_scalar_type_3d_view>;
3734 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::NumericPhase", NumericPhase);
3736 int blocksize = btdm.values.extent(1);
3739 int scratch_required;
3740 if (!use_fused_jacobi) {
3742 scratch_required = internal_vector_scratch_type_3d_view::shmem_size(blocksize, blocksize, impl_type::vector_length / impl_type::internal_vector_length);
3745 scratch_required = btdm_scalar_scratch_type_3d_view::shmem_size(blocksize, blocksize, 2 * impl_type::half_vector_length);
3748 int max_scratch = team_policy_type::scratch_size_max(0);
3750 if (scratch_required < max_scratch) {
3752 ExtractAndFactorizeTridiags<MatrixType, 0> function(btdm, interf, A, G, tiny);
3753 if (!use_fused_jacobi)
3756 function.run_fused_jacobi();
3759 ExtractAndFactorizeTridiags<MatrixType, 1> function(btdm, interf, A, G, tiny);
3760 if (!use_fused_jacobi)
3763 function.run_fused_jacobi();
3765 IFPACK2_BLOCKHELPER_TIMER_FENCE(
typename BlockHelperDetails::ImplType<MatrixType>::execution_space)
3771template <
typename MatrixType>
3772struct MultiVectorConverter {
3775 using execution_space =
typename impl_type::execution_space;
3776 using memory_space =
typename impl_type::memory_space;
3778 using local_ordinal_type =
typename impl_type::local_ordinal_type;
3780 using btdm_scalar_type =
typename impl_type::btdm_scalar_type;
3781 using tpetra_multivector_type =
typename impl_type::tpetra_multivector_type;
3782 using local_ordinal_type_1d_view =
typename impl_type::local_ordinal_type_1d_view;
3783 using vector_type_3d_view =
typename impl_type::vector_type_3d_view;
3784 using impl_scalar_type_2d_view_tpetra =
typename impl_type::impl_scalar_type_2d_view_tpetra;
3785 using const_impl_scalar_type_2d_view_tpetra =
typename impl_scalar_type_2d_view_tpetra::const_type;
3786 static constexpr int vector_length = impl_type::vector_length;
3788 using member_type =
typename Kokkos::TeamPolicy<execution_space>::member_type;
3792 const ConstUnmanaged<local_ordinal_type_1d_view> partptr;
3793 const ConstUnmanaged<local_ordinal_type_1d_view> packptr;
3794 const ConstUnmanaged<local_ordinal_type_1d_view> part2packrowidx0;
3795 const ConstUnmanaged<local_ordinal_type_1d_view> part2rowidx0;
3796 const ConstUnmanaged<local_ordinal_type_1d_view> lclrow;
3797 const local_ordinal_type blocksize;
3798 const local_ordinal_type num_vectors;
3801 vector_type_3d_view packed_multivector;
3802 const_impl_scalar_type_2d_view_tpetra scalar_multivector;
3804 template <
typename TagType>
3805 KOKKOS_INLINE_FUNCTION
void copy_multivectors(
const local_ordinal_type &j,
3806 const local_ordinal_type &vi,
3807 const local_ordinal_type &pri,
3808 const local_ordinal_type &ri0)
const {
3809 for (local_ordinal_type col = 0; col < num_vectors; ++col)
3810 for (local_ordinal_type i = 0; i < blocksize; ++i)
3811 packed_multivector(pri, i, col)[vi] =
static_cast<btdm_scalar_type
>(scalar_multivector(blocksize * lclrow(ri0 + j) + i, col));
3815 MultiVectorConverter(
const BlockHelperDetails::PartInterface<MatrixType> &interf,
3816 const vector_type_3d_view &pmv)
3817 : partptr(interf.partptr)
3818 , packptr(interf.packptr)
3819 , part2packrowidx0(interf.part2packrowidx0)
3820 , part2rowidx0(interf.part2rowidx0)
3821 , lclrow(interf.lclrow)
3822 , blocksize(pmv.extent(1))
3823 , num_vectors(pmv.extent(2))
3824 , packed_multivector(pmv) {}
3827 KOKKOS_INLINE_FUNCTION
3829 operator()(
const local_ordinal_type &packidx)
const {
3830 local_ordinal_type partidx = packptr(packidx);
3831 local_ordinal_type npacks = packptr(packidx + 1) - partidx;
3832 const local_ordinal_type pri0 = part2packrowidx0(partidx);
3834 local_ordinal_type ri0[vector_length] = {};
3835 local_ordinal_type nrows[vector_length] = {};
3836 for (local_ordinal_type v = 0; v < npacks; ++v, ++partidx) {
3837 ri0[v] = part2rowidx0(partidx);
3838 nrows[v] = part2rowidx0(partidx + 1) - ri0[v];
3840 for (local_ordinal_type j = 0; j < nrows[0]; ++j) {
3841 local_ordinal_type cnt = 1;
3842 for (; cnt < npacks && j != nrows[cnt]; ++cnt)
3845 const local_ordinal_type pri = pri0 + j;
3846 for (local_ordinal_type col = 0; col < num_vectors; ++col)
3847 for (local_ordinal_type i = 0; i < blocksize; ++i)
3848 for (local_ordinal_type v = 0; v < npacks; ++v)
3849 packed_multivector(pri, i, col)[v] =
static_cast<btdm_scalar_type
>(scalar_multivector(blocksize * lclrow(ri0[v] + j) + i, col));
3853 KOKKOS_INLINE_FUNCTION
3855 operator()(
const member_type &member)
const {
3856 const local_ordinal_type packidx = member.league_rank();
3857 const local_ordinal_type partidx_begin = packptr(packidx);
3858 const local_ordinal_type npacks = packptr(packidx + 1) - partidx_begin;
3859 const local_ordinal_type pri0 = part2packrowidx0(partidx_begin);
3860 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, npacks), [&](
const local_ordinal_type &v) {
3861 const local_ordinal_type partidx = partidx_begin + v;
3862 const local_ordinal_type ri0 = part2rowidx0(partidx);
3863 const local_ordinal_type nrows = part2rowidx0(partidx + 1) - ri0;
3866 const local_ordinal_type pri = pri0;
3867 for (local_ordinal_type col = 0; col < num_vectors; ++col) {
3868 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, blocksize), [&](
const local_ordinal_type &i) {
3869 packed_multivector(pri, i, col)[v] =
static_cast<btdm_scalar_type
>(scalar_multivector(blocksize * lclrow(ri0) + i, col));
3873 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, nrows), [&](
const local_ordinal_type &j) {
3874 const local_ordinal_type pri = pri0 + j;
3875 for (local_ordinal_type col = 0; col < num_vectors; ++col)
3876 for (local_ordinal_type i = 0; i < blocksize; ++i)
3877 packed_multivector(pri, i, col)[v] =
static_cast<btdm_scalar_type
>(scalar_multivector(blocksize * lclrow(ri0 + j) + i, col));
3883 void run(
const const_impl_scalar_type_2d_view_tpetra &scalar_multivector_) {
3884 IFPACK2_BLOCKTRIDICONTAINER_PROFILER_REGION_BEGIN;
3885 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::MultiVectorConverter", MultiVectorConverter0);
3887 scalar_multivector = scalar_multivector_;
3888 if constexpr (BlockHelperDetails::is_device<execution_space>::value) {
3889 const local_ordinal_type vl = vector_length;
3890 const Kokkos::TeamPolicy<execution_space> policy(packptr.extent(0) - 1, Kokkos::AUTO(), vl);
3891 Kokkos::parallel_for(
"MultiVectorConverter::TeamPolicy", policy, *
this);
3893 const Kokkos::RangePolicy<execution_space> policy(0, packptr.extent(0) - 1);
3894 Kokkos::parallel_for(
"MultiVectorConverter::RangePolicy", policy, *
this);
3896 IFPACK2_BLOCKTRIDICONTAINER_PROFILER_REGION_END;
3897 IFPACK2_BLOCKHELPER_TIMER_FENCE(execution_space)
3906struct SolveTridiagsDefaultModeAndAlgo<Kokkos::HostSpace> {
3907 typedef KB::Mode::Serial mode_type;
3908 typedef KB::Algo::Level2::Unblocked single_vector_algo_type;
3909#if defined(__KOKKOSBATCHED_INTEL_MKL_COMPACT_BATCHED__)
3910 typedef KB::Algo::Level3::CompactMKL multi_vector_algo_type;
3912 typedef KB::Algo::Level3::Blocked multi_vector_algo_type;
3914 static int recommended_team_size(
const int ,
3921#if defined(KOKKOS_ENABLE_CUDA)
3922static inline int SolveTridiagsRecommendedCudaTeamSize(
const int blksize,
3923 const int vector_length,
3924 const int internal_vector_length) {
3925 const int vector_size = vector_length / internal_vector_length;
3926 int total_team_size(0);
3928 total_team_size = 32;
3929 else if (blksize <= 9)
3930 total_team_size = 32;
3931 else if (blksize <= 12)
3932 total_team_size = 96;
3933 else if (blksize <= 16)
3934 total_team_size = 128;
3935 else if (blksize <= 20)
3936 total_team_size = 160;
3938 total_team_size = 160;
3939 return total_team_size / vector_size;
3943struct SolveTridiagsDefaultModeAndAlgo<Kokkos::CudaSpace> {
3944 typedef KB::Mode::Team mode_type;
3945 typedef KB::Algo::Level2::Unblocked single_vector_algo_type;
3946 typedef KB::Algo::Level3::Unblocked multi_vector_algo_type;
3947 static int recommended_team_size(
const int blksize,
3948 const int vector_length,
3949 const int internal_vector_length) {
3950 return SolveTridiagsRecommendedCudaTeamSize(blksize, vector_length, internal_vector_length);
3954struct SolveTridiagsDefaultModeAndAlgo<Kokkos::CudaUVMSpace> {
3955 typedef KB::Mode::Team mode_type;
3956 typedef KB::Algo::Level2::Unblocked single_vector_algo_type;
3957 typedef KB::Algo::Level3::Unblocked multi_vector_algo_type;
3958 static int recommended_team_size(
const int blksize,
3959 const int vector_length,
3960 const int internal_vector_length) {
3961 return SolveTridiagsRecommendedCudaTeamSize(blksize, vector_length, internal_vector_length);
3966#if defined(KOKKOS_ENABLE_HIP)
3967static inline int SolveTridiagsRecommendedHIPTeamSize(
const int blksize,
3968 const int vector_length,
3969 const int internal_vector_length) {
3970 const int vector_size = vector_length / internal_vector_length;
3971 int total_team_size(0);
3973 total_team_size = 32;
3974 else if (blksize <= 9)
3975 total_team_size = 32;
3976 else if (blksize <= 12)
3977 total_team_size = 96;
3978 else if (blksize <= 16)
3979 total_team_size = 128;
3980 else if (blksize <= 20)
3981 total_team_size = 160;
3983 total_team_size = 160;
3984 return total_team_size / vector_size;
3988struct SolveTridiagsDefaultModeAndAlgo<Kokkos::HIPSpace> {
3989 typedef KB::Mode::Team mode_type;
3990 typedef KB::Algo::Level2::Unblocked single_vector_algo_type;
3991 typedef KB::Algo::Level3::Unblocked multi_vector_algo_type;
3992 static int recommended_team_size(
const int blksize,
3993 const int vector_length,
3994 const int internal_vector_length) {
3995 return SolveTridiagsRecommendedHIPTeamSize(blksize, vector_length, internal_vector_length);
3999struct SolveTridiagsDefaultModeAndAlgo<Kokkos::HIPHostPinnedSpace> {
4000 typedef KB::Mode::Team mode_type;
4001 typedef KB::Algo::Level2::Unblocked single_vector_algo_type;
4002 typedef KB::Algo::Level3::Unblocked multi_vector_algo_type;
4003 static int recommended_team_size(
const int blksize,
4004 const int vector_length,
4005 const int internal_vector_length) {
4006 return SolveTridiagsRecommendedHIPTeamSize(blksize, vector_length, internal_vector_length);
4011#if defined(KOKKOS_ENABLE_SYCL)
4012static inline int SolveTridiagsRecommendedSYCLTeamSize(
const int blksize,
4013 const int vector_length,
4014 const int internal_vector_length) {
4015 const int vector_size = vector_length / internal_vector_length;
4016 int total_team_size(0);
4018 total_team_size = 32;
4019 else if (blksize <= 9)
4020 total_team_size = 32;
4021 else if (blksize <= 12)
4022 total_team_size = 96;
4023 else if (blksize <= 16)
4024 total_team_size = 128;
4025 else if (blksize <= 20)
4026 total_team_size = 160;
4028 total_team_size = 160;
4029 return total_team_size / vector_size;
4033struct SolveTridiagsDefaultModeAndAlgo<Kokkos::SYCLSharedUSMSpace> {
4034 typedef KB::Mode::Team mode_type;
4035 typedef KB::Algo::Level2::Unblocked single_vector_algo_type;
4036 typedef KB::Algo::Level3::Unblocked multi_vector_algo_type;
4037 static int recommended_team_size(
const int blksize,
4038 const int vector_length,
4039 const int internal_vector_length) {
4040 return SolveTridiagsRecommendedSYCLTeamSize(blksize, vector_length, internal_vector_length);
4044struct SolveTridiagsDefaultModeAndAlgo<Kokkos::SYCLDeviceUSMSpace> {
4045 typedef KB::Mode::Team mode_type;
4046 typedef KB::Algo::Level2::Unblocked single_vector_algo_type;
4047 typedef KB::Algo::Level3::Unblocked multi_vector_algo_type;
4048 static int recommended_team_size(
const int blksize,
4049 const int vector_length,
4050 const int internal_vector_length) {
4051 return SolveTridiagsRecommendedSYCLTeamSize(blksize, vector_length, internal_vector_length);
4056template <
typename MatrixType>
4057struct SolveTridiags {
4059 using impl_type = BlockHelperDetails::ImplType<MatrixType>;
4060 using execution_space =
typename impl_type::execution_space;
4062 using local_ordinal_type =
typename impl_type::local_ordinal_type;
4063 using size_type =
typename impl_type::size_type;
4064 using impl_scalar_type =
typename impl_type::impl_scalar_type;
4065 using magnitude_type =
typename impl_type::magnitude_type;
4066 using btdm_scalar_type =
typename impl_type::btdm_scalar_type;
4067 using btdm_magnitude_type =
typename impl_type::btdm_magnitude_type;
4069 using local_ordinal_type_1d_view =
typename impl_type::local_ordinal_type_1d_view;
4070 using local_ordinal_type_2d_view =
typename impl_type::local_ordinal_type_2d_view;
4071 using size_type_2d_view =
typename impl_type::size_type_2d_view;
4073 using vector_type_3d_view =
typename impl_type::vector_type_3d_view;
4074 using internal_vector_type_3d_view =
typename impl_type::internal_vector_type_3d_view;
4075 using internal_vector_type_4d_view =
typename impl_type::internal_vector_type_4d_view;
4076 using internal_vector_type_5d_view =
typename impl_type::internal_vector_type_5d_view;
4077 using btdm_scalar_type_4d_view =
typename impl_type::btdm_scalar_type_4d_view;
4079 using internal_vector_scratch_type_3d_view = Scratch<typename impl_type::internal_vector_type_3d_view>;
4081 using internal_vector_type =
typename impl_type::internal_vector_type;
4082 static constexpr int vector_length = impl_type::vector_length;
4083 static constexpr int internal_vector_length = impl_type::internal_vector_length;
4086 using impl_scalar_type_1d_view =
typename impl_type::impl_scalar_type_1d_view;
4087 using impl_scalar_type_2d_view_tpetra =
typename impl_type::impl_scalar_type_2d_view_tpetra;
4090 using team_policy_type = Kokkos::TeamPolicy<execution_space>;
4091 using member_type =
typename team_policy_type::member_type;
4095 local_ordinal_type n_subparts_per_part;
4096 const ConstUnmanaged<local_ordinal_type_1d_view> partptr;
4097 const ConstUnmanaged<local_ordinal_type_1d_view> packptr;
4098 const ConstUnmanaged<local_ordinal_type_1d_view> packindices_sub;
4099 const ConstUnmanaged<local_ordinal_type_2d_view> packindices_schur;
4100 const ConstUnmanaged<local_ordinal_type_1d_view> part2packrowidx0;
4101 const ConstUnmanaged<local_ordinal_type_2d_view> part2packrowidx0_sub;
4102 const ConstUnmanaged<local_ordinal_type_1d_view> lclrow;
4103 const ConstUnmanaged<local_ordinal_type_1d_view> packptr_sub;
4105 const ConstUnmanaged<local_ordinal_type_2d_view> partptr_sub;
4106 const ConstUnmanaged<size_type_2d_view> pack_td_ptr_schur;
4109 const ConstUnmanaged<size_type_2d_view> pack_td_ptr;
4112 const ConstUnmanaged<internal_vector_type_4d_view> D_internal_vector_values;
4113 const Unmanaged<internal_vector_type_4d_view> X_internal_vector_values;
4114 const Unmanaged<btdm_scalar_type_4d_view> X_internal_scalar_values;
4116 const Unmanaged<internal_vector_type_3d_view> X_internal_vector_values_schur;
4118 const ConstUnmanaged<internal_vector_type_4d_view> D_internal_vector_values_schur;
4119 const ConstUnmanaged<internal_vector_type_5d_view> e_internal_vector_values;
4121 const local_ordinal_type vector_loop_size;
4124 Unmanaged<impl_scalar_type_2d_view_tpetra> Y_scalar_multivector;
4125#if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__) || defined(__SYCL_DEVICE_ONLY__)
4126 AtomicUnmanaged<impl_scalar_type_1d_view> Z_scalar_vector;
4128 Unmanaged<impl_scalar_type_1d_view> Z_scalar_vector;
4130 const impl_scalar_type df;
4131 const bool compute_diff;
4135 local_ordinal_type active_schur_solve_vec;
4138 SolveTridiags(
const BlockHelperDetails::PartInterface<MatrixType> &interf,
4139 const BlockTridiags<MatrixType> &btdm,
4140 const vector_type_3d_view &pmv,
4141 const impl_scalar_type damping_factor,
4142 const bool is_norm_manager_active)
4144 n_subparts_per_part(interf.n_subparts_per_part)
4145 , partptr(interf.partptr)
4146 , packptr(interf.packptr)
4147 , packindices_sub(interf.packindices_sub)
4148 , packindices_schur(interf.packindices_schur)
4149 , part2packrowidx0(interf.part2packrowidx0)
4150 , part2packrowidx0_sub(interf.part2packrowidx0_sub)
4151 , lclrow(interf.lclrow)
4152 , packptr_sub(interf.packptr_sub)
4153 , partptr_sub(interf.partptr_sub)
4154 , pack_td_ptr_schur(btdm.pack_td_ptr_schur)
4157 pack_td_ptr(btdm.pack_td_ptr)
4158 , D_internal_vector_values((internal_vector_type *)btdm.values.data(),
4159 btdm.values.extent(0),
4160 btdm.values.extent(1),
4161 btdm.values.extent(2),
4162 vector_length / internal_vector_length)
4163 , X_internal_vector_values((internal_vector_type *)pmv.data(),
4167 vector_length / internal_vector_length)
4168 , X_internal_scalar_values((btdm_scalar_type *)pmv.data(),
4173 , X_internal_vector_values_schur(btdm.X_internal_vector_values_schur)
4174 , D_internal_vector_values_schur((internal_vector_type *)btdm.values_schur.data(),
4175 btdm.values_schur.extent(0),
4176 btdm.values_schur.extent(1),
4177 btdm.values_schur.extent(2),
4178 vector_length / internal_vector_length)
4179 , e_internal_vector_values((internal_vector_type *)btdm.e_values.data(),
4180 btdm.e_values.extent(0),
4181 btdm.e_values.extent(1),
4182 btdm.e_values.extent(2),
4183 btdm.e_values.extent(3),
4184 vector_length / internal_vector_length)
4185 , vector_loop_size(vector_length / internal_vector_length)
4186 , Y_scalar_multivector()
4188 , df(damping_factor)
4189 , compute_diff(is_norm_manager_active)
4190 , active_schur_solve_vec(0) {}
4194 KOKKOS_INLINE_FUNCTION
4196 copyToFlatMultiVector(
const member_type &member,
4197 const local_ordinal_type partidxbeg,
4198 const local_ordinal_type npacks,
4199 const local_ordinal_type pri0,
4200 const local_ordinal_type v,
4201 const local_ordinal_type blocksize,
4202 const local_ordinal_type num_vectors)
const {
4203 const local_ordinal_type vbeg = v * internal_vector_length;
4204 if (vbeg < npacks) {
4205 local_ordinal_type ri0_vals[internal_vector_length] = {};
4206 local_ordinal_type nrows_vals[internal_vector_length] = {};
4207 for (local_ordinal_type vv = vbeg, vi = 0; vv < npacks && vi < internal_vector_length; ++vv, ++vi) {
4208 const local_ordinal_type partidx = partidxbeg + vv;
4209 ri0_vals[vi] = partptr(partidx);
4210 nrows_vals[vi] = partptr(partidx + 1) - ri0_vals[vi];
4213 impl_scalar_type z_partial_sum(0);
4214 if (nrows_vals[0] == 1) {
4215 const local_ordinal_type j = 0, pri = pri0;
4217 for (local_ordinal_type vv = vbeg, vi = 0; vv < npacks && vi < internal_vector_length; ++vv, ++vi) {
4218 const local_ordinal_type ri0 = ri0_vals[vi];
4219 const local_ordinal_type nrows = nrows_vals[vi];
4221 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, blocksize),
4222 [&](
const local_ordinal_type &i) {
4223 const local_ordinal_type row = blocksize * lclrow(ri0 + j) + i;
4224 for (local_ordinal_type col = 0; col < num_vectors; ++col) {
4225 impl_scalar_type &y = Y_scalar_multivector(row, col);
4226 const impl_scalar_type yd = X_internal_vector_values(pri, i, col, v)[vi] - y;
4230 const auto yd_abs = KokkosKernels::ArithTraits<impl_scalar_type>::abs(yd);
4231 z_partial_sum += yd_abs * yd_abs;
4239 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, nrows_vals[0]),
4240 [&](
const local_ordinal_type &j) {
4241 const local_ordinal_type pri = pri0 + j;
4242 for (local_ordinal_type vv = vbeg, vi = 0; vv < npacks && vi < internal_vector_length; ++vv, ++vi) {
4243 const local_ordinal_type ri0 = ri0_vals[vi];
4244 const local_ordinal_type nrows = nrows_vals[vi];
4246 for (local_ordinal_type col = 0; col < num_vectors; ++col) {
4247 for (local_ordinal_type i = 0; i < blocksize; ++i) {
4248 const local_ordinal_type row = blocksize * lclrow(ri0 + j) + i;
4249 impl_scalar_type &y = Y_scalar_multivector(row, col);
4250 const impl_scalar_type yd = X_internal_vector_values(pri, i, col, v)[vi] - y;
4254 const auto yd_abs = KokkosKernels::ArithTraits<impl_scalar_type>::abs(yd);
4255 z_partial_sum += yd_abs * yd_abs;
4264 Z_scalar_vector(member.league_rank()) += z_partial_sum;
4271 template <
typename WWViewType>
4272 KOKKOS_INLINE_FUNCTION
void
4273 solveSingleVector(
const member_type &member,
4274 const local_ordinal_type &blocksize,
4275 const local_ordinal_type &i0,
4276 const local_ordinal_type &r0,
4277 const local_ordinal_type &nrows,
4278 const local_ordinal_type &v,
4279 const WWViewType &WW)
const {
4280 typedef SolveTridiagsDefaultModeAndAlgo<typename execution_space::memory_space> default_mode_and_algo_type;
4282 typedef typename default_mode_and_algo_type::mode_type default_mode_type;
4283 typedef typename default_mode_and_algo_type::single_vector_algo_type default_algo_type;
4286 auto A = D_internal_vector_values.data();
4287 auto X = X_internal_vector_values.data();
4290 const auto one = KokkosKernels::ArithTraits<btdm_magnitude_type>::one();
4291 const auto zero = KokkosKernels::ArithTraits<btdm_magnitude_type>::zero();
4295 const local_ordinal_type astep = D_internal_vector_values.stride(0);
4296 const local_ordinal_type as0 = D_internal_vector_values.stride(1);
4297 const local_ordinal_type as1 = D_internal_vector_values.stride(2);
4298 const local_ordinal_type xstep = X_internal_vector_values.stride(0);
4299 const local_ordinal_type xs0 = X_internal_vector_values.stride(1);
4302 A += i0 * astep + v;
4303 X += r0 * xstep + v;
4308 KOKKOSBATCHED_TRSV_LOWER_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4311 blocksize, blocksize,
4316 for (local_ordinal_type tr = 1; tr < nrows; ++tr) {
4317 member.team_barrier();
4318 KOKKOSBATCHED_GEMV_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4320 blocksize, blocksize,
4322 A + 2 * astep, as0, as1,
4325 X + 1 * xstep, xs0);
4326 KOKKOSBATCHED_TRSV_LOWER_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4329 blocksize, blocksize,
4331 A + 3 * astep, as0, as1,
4332 X + 1 * xstep, xs0);
4339 KOKKOSBATCHED_TRSV_UPPER_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4342 blocksize, blocksize,
4347 for (local_ordinal_type tr = nrows; tr > 1; --tr) {
4349 member.team_barrier();
4350 KOKKOSBATCHED_GEMV_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4352 blocksize, blocksize,
4354 A + 1 * astep, as0, as1,
4357 X - 1 * xstep, xs0);
4358 KOKKOSBATCHED_TRSV_UPPER_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4361 blocksize, blocksize,
4364 X - 1 * xstep, xs0);
4370 const local_ordinal_type ws0 = WW.stride(0);
4371 auto W = WW.data() + v;
4372 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, blocksize), [&](
int i) { W[i * ws0] = X[i * xs0]; });
4373 member.team_barrier();
4374 KOKKOSBATCHED_GEMV_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4376 blocksize, blocksize,
4385 template <
typename WWViewType>
4386 KOKKOS_INLINE_FUNCTION
void
4387 solveMultiVector(
const member_type &member,
4388 const local_ordinal_type & ,
4389 const local_ordinal_type &i0,
4390 const local_ordinal_type &r0,
4391 const local_ordinal_type &nrows,
4392 const local_ordinal_type &v,
4393 const WWViewType &WW)
const {
4394 typedef SolveTridiagsDefaultModeAndAlgo<typename execution_space::memory_space> default_mode_and_algo_type;
4396 typedef typename default_mode_and_algo_type::mode_type default_mode_type;
4397 typedef typename default_mode_and_algo_type::multi_vector_algo_type default_algo_type;
4400 const auto one = KokkosKernels::ArithTraits<btdm_magnitude_type>::one();
4401 const auto zero = KokkosKernels::ArithTraits<btdm_magnitude_type>::zero();
4404 auto A = Kokkos::subview(D_internal_vector_values, i0, Kokkos::ALL(), Kokkos::ALL(), v);
4405 auto X1 = Kokkos::subview(X_internal_vector_values, r0, Kokkos::ALL(), Kokkos::ALL(), v);
4408 local_ordinal_type i = i0, r = r0;
4412 KB::Trsm<member_type,
4413 KB::Side::Left, KB::Uplo::Lower, KB::Trans::NoTranspose, KB::Diag::Unit,
4414 default_mode_type, default_algo_type>::invoke(member, one, A, X1);
4415 for (local_ordinal_type tr = 1; tr < nrows; ++tr, i += 3) {
4416 A.assign_data(&D_internal_vector_values(i + 2, 0, 0, v));
4417 X2.assign_data(&X_internal_vector_values(++r, 0, 0, v));
4418 member.team_barrier();
4419 KB::Gemm<member_type,
4420 KB::Trans::NoTranspose, KB::Trans::NoTranspose,
4421 default_mode_type, default_algo_type>::invoke(member, -one, A, X1, one, X2);
4422 A.assign_data(&D_internal_vector_values(i + 3, 0, 0, v));
4423 KB::Trsm<member_type,
4424 KB::Side::Left, KB::Uplo::Lower, KB::Trans::NoTranspose, KB::Diag::Unit,
4425 default_mode_type, default_algo_type>::invoke(member, one, A, X2);
4426 X1.assign_data(X2.data());
4430 KB::Trsm<member_type,
4431 KB::Side::Left, KB::Uplo::Upper, KB::Trans::NoTranspose, KB::Diag::NonUnit,
4432 default_mode_type, default_algo_type>::invoke(member, one, A, X1);
4433 for (local_ordinal_type tr = nrows; tr > 1; --tr) {
4435 A.assign_data(&D_internal_vector_values(i + 1, 0, 0, v));
4436 X2.assign_data(&X_internal_vector_values(--r, 0, 0, v));
4437 member.team_barrier();
4438 KB::Gemm<member_type,
4439 KB::Trans::NoTranspose, KB::Trans::NoTranspose,
4440 default_mode_type, default_algo_type>::invoke(member, -one, A, X1, one, X2);
4442 A.assign_data(&D_internal_vector_values(i, 0, 0, v));
4443 KB::Trsm<member_type,
4444 KB::Side::Left, KB::Uplo::Upper, KB::Trans::NoTranspose, KB::Diag::NonUnit,
4445 default_mode_type, default_algo_type>::invoke(member, one, A, X2);
4446 X1.assign_data(X2.data());
4450 auto W = Kokkos::subview(WW, Kokkos::ALL(), Kokkos::ALL(), v);
4451 KB::Copy<member_type, KB::Trans::NoTranspose, default_mode_type>::invoke(member, X1, W);
4452 member.team_barrier();
4453 KB::Gemm<member_type,
4454 KB::Trans::NoTranspose, KB::Trans::NoTranspose,
4455 default_mode_type, default_algo_type>::invoke(member, one, A, W, zero, X1);
4459 template <
int B,
int ScratchLevel>
4460 struct MultiVectorTag {};
4462 template <
int B,
int ScratchLevel>
4463 struct SingleVectorSubLineTag {};
4465 struct SingleVectorApplyCTag {};
4466 template <
int B,
int ScratchLevel>
4467 struct SingleVectorSchurTag {};
4469 struct SingleVectorApplyETag {};
4471 struct CopyVectorToFlatTag {};
4473 template <
int B,
int ScratchLevel>
4474 struct SingleVectorFunctor {
4475 SingleVectorFunctor(
const SolveTridiags<MatrixType> &solve)
4476 : packptr(solve.packptr)
4477 , part2packrowidx0(solve.part2packrowidx0)
4478 , pack_td_ptr(solve.pack_td_ptr)
4479 , partptr(solve.partptr)
4480 , D_internal_vector_values(solve.D_internal_vector_values)
4481 , lclrow(solve.lclrow)
4482 , X_internal_vector_values(solve.X_internal_vector_values)
4483 , Y_scalar_multivector(solve.Y_scalar_multivector)
4484 , Z_scalar_vector(solve.Z_scalar_vector)
4486 , vector_loop_size(solve.vector_loop_size) {}
4491 BlockHelperDetails::Uint32IndexedConstView<local_ordinal_type_1d_view> packptr;
4492 BlockHelperDetails::Uint32IndexedConstView<local_ordinal_type_1d_view> part2packrowidx0;
4493 BlockHelperDetails::Uint32IndexedConstView<size_type_2d_view> pack_td_ptr;
4494 BlockHelperDetails::Uint32IndexedConstView<local_ordinal_type_1d_view> partptr;
4495 BlockHelperDetails::Uint32IndexedConstView<internal_vector_type_4d_view> D_internal_vector_values;
4496 BlockHelperDetails::Uint32IndexedConstView<local_ordinal_type_1d_view> lclrow;
4497 BlockHelperDetails::Uint32IndexedView<internal_vector_type_4d_view> X_internal_vector_values;
4498 BlockHelperDetails::Uint32IndexedView<impl_scalar_type_2d_view_tpetra> Y_scalar_multivector;
4499 BlockHelperDetails::Uint32IndexedView<impl_scalar_type_1d_view> Z_scalar_vector;
4500 const impl_scalar_type df;
4501 const local_ordinal_type vector_loop_size;
4503 template <
typename WWViewType>
4504 KOKKOS_INLINE_FUNCTION
void
4505 solveSingleVector(
const member_type &member,
4506 const local_ordinal_type &blocksize,
4507 const local_ordinal_type &i0,
4508 const local_ordinal_type &r0,
4509 const local_ordinal_type &nrows,
4510 const local_ordinal_type &v,
4511 const WWViewType &WW)
const {
4512 typedef SolveTridiagsDefaultModeAndAlgo<typename execution_space::memory_space> default_mode_and_algo_type;
4514 typedef typename default_mode_and_algo_type::mode_type default_mode_type;
4515 typedef typename default_mode_and_algo_type::single_vector_algo_type default_algo_type;
4518 auto A = D_internal_vector_values.data();
4519 auto X = X_internal_vector_values.data();
4522 const auto one = KokkosKernels::ArithTraits<btdm_magnitude_type>::one();
4523 const auto zero = KokkosKernels::ArithTraits<btdm_magnitude_type>::zero();
4527 const local_ordinal_type astep = D_internal_vector_values.stride(0);
4528 const local_ordinal_type as0 = D_internal_vector_values.stride(1);
4529 const local_ordinal_type as1 = D_internal_vector_values.stride(2);
4530 const local_ordinal_type xstep = X_internal_vector_values.stride(0);
4531 const local_ordinal_type xs0 = X_internal_vector_values.stride(1);
4534 A += i0 * astep + v;
4535 X += r0 * xstep + v;
4540 KOKKOSBATCHED_TRSV_LOWER_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4543 blocksize, blocksize,
4548 for (local_ordinal_type tr = 1; tr < nrows; ++tr) {
4549 member.team_barrier();
4550 KOKKOSBATCHED_GEMV_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4552 blocksize, blocksize,
4554 A + 2 * astep, as0, as1,
4557 X + 1 * xstep, xs0);
4558 KOKKOSBATCHED_TRSV_LOWER_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4561 blocksize, blocksize,
4563 A + 3 * astep, as0, as1,
4564 X + 1 * xstep, xs0);
4571 KOKKOSBATCHED_TRSV_UPPER_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4574 blocksize, blocksize,
4579 for (local_ordinal_type tr = nrows; tr > 1; --tr) {
4581 member.team_barrier();
4582 KOKKOSBATCHED_GEMV_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4584 blocksize, blocksize,
4586 A + 1 * astep, as0, as1,
4589 X - 1 * xstep, xs0);
4590 KOKKOSBATCHED_TRSV_UPPER_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4593 blocksize, blocksize,
4596 X - 1 * xstep, xs0);
4602 const local_ordinal_type ws0 = WW.stride(0);
4603 auto W = WW.data() + v;
4604 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, blocksize), [&](
int i) { W[i * ws0] = X[i * xs0]; });
4605 member.team_barrier();
4606 KOKKOSBATCHED_GEMV_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4608 blocksize, blocksize,
4617 KOKKOS_INLINE_FUNCTION
4619 copyToFlatMultiVector(
const member_type &member,
4620 const local_ordinal_type partidxbeg,
4621 const local_ordinal_type npacks,
4622 const local_ordinal_type pri0,
4623 const local_ordinal_type v,
4624 const local_ordinal_type blocksize)
const {
4625 const local_ordinal_type vbeg = v * internal_vector_length;
4626 if (vbeg < npacks) {
4627 local_ordinal_type ri0_vals[internal_vector_length] = {};
4628 local_ordinal_type nrows_vals[internal_vector_length] = {};
4629 for (local_ordinal_type vv = vbeg, vi = 0; vv < npacks && vi < internal_vector_length; ++vv, ++vi) {
4630 const local_ordinal_type partidx = partidxbeg + vv;
4631 ri0_vals[vi] = partptr(partidx);
4632 nrows_vals[vi] = partptr(partidx + 1) - ri0_vals[vi];
4635 impl_scalar_type z_partial_sum(0);
4636 if (nrows_vals[0] == 1) {
4637 const local_ordinal_type j = 0, pri = pri0;
4639 for (local_ordinal_type vv = vbeg, vi = 0; vv < npacks && vi < internal_vector_length; ++vv, ++vi) {
4640 const local_ordinal_type ri0 = ri0_vals[vi];
4641 const local_ordinal_type nrows = nrows_vals[vi];
4643 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, blocksize),
4644 [&](
const local_ordinal_type &i) {
4645 const local_ordinal_type row = blocksize * lclrow(ri0 + j) + i;
4646 impl_scalar_type &y = Y_scalar_multivector(row, 0);
4647 const impl_scalar_type yd = X_internal_vector_values(pri, i, 0, v)[vi] - y;
4651 const auto yd_abs = KokkosKernels::ArithTraits<impl_scalar_type>::abs(yd);
4652 z_partial_sum += yd_abs * yd_abs;
4659 Kokkos::parallel_for(Kokkos::TeamThreadRange(member, nrows_vals[0]),
4660 [&](
const local_ordinal_type &j) {
4661 const local_ordinal_type pri = pri0 + j;
4662 for (local_ordinal_type vv = vbeg, vi = 0; vv < npacks && vi < internal_vector_length; ++vv, ++vi) {
4663 const local_ordinal_type ri0 = ri0_vals[vi];
4664 const local_ordinal_type nrows = nrows_vals[vi];
4666 for (local_ordinal_type i = 0; i < blocksize; ++i) {
4667 const local_ordinal_type row = blocksize * lclrow(ri0 + j) + i;
4668 impl_scalar_type &y = Y_scalar_multivector(row, 0);
4669 const impl_scalar_type yd = X_internal_vector_values(pri, i, 0, v)[vi] - y;
4673 const auto yd_abs = KokkosKernels::ArithTraits<impl_scalar_type>::abs(yd);
4674 z_partial_sum += yd_abs * yd_abs;
4682 Kokkos::atomic_add(&Z_scalar_vector(member.league_rank()), z_partial_sum);
4686 KOKKOS_INLINE_FUNCTION
void
4687 operator()(
const member_type &member)
const {
4688 const local_ordinal_type packidx = member.league_rank();
4689 const local_ordinal_type partidx = packptr(packidx);
4690 const local_ordinal_type npacks = packptr(packidx + 1) - partidx;
4691 const local_ordinal_type pri0 = part2packrowidx0(partidx);
4692 const local_ordinal_type i0 = pack_td_ptr(partidx, 0);
4693 const local_ordinal_type r0 = pri0;
4694 const local_ordinal_type nrows = partptr(partidx + 1) - partptr(partidx);
4695 const local_ordinal_type blocksize = (B == 0 ? D_internal_vector_values.extent(1) : B);
4701#if defined(KOKKOS_ENABLE_IMPL_MDSPAN) || (KOKKOS_VERSION >= 50299)
4702 using WW_u32_view_type = Kokkos::View<
4703 internal_vector_type,
4704 Kokkos::extents<uint32_t, Kokkos::dynamic_extent, Kokkos::dynamic_extent>,
4705 Kokkos::layout_right,
4706 Kokkos::Experimental::Accessor<internal_vector_type,
4707 typename execution_space::scratch_memory_space,
4708 Kokkos::MemoryTraits<Kokkos::Unmanaged>>>;
4710 using WW_u32_view_type = Kokkos::View<
4711 internal_vector_type **,
4712 Kokkos::LayoutRight,
4713 typename execution_space::scratch_memory_space,
4714 Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
4716 constexpr size_t WW_alignment =
4717 Kokkos::max({
sizeof(internal_vector_type),
alignof(internal_vector_type),
4718 static_cast<size_t>(execution_space::scratch_memory_space::ALIGN)});
4719 internal_vector_type *WW_ptr =
reinterpret_cast<internal_vector_type *
>(
4720 member.team_scratch(ScratchLevel)
4721 .get_shmem_aligned(
sizeof(internal_vector_type) *
4722 static_cast<size_t>(blocksize) *
4723 static_cast<size_t>(vector_loop_size),
4725 WW_u32_view_type WW(WW_ptr,
4726 static_cast<uint32_t
>(blocksize),
4727 static_cast<uint32_t
>(vector_loop_size));
4728 Kokkos::single(Kokkos::PerTeam(member), [&]() {
4729 Z_scalar_vector(member.league_rank()) = impl_scalar_type(0);
4731 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size), [&](
const int &v) {
4732 solveSingleVector(member, blocksize, i0, r0, nrows, v, WW);
4733 copyToFlatMultiVector(member, partidx, npacks, pri0, v, blocksize);
4738 template <
int B,
int ScratchLevel>
4739 KOKKOS_INLINE_FUNCTION
void
4740 operator()(
const MultiVectorTag<B, ScratchLevel> &,
const member_type &member)
const {
4741 const local_ordinal_type packidx = member.league_rank();
4742 const local_ordinal_type partidx = packptr(packidx);
4743 const local_ordinal_type npacks = packptr(packidx + 1) - partidx;
4744 const local_ordinal_type pri0 = part2packrowidx0(partidx);
4745 const local_ordinal_type i0 = pack_td_ptr(partidx, 0);
4746 const local_ordinal_type r0 = part2packrowidx0(partidx);
4747 const local_ordinal_type nrows = partptr(partidx + 1) - partptr(partidx);
4748 const local_ordinal_type blocksize = (B == 0 ? D_internal_vector_values.extent(1) : B);
4749 const local_ordinal_type num_vectors = X_internal_vector_values.extent(2);
4751 internal_vector_scratch_type_3d_view
4752 WW(member.team_scratch(ScratchLevel), blocksize, num_vectors, vector_loop_size);
4753 Kokkos::single(Kokkos::PerTeam(member), [&]() {
4754 Z_scalar_vector(member.league_rank()) = impl_scalar_type(0);
4756 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size), [&](
const int &v) {
4757 solveMultiVector(member, blocksize, i0, r0, nrows, v, WW);
4758 copyToFlatMultiVector(member, partidx, npacks, pri0, v, blocksize, num_vectors);
4762 template <
int B,
int ScratchLevel>
4763 KOKKOS_INLINE_FUNCTION
void
4764 operator()(
const SingleVectorSubLineTag<B, ScratchLevel> &,
const member_type &member)
const {
4766 const local_ordinal_type packidx = packindices_sub(member.league_rank());
4768 const local_ordinal_type subpartidx = packptr_sub(packidx);
4769 const local_ordinal_type n_parts = part2packrowidx0_sub.extent(0);
4770 const local_ordinal_type local_subpartidx = subpartidx / n_parts;
4771 const local_ordinal_type partidx = subpartidx % n_parts;
4773 const local_ordinal_type npacks = packptr_sub(packidx + 1) - subpartidx;
4774 const local_ordinal_type i0 = pack_td_ptr(partidx, local_subpartidx);
4775 const local_ordinal_type r0 = part2packrowidx0_sub(partidx, local_subpartidx);
4776 const local_ordinal_type nrows = partptr_sub(subpartidx, 1) - partptr_sub(subpartidx, 0);
4777 const local_ordinal_type blocksize = e_internal_vector_values.extent(2);
4783 internal_vector_scratch_type_3d_view
4784 WW(member.team_scratch(ScratchLevel), blocksize, 1, vector_loop_size);
4786 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size), [&](
const int &v) {
4787 auto X_internal_vec = Kokkos::subview(X_internal_vector_values, Kokkos::ALL(), Kokkos::ALL(), active_schur_solve_vec, Kokkos::ALL());
4788 solveSingleVectorNew<impl_type, internal_vector_scratch_type_3d_view>(member, blocksize, i0, r0, nrows, v, D_internal_vector_values, X_internal_vec, WW);
4793 KOKKOS_INLINE_FUNCTION
void
4794 operator()(
const SingleVectorApplyCTag<B> &,
const member_type &member)
const {
4797 const local_ordinal_type packidx = packindices_sub(member.league_rank());
4799 const local_ordinal_type subpartidx = packptr_sub(packidx);
4800 const local_ordinal_type n_parts = part2packrowidx0_sub.extent(0);
4801 const local_ordinal_type local_subpartidx = subpartidx / n_parts;
4802 const local_ordinal_type partidx = subpartidx % n_parts;
4803 const local_ordinal_type blocksize = e_internal_vector_values.extent(2);
4806 const local_ordinal_type i0 = pack_td_ptr(partidx, local_subpartidx);
4807 const local_ordinal_type r0 = part2packrowidx0_sub(partidx, local_subpartidx);
4808 const local_ordinal_type nrows = partptr_sub(subpartidx, 1) - partptr_sub(subpartidx, 0);
4812 const local_ordinal_type local_subpartidx_schur = (local_subpartidx - 1) / 2;
4813 const local_ordinal_type i0_schur = local_subpartidx_schur == 0 ? pack_td_ptr_schur(partidx, local_subpartidx_schur) : pack_td_ptr_schur(partidx, local_subpartidx_schur) + 1;
4814 const local_ordinal_type i0_offset = local_subpartidx_schur == 0 ? i0 + 2 : i0 + 2;
4819 const auto one = KokkosKernels::ArithTraits<btdm_magnitude_type>::one();
4821 const size_type c_kps2 = local_subpartidx > 0 ? pack_td_ptr(partidx, local_subpartidx) - 2 : 0;
4822 const size_type c_kps1 = pack_td_ptr(partidx, local_subpartidx + 1) + 1;
4824 typedef SolveTridiagsDefaultModeAndAlgo<typename execution_space::memory_space> default_mode_and_algo_type;
4826 typedef typename default_mode_and_algo_type::mode_type default_mode_type;
4827 typedef typename default_mode_and_algo_type::single_vector_algo_type default_algo_type;
4829 if (local_subpartidx == 0) {
4830 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size), [&](
const int &v) {
4831 auto v_1 = Kokkos::subview(X_internal_vector_values, r0 + nrows - 1, Kokkos::ALL(), active_schur_solve_vec, v);
4832 auto v_2 = Kokkos::subview(X_internal_vector_values, r0 + nrows, Kokkos::ALL(), active_schur_solve_vec, v);
4833 auto C = Kokkos::subview(D_internal_vector_values, c_kps1, Kokkos::ALL(), Kokkos::ALL(), v);
4835 KOKKOSBATCHED_GEMV_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4837 blocksize, blocksize,
4839 C.data(), C.stride(0), C.stride(1),
4840 v_1.data(), v_1.stride(0),
4842 v_2.data(), v_2.stride(0));
4844 }
else if (local_subpartidx == (local_ordinal_type)part2packrowidx0_sub.extent(1) - 2) {
4845 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size), [&](
const int &v) {
4846 auto v_1 = Kokkos::subview(X_internal_vector_values, r0, Kokkos::ALL(), active_schur_solve_vec, v);
4847 auto v_2 = Kokkos::subview(X_internal_vector_values, r0 - 1, Kokkos::ALL(), active_schur_solve_vec, v);
4848 auto C = Kokkos::subview(D_internal_vector_values, c_kps2, Kokkos::ALL(), Kokkos::ALL(), v);
4850 KOKKOSBATCHED_GEMV_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4852 blocksize, blocksize,
4854 C.data(), C.stride(0), C.stride(1),
4855 v_1.data(), v_1.stride(0),
4857 v_2.data(), v_2.stride(0));
4860 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size), [&](
const int &v) {
4862 auto v_1 = Kokkos::subview(X_internal_vector_values, r0 + nrows - 1, Kokkos::ALL(), active_schur_solve_vec, v);
4863 auto v_2 = Kokkos::subview(X_internal_vector_values, r0 + nrows, Kokkos::ALL(), active_schur_solve_vec, v);
4864 auto C = Kokkos::subview(D_internal_vector_values, c_kps1, Kokkos::ALL(), Kokkos::ALL(), v);
4866 KOKKOSBATCHED_GEMV_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4868 blocksize, blocksize,
4870 C.data(), C.stride(0), C.stride(1),
4871 v_1.data(), v_1.stride(0),
4873 v_2.data(), v_2.stride(0));
4876 auto v_1 = Kokkos::subview(X_internal_vector_values, r0, Kokkos::ALL(), active_schur_solve_vec, v);
4877 auto v_2 = Kokkos::subview(X_internal_vector_values, r0 - 1, Kokkos::ALL(), active_schur_solve_vec, v);
4878 auto C = Kokkos::subview(D_internal_vector_values, c_kps2, Kokkos::ALL(), Kokkos::ALL(), v);
4880 KOKKOSBATCHED_GEMV_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4882 blocksize, blocksize,
4884 C.data(), C.stride(0), C.stride(1),
4885 v_1.data(), v_1.stride(0),
4887 v_2.data(), v_2.stride(0));
4893 template <
int B,
int ScratchLevel>
4894 KOKKOS_INLINE_FUNCTION
void
4895 operator()(
const SingleVectorSchurTag<B, ScratchLevel> &,
const member_type &member)
const {
4896 const local_ordinal_type packidx = packindices_sub(member.league_rank());
4898 const local_ordinal_type partidx = packptr_sub(packidx);
4900 const local_ordinal_type blocksize = e_internal_vector_values.extent(2);
4902 const local_ordinal_type i0_schur = pack_td_ptr_schur(partidx, 0);
4903 const local_ordinal_type nrows = 2 * (n_subparts_per_part - 1);
4905 const local_ordinal_type r0_schur = nrows * member.league_rank();
4907 internal_vector_scratch_type_3d_view
4908 WW(member.team_scratch(ScratchLevel), blocksize, blocksize, vector_loop_size);
4910 for (local_ordinal_type schur_sub_part = 0; schur_sub_part < n_subparts_per_part - 1; ++schur_sub_part) {
4911 const local_ordinal_type r0 = part2packrowidx0_sub(partidx, 2 * schur_sub_part + 1);
4912 for (local_ordinal_type i = 0; i < 2; ++i) {
4913 copy3DView<local_ordinal_type>(member,
4914 Kokkos::subview(X_internal_vector_values_schur, r0_schur + 2 * schur_sub_part + i, Kokkos::ALL(), Kokkos::ALL()),
4915 Kokkos::subview(X_internal_vector_values, r0 + i, Kokkos::ALL(), active_schur_solve_vec, Kokkos::ALL()));
4919 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size), [&](
const int &v) {
4920 solveSingleVectorNew<impl_type, internal_vector_scratch_type_3d_view>(member, blocksize, i0_schur, r0_schur, nrows, v, D_internal_vector_values_schur, X_internal_vector_values_schur, WW);
4923 for (local_ordinal_type schur_sub_part = 0; schur_sub_part < n_subparts_per_part - 1; ++schur_sub_part) {
4924 const local_ordinal_type r0 = part2packrowidx0_sub(partidx, 2 * schur_sub_part + 1);
4925 for (local_ordinal_type i = 0; i < 2; ++i) {
4926 copy3DView<local_ordinal_type>(member,
4927 Kokkos::subview(X_internal_vector_values, r0 + i, Kokkos::ALL(), active_schur_solve_vec, Kokkos::ALL()),
4928 Kokkos::subview(X_internal_vector_values_schur, r0_schur + 2 * schur_sub_part + i, Kokkos::ALL(), Kokkos::ALL()));
4934 KOKKOS_INLINE_FUNCTION
void
4935 operator()(
const SingleVectorApplyETag<B> &,
const member_type &member)
const {
4936 const local_ordinal_type packidx = packindices_sub(member.league_rank());
4938 const local_ordinal_type subpartidx = packptr_sub(packidx);
4939 const local_ordinal_type n_parts = part2packrowidx0_sub.extent(0);
4940 const local_ordinal_type local_subpartidx = subpartidx / n_parts;
4941 const local_ordinal_type partidx = subpartidx % n_parts;
4942 const local_ordinal_type blocksize = e_internal_vector_values.extent(2);
4944 const local_ordinal_type r0 = part2packrowidx0_sub(partidx, local_subpartidx);
4945 const local_ordinal_type nrows = partptr_sub(subpartidx, 1) - partptr_sub(subpartidx, 0);
4949 const auto one = KokkosKernels::ArithTraits<btdm_magnitude_type>::one();
4951 typedef SolveTridiagsDefaultModeAndAlgo<typename execution_space::memory_space> default_mode_and_algo_type;
4953 typedef typename default_mode_and_algo_type::mode_type default_mode_type;
4954 typedef typename default_mode_and_algo_type::single_vector_algo_type default_algo_type;
4956 if (local_subpartidx == 0) {
4957 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size), [&](
const int &v) {
4958 auto v_2 = Kokkos::subview(X_internal_vector_values, r0 + nrows, Kokkos::ALL(), active_schur_solve_vec, v);
4960 for (local_ordinal_type row = 0; row < nrows; ++row) {
4961 auto v_1 = Kokkos::subview(X_internal_vector_values, r0 + row, Kokkos::ALL(), active_schur_solve_vec, v);
4962 auto E = Kokkos::subview(e_internal_vector_values, 0, r0 + row, Kokkos::ALL(), Kokkos::ALL(), v);
4964 KOKKOSBATCHED_GEMV_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4966 blocksize, blocksize,
4968 E.data(), E.stride(0), E.stride(1),
4969 v_2.data(), v_2.stride(0),
4971 v_1.data(), v_1.stride(0));
4974 }
else if (local_subpartidx == (local_ordinal_type)part2packrowidx0_sub.extent(1) - 2) {
4975 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size), [&](
const int &v) {
4976 auto v_2 = Kokkos::subview(X_internal_vector_values, r0 - 1, Kokkos::ALL(), active_schur_solve_vec, v);
4978 for (local_ordinal_type row = 0; row < nrows; ++row) {
4979 auto v_1 = Kokkos::subview(X_internal_vector_values, r0 + row, Kokkos::ALL(), active_schur_solve_vec, v);
4980 auto E = Kokkos::subview(e_internal_vector_values, 1, r0 + row, Kokkos::ALL(), Kokkos::ALL(), v);
4982 KOKKOSBATCHED_GEMV_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
4984 blocksize, blocksize,
4986 E.data(), E.stride(0), E.stride(1),
4987 v_2.data(), v_2.stride(0),
4989 v_1.data(), v_1.stride(0));
4993 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size), [&](
const int &v) {
4995 auto v_2 = Kokkos::subview(X_internal_vector_values, r0 + nrows, Kokkos::ALL(), active_schur_solve_vec, v);
4997 for (local_ordinal_type row = 0; row < nrows; ++row) {
4998 auto v_1 = Kokkos::subview(X_internal_vector_values, r0 + row, Kokkos::ALL(), active_schur_solve_vec, v);
4999 auto E = Kokkos::subview(e_internal_vector_values, 0, r0 + row, Kokkos::ALL(), Kokkos::ALL(), v);
5001 KOKKOSBATCHED_GEMV_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
5003 blocksize, blocksize,
5005 E.data(), E.stride(0), E.stride(1),
5006 v_2.data(), v_2.stride(0),
5008 v_1.data(), v_1.stride(0));
5012 auto v_2 = Kokkos::subview(X_internal_vector_values, r0 - 1, Kokkos::ALL(), active_schur_solve_vec, v);
5014 for (local_ordinal_type row = 0; row < nrows; ++row) {
5015 auto v_1 = Kokkos::subview(X_internal_vector_values, r0 + row, Kokkos::ALL(), active_schur_solve_vec, v);
5016 auto E = Kokkos::subview(e_internal_vector_values, 1, r0 + row, Kokkos::ALL(), Kokkos::ALL(), v);
5018 KOKKOSBATCHED_GEMV_NO_TRANSPOSE_INTERNAL_INVOKE(default_mode_type, default_algo_type,
5020 blocksize, blocksize,
5022 E.data(), E.stride(0), E.stride(1),
5023 v_2.data(), v_2.stride(0),
5025 v_1.data(), v_1.stride(0));
5033 KOKKOS_INLINE_FUNCTION
void
5034 operator()(
const CopyVectorToFlatTag<B> &,
const member_type &member)
const {
5035 const local_ordinal_type packidx = member.league_rank();
5036 const local_ordinal_type partidx = packptr(packidx);
5037 const local_ordinal_type npacks = packptr(packidx + 1) - partidx;
5038 const local_ordinal_type pri0 = part2packrowidx0(partidx);
5039 const local_ordinal_type blocksize = (B == 0 ? D_internal_vector_values.extent(1) : B);
5040 const local_ordinal_type num_vectors = X_internal_vector_values.extent(2);
5042 Kokkos::parallel_for(Kokkos::ThreadVectorRange(member, vector_loop_size), [&](
const int &v) {
5043 copyToFlatMultiVector(member, partidx, npacks, pri0, v, blocksize, num_vectors);
5047 void run(
const impl_scalar_type_2d_view_tpetra &Y,
5048 const impl_scalar_type_1d_view &Z) {
5049 IFPACK2_BLOCKTRIDICONTAINER_PROFILER_REGION_BEGIN;
5050 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::SolveTridiags", SolveTridiags);
5053 this->Y_scalar_multivector = Y;
5054 this->Z_scalar_vector = Z;
5056 const local_ordinal_type num_vectors = X_internal_vector_values.extent(2);
5057 const local_ordinal_type blocksize = D_internal_vector_values.extent(1);
5059 const local_ordinal_type team_size =
5060 SolveTridiagsDefaultModeAndAlgo<typename execution_space::memory_space>::
5061 recommended_team_size(blocksize, vector_length, internal_vector_length);
5062 const int per_team_scratch = internal_vector_scratch_type_3d_view ::shmem_size(blocksize, num_vectors, vector_loop_size);
5063 const int max_scratch = team_policy_type::scratch_size_max(0);
5065#define BLOCKTRIDICONTAINER_DETAILS_SOLVETRIDIAGS(B) \
5066 if (packindices_schur.extent(1) <= 0) { \
5067 if (num_vectors == 1) { \
5068 if (per_team_scratch < max_scratch) { \
5069 Kokkos::TeamPolicy<execution_space> \
5070 policy(packptr.extent(0) - 1, team_size, vector_loop_size); \
5071 policy.set_scratch_size(0, Kokkos::PerTeam(per_team_scratch)); \
5072 Kokkos::parallel_for("SolveTridiags::TeamPolicy::run<SingleVector,0>", \
5074 SingleVectorFunctor<B, 0>(*this)); \
5076 Kokkos::TeamPolicy<execution_space> \
5077 policy(packptr.extent(0) - 1, team_size, vector_loop_size); \
5078 policy.set_scratch_size(1, Kokkos::PerTeam(per_team_scratch)); \
5079 Kokkos::parallel_for("SolveTridiags::TeamPolicy::run<SingleVector,1>", \
5081 SingleVectorFunctor<B, 1>(*this)); \
5084 if (per_team_scratch < max_scratch) { \
5085 Kokkos::TeamPolicy<execution_space, MultiVectorTag<B, 0>> \
5086 policy(packptr.extent(0) - 1, team_size, vector_loop_size); \
5087 policy.set_scratch_size(0, Kokkos::PerTeam(per_team_scratch)); \
5088 Kokkos::parallel_for("SolveTridiags::TeamPolicy::run<MultiVector,0>", \
5091 Kokkos::TeamPolicy<execution_space, MultiVectorTag<B, 1>> \
5092 policy(packptr.extent(0) - 1, team_size, vector_loop_size); \
5093 policy.set_scratch_size(1, Kokkos::PerTeam(per_team_scratch)); \
5094 Kokkos::parallel_for("SolveTridiags::TeamPolicy::run<MultiVector,1>", \
5099 Kokkos::deep_copy(execution_space(), Z, impl_scalar_type(0)); \
5100 for (local_ordinal_type vec = 0; vec < num_vectors; vec++) { \
5101 this->active_schur_solve_vec = vec; \
5103 IFPACK2_BLOCKHELPER_TIMER("BlockTriDi::ApplyInverseJacobi::SingleVectorSubLineTag", SingleVectorSubLineTag0); \
5104 write4DMultiVectorValuesToFile(part2packrowidx0_sub.extent(0), X_internal_scalar_values, "x_scalar_values_before_SingleVectorSubLineTag.mm"); \
5105 if (per_team_scratch < max_scratch) { \
5106 Kokkos::TeamPolicy<execution_space, SingleVectorSubLineTag<B, 0>> \
5107 policy(packindices_sub.extent(0), team_size, vector_loop_size); \
5108 policy.set_scratch_size(0, Kokkos::PerTeam(per_team_scratch)); \
5109 Kokkos::parallel_for("SolveTridiags::TeamPolicy::run<SingleVector,0>", \
5112 Kokkos::TeamPolicy<execution_space, SingleVectorSubLineTag<B, 1>> \
5113 policy(packindices_sub.extent(0), team_size, vector_loop_size); \
5114 policy.set_scratch_size(1, Kokkos::PerTeam(per_team_scratch)); \
5115 Kokkos::parallel_for("SolveTridiags::TeamPolicy::run<SingleVector,1>", \
5118 write4DMultiVectorValuesToFile(part2packrowidx0_sub.extent(0), X_internal_scalar_values, "x_scalar_values_after_SingleVectorSubLineTag.mm"); \
5119 IFPACK2_BLOCKHELPER_TIMER_FENCE(execution_space) \
5122 IFPACK2_BLOCKHELPER_TIMER("BlockTriDi::ApplyInverseJacobi::SingleVectorApplyCTag", SingleVectorApplyCTag0); \
5123 write4DMultiVectorValuesToFile(part2packrowidx0_sub.extent(0), X_internal_scalar_values, "x_scalar_values_before_SingleVectorApplyCTag.mm"); \
5124 Kokkos::TeamPolicy<execution_space, SingleVectorApplyCTag<B>> \
5125 policy(packindices_sub.extent(0), team_size, vector_loop_size); \
5126 Kokkos::parallel_for("SolveTridiags::TeamPolicy::run<SingleVector>", \
5128 write4DMultiVectorValuesToFile(part2packrowidx0_sub.extent(0), X_internal_scalar_values, "x_scalar_values_after_SingleVectorApplyCTag.mm"); \
5129 IFPACK2_BLOCKHELPER_TIMER_FENCE(execution_space) \
5132 IFPACK2_BLOCKHELPER_TIMER("BlockTriDi::ApplyInverseJacobi::SingleVectorSchurTag", SingleVectorSchurTag0); \
5133 write4DMultiVectorValuesToFile(part2packrowidx0_sub.extent(0), X_internal_scalar_values, "x_scalar_values_before_SingleVectorSchurTag.mm"); \
5134 if (per_team_scratch < max_scratch) { \
5135 Kokkos::TeamPolicy<execution_space, SingleVectorSchurTag<B, 0>> \
5136 policy(packindices_schur.extent(0), team_size, vector_loop_size); \
5137 policy.set_scratch_size(0, Kokkos::PerTeam(per_team_scratch)); \
5138 Kokkos::parallel_for("SolveTridiags::TeamPolicy::run<SingleVector,0>", \
5141 Kokkos::TeamPolicy<execution_space, SingleVectorSchurTag<B, 1>> \
5142 policy(packindices_schur.extent(0), team_size, vector_loop_size); \
5143 policy.set_scratch_size(1, Kokkos::PerTeam(per_team_scratch)); \
5144 Kokkos::parallel_for("SolveTridiags::TeamPolicy::run<SingleVector,1>", \
5147 write4DMultiVectorValuesToFile(part2packrowidx0_sub.extent(0), X_internal_scalar_values, "x_scalar_values_after_SingleVectorSchurTag.mm"); \
5148 IFPACK2_BLOCKHELPER_TIMER_FENCE(execution_space) \
5151 IFPACK2_BLOCKHELPER_TIMER("BlockTriDi::ApplyInverseJacobi::SingleVectorApplyETag", SingleVectorApplyETag0); \
5152 write4DMultiVectorValuesToFile(part2packrowidx0_sub.extent(0), X_internal_scalar_values, "x_scalar_values_before_SingleVectorApplyETag.mm"); \
5153 Kokkos::TeamPolicy<execution_space, SingleVectorApplyETag<B>> \
5154 policy(packindices_sub.extent(0), team_size, vector_loop_size); \
5155 Kokkos::parallel_for("SolveTridiags::TeamPolicy::run<SingleVector>", \
5157 write4DMultiVectorValuesToFile(part2packrowidx0_sub.extent(0), X_internal_scalar_values, "x_scalar_values_after_SingleVectorApplyETag.mm"); \
5158 IFPACK2_BLOCKHELPER_TIMER_FENCE(execution_space) \
5162 Kokkos::TeamPolicy<execution_space, CopyVectorToFlatTag<B>> \
5163 policy(packptr.extent(0) - 1, team_size, vector_loop_size); \
5164 Kokkos::parallel_for("SolveTridiags::TeamPolicy::run<CopyVectorToFlatTag>", \
5169 switch (blocksize) {
5170 case 3: BLOCKTRIDICONTAINER_DETAILS_SOLVETRIDIAGS(3);
5171 case 5: BLOCKTRIDICONTAINER_DETAILS_SOLVETRIDIAGS(5);
5172 case 6: BLOCKTRIDICONTAINER_DETAILS_SOLVETRIDIAGS(6);
5173 case 7: BLOCKTRIDICONTAINER_DETAILS_SOLVETRIDIAGS(7);
5174 case 10: BLOCKTRIDICONTAINER_DETAILS_SOLVETRIDIAGS(10);
5175 case 11: BLOCKTRIDICONTAINER_DETAILS_SOLVETRIDIAGS(11);
5176 case 12: BLOCKTRIDICONTAINER_DETAILS_SOLVETRIDIAGS(12);
5177 case 13: BLOCKTRIDICONTAINER_DETAILS_SOLVETRIDIAGS(13);
5178 case 16: BLOCKTRIDICONTAINER_DETAILS_SOLVETRIDIAGS(16);
5179 case 17: BLOCKTRIDICONTAINER_DETAILS_SOLVETRIDIAGS(17);
5180 case 18: BLOCKTRIDICONTAINER_DETAILS_SOLVETRIDIAGS(18);
5181 case 19: BLOCKTRIDICONTAINER_DETAILS_SOLVETRIDIAGS(19);
5182 default: BLOCKTRIDICONTAINER_DETAILS_SOLVETRIDIAGS(0);
5184#undef BLOCKTRIDICONTAINER_DETAILS_SOLVETRIDIAGS
5186 IFPACK2_BLOCKTRIDICONTAINER_PROFILER_REGION_END;
5187 IFPACK2_BLOCKHELPER_TIMER_FENCE(execution_space)
5194template <
typename MatrixType>
5195int applyInverseJacobi(
5196 const Teuchos::RCP<
const typename BlockHelperDetails::ImplType<MatrixType>::tpetra_row_matrix_type> &A,
5197 const Teuchos::RCP<
const typename BlockHelperDetails::ImplType<MatrixType>::tpetra_crs_graph_type> &G,
5198 const Teuchos::RCP<
const typename BlockHelperDetails::ImplType<MatrixType>::tpetra_import_type> &tpetra_importer,
5199 const Teuchos::RCP<AsyncableImport<MatrixType>> &async_importer,
5200 const bool overlap_communication_and_computation,
5202 const typename BlockHelperDetails::ImplType<MatrixType>::tpetra_multivector_type &X,
5203 typename BlockHelperDetails::ImplType<MatrixType>::tpetra_multivector_type &Y,
5204 typename BlockHelperDetails::ImplType<MatrixType>::tpetra_multivector_type &Z,
5205 typename BlockHelperDetails::ImplType<MatrixType>::impl_scalar_type_1d_view &W,
5207 const BlockHelperDetails::PartInterface<MatrixType> &interf,
5208 const BlockTridiags<MatrixType> &btdm,
5210 typename BlockHelperDetails::ImplType<MatrixType>::vector_type_1d_view &work,
5215 const int max_num_sweeps,
5216 const typename BlockHelperDetails::ImplType<MatrixType>::magnitude_type tol,
5217 const int check_tol_every) {
5218 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::ApplyInverseJacobi", ApplyInverseJacobi);
5221 using node_memory_space =
typename impl_type::node_memory_space;
5223 using size_type =
typename impl_type::size_type;
5224 using impl_scalar_type =
typename impl_type::impl_scalar_type;
5225 using magnitude_type =
typename impl_type::magnitude_type;
5226 using local_ordinal_type_1d_view =
typename impl_type::local_ordinal_type_1d_view;
5227 using vector_type_1d_view =
typename impl_type::vector_type_1d_view;
5228 using vector_type_3d_view =
typename impl_type::vector_type_3d_view;
5229 using tpetra_multivector_type =
typename impl_type::tpetra_multivector_type;
5231 using impl_scalar_type_1d_view =
typename impl_type::impl_scalar_type_1d_view;
5234 TEUCHOS_TEST_FOR_EXCEPT_MSG(!tpetra_importer.is_null() && !async_importer.is_null(),
5235 "Neither Tpetra importer nor Async importer is null.");
5237 TEUCHOS_TEST_FOR_EXCEPT_MSG(max_num_sweeps <= 0,
5238 "Maximum number of sweeps must be >= 1.");
5241 const bool is_seq_method_requested = !tpetra_importer.is_null();
5242 const bool is_async_importer_active = !async_importer.is_null();
5243 const bool is_norm_manager_active = tol > KokkosKernels::ArithTraits<magnitude_type>::zero();
5244 const magnitude_type tolerance = tol * tol;
5249 const impl_scalar_type zero(0.0);
5251 TEUCHOS_TEST_FOR_EXCEPT_MSG(is_norm_manager_active && is_seq_method_requested,
5252 "The seq method for applyInverseJacobi, "
5253 <<
"which in any case is for developer use only, "
5254 <<
"does not support norm-based termination.");
5255 const bool device_accessible_from_host = Kokkos::SpaceAccessibility<
5256 Kokkos::DefaultHostExecutionSpace, node_memory_space>::accessible;
5257 TEUCHOS_TEST_FOR_EXCEPTION(is_seq_method_requested && !device_accessible_from_host,
5258 std::invalid_argument,
5259 "The seq method for applyInverseJacobi, "
5260 <<
"which in any case is for developer use only, "
5261 <<
"only supports memory spaces accessible from host.");
5264 const size_type work_span_required = num_blockrows * num_vectors * blocksize;
5265 if (work.span() < work_span_required)
5266 work = vector_type_1d_view(
"vector workspace 1d view", work_span_required);
5271 W = impl_scalar_type_1d_view(
"W", W_size);
5273 typename impl_type::impl_scalar_type_2d_view_tpetra remote_multivector;
5275 if (is_seq_method_requested) {
5276 if (Z.getNumVectors() != Y.getNumVectors())
5277 Z = tpetra_multivector_type(tpetra_importer->getTargetMap(), num_vectors,
false);
5279 if (is_async_importer_active) {
5281 async_importer->createDataBuffer(num_vectors);
5282 remote_multivector = async_importer->getRemoteMultiVectorLocalView();
5288 vector_type_3d_view pmv(work.data(), num_blockrows, blocksize, num_vectors);
5289 const auto XX = X.getLocalViewDevice(Tpetra::Access::ReadOnly);
5290 const auto YY = Y.getLocalViewDevice(Tpetra::Access::ReadWrite);
5291 const auto ZZ = Z.getLocalViewDevice(Tpetra::Access::ReadWrite);
5292 if (is_y_zero) Kokkos::deep_copy(YY, zero);
5294 MultiVectorConverter<MatrixType> multivector_converter(interf, pmv);
5295 SolveTridiags<MatrixType> solve_tridiags(interf, btdm, pmv,
5296 damping_factor, is_norm_manager_active);
5298 const local_ordinal_type_1d_view dummy_local_ordinal_type_1d_view;
5300 auto A_crs = Teuchos::rcp_dynamic_cast<const typename impl_type::tpetra_crs_matrix_type>(A);
5301 auto A_bcrs = Teuchos::rcp_dynamic_cast<const typename impl_type::tpetra_block_crs_matrix_type>(A);
5303 bool hasBlockCrsMatrix = !A_bcrs.is_null();
5306 const auto g = hasBlockCrsMatrix ? A_bcrs->getCrsGraph() : *(A_crs->getCrsGraph());
5308 BlockHelperDetails::ComputeResidualVector<MatrixType>
5309 compute_residual_vector(amd, G->getLocalGraphDevice(), g.getLocalGraphDevice(), blocksize, interf,
5310 is_async_importer_active ? async_importer->dm2cm : dummy_local_ordinal_type_1d_view,
5314 if (is_norm_manager_active)
5315 norm_manager.setCheckFrequency(check_tol_every);
5319 for (; sweep < max_num_sweeps; ++sweep) {
5323 multivector_converter.run(XX);
5325 if (is_seq_method_requested) {
5329 Z.doImport(Y, *tpetra_importer, Tpetra::REPLACE);
5330 compute_residual_vector.run(YY, XX, ZZ);
5333 multivector_converter.run(YY);
5337 if (overlap_communication_and_computation || !is_async_importer_active) {
5338 if (is_async_importer_active) async_importer->asyncSendRecv(YY);
5340 compute_residual_vector.run(pmv, XX, YY, remote_multivector,
true);
5341 if (is_norm_manager_active && norm_manager.checkDone(sweep, tolerance)) {
5342 if (is_async_importer_active) async_importer->cancel();
5345 if (is_async_importer_active) {
5346 async_importer->syncRecv();
5348 compute_residual_vector.run(pmv, XX, YY, remote_multivector,
false);
5351 if (is_async_importer_active)
5352 async_importer->syncExchange(YY);
5353 if (is_norm_manager_active && norm_manager.checkDone(sweep, tolerance))
break;
5355 compute_residual_vector.run(pmv, XX, YY, remote_multivector);
5363 solve_tridiags.run(YY, W);
5366 if (is_norm_manager_active) {
5368 BlockHelperDetails::reduceVector<MatrixType>(W, norm_manager.getBuffer());
5369 if (sweep + 1 == max_num_sweeps) {
5370 norm_manager.ireduce(sweep,
true);
5371 norm_manager.checkDone(sweep + 1, tolerance,
true);
5373 norm_manager.ireduce(sweep);
5381 if (is_norm_manager_active) norm_manager.finalize();
5389template <
typename MatrixType>
5390int applyFusedBlockJacobi(
5391 const Teuchos::RCP<
const typename BlockHelperDetails::ImplType<MatrixType>::tpetra_import_type> &tpetra_importer,
5392 const Teuchos::RCP<AsyncableImport<MatrixType>> &async_importer,
5393 const bool overlap_communication_and_computation,
5395 const typename BlockHelperDetails::ImplType<MatrixType>::tpetra_multivector_type &X,
5396 typename BlockHelperDetails::ImplType<MatrixType>::tpetra_multivector_type &Y,
5397 typename BlockHelperDetails::ImplType<MatrixType>::impl_scalar_type_1d_view &W,
5399 const BlockHelperDetails::PartInterface<MatrixType> &interf,
5400 const BlockTridiags<MatrixType> &btdm,
5402 typename BlockHelperDetails::ImplType<MatrixType>::impl_scalar_type_1d_view &work,
5407 const int max_num_sweeps,
5408 const typename BlockHelperDetails::ImplType<MatrixType>::magnitude_type tol,
5409 const int check_tol_every) {
5412 using size_type =
typename impl_type::size_type;
5413 using magnitude_type =
typename impl_type::magnitude_type;
5414 using impl_scalar_type_1d_view =
typename impl_type::impl_scalar_type_1d_view;
5415 using impl_scalar_type_2d_view_tpetra =
typename impl_type::impl_scalar_type_2d_view_tpetra;
5417 IFPACK2_BLOCKHELPER_TIMER(
"BlockTriDi::ApplyFusedBlockJacobi", ApplyFusedBlockJacobi);
5420 TEUCHOS_TEST_FOR_EXCEPT_MSG(!tpetra_importer.is_null() && !async_importer.is_null(),
5421 "Neither Tpetra importer nor Async importer is null.");
5423 TEUCHOS_TEST_FOR_EXCEPT_MSG(max_num_sweeps <= 0,
5424 "Maximum number of sweeps must be >= 1.");
5427 const bool is_async_importer_active = !async_importer.is_null();
5428 const bool is_norm_manager_active = tol > KokkosKernels::ArithTraits<magnitude_type>::zero();
5429 const magnitude_type tolerance = tol * tol;
5434 typename impl_type::impl_scalar_type_2d_view_tpetra remote_multivector;
5436 if (is_async_importer_active) {
5438 async_importer->createDataBuffer(num_vectors);
5439 remote_multivector = async_importer->getRemoteMultiVectorLocalView();
5443 const auto XX = X.getLocalViewDevice(Tpetra::Access::ReadOnly);
5444 const auto YY = Y.getLocalViewDevice(Tpetra::Access::ReadWrite);
5446 const bool two_pass_residual =
5447 overlap_communication_and_computation && is_async_importer_active;
5451 TEUCHOS_TEST_FOR_EXCEPT_MSG(
5452 size_t(num_blockrows) * blocksize * num_vectors != YY.extent(0) * YY.extent(1),
5453 "Local LHS vector (YY) has total size " << YY.extent(0) <<
"x" << YY.extent(1) <<
" = " << YY.extent(0) * YY.extent(1) <<
",\n"
5454 <<
"but expected " << num_blockrows <<
"x" << blocksize <<
"x" << num_vectors <<
" = " <<
size_t(num_blockrows) * blocksize * num_vectors <<
'\n');
5455 size_type work_required = size_type(num_blockrows) * blocksize * num_vectors;
5456 if (work.extent(0) < work_required) {
5457 work = impl_scalar_type_1d_view(do_not_initialize_tag(
"flat workspace 1d view"), work_required);
5460 Unmanaged<impl_scalar_type_2d_view_tpetra> y_doublebuf(work.data(), num_blockrows * blocksize, num_vectors);
5463 if (W.extent(0) !=
size_t(num_blockrows))
5464 W = impl_scalar_type_1d_view(do_not_initialize_tag(
"W"), num_blockrows);
5466 BlockHelperDetails::ComputeResidualAndSolve<MatrixType>
5467 residualAndSolve(amd, btdm.d_inv, W, blocksize, damping_factor);
5470 if (is_norm_manager_active)
5471 norm_manager.setCheckFrequency(check_tol_every);
5476 Unmanaged<impl_scalar_type_2d_view_tpetra> y_buffers[2] = {YY, y_doublebuf};
5481 for (; sweep < max_num_sweeps; ++sweep) {
5484 residualAndSolve.run_y_zero(XX, y_buffers[1 - current_y]);
5487 if (overlap_communication_and_computation || !is_async_importer_active) {
5488 if (is_async_importer_active) async_importer->asyncSendRecv(y_buffers[current_y]);
5489 if (two_pass_residual) {
5492 residualAndSolve.run_pass1_of_2(XX, y_buffers[current_y], y_buffers[1 - current_y]);
5496 residualAndSolve.run_single_pass(XX, y_buffers[current_y], remote_multivector, y_buffers[1 - current_y]);
5498 if (is_norm_manager_active && norm_manager.checkDone(sweep, tolerance)) {
5499 if (is_async_importer_active) async_importer->cancel();
5502 if (is_async_importer_active) {
5503 async_importer->syncRecv();
5505 residualAndSolve.run_pass2_of_2(y_buffers[current_y], remote_multivector, y_buffers[1 - current_y]);
5508 if (is_async_importer_active)
5509 async_importer->syncExchange(y_buffers[current_y]);
5510 if (is_norm_manager_active && norm_manager.checkDone(sweep, tolerance))
break;
5512 residualAndSolve.run_single_pass(XX, y_buffers[current_y], remote_multivector, y_buffers[1 - current_y]);
5517 if (is_norm_manager_active) {
5518 BlockHelperDetails::reduceVector<MatrixType>(W, norm_manager.getBuffer());
5519 if (sweep + 1 == max_num_sweeps) {
5520 norm_manager.ireduce(sweep,
true);
5521 norm_manager.checkDone(sweep + 1, tolerance,
true);
5523 norm_manager.ireduce(sweep);
5528 current_y = 1 - current_y;
5530 if (current_y == 1) {
5532 Kokkos::deep_copy(YY, y_doublebuf);
5536 if (is_norm_manager_active) norm_manager.finalize();
5541template <
typename MatrixType>
5544 using part_interface_type = BlockHelperDetails::PartInterface<MatrixType>;
5548 using async_import_type = AsyncableImport<MatrixType>;
5551 Teuchos::RCP<const typename impl_type::tpetra_row_matrix_type> A;
5552 Teuchos::RCP<const typename impl_type::tpetra_crs_graph_type> blockGraph;
5553 Teuchos::RCP<const typename impl_type::tpetra_import_type> tpetra_importer;
5554 Teuchos::RCP<async_import_type> async_importer;
5555 bool overlap_communication_and_computation;
5558 mutable typename impl_type::tpetra_multivector_type Z;
5559 mutable typename impl_type::impl_scalar_type_1d_view W;
5562 part_interface_type part_interface;
5563 block_tridiags_type block_tridiags;
5567 bool use_fused_jacobi;
5570 mutable typename impl_type::vector_type_1d_view work;
5572 mutable typename impl_type::impl_scalar_type_1d_view work_flat;
5573 mutable norm_manager_type norm_manager;
Preconditioners and smoothers for Tpetra sparse matrices.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:40
Definition Ifpack2_BlockHelper.hpp:421
Definition Ifpack2_BlockHelper.hpp:313
size_t size_type
Definition Ifpack2_BlockHelper.hpp:317
KokkosKernels::ArithTraits< scalar_type >::val_type impl_scalar_type
Definition Ifpack2_BlockHelper.hpp:327
Kokkos::View< size_type *, device_type > size_type_1d_view
Definition Ifpack2_BlockHelper.hpp:390
Definition Ifpack2_BlockHelper.hpp:519
Definition Ifpack2_BlockTriDiContainer_impl.hpp:140
Definition Ifpack2_BlockTriDiContainer_impl.hpp:1605
Definition Ifpack2_BlockTriDiContainer_impl.hpp:2293
forward declaration
Definition Ifpack2_BlockTriDiContainer_impl.hpp:5542