-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for create_mirror[_view][_and_copy] functions
- Loading branch information
1 parent
6e88f6b
commit 9c71300
Showing
2 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
// Copyright (C) The DDC development team, see COPYRIGHT.md file | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include <ddc/ddc.hpp> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace DDC_HIP_5_7_ANONYMOUS_NAMESPACE_WORKAROUND(CHUNK_CPP) | ||
{ | ||
struct DDimX | ||
{ | ||
}; | ||
using DElemX = ddc::DiscreteElement<DDimX>; | ||
using DVectX = ddc::DiscreteVector<DDimX>; | ||
using DDomX = ddc::DiscreteDomain<DDimX>; | ||
|
||
static DElemX constexpr lbound_x(50); | ||
static DVectX constexpr nelems_x(3); | ||
static DDomX constexpr dom_x(lbound_x, nelems_x); | ||
|
||
template <class ChunkSpan, class T> | ||
[[nodiscard]] bool all_equal_to(ChunkSpan const chunk_span, T const t) | ||
{ | ||
static_assert(ddc::is_borrowed_chunk_v<ChunkSpan>); | ||
return ddc::parallel_transform_reduce( | ||
"all_equal_to", | ||
typename ChunkSpan::memory_space::execution_space(), | ||
chunk_span.domain(), | ||
true, | ||
ddc::reducer::land<bool>(), | ||
KOKKOS_LAMBDA(DElemX elem_x) { return chunk_span(elem_x) == t; }); | ||
} | ||
|
||
} // namespace ) | ||
|
||
TEST(CreateMirror, Host) | ||
{ | ||
ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator<int>()); | ||
ddc::parallel_fill(chunk, 3); | ||
auto mirror = ddc::create_mirror(chunk.span_cview()); | ||
EXPECT_EQ(chunk.domain(), mirror.domain()); | ||
EXPECT_NE(chunk.data_handle(), mirror.data_handle()); | ||
EXPECT_TRUE((std::is_same_v<decltype(mirror)::memory_space, Kokkos::HostSpace>)); | ||
} | ||
|
||
TEST(CreateMirror, Device) | ||
{ | ||
ddc::Chunk chunk("chunk", dom_x, ddc::DeviceAllocator<int>()); | ||
ddc::parallel_fill(chunk, 3); | ||
auto mirror = ddc::create_mirror(chunk.span_cview()); | ||
EXPECT_EQ(chunk.domain(), mirror.domain()); | ||
EXPECT_NE(chunk.data_handle(), mirror.data_handle()); | ||
EXPECT_TRUE((std::is_same_v<decltype(mirror)::memory_space, Kokkos::HostSpace>)); | ||
} | ||
|
||
TEST(CreateMirrorWithExecutionSpace, HostToDevice) | ||
{ | ||
ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator<int>()); | ||
ddc::parallel_fill(chunk, 3); | ||
auto mirror = ddc::create_mirror(Kokkos::DefaultExecutionSpace(), chunk.span_cview()); | ||
EXPECT_EQ(chunk.domain(), mirror.domain()); | ||
EXPECT_NE(chunk.data_handle(), mirror.data_handle()); | ||
EXPECT_TRUE((std::is_same_v< | ||
decltype(mirror)::memory_space, | ||
Kokkos::DefaultExecutionSpace::memory_space>)); | ||
} | ||
|
||
TEST(CreateMirrorAndCopy, Host) | ||
{ | ||
ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator<int>()); | ||
ddc::parallel_fill(chunk, 3); | ||
auto mirror = ddc::create_mirror_and_copy(chunk.span_cview()); | ||
EXPECT_EQ(chunk.domain(), mirror.domain()); | ||
EXPECT_NE(chunk.data_handle(), mirror.data_handle()); | ||
EXPECT_TRUE((std::is_same_v<decltype(mirror)::memory_space, Kokkos::HostSpace>)); | ||
EXPECT_TRUE(all_equal_to(mirror.span_cview(), 3)); | ||
} | ||
|
||
TEST(CreateMirrorAndCopy, Device) | ||
{ | ||
ddc::Chunk chunk("chunk", dom_x, ddc::DeviceAllocator<int>()); | ||
ddc::parallel_fill(chunk, 3); | ||
auto mirror = ddc::create_mirror_and_copy(chunk.span_cview()); | ||
EXPECT_EQ(chunk.domain(), mirror.domain()); | ||
EXPECT_NE(chunk.data_handle(), mirror.data_handle()); | ||
EXPECT_TRUE((std::is_same_v<decltype(mirror)::memory_space, Kokkos::HostSpace>)); | ||
EXPECT_TRUE(all_equal_to(mirror.span_cview(), 3)); | ||
} | ||
|
||
TEST(CreateMirrorAndCopyWithExecutionSpace, HostToDevice) | ||
{ | ||
ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator<int>()); | ||
ddc::parallel_fill(chunk, 3); | ||
auto mirror = ddc::create_mirror_and_copy(Kokkos::DefaultExecutionSpace(), chunk.span_cview()); | ||
EXPECT_EQ(chunk.domain(), mirror.domain()); | ||
EXPECT_NE(chunk.data_handle(), mirror.data_handle()); | ||
EXPECT_TRUE((std::is_same_v< | ||
decltype(mirror)::memory_space, | ||
Kokkos::DefaultExecutionSpace::memory_space>)); | ||
EXPECT_TRUE(all_equal_to(mirror.span_cview(), 3)); | ||
} | ||
|
||
TEST(CreateMirrorView, Host) | ||
{ | ||
ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator<int>()); | ||
ddc::parallel_fill(chunk, 3); | ||
auto mirror = ddc::create_mirror_view(chunk.span_view()); | ||
EXPECT_EQ(chunk.domain(), mirror.domain()); | ||
EXPECT_EQ(chunk.data_handle(), mirror.data_handle()); | ||
EXPECT_TRUE((std::is_same_v<decltype(mirror)::memory_space, Kokkos::HostSpace>)); | ||
} | ||
|
||
TEST(CreateMirrorView, Device) | ||
{ | ||
ddc::Chunk chunk("chunk", dom_x, ddc::DeviceAllocator<int>()); | ||
ddc::parallel_fill(chunk, 3); | ||
auto mirror = ddc::create_mirror_view(chunk.span_view()); | ||
EXPECT_EQ(chunk.domain(), mirror.domain()); | ||
if (Kokkos::SpaceAccessibility<Kokkos::HostSpace, decltype(chunk)::memory_space>::accessible) { | ||
EXPECT_EQ(chunk.data_handle(), mirror.data_handle()); | ||
} else { | ||
EXPECT_NE(chunk.data_handle(), mirror.data_handle()); | ||
} | ||
EXPECT_TRUE((std::is_same_v<decltype(mirror)::memory_space, Kokkos::HostSpace>)); | ||
} | ||
|
||
TEST(CreateMirrorViewWithExecutionSpace, HostToDevice) | ||
{ | ||
ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator<int>()); | ||
ddc::parallel_fill(chunk, 3); | ||
auto mirror = ddc::create_mirror_view(Kokkos::DefaultExecutionSpace(), chunk.span_view()); | ||
EXPECT_EQ(chunk.domain(), mirror.domain()); | ||
if (Kokkos::SpaceAccessibility<Kokkos::DefaultExecutionSpace, decltype(chunk)::memory_space>:: | ||
accessible) { | ||
EXPECT_EQ(chunk.data_handle(), mirror.data_handle()); | ||
} else { | ||
EXPECT_NE(chunk.data_handle(), mirror.data_handle()); | ||
} | ||
EXPECT_TRUE((std::is_same_v< | ||
decltype(mirror)::memory_space, | ||
Kokkos::DefaultExecutionSpace::memory_space>)); | ||
} | ||
|
||
TEST(CreateMirrorViewAndCopy, Host) | ||
{ | ||
ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator<int>()); | ||
ddc::parallel_fill(chunk, 3); | ||
auto mirror = ddc::create_mirror_view_and_copy(chunk.span_cview()); | ||
EXPECT_EQ(chunk.domain(), mirror.domain()); | ||
EXPECT_EQ(chunk.data_handle(), mirror.data_handle()); | ||
EXPECT_TRUE((std::is_same_v<decltype(mirror)::memory_space, Kokkos::HostSpace>)); | ||
EXPECT_TRUE(all_equal_to(mirror.span_cview(), 3)); | ||
} | ||
|
||
TEST(CreateMirrorViewAndCopy, Device) | ||
{ | ||
ddc::Chunk chunk("chunk", dom_x, ddc::DeviceAllocator<int>()); | ||
ddc::parallel_fill(chunk, 3); | ||
auto mirror = ddc::create_mirror_view_and_copy(chunk.span_cview()); | ||
EXPECT_EQ(chunk.domain(), mirror.domain()); | ||
if (Kokkos::SpaceAccessibility<Kokkos::HostSpace, decltype(chunk)::memory_space>::accessible) { | ||
EXPECT_EQ(chunk.data_handle(), mirror.data_handle()); | ||
} else { | ||
EXPECT_NE(chunk.data_handle(), mirror.data_handle()); | ||
} | ||
EXPECT_TRUE((std::is_same_v<decltype(mirror)::memory_space, Kokkos::HostSpace>)); | ||
EXPECT_TRUE(all_equal_to(mirror.span_cview(), 3)); | ||
} | ||
|
||
TEST(CreateMirrorViewAndCopyWithExecutionSpace, HostToDevice) | ||
{ | ||
ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator<int>()); | ||
ddc::parallel_fill(chunk, 3); | ||
auto mirror | ||
= ddc::create_mirror_view_and_copy(Kokkos::DefaultExecutionSpace(), chunk.span_cview()); | ||
EXPECT_EQ(chunk.domain(), mirror.domain()); | ||
if (Kokkos::SpaceAccessibility<Kokkos::DefaultExecutionSpace, decltype(chunk)::memory_space>:: | ||
accessible) { | ||
EXPECT_EQ(chunk.data_handle(), mirror.data_handle()); | ||
} else { | ||
EXPECT_NE(chunk.data_handle(), mirror.data_handle()); | ||
} | ||
EXPECT_TRUE((std::is_same_v< | ||
decltype(mirror)::memory_space, | ||
Kokkos::DefaultExecutionSpace::memory_space>)); | ||
EXPECT_TRUE(all_equal_to(mirror.span_cview(), 3)); | ||
} |