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 Feb 20, 2024
1 parent 191d8a5 commit 0b07327
Show file tree
Hide file tree
Showing 19 changed files with 343 additions and 137 deletions.
15 changes: 12 additions & 3 deletions examples/characteristics_advection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ int main(int argc, char** argv)

//! [initial output]
// display the initial data
ddc::deepcopy(host_density_alloc, last_density_alloc);
ddc::deepcopy(
Kokkos::DefaultExecutionSpace(),
host_density_alloc.span_view(),
last_density_alloc.span_cview());
display(ddc::coordinate(time_domain.front()),
host_density_alloc[x_domain][y_domain]);
// time of the iteration where the last output happened
Expand Down Expand Up @@ -279,7 +282,10 @@ int main(int argc, char** argv)
//! [output]
if (iter - last_output >= t_output_period) {
last_output = iter;
ddc::deepcopy(host_density_alloc, last_density_alloc);
ddc::deepcopy(
Kokkos::DefaultExecutionSpace(),
host_density_alloc.span_view(),
last_density_alloc.span_cview());
display(ddc::coordinate(iter),
host_density_alloc[x_domain][y_domain]);
}
Expand All @@ -293,7 +299,10 @@ int main(int argc, char** argv)

//! [final output]
if (last_output < time_domain.back()) {
ddc::deepcopy(host_density_alloc, last_density_alloc);
ddc::deepcopy(
Kokkos::DefaultExecutionSpace(),
host_density_alloc.span_view(),
last_density_alloc.span_cview());
display(ddc::coordinate(time_domain.back()),
host_density_alloc[x_domain][y_domain]);
}
Expand Down
8 changes: 6 additions & 2 deletions examples/game_of_life.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ int main()

std::size_t iter = 0;
for (; iter < nt; ++iter) {
ddc::deepcopy(cells_in_host_alloc, cells_in);
ddc::deepcopy(
cells_in_host_alloc.span_view(),
cells_in.span_cview());
print_2DChunk(std::cout, cells_in_host_alloc.span_cview())
<< "\n";
ddc::parallel_for_each(
Expand Down Expand Up @@ -125,7 +127,9 @@ int main()
});
ddc::deepcopy(cells_in, cells_out);
}
ddc::deepcopy(cells_in_host_alloc, cells_in);
ddc::deepcopy(
cells_in_host_alloc.span_view(),
cells_in.span_cview());
print_2DChunk(std::cout, cells_in_host_alloc.span_cview()) << "\n";

return 0;
Expand Down
19 changes: 16 additions & 3 deletions examples/heat_equation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,10 @@ int main(int argc, char** argv)

//! [initial output]
// display the initial data
ddc::deepcopy(ghosted_temp, ghosted_last_temp);
ddc::deepcopy(
Kokkos::DefaultExecutionSpace(),
ghosted_temp,
ghosted_last_temp);
display(ddc::coordinate(time_domain.front()),
ghosted_temp[x_domain][y_domain]);
// time of the iteration where the last output happened
Expand All @@ -273,15 +276,19 @@ int main(int argc, char** argv)
//! [boundary conditions]
// Periodic boundary conditions
ddc::deepcopy(
Kokkos::DefaultExecutionSpace(),
ghosted_last_temp[x_pre_ghost][y_domain],
ghosted_last_temp[y_domain][x_domain_end]);
ddc::deepcopy(
Kokkos::DefaultExecutionSpace(),
ghosted_last_temp[y_domain][x_post_ghost],
ghosted_last_temp[y_domain][x_domain_begin]);
ddc::deepcopy(
Kokkos::DefaultExecutionSpace(),
ghosted_last_temp[x_domain][y_pre_ghost],
ghosted_last_temp[x_domain][y_domain_end]);
ddc::deepcopy(
Kokkos::DefaultExecutionSpace(),
ghosted_last_temp[x_domain][y_post_ghost],
ghosted_last_temp[x_domain][y_domain_begin]);
//! [boundary conditions]
Expand Down Expand Up @@ -331,7 +338,10 @@ int main(int argc, char** argv)
//! [output]
if (iter - last_output_iter >= t_output_period) {
last_output_iter = iter;
ddc::deepcopy(ghosted_temp, ghosted_last_temp);
ddc::deepcopy(
Kokkos::DefaultExecutionSpace(),
ghosted_temp,
ghosted_last_temp);
display(ddc::coordinate(iter),
ghosted_temp[x_domain][y_domain]);
}
Expand All @@ -345,7 +355,10 @@ int main(int argc, char** argv)

//! [final output]
if (last_output_iter < time_domain.back()) {
ddc::deepcopy(ghosted_temp, ghosted_last_temp);
ddc::deepcopy(
Kokkos::DefaultExecutionSpace(),
ghosted_temp,
ghosted_last_temp);
display(ddc::coordinate(time_domain.back()),
ghosted_temp[x_domain][y_domain]);
}
Expand Down
15 changes: 12 additions & 3 deletions examples/heat_equation_spectral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ int main(int argc, char** argv)

//! [initial output]
// display the initial data
ddc::deepcopy(_host_temp, _last_temp);
ddc::deepcopy(
Kokkos::DefaultExecutionSpace(),
_host_temp,
_last_temp);
display(ddc::coordinate(time_domain.front()),
_host_temp[x_domain][y_domain]);
// time of the iteration where the last output happened
Expand Down Expand Up @@ -274,7 +277,10 @@ int main(int argc, char** argv)
//! [output]
if (iter - last_output >= t_output_period) {
last_output = iter;
ddc::deepcopy(_host_temp, _last_temp);
ddc::deepcopy(
Kokkos::DefaultExecutionSpace(),
_host_temp,
_last_temp);
display(ddc::coordinate(iter),
_host_temp[x_domain][y_domain]);
}
Expand All @@ -288,7 +294,10 @@ int main(int argc, char** argv)

//! [final output]
if (last_output < time_domain.back()) {
ddc::deepcopy(_host_temp, _last_temp);
ddc::deepcopy(
Kokkos::DefaultExecutionSpace(),
_host_temp,
_last_temp);
display(ddc::coordinate(time_domain.back()),
_host_temp[x_domain][y_domain]);
}
Expand Down
72 changes: 70 additions & 2 deletions include/ddc/deepcopy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,93 @@
#include <Kokkos_Core.hpp>

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

namespace ddc {

/** Copy the content of a borrowed chunk into another
* @param[out] dst the borrowed chunk in which to copy
* @param[in] src the borrowed chunk from which to copy
* @return dst as a ChunkSpan
*/
template <class ExecutionSpace, class ChunkDst, class ChunkSrc>
auto parallel_deepcopy(ExecutionSpace&& exec_space, ChunkDst&& dst, ChunkSrc&& src)
{
static_assert(is_borrowed_chunk_v<ChunkDst>);
static_assert(is_borrowed_chunk_v<ChunkSrc>);
static_assert(
std::is_assignable_v<chunk_reference_t<ChunkDst>, chunk_reference_t<ChunkSrc>>,
"Not assignable");
assert(dst.domain().extents() == src.domain().extents());
Kokkos::deep_copy(
std::forward<ExecutionSpace>(exec_space),
dst.allocation_kokkos_view(),
src.allocation_kokkos_view());
return dst.span_view();
}

/** Copy the content of a borrowed chunk into another
* @param[out] dst the borrowed chunk in which to copy
* @param[in] src the borrowed chunk from which to copy
* @return dst as a ChunkSpan
*/
template <class ChunkDst, class ChunkSrc>
auto deepcopy(ChunkDst&& dst, ChunkSrc&& src)
auto parallel_deepcopy(ChunkDst&& dst, ChunkSrc&& src)
{
return parallel_deepcopy(std::forward<ChunkDst>(dst), std::forward<ChunkSrc>(src));
}

/** Copy the content of a borrowed chunk into another
* @param[out] dst the borrowed chunk in which to copy
* @param[in] src the borrowed chunk from which to copy
* @return dst as a ChunkSpan
*/
template <class ExecutionSpace, class ChunkDst, class ChunkSrc>
auto deepcopy(ExecutionSpace&& exec_space, ChunkDst&& dst, ChunkSrc&& src)
{
static_assert(is_borrowed_chunk_v<ChunkDst>);
static_assert(is_borrowed_chunk_v<ChunkSrc>);
static_assert(
std::is_assignable_v<chunk_reference_t<ChunkDst>, chunk_reference_t<ChunkSrc>>,
"Not assignable");
assert(dst.domain().extents() == src.domain().extents());
Kokkos::deep_copy(dst.allocation_kokkos_view(), src.allocation_kokkos_view());
Kokkos::deep_copy(
std::forward<ExecutionSpace>(exec_space),
dst.allocation_kokkos_view(),
src.allocation_kokkos_view());
return dst.span_view();
}

/** Copy the content of a borrowed chunk into another
* @param[out] dst the borrowed chunk in which to copy
* @param[in] src the borrowed chunk 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)
{
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");
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(), [&](auto elem) { dst(elem) = src(elem); });
return dst;
}

} // namespace ddc
46 changes: 42 additions & 4 deletions include/ddc/fill.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
#pragma once

#include <type_traits>
#include <utility>
#include <variant>

#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 All @@ -15,13 +19,47 @@ namespace ddc {
* @param[in] value the value to fill `dst`
* @return dst as a ChunkSpan
*/
template <class ChunkDst, class T>
auto fill(ChunkDst&& dst, T const& value)
template <class ExecutionSpace, class ChunkDst, class T>
auto parallel_fill(ExecutionSpace&& exec_space, ChunkDst&& dst, T const& value)
{
static_assert(is_borrowed_chunk_v<ChunkDst>);
static_assert(std::is_assignable_v<chunk_reference_t<ChunkDst>, T>, "Not assignable");
Kokkos::deep_copy(dst.allocation_kokkos_view(), value);
Kokkos::deep_copy(
std::forward<ExecutionSpace>(exec_space),
dst.allocation_kokkos_view(),
value);
return dst.span_view();
}

/** Fill a borrowed chunk with a given value
* @param[out] dst the borrowed chunk in which to copy
* @param[in] value the value to fill `dst`
* @return dst as a ChunkSpan
*/
template <class ChunkDst, class T>
auto parallel_fill(ChunkDst&& dst, T const& value)
{
return parallel_fill(Kokkos::DefaultExecutionSpace(), std::forward<ChunkDst>(dst), value);
}

/** Fill a borrowed chunk with a given value
* @param[out] dst the borrowed chunk 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)
{
static_assert(
std::is_assignable_v<
chunk_reference_t<ChunkSpan<ElementType, Support, Layout, MemorySpace>>,
T>,
"Not assignable");
KOKKOS_ENSURES((Kokkos::SpaceAccessibility<DDC_CURRENT_KOKKOS_SPACE, MemorySpace>::accessible));
for_each(dst.domain(), [&](auto elem) { dst(elem) = value; });
return dst;
}

} // namespace ddc
8 changes: 4 additions & 4 deletions include/ddc/for_each.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ForEachKokkosLambdaAdapter
F m_f;

public:
ForEachKokkosLambdaAdapter(F const& f) : m_f(f) {}
explicit ForEachKokkosLambdaAdapter(F const& f) : m_f(f) {}

template <std::size_t N = sizeof...(DDims), std::enable_if_t<(N == 0), bool> = true>
KOKKOS_IMPL_FORCEINLINE void operator()([[maybe_unused]] index_type<void> unused_id) const
Expand Down Expand Up @@ -133,13 +133,13 @@ inline void for_each_kokkos(
}

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 @@ -156,7 +156,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
2 changes: 1 addition & 1 deletion include/ddc/mirror.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ auto create_mirror_and_copy(
Support,
KokkosAllocator<std::remove_const_t<ElementType>, typename Space::memory_space>>
chunk(src.domain());
deepcopy(chunk, src);
deepcopy(chunk.span_view(), src);
return chunk;
}

Expand Down
Loading

0 comments on commit 0b07327

Please sign in to comment.