|
| 1 | +// Copyright (C) The DDC development team, see COPYRIGHT.md file |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +#include <ddc/ddc.hpp> |
| 6 | + |
| 7 | +#include <gtest/gtest.h> |
| 8 | + |
| 9 | +#include <Kokkos_Core.hpp> |
| 10 | +#include <Kokkos_StdAlgorithms.hpp> |
| 11 | + |
| 12 | +#include "ddc/kokkos_allocator.hpp" |
| 13 | + |
| 14 | +inline namespace anonymous_namespace_workaround_parallel_copy_cpp { |
| 15 | + |
| 16 | +using DElem0D = ddc::DiscreteElement<>; |
| 17 | +using DVect0D = ddc::DiscreteVector<>; |
| 18 | +using DDom0D = ddc::DiscreteDomain<>; |
| 19 | + |
| 20 | +template <class Datatype> |
| 21 | +using Chunk0D = ddc::Chunk<Datatype, DDom0D>; |
| 22 | +template <class Datatype> |
| 23 | +using ChunkSpan0D = ddc::ChunkSpan<Datatype, DDom0D>; |
| 24 | + |
| 25 | + |
| 26 | +struct DDimX |
| 27 | +{ |
| 28 | +}; |
| 29 | +using DElemX = ddc::DiscreteElement<DDimX>; |
| 30 | +using DVectX = ddc::DiscreteVector<DDimX>; |
| 31 | +using DDomX = ddc::DiscreteDomain<DDimX>; |
| 32 | + |
| 33 | +template <class Datatype> |
| 34 | +using ChunkX = ddc::Chunk<Datatype, DDomX>; |
| 35 | + |
| 36 | + |
| 37 | +struct DDimY |
| 38 | +{ |
| 39 | +}; |
| 40 | +using DElemY = ddc::DiscreteElement<DDimY>; |
| 41 | +using DVectY = ddc::DiscreteVector<DDimY>; |
| 42 | +using DDomY = ddc::DiscreteDomain<DDimY>; |
| 43 | + |
| 44 | +template <class Datatype> |
| 45 | +using ChunkY = ddc::Chunk<Datatype, DDomY>; |
| 46 | + |
| 47 | + |
| 48 | +struct DDimZ |
| 49 | +{ |
| 50 | +}; |
| 51 | +using DElemZ = ddc::DiscreteElement<DDimZ>; |
| 52 | +using DVectZ = ddc::DiscreteVector<DDimZ>; |
| 53 | +using DDomZ = ddc::DiscreteDomain<DDimZ>; |
| 54 | + |
| 55 | + |
| 56 | +using DElemXY = ddc::DiscreteElement<DDimX, DDimY>; |
| 57 | +using DVectXY = ddc::DiscreteVector<DDimX, DDimY>; |
| 58 | +using DDomXY = ddc::DiscreteDomain<DDimX, DDimY>; |
| 59 | + |
| 60 | +template <class Datatype> |
| 61 | +using ChunkXY = ddc::Chunk<Datatype, DDomXY>; |
| 62 | + |
| 63 | + |
| 64 | +using DElemYX = ddc::DiscreteElement<DDimY, DDimX>; |
| 65 | +using DVectYX = ddc::DiscreteVector<DDimY, DDimX>; |
| 66 | +using DDomYX = ddc::DiscreteDomain<DDimY, DDimX>; |
| 67 | + |
| 68 | +template <class Datatype> |
| 69 | +using ChunkYX = ddc::Chunk<Datatype, DDomYX>; |
| 70 | + |
| 71 | + |
| 72 | +using DElemXYZ = ddc::DiscreteElement<DDimX, DDimY, DDimZ>; |
| 73 | +using DVectXYZ = ddc::DiscreteVector<DDimX, DDimY, DDimZ>; |
| 74 | +using DDomXYZ = ddc::DiscreteDomain<DDimX, DDimY, DDimZ>; |
| 75 | + |
| 76 | +DElem0D constexpr lbound_0d {}; |
| 77 | +DVect0D constexpr nelems_0d {}; |
| 78 | +DDom0D constexpr dom_0d(lbound_0d, nelems_0d); |
| 79 | + |
| 80 | +DElemX constexpr lbound_x = ddc::init_trivial_half_bounded_space<DDimX>(); |
| 81 | +DVectX constexpr nelems_x(3); |
| 82 | +DDomX constexpr dom_x(lbound_x, nelems_x); |
| 83 | + |
| 84 | +DElemY constexpr lbound_y = ddc::init_trivial_half_bounded_space<DDimY>(); |
| 85 | +DVectY constexpr nelems_y(12); |
| 86 | + |
| 87 | +DElemZ constexpr lbound_z = ddc::init_trivial_half_bounded_space<DDimZ>(); |
| 88 | +DVectZ constexpr nelems_z(5); |
| 89 | + |
| 90 | +DElemXY constexpr lbound_x_y(lbound_x, lbound_y); |
| 91 | +DVectXY constexpr nelems_x_y(nelems_x, nelems_y); |
| 92 | +DDomXY constexpr dom_x_y(lbound_x_y, nelems_x_y); |
| 93 | + |
| 94 | +DElemXYZ constexpr lbound_x_y_z(lbound_x, lbound_y, lbound_z); |
| 95 | +DVectXYZ constexpr nelems_x_y_z(nelems_x, nelems_y, nelems_z); |
| 96 | +DDomXYZ constexpr dom_x_y_z(lbound_x_y_z, nelems_x_y_z); |
| 97 | + |
| 98 | +} // namespace anonymous_namespace_workaround_parallel_copy_cpp |
| 99 | + |
| 100 | +TEST(ParallelCopy, BroadcastScalar2XY) |
| 101 | +{ |
| 102 | + Kokkos::DefaultExecutionSpace const exec_space; |
| 103 | + |
| 104 | + Kokkos::View<int*> const storage(Kokkos::view_alloc("storage", exec_space), dom_x_y.size()); |
| 105 | + ddc::ChunkSpan const |
| 106 | + chunk_x_y(Kokkos::View<int**>(storage.data(), nelems_x, nelems_y), dom_x_y); |
| 107 | + ddc::parallel_fill(exec_space, chunk_x_y, 0); |
| 108 | + |
| 109 | + ddc::Chunk chunk(dom_0d, ddc::DeviceAllocator<int>()); |
| 110 | + ddc::parallel_fill(exec_space, chunk, 1); |
| 111 | + |
| 112 | + ddc::parallel_copy(exec_space, chunk_x_y, chunk); |
| 113 | + |
| 114 | + EXPECT_EQ(Kokkos::Experimental::count(exec_space, storage, 1), dom_x_y.size()); |
| 115 | +} |
| 116 | + |
| 117 | +TEST(ParallelCopy, BroadcastX2XY) |
| 118 | +{ |
| 119 | + Kokkos::DefaultExecutionSpace const exec_space; |
| 120 | + |
| 121 | + Kokkos::View<int*> const storage(Kokkos::view_alloc("storage", exec_space), dom_x_y.size()); |
| 122 | + ddc::ChunkSpan const |
| 123 | + chunk_x_y(Kokkos::View<int**>(storage.data(), nelems_x, nelems_y), dom_x_y); |
| 124 | + ddc::parallel_fill(exec_space, chunk_x_y, 0); |
| 125 | + |
| 126 | + ddc::Chunk chunk_x(dom_x, ddc::DeviceAllocator<int>()); |
| 127 | + ddc::parallel_fill(exec_space, chunk_x, 1); |
| 128 | + |
| 129 | + ddc::parallel_copy(exec_space, chunk_x_y, chunk_x); |
| 130 | + |
| 131 | + EXPECT_EQ(Kokkos::Experimental::count(exec_space, storage, 1), dom_x_y.size()); |
| 132 | +} |
| 133 | + |
| 134 | +TEST(ParallelCopy, TransposeXY2XY) |
| 135 | +{ |
| 136 | + Kokkos::DefaultExecutionSpace const exec_space; |
| 137 | + |
| 138 | + Kokkos::View<int*> const storage(Kokkos::view_alloc("storage", exec_space), dom_x_y.size()); |
| 139 | + ddc::ChunkSpan const |
| 140 | + chunk_x_y_out(Kokkos::View<int**>(storage.data(), nelems_x, nelems_y), dom_x_y); |
| 141 | + ddc::parallel_fill(exec_space, chunk_x_y_out, 0); |
| 142 | + |
| 143 | + ddc::Chunk chunk_x_y_in(dom_x_y, ddc::DeviceAllocator<int>()); |
| 144 | + ddc::parallel_fill(exec_space, chunk_x_y_in, 1); |
| 145 | + |
| 146 | + ddc::parallel_copy(exec_space, chunk_x_y_out, chunk_x_y_in); |
| 147 | + |
| 148 | + EXPECT_EQ(Kokkos::Experimental::count(exec_space, storage, 1), dom_x_y.size()); |
| 149 | +} |
| 150 | + |
| 151 | +TEST(ParallelCopy, BroadcastAndTransposeYX2XYZ) |
| 152 | +{ |
| 153 | + Kokkos::DefaultExecutionSpace const exec_space; |
| 154 | + |
| 155 | + Kokkos::View<int*> const storage(Kokkos::view_alloc("storage", exec_space), dom_x_y_z.size()); |
| 156 | + ddc::ChunkSpan const chunk_x_y_z( |
| 157 | + Kokkos::View<int***>(storage.data(), nelems_x, nelems_y, nelems_z), |
| 158 | + dom_x_y_z); |
| 159 | + ddc::parallel_fill(exec_space, chunk_x_y_z, 0); |
| 160 | + |
| 161 | + DDomYX const dom_y_x(dom_x_y); |
| 162 | + ddc::Chunk chunk_y_x(dom_y_x, ddc::DeviceAllocator<int>()); |
| 163 | + ddc::parallel_fill(exec_space, chunk_y_x, 1); |
| 164 | + |
| 165 | + ddc::parallel_copy(exec_space, chunk_x_y_z, chunk_y_x); |
| 166 | + |
| 167 | + EXPECT_EQ(Kokkos::Experimental::count(exec_space, storage, 1), dom_x_y_z.size()); |
| 168 | +} |
0 commit comments