Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Dec 9, 2024
1 parent 1997684 commit e8143a3
Show file tree
Hide file tree
Showing 16 changed files with 298 additions and 121 deletions.
5 changes: 3 additions & 2 deletions src/core/data/field/field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ template<std::size_t dim, typename PhysicalQuantity, typename Data_t = double,
class Field : public NdArrayView<dim, Data_t>
{
static_assert(std::is_same_v<decltype(alloc_mode_), AllocatorMode>);
using Super = NdArrayView<dim, Data_t>;

public:
using Super = NdArrayView<dim, Data_t>;
auto constexpr static dimension = dim;
auto constexpr static alloc_mode = alloc_mode_;
using value_type = Data_t;
Expand Down Expand Up @@ -62,7 +62,8 @@ class Field : public NdArrayView<dim, Data_t>

void copyData(Field const& source) { Super::fill_from(source); }

void setBuffer(Field* const field)
template<typename FieldLike>
void setBuffer(FieldLike* const field) _PHARE_ALL_FN_
{
auto data = field ? field->data() : nullptr;
if (data)
Expand Down
15 changes: 12 additions & 3 deletions src/core/data/grid/gridlayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,8 +821,10 @@ namespace core
* @brief AMRToLocal returns the local index associated with the given AMR one.
* This method only deals with **cell** indexes.
*/

template<typename T>
NO_DISCARD auto AMRToLocal(Point<T, dimension> const& AMRPoint) const _PHARE_ALL_FN_
NO_DISCARD auto AMRToLocal(Box<int, dimension> const& AMRBox,
Point<T, dimension> const& AMRPoint) const _PHARE_ALL_FN_
{
static_assert(std::is_integral_v<T>, "Error, must be MeshIndex (integral Point)");
Point<std::uint32_t, dimension> localPoint;
Expand All @@ -833,14 +835,21 @@ namespace core
//
for (auto i = 0u; i < dimension; ++i)
{
int local = AMRPoint[i] - (AMRBox_.lower[i] - localStart);
// assert(local >= 0);
int local = AMRPoint[i] - (AMRBox.lower[i] - localStart);
assert(local >= 0);
localPoint[i] = local;
}
return localPoint;
}


template<typename T>
NO_DISCARD auto AMRToLocal(Point<T, dimension> const& AMRPoint) const _PHARE_ALL_FN_
{
return AMRToLocal(AMRBox_, AMRPoint);
}


/**
* @brief AMRToLocal returns the local Box associated with the given AMR one.
* This method only deals with **cell** indexes.
Expand Down
12 changes: 9 additions & 3 deletions src/core/data/ndarray/ndarray_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,15 @@ class NdArrayView
return true;
}

protected:
template<typename View>
void reset(View& view) _PHARE_ALL_FN_
{
this->ptr_ = view.data();
this->size_ = view.size();
this->nCells_ = view.nCells_;
}
template<typename Vec>
void reset(Vec& vec, std::array<std::uint32_t, dim> const& nCells)
void reset(Vec& vec, std::array<std::uint32_t, dim> const& nCells) _PHARE_ALL_FN_
{
this->ptr_ = vec.data();
this->size_ = vec.size();
Expand All @@ -198,7 +204,7 @@ class NdArrayView
}

void setBuffer(pointer_type ptr) _PHARE_ALL_FN_ { ptr_ = ptr; }
void setShape(std::array<std::uint32_t, dim> const nCells)
void setShape(std::array<std::uint32_t, dim> const nCells) _PHARE_ALL_FN_
{
nCells_ = nCells;
size_ = core::product(nCells);
Expand Down
58 changes: 38 additions & 20 deletions src/core/data/particles/arrays/particle_array_ts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
#include "core/data/particles/particle_array_partitioner.hpp"


#include <tuple>
#include <iterator>
#include <stdexcept>


namespace PHARE::core
{


template<typename Particles, typename NdArray_t>
class ParticlesTile : public Box<std::int32_t, Particles::dimension>
{
Expand All @@ -30,23 +32,46 @@ class ParticlesTile : public Box<std::int32_t, Particles::dimension>
friend class ParticlesTile;

auto constexpr static ndarray_builder = [](auto const& box) {
auto const cells = *(box.shape().as_unsigned() - 1);
auto const cells = *(box.shape().as_unsigned()); // no -1 for primal
if constexpr (Particles::storage_mode == StorageMode::VECTOR)
return NdArray_t{cells}; // allocating
else
return NdArray_t{0, cells}; // span with nullptr, updated later
};

template<typename... Args>
auto constexpr static bounded_ghost_box(Args&&... args)
{
auto const& [tilebox, particle_array] = std::forward_as_tuple(args...);
auto tgbox = grow(tilebox, 2); // assume interp 1 for now
auto bounded_tgbox = tgbox * particle_array.safe_box();
assert(bounded_tgbox);
return *bounded_tgbox;
}

public:
ParticlesTile(Super const& box)
template<typename Ps, typename Nd>
ParticlesTile(ParticlesTile<Ps, Nd>& tile) // span constructor
: Super{tile}
, particles{tile(), tile().size()}
, field_ghost_box{tile.field_ghost_box}
, rho{*tile.rho}
, fx{*tile.fx}
, fy{*tile.fy}
, fz{*tile.fz}
{
}

template<typename... Args>
ParticlesTile(Super const& box, Args&&... args)
: Super{box}
, particles{[&]() {
if constexpr (std::is_constructible_v<Particles, Super>)
return Particles{box};
else
return Particles{};
}()}
, field_ghost_box{grow(box, 2)} // assume interp 1 for now
, field_ghost_box{bounded_ghost_box(box, args...)}
, rho{ndarray_builder(field_ghost_box)}
, fx{ndarray_builder(field_ghost_box)}
, fy{ndarray_builder(field_ghost_box)}
Expand All @@ -57,17 +82,6 @@ class ParticlesTile : public Box<std::int32_t, Particles::dimension>

ParticlesTile(ParticlesTile<Particles, NdArray_t>&) = default;

template<typename Ps, typename Nd>
ParticlesTile(ParticlesTile<Ps, Nd>& tile) // span constructor
: Super{tile}
, particles{tile(), tile().size()}
, field_ghost_box{tile.field_ghost_box}
, rho{*tile.rho}
, fx{*tile.fx}
, fy{*tile.fy}
, fz{*tile.fz}
{
}

ParticlesTile(ParticlesTile&&) = default;
ParticlesTile(ParticlesTile const&) = default;
Expand All @@ -84,6 +98,10 @@ class ParticlesTile : public Box<std::int32_t, Particles::dimension>
auto& links() { return _links; }
auto& links() const { return _links; }

auto fields() _PHARE_ALL_FN_ { return std::forward_as_tuple(rho, fx, fy, fz); }
auto fields() const _PHARE_ALL_FN_ { return std::forward_as_tuple(rho, fx, fy, fz); }
auto& field_box() const _PHARE_ALL_FN_ { return field_ghost_box; }

private:
Particles particles;
Super field_ghost_box; // tilebox + field ghosts
Expand Down Expand Up @@ -173,6 +191,7 @@ class TileSetSpan

auto& box() const _PHARE_ALL_FN_ { return box_; }
auto& ghost_box() const _PHARE_ALL_FN_ { return ghost_box_; }
auto& safe_box() const { return safe_box_; }
auto local_cell(std::array<int, dim> const& icell) const _PHARE_ALL_FN_
{
return as_local_cell(safe_box_, icell);
Expand Down Expand Up @@ -498,11 +517,11 @@ class TileSetVector
nd_array_t<SIZE_T> cap_{local_box().shape()};
nd_array_t<std::size_t> cell_size_{local_box().shape()};

TileSet<VecTile, alloc_mode> particles_{safe_box_, 4};
TileSet<VecTile, alloc_mode> particles_{safe_box_, 4, *this};

// only used for GPU
TileSet<SpnTile, alloc_mode> particles_views_{
generate_from<alloc_mode>(reset_particle_views_fn(), particles_)};
generate_from<alloc_mode>(reset_particle_views_fn(), particles_, *this)};


std::size_t total_size = 0;
Expand Down Expand Up @@ -666,7 +685,7 @@ template<typename Particles, std::uint8_t impl>
template<auto type>
void TileSetVector<Particles, impl>::sync_cpu_gaps_and_tmp()
{
PHARE_LOG_LINE_STR("sync_cpu_gaps_and_tmp " << magic_enum::enum_name(type));
// PHARE_LOG_LINE_STR("sync_cpu_gaps_and_tmp " << magic_enum::enum_name(type));

for (auto& tile : particles_)
{
Expand Down Expand Up @@ -820,9 +839,8 @@ void TileSetVector<Particles, impl>::sync()
static_assert(type != ParticleType::All);

PHARE_LOG_SCOPE(1, "TileSetVector::sync");
PHARE_LOG_LINE_STR("sync " << static_cast<std::uint32_t>(PHASE) << " "
<< magic_enum::enum_name(type));

// PHARE_LOG_LINE_STR("sync " << static_cast<std::uint32_t>(PHASE) << " "
// << magic_enum::enum_name(type));
// auto const lbox = local_box();

if constexpr (PHASE < 2 and alloc_mode == AllocatorMode::CPU)
Expand Down
23 changes: 10 additions & 13 deletions src/core/data/particles/particle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,23 +170,20 @@ struct CountedParticle : public Particle<dim>
template<template<std::size_t> typename Particle_t, std::size_t dim, typename Stream>
Stream& write_to_stream(Particle_t<dim> const& particle, Stream& out, bool const new_line = true)
{
out << "iCell(";
for (auto c : particle.iCell())
out << c << ",";
out << "), delta(";
for (auto d : particle.delta())
out << d << ",";
out << "), v(";
for (auto v : particle.v())
out << v << ",";
out << "), charge : " << particle.charge() << ", weight : " << particle.weight();

auto const append = [&](std::string const name, auto const& value) {
out << name << "(" << value[0];
for (std::size_t i = 1; i < dim; ++i)
out << "," << value[i];
out << "), ";
};
append("iCell", particle.iCell());
append("delta", particle.delta());
append("v", particle.v());
out << "), charge: " << particle.charge() << ", weight: " << particle.weight();
if constexpr (std::is_same_v<Particle_t<dim>, CountedParticle<dim>>)
out << ", id : " << particle.id;

if (new_line)
out << '\n';

return out;
}

Expand Down
33 changes: 19 additions & 14 deletions src/core/data/tiles/tile_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ class TileSet
using Box_t = Box<int, Tile::dimension>;
static auto constexpr dimension = Tile::dimension;

TileSet(Box_t const& box, std::array<std::size_t, dimension> const& tile_size)
template<typename... Args>
TileSet(Box_t const& box, std::array<std::size_t, dimension> const& tile_size, Args&&... args)
: box_{box}
, tile_size_{tile_size}
, shape_{[&]() {
Expand All @@ -161,13 +162,14 @@ class TileSet
{
tiles_.reserve(product(shape_));
consistent_tile_size_();
make_tiles_();
make_tiles_(args...);
tag_cells_();
}


TileSet(Box_t const& box, std::size_t const& tile_size)
: TileSet{box, ConstArray<std::size_t, dimension>(tile_size)}
template<typename... Args>
TileSet(Box_t const& box, std::size_t const& tile_size, Args&&... args)
: TileSet{box, ConstArray<std::size_t, dimension>(tile_size), args...}
{
}

Expand Down Expand Up @@ -472,7 +474,8 @@ class TileSet
}
}

void make_tiles_()
template<typename... Args>
void make_tiles_(Args&&... args)
{
auto const size_me = [&](auto dim, auto idx) {
if (idx == shape_[dim] - 1)
Expand All @@ -492,7 +495,7 @@ class TileSet
// -1 because upper is included
tile.lower[0] = box_.lower[0] + ix * tile_size_[0];
tile.upper[0] = tile.lower[0] + size_me(0, ix) - 1;
tiles_.emplace_back(tile);
tiles_.emplace_back(tile, args...);
}
else
{
Expand All @@ -505,7 +508,7 @@ class TileSet
tile.upper[0] = tile.lower[0] + size_me(0, ix) - 1;
tile.lower[1] = box_.lower[1] + iy * tile_size_[1];
tile.upper[1] = tile.lower[1] + size_me(1, iy) - 1;
tiles_.emplace_back(tile);
tiles_.emplace_back(tile, args...);
}
else
{
Expand All @@ -518,7 +521,7 @@ class TileSet
tile.upper[1] = tile.lower[1] + size_me(1, iy) - 1;
tile.lower[2] = box_.lower[2] + iz * tile_size_[2];
tile.upper[2] = tile.lower[2] + size_me(2, iz) - 1;
tiles_.emplace_back(tile);
tiles_.emplace_back(tile, args...);
}
}
}
Expand Down Expand Up @@ -558,21 +561,23 @@ auto& update_from(F f, TileSet<Tile, alloc_mode>& in)
return in;
}

template<auto alloc_mode = AllocatorMode::CPU, typename F, typename Tile, auto alloc_mode1>
auto generate_from(F f, TileSet<Tile, alloc_mode1>& in)
template<auto alloc_mode = AllocatorMode::CPU, typename F, typename Tile, auto am1,
typename... Args>
auto generate_from(F f, TileSet<Tile, am1>& in, Args&&... args)
{
using value_type = std::decay_t<std::invoke_result_t<F&, std::size_t const&>>;
TileSet<value_type, alloc_mode> ret{in.box(), in.tile_size()};
TileSet<value_type, alloc_mode> ret{in.box(), in.tile_size(), args...};
for (std::size_t i = 0; i < in.size(); ++i)
ret.data()[i] = f(i);
return ret;
}

template<auto alloc_mode = AllocatorMode::CPU, typename F, typename Tile, auto alloc_mode1>
auto generate_from(F f, TileSet<Tile, alloc_mode1> const& in)
template<auto alloc_mode = AllocatorMode::CPU, typename F, typename Tile, auto am1,
typename... Args>
auto generate_from(F f, TileSet<Tile, am1> const& in, Args&&... args)
{
using value_type = std::decay_t<std::invoke_result_t<F&, std::size_t const&>>;
TileSet<value_type, alloc_mode> ret{in.box(), in.tile_size()};
TileSet<value_type, alloc_mode> ret{in.box(), in.tile_size(), args...};
for (std::size_t i = 0; i < in.size(); ++i)
ret.data()[i] = f(i);
return ret;
Expand Down
Loading

0 comments on commit e8143a3

Please sign in to comment.