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 Nov 23, 2023
1 parent c8cd5f0 commit 327a7ea
Show file tree
Hide file tree
Showing 16 changed files with 388 additions and 77 deletions.
14 changes: 10 additions & 4 deletions examples/characteristics_advection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void display(double time, ChunkType density)
std::cout << " * density[y:"
<< ddc::get_domain<DDimY>(density).size() / 2 << "] = {";
ddc::for_each(
ddc::policies::serial_host,
ddc::policies::serial,
ddc::get_domain<DDimX>(density),
[=](ddc::DiscreteElement<DDimX> const ix) {
std::cout << std::setw(6) << density_slice(ix) << " ";
Expand Down Expand Up @@ -194,7 +194,9 @@ int main(int argc, char** argv)

//! [initial output]
// display the initial data
ddc::deepcopy(host_density_alloc, last_density_alloc);
ddc::deepcopy(
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 @@ -280,7 +282,9 @@ 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(
host_density_alloc.span_view(),
last_density_alloc.span_cview());
display(ddc::coordinate(iter),
host_density_alloc[x_domain][y_domain]);
}
Expand All @@ -294,7 +298,9 @@ int main(int argc, char** argv)

//! [final output]
if (last_output < time_domain.back()) {
ddc::deepcopy(host_density_alloc, last_density_alloc);
ddc::deepcopy(
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 @@ -93,7 +93,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::for_each(
Expand Down Expand Up @@ -127,7 +129,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
21 changes: 17 additions & 4 deletions examples/heat_equation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void display(double time, ChunkType temp)
std::cout << " * temperature[y:"
<< ddc::get_domain<DDimY>(temp).size() / 2 << "] = {";
ddc::for_each(
ddc::policies::serial_host,
ddc::policies::serial,
ddc::get_domain<DDimX>(temp),
[=](ddc::DiscreteElement<DDimX> const ix) {
std::cout << std::setw(6) << temp_slice(ix);
Expand Down Expand Up @@ -239,7 +239,10 @@ int main(int argc, char** argv)

//! [initial output]
// display the initial data
ddc::deepcopy(ghosted_temp, ghosted_last_temp);
ddc::deepcopy(
ddc::policies::parallel_device,
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 @@ -254,15 +257,19 @@ int main(int argc, char** argv)
//! [boundary conditions]
// Periodic boundary conditions
ddc::deepcopy(
ddc::policies::parallel_device,
ghosted_last_temp[x_pre_ghost][y_domain],
ghosted_last_temp[y_domain][x_domain_end]);
ddc::deepcopy(
ddc::policies::parallel_device,
ghosted_last_temp[y_domain][x_post_ghost],
ghosted_last_temp[y_domain][x_domain_begin]);
ddc::deepcopy(
ddc::policies::parallel_device,
ghosted_last_temp[x_domain][y_pre_ghost],
ghosted_last_temp[x_domain][y_domain_end]);
ddc::deepcopy(
ddc::policies::parallel_device,
ghosted_last_temp[x_domain][y_post_ghost],
ghosted_last_temp[x_domain][y_domain_begin]);
//! [boundary conditions]
Expand Down Expand Up @@ -312,7 +319,10 @@ int main(int argc, char** argv)
//! [output]
if (iter - last_output >= t_output_period) {
last_output = iter;
ddc::deepcopy(ghosted_temp, ghosted_last_temp);
ddc::deepcopy(
ddc::policies::parallel_device,
ghosted_temp,
ghosted_last_temp);
display(ddc::coordinate(iter),
ghosted_temp[x_domain][y_domain]);
}
Expand All @@ -326,7 +336,10 @@ int main(int argc, char** argv)

//! [final output]
if (last_output < time_domain.back()) {
ddc::deepcopy(ghosted_temp, ghosted_last_temp);
ddc::deepcopy(
ddc::policies::parallel_device,
ghosted_temp,
ghosted_last_temp);
display(ddc::coordinate(time_domain.back()),
ghosted_temp[x_domain][y_domain]);
}
Expand Down
17 changes: 13 additions & 4 deletions examples/heat_equation_spectral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void display(double time, ChunkType temp)
std::cout << " * temperature[y:"
<< ddc::get_domain<DDimY>(temp).size() / 2 << "] = {";
ddc::for_each(
ddc::policies::serial_host,
ddc::policies::serial,
ddc::get_domain<DDimX>(temp),
[=](ddc::DiscreteElement<DDimX> const ix) {
std::cout << std::setw(6) << temp_slice(ix);
Expand Down Expand Up @@ -203,7 +203,10 @@ int main(int argc, char** argv)

//! [initial output]
// display the initial data
ddc::deepcopy(_host_temp, _last_temp);
ddc::deepcopy(
ddc::policies::parallel_device,
_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 @@ -277,7 +280,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(
ddc::policies::parallel_device,
_host_temp,
_last_temp);
display(ddc::coordinate(iter),
_host_temp[x_domain][y_domain]);
}
Expand All @@ -291,7 +297,10 @@ int main(int argc, char** argv)

//! [final output]
if (last_output < time_domain.back()) {
ddc::deepcopy(_host_temp, _last_temp);
ddc::deepcopy(
ddc::policies::parallel_device,
_host_temp,
_last_temp);
display(ddc::coordinate(time_domain.back()),
_host_temp[x_domain][y_domain]);
}
Expand Down
65 changes: 64 additions & 1 deletion include/ddc/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_span.hpp"
#include "ddc/for_each.hpp"

namespace ddc {

Expand All @@ -16,7 +17,7 @@ namespace ddc {
* @return dst as a ChunkSpan
*/
template <class ChunkDst, class ChunkSrc>
auto deepcopy(ChunkDst&& dst, ChunkSrc&& src)
auto deepcopy(parallel_host_policy, ChunkDst&& dst, ChunkSrc&& src)
{
static_assert(is_borrowed_chunk_v<ChunkDst>);
static_assert(is_borrowed_chunk_v<ChunkSrc>);
Expand All @@ -28,4 +29,66 @@ auto deepcopy(ChunkDst&& dst, ChunkSrc&& src)
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(parallel_device_policy, ChunkDst&& dst, ChunkSrc&& src)
{
return deepcopy(
policies::parallel_host,
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 ElementTypeDst,
class SupportDst,
class LayoutDst,
class MemorySpaceDst,
class ElementTypeSrc,
class SupportSrc,
class LayoutSrc,
class MemorySpaceSrc>
KOKKOS_FUNCTION auto deepcopy(
serial_policy,
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_ENSURES((Kokkos::SpaceAccessibility<
DDC_CURRENT_KOKKOS_SPACE,
typename std::remove_cv_t<std::remove_reference_t<ChunkSrc>>::memory_space>::
accessible));
KOKKOS_ENSURES((Kokkos::SpaceAccessibility<
DDC_CURRENT_KOKKOS_SPACE,
typename std::remove_cv_t<std::remove_reference_t<ChunkDst>>::memory_space>::
accessible));
for_each(policies::serial, dst.domain(), [&](auto elem) { dst(elem) = src(elem); });
return dst;
}

/** 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>
KOKKOS_FUNCTION auto deepcopy(ChunkDst&& dst, ChunkSrc&& src)
{
return deepcopy(policies::serial, std::forward<ChunkDst>(dst), std::forward<ChunkSrc>(src));
}

} // namespace ddc
49 changes: 48 additions & 1 deletion 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_span.hpp"
#include "ddc/detail/macros.hpp"
#include "ddc/for_each.hpp"

namespace ddc {

Expand All @@ -16,12 +20,55 @@ namespace ddc {
* @return dst as a ChunkSpan
*/
template <class ChunkDst, class T>
auto fill(ChunkDst&& dst, T const& value)
auto fill(parallel_host_policy, 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);
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 fill(parallel_device_policy, ChunkDst&& dst, T const& value)
{
return fill(ddc::policies::parallel_host, 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(
serial_policy policy,
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(policy, dst.domain(), [&](auto elem) { dst(elem) = value; });
return dst;
}

/** 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>
KOKKOS_FUNCTION auto fill(ChunkDst&& dst, T const& value)
{
return fill(policies::serial, std::forward<ChunkDst>(dst), value);
}

} // namespace ddc
Loading

0 comments on commit 327a7ea

Please sign in to comment.