Skip to content

Commit

Permalink
Generalize serial_host to serial
Browse files Browse the repository at this point in the history
  • Loading branch information
tpadioleau committed Mar 7, 2024
1 parent 417bd90 commit e48bf68
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 6 deletions.
2 changes: 2 additions & 0 deletions include/ddc/ddc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "ddc/uniform_point_sampling.hpp"

// Algorithms
#include "ddc/deepcopy.hpp"
#include "ddc/fill.hpp"
#include "ddc/for_each.hpp"
#include "ddc/mirror.hpp"
#include "ddc/parallel_deepcopy.hpp"
Expand Down
48 changes: 48 additions & 0 deletions include/ddc/deepcopy.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: MIT

#pragma once

#include <type_traits>

#include <Kokkos_Core.hpp>

#include "ddc/chunk_common.hpp"
#include "ddc/chunk_span.hpp"

namespace ddc {

/** Copy the content of a chunk span into another
* @param[out] dst the chunk span in which to copy
* @param[in] src the chunk span from which to copy
* @return dst as a ChunkSpan
*/
template <
class ElementTypeDst,
class SupportDst,
class LayoutDst,
class MemorySpaceDst,
class ElementTypeSrc,
class SupportSrc,
class LayoutSrc,
class MemorySpaceSrc>
KOKKOS_FUNCTION auto deepcopy(
ChunkSpan<ElementTypeDst, SupportDst, LayoutDst, MemorySpaceDst> const& dst,
ChunkSpan<ElementTypeSrc, SupportSrc, LayoutSrc, MemorySpaceSrc> const& src) noexcept
{
using ChunkDst = ChunkSpan<ElementTypeDst, SupportDst, LayoutDst, MemorySpaceDst>;
using ChunkSrc = ChunkSpan<ElementTypeSrc, SupportSrc, LayoutSrc, MemorySpaceSrc>;
static_assert(
std::is_assignable_v<chunk_reference_t<ChunkDst>, chunk_reference_t<ChunkSrc>>,
"Not assignable");
KOKKOS_ASSERT(dst.domain().extents() == src.domain().extents());
KOKKOS_ASSERT(
(Kokkos::SpaceAccessibility<DDC_CURRENT_KOKKOS_SPACE, MemorySpaceSrc>::accessible));
KOKKOS_ASSERT(
(Kokkos::SpaceAccessibility<DDC_CURRENT_KOKKOS_SPACE, MemorySpaceDst>::accessible));
for_each(dst.domain(), [&dst, &src](typename SupportDst::discrete_element_type const& elem) {
dst(elem) = src(elem);
});
return dst;
}

} // namespace ddc
37 changes: 37 additions & 0 deletions include/ddc/fill.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: MIT

#pragma once

#include <type_traits>

#include <Kokkos_Core.hpp>

#include "ddc/chunk_span.hpp"
#include "ddc/detail/macros.hpp"
#include "ddc/for_each.hpp"

namespace ddc {

/** Fill a chunk span with a given value
* @param[out] dst the chunk span in which to copy
* @param[in] value the value to fill `dst`
* @return dst as a ChunkSpan
*/
template <class ElementType, class Support, class Layout, class MemorySpace, class T>
KOKKOS_FUNCTION auto fill(
ChunkSpan<ElementType, Support, Layout, MemorySpace> const& dst,
T const& value) noexcept
{
static_assert(
std::is_assignable_v<
chunk_reference_t<ChunkSpan<ElementType, Support, Layout, MemorySpace>>,
T>,
"Not assignable");
KOKKOS_ASSERT((Kokkos::SpaceAccessibility<DDC_CURRENT_KOKKOS_SPACE, MemorySpace>::accessible));
for_each(dst.domain(), [&dst, &value](typename Support::discrete_element_type const& elem) {
dst(elem) = value;
});
return dst;
}

} // namespace ddc
6 changes: 3 additions & 3 deletions include/ddc/for_each.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ namespace ddc {
namespace detail {

template <class RetType, class Element, std::size_t N, class Functor, class... Is>
inline void for_each_serial(
KOKKOS_FUNCTION void for_each_serial(
std::array<Element, N> const& begin,
std::array<Element, N> const& end,
Functor const& f,
Is const&... is) noexcept
{
static constexpr std::size_t I = sizeof...(Is);
constexpr std::size_t I = sizeof...(Is);
if constexpr (I == N) {
f(RetType(is...));
} else {
Expand All @@ -43,7 +43,7 @@ inline void for_each_serial(
* @param[in] f a functor taking an index as parameter
*/
template <class... DDims, class Functor>
inline void for_each(DiscreteDomain<DDims...> const& domain, Functor&& f) noexcept
KOKKOS_FUNCTION void for_each(DiscreteDomain<DDims...> const& domain, Functor&& f) noexcept
{
DiscreteElement<DDims...> const ddc_begin = domain.front();
DiscreteElement<DDims...> const ddc_end = domain.front() + domain.extents();
Expand Down
1 change: 1 addition & 0 deletions include/ddc/parallel_deepcopy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <Kokkos_Core.hpp>

#include "ddc/chunk_common.hpp"
#include "ddc/chunk_span.hpp"

namespace ddc {

Expand Down
4 changes: 3 additions & 1 deletion include/ddc/parallel_fill.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

#include <Kokkos_Core.hpp>

#include "ddc/chunk_common.hpp"
#include "ddc/chunk_span.hpp"
#include "ddc/detail/macros.hpp"
#include "ddc/for_each.hpp"

namespace ddc {

Expand Down
4 changes: 2 additions & 2 deletions include/ddc/transform_reduce.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ template <
class BinaryReductionOp,
class UnaryTransformOp,
class... DCoords>
inline T transform_reduce_serial(
KOKKOS_FUNCTION T transform_reduce_serial(
DiscreteDomain<DDims...> const& domain,
[[maybe_unused]] T const neutral,
BinaryReductionOp const& reduce,
Expand Down Expand Up @@ -67,7 +67,7 @@ inline T transform_reduce_serial(
* range. The return type must be acceptable as input to reduce
*/
template <class... DDims, class T, class BinaryReductionOp, class UnaryTransformOp>
inline T transform_reduce(
KOKKOS_FUNCTION T transform_reduce(
DiscreteDomain<DDims...> const& domain,
T neutral,
BinaryReductionOp&& reduce,
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ add_executable(ddc_tests
parallel_for_each.cpp
parallel_deepcopy.cpp
parallel_transform_reduce.cpp
nested_algorithms.cpp
)
target_compile_features(ddc_tests PUBLIC cxx_std_17)
target_link_libraries(ddc_tests
Expand Down
94 changes: 94 additions & 0 deletions tests/nested_algorithms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <ddc/ddc.hpp>

#include <gtest/gtest.h>

namespace {

struct DDimX;
using DElemX = ddc::DiscreteElement<DDimX>;
using DVectX = ddc::DiscreteVector<DDimX>;
using DDomX = ddc::DiscreteDomain<DDimX>;

static DElemX constexpr lbound_x(0);
static DVectX constexpr nelems_x(10);

} // namespace

void test_nested_algorithms_for_each()
{
DDomX ddom(lbound_x, nelems_x);
ddc::Chunk chk(ddom, ddc::DeviceAllocator<int>());
ddc::ChunkSpan chks = chk.span_view();
ddc::parallel_for_each(
ddc::DiscreteDomain<>(),
KOKKOS_LAMBDA(ddc::DiscreteElement<>) {
ddc::for_each(chks.domain(), [&](DElemX elem) { chks(elem) = 10; });
});
int res = ddc::parallel_transform_reduce(ddom, 0, ddc::reducer::sum<int>(), chks);
EXPECT_EQ(res, 10 * ddom.size());
}

TEST(NestedAlgorithms, ForEach)
{
test_nested_algorithms_for_each();
}

void test_nested_algorithms_transform_reduce()
{
DDomX ddom(lbound_x, nelems_x);
ddc::Chunk chk(ddom, ddc::DeviceAllocator<int>());
ddc::ChunkSpan chks = chk.span_view();
ddc::parallel_for_each(
ddom,
KOKKOS_LAMBDA(DElemX elem) {
chks(elem) = ddc::transform_reduce(
DDomX(lbound_x, DVectX(10)),
0,
ddc::reducer::sum<int>(),
[&](DElemX) { return 1; });
});
int res = ddc::parallel_transform_reduce(ddom, 0, ddc::reducer::sum<int>(), chks);
EXPECT_EQ(res, 10 * ddom.size());
}

TEST(NestedAlgorithms, TransformReduce)
{
test_nested_algorithms_transform_reduce();
}

void test_nested_algorithms_fill()
{
DDomX ddom(lbound_x, nelems_x);
ddc::Chunk chk(ddom, ddc::DeviceAllocator<int>());
ddc::ChunkSpan chks = chk.span_view();
ddc::parallel_for_each(
ddc::DiscreteDomain<>(),
KOKKOS_LAMBDA(ddc::DiscreteElement<>) { ddc::fill(chks, 10); });
int res = ddc::parallel_transform_reduce(ddom, 0, ddc::reducer::sum<int>(), chks);
EXPECT_EQ(res, 10 * ddom.size());
}

TEST(NestedAlgorithms, Fill)
{
test_nested_algorithms_fill();
}

void test_nested_algorithms_deepcopy()
{
DDomX ddom(lbound_x, nelems_x);
ddc::Chunk chk(ddom, ddc::DeviceAllocator<int>());
ddc::ChunkSpan chks = chk.span_view();
ddc::parallel_fill(Kokkos::DefaultExecutionSpace(), chks, 10);
ddc::Chunk chk2(ddom, ddc::DeviceAllocator<int>());
ddc::ChunkSpan chk2s = chk2.span_view();
ddc::parallel_for_each(
ddc::DiscreteDomain<>(),
KOKKOS_LAMBDA(ddc::DiscreteElement<>) { ddc::deepcopy(chk2s, chks); });
int res = ddc::parallel_transform_reduce(ddom, 0, ddc::reducer::sum<int>(), chk2s);
EXPECT_EQ(res, 10 * ddom.size());
}

TEST(NestedAlgorithms, Deepcopy)
{
test_nested_algorithms_deepcopy();
}

0 comments on commit e48bf68

Please sign in to comment.