Skip to content

Commit a9de9d2

Browse files
authored
Support printing a layout stride (#1134)
1 parent 8823d4c commit a9de9d2

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

src/ddc/create_mirror.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,33 @@
1313

1414
namespace ddc {
1515

16+
namespace detail {
17+
18+
/**
19+
* @brief Ensure a layout_right view of a ChunkSpan, copying if necessary.
20+
*
21+
* If the input `src` already uses `Kokkos::layout_right`, it is returned as-is.
22+
* Otherwise, a new chunk with layout_right is allocated and a deep copy of
23+
* `src` is performed into it.
24+
*
25+
* @param[in] src Source ChunkSpan to adapt.
26+
* @return Either the original view (if already LayoutRight) or a copied chunk.
27+
*/
28+
template <class ElementType, class Support, class Layout, class MemorySpace>
29+
auto create_layout_right_view_and_copy(
30+
ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
31+
{
32+
if constexpr (std::is_same_v<Layout, Kokkos::layout_right>) {
33+
return src;
34+
} else {
35+
Chunk chunk(src.domain(), KokkosAllocator<std::remove_const_t<ElementType>, MemorySpace>());
36+
parallel_deepcopy(chunk, src);
37+
return chunk;
38+
}
39+
}
40+
41+
} // namespace detail
42+
1643
/// @param[in] space A Kokkos memory space or execution space.
1744
/// @param[in] src A layout right ChunkSpan.
1845
/// @return a `Chunk` with the same support and layout as `src` allocated on the `Space::memory_space` memory space.

src/ddc/print.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <Kokkos_Core.hpp>
1919

2020
#include "chunk_span.hpp"
21+
#include "create_mirror.hpp"
2122
#include "discrete_vector.hpp"
2223

2324
namespace ddc {
@@ -350,13 +351,16 @@ std::ostream& print_content(
350351
std::ostream& os,
351352
ChunkSpan<ElementType, SupportType, LayoutStridedPolicy, MemorySpace> const& chunk_span)
352353
{
353-
auto h_chunk_span = create_mirror_view_and_copy(Kokkos::HostSpace(), chunk_span);
354+
auto chunk_span_right = detail::create_layout_right_view_and_copy(chunk_span);
355+
auto chunk_span_right_host
356+
= create_mirror_view_and_copy(Kokkos::HostSpace(), chunk_span_right.span_view());
354357

355-
using chunkspan_type = std::remove_cv_t<std::remove_reference_t<decltype(h_chunk_span)>>;
358+
using chunkspan_type
359+
= std::remove_cv_t<std::remove_reference_t<decltype(chunk_span_right_host)>>;
356360
using mdspan_type = chunkspan_type::allocation_mdspan_type;
357361
using extents = mdspan_type::extents_type;
358362

359-
mdspan_type const allocated_mdspan = h_chunk_span.allocation_mdspan();
363+
mdspan_type const allocated_mdspan = chunk_span_right_host.allocation_mdspan();
360364

361365
ddc::detail::ChunkPrinter& printer = ddc::detail::ChunkPrinter::get_instance();
362366
std::scoped_lock const lock(printer.m_global_lock);

tests/print.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,16 @@ void TestPrintTestCheckOutput3d()
496496
" [0.12345 0.12345 0.12345]\n"
497497
" [0.12345 0.12345 0.12345]]]");
498498
}
499+
500+
{
501+
ddc::ChunkSpan const sub_chunk_span = chunk_span[domain_1.front()][domain_2.front()];
502+
ASSERT_TRUE((std::is_same_v<
503+
typename decltype(sub_chunk_span)::layout_type,
504+
Kokkos::layout_stride>));
505+
std::stringstream ss;
506+
print_content(ss, sub_chunk_span);
507+
EXPECT_EQ(ss.str(), "[0.12345 0.12345 0.12345]");
508+
}
499509
}
500510

501511
TEST(Print, CheckOutput3d)

0 commit comments

Comments
 (0)