Skip to content

Commit

Permalink
push benchmark updates
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Nov 18, 2023
1 parent d4657f7 commit be0eef0
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 67 deletions.
2 changes: 2 additions & 0 deletions src/core/data/particles/particle_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ class ParticleArray
NO_DISCARD auto& vector() { return particles_; }
NO_DISCARD auto& vector() const { return particles_; }

auto& box() const { return box_; }

private:
Vector particles_;
box_t box_;
Expand Down
7 changes: 5 additions & 2 deletions src/core/utilities/box/box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class box_iterator;
template<typename Type, std::size_t dim>
struct Box
{
static const size_t dimension = dim;


Point<Type, dim> lower;
Point<Type, dim> upper;

Expand Down Expand Up @@ -257,8 +260,8 @@ NO_DISCARD bool isIn(Point const& point,
}


template<typename Type, std::size_t dim>
Box<Type, dim> grow(Box<Type, dim> const& box, Type const& size)
template<typename Type, std::size_t dim, typename OType>
Box<Type, dim> grow(Box<Type, dim> const& box, OType const& size)
{
auto copy{box};
copy.grow(size);
Expand Down
2 changes: 1 addition & 1 deletion src/core/utilities/point/point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace core
NO_DISCARD bool operator!=(Point const& other) const { return !(*this == other); }


template<typename DestType>
template<typename DestType = Type>
NO_DISCARD auto toArray() const
{
std::array<DestType, dimension> destArray;
Expand Down
1 change: 1 addition & 0 deletions tests/core/data/gridlayout/gridlayout_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "core/data/grid/gridlayoutimplyee.hpp"
#include "core/utilities/types.hpp"

#include "test_gridlayout.hpp"

#include "gmock/gmock.h"
#include "gtest/gtest.h"
Expand Down
57 changes: 56 additions & 1 deletion tools/bench/core/bench.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define PHARE_BENCH_CORE_BENCH_H

#include "phare_core.hpp"
#include "benchmark/benchmark.hpp"
#include "benchmark/benchmark.h"


namespace PHARE::core::bench
Expand Down Expand Up @@ -166,4 +166,59 @@ class Electromag : public PHARE::core::Electromag<VecField<GridLayout::dimension

} // namespace PHARE::core::bench

namespace PHARE::core
{
template<typename Box_t, typename RValue = std::uint32_t>
struct LocalisedCellFlattener
{
static const size_t dim = Box_t::dimension;

RValue operator()(std::array<int, dim> icell) const
{
for (std::size_t i = 0; i < dim; ++i)
icell[i] -= box.lower[i];

if constexpr (dim == 2)
return icell[1] + icell[0] * shape[1];
if constexpr (dim == 3)
return icell[2] + icell[1] * shape[2] + icell[0] * shape[1] * shape[2];
return icell[0];
}
template<typename Particle>
RValue operator()(Particle const& particle) const
{
return (*this)(particle.iCell);
}

Box_t const box;
std::array<int, dim> shape = box.shape().toArray();
};
} // namespace PHARE::core


namespace std
{

template<std::size_t dim>
auto& sort(PHARE::core::ParticleArray<dim>& particles)
{
using box_t = typename PHARE::core::ParticleArray<dim>::box_t;
PHARE::core::LocalisedCellFlattener<box_t> cell_flattener{grow(particles.box(), 1)};
std::sort(particles.vector().begin(), particles.vector().end(),
[&](auto const& a, auto const& b) {
return cell_flattener(a.iCell) < cell_flattener(b.iCell);
});
return particles;
}

template<std::size_t dim>
auto sort(PHARE::core::ParticleArray<dim>&& particles)
{
return sort(particles);
}


} // namespace std


#endif /*PHARE_BENCH_CORE_BENCH_H*/
92 changes: 30 additions & 62 deletions tools/bench/core/numerics/pusher/pusher.cpp
Original file line number Diff line number Diff line change
@@ -1,92 +1,60 @@
#include "tools/bench/core/bench.hpp"
#include "tests/core/data/gridlayout/test_gridlayout.hpp"

#include "benchmark/benchmark.hpp"

#include "phare_core.hpp"
#include "core/numerics/pusher/boris.hpp"
#include "core/numerics/ion_updater/ion_updater.hpp"

template<std::size_t dim>
using Field = PHARE::core::Field<PHARE::core::NdArrayVector<dim>,
typename PHARE::core::HybridQuantity::Scalar>;
template<std::size_t dim>
using VecField
= PHARE::core::VecField<PHARE::core::NdArrayVector<dim>, typename PHARE::core::HybridQuantity>;

template<std::size_t dim>
PHARE::core::Particle<dim> particle()
{
return {//
/*.weight = */ 0,
/*.charge = */ 1,
/*.iCell = */ PHARE::core::ConstArray<int, dim>(35),
/*.delta = */ PHARE::core::ConstArray<double, dim>(.01),
/*.v = */ {{0, 10., 0}}};
}

template<typename GridLayout, typename Quantity, std::size_t dim = GridLayout::dimension>
Field<dim> field(std::string key, Quantity type, GridLayout const& layout)
{
Field<dim> feeld{key, type, layout.allocSize(type)};
std::fill(feeld.begin(), feeld.end(), 1);
return feeld;
}
using namespace PHARE;

template<std::size_t dim, std::size_t interp>
void push(benchmark::State& state)
{
constexpr std::uint32_t cells = 65;
constexpr std::uint32_t parts = 1e7;
constexpr std::uint32_t cells = 65;
constexpr std::uint32_t n_parts = 1e7;

using PHARE_Types = PHARE::core::PHARE_Types<dim, interp>;
using GridLayout_t = TestGridLayout<typename PHARE_Types::GridLayout_t>;
using Interpolator = PHARE::core::Interpolator<dim, interp>;
using BoundaryCondition = PHARE::core::BoundaryCondition<dim, interp>;
using Electromag_t = core::bench::Electromag<GridLayout_t>;
using Ions_t = typename PHARE_Types::Ions_t;
using Electromag_t = typename PHARE_Types::Electromag_t;
using GridLayout_t = typename PHARE_Types::GridLayout_t;
using ParticleArray = typename Ions_t::particle_array_type;
using PartIterator = typename ParticleArray::iterator;
using Particle_t = typename ParticleArray::value_type;
using ParticleRange = core::IndexRange<ParticleArray>;

using BorisPusher_t = PHARE::core::BorisPusher<dim, PartIterator, Electromag_t, Interpolator,
using BorisPusher_t = PHARE::core::BorisPusher<dim, ParticleRange, Electromag_t, Interpolator,
BoundaryCondition, GridLayout_t>;

Interpolator interpolator;
ParticleArray domainParticles{parts, particle<dim>()};
ParticleArray tmpDomain{domainParticles.size(), particle<dim>()};

auto rangeIn = PHARE::core::makeRange(domainParticles);
auto rangeOut = PHARE::core::makeRange(tmpDomain);
GridLayout_t layout{cells};
Electromag_t em{layout};

auto meshSize = PHARE::core::ConstArray<double, dim>(1.0 / cells);
auto nCells = PHARE::core::ConstArray<std::uint32_t, dim>(cells);
auto origin = PHARE::core::Point<double, dim>{PHARE::core::ConstArray<double, dim>(0)};
GridLayout_t layout{meshSize, nCells, origin};
ParticleArray domainParticles{layout.AMRBox()};
domainParticles.vector() = std::vector<Particle_t>(n_parts, core::bench::particle<dim>());
core::bench::disperse(domainParticles, 0, cells - 1, 13337);
// std::sort(domainParticles);

Field<dim> bx = field("Bx", PHARE::core::HybridQuantity::Scalar::Bx, layout);
Field<dim> by = field("By", PHARE::core::HybridQuantity::Scalar::By, layout);
Field<dim> bz = field("Bz", PHARE::core::HybridQuantity::Scalar::Bz, layout);
ParticleArray tmpDomain{layout.AMRBox()};
tmpDomain.vector() = domainParticles.vector();

Field<dim> ex = field("Ex", PHARE::core::HybridQuantity::Scalar::Ex, layout);
Field<dim> ey = field("Ey", PHARE::core::HybridQuantity::Scalar::Ey, layout);
Field<dim> ez = field("Ez", PHARE::core::HybridQuantity::Scalar::Ez, layout);

PHARE::core::Electromag<VecField<dim>> emFields{std::string{"EM"}};
emFields.B.setBuffer("EM_B_x", &bx);
emFields.B.setBuffer("EM_B_y", &by);
emFields.B.setBuffer("EM_B_z", &bz);
emFields.E.setBuffer("EM_E_x", &ex);
emFields.E.setBuffer("EM_E_y", &ey);
emFields.E.setBuffer("EM_E_z", &ez);
auto rangeIn = PHARE::core::makeIndexRange(domainParticles);
auto rangeOut = PHARE::core::makeIndexRange(tmpDomain);

BorisPusher_t pusher;
pusher.setMeshAndTimeStep(layout.meshSize(), .001);

while (state.KeepRunning())
Interpolator interpolator;
auto const no_op = [](auto& particleRange) { return particleRange; };
while (state.KeepRunningBatch(1))
{
pusher.move(
/*ParticleRange const&*/ rangeIn, /*ParticleRange&*/ rangeOut,
/*Electromag const&*/ emFields, /*double mass*/ 1, /*Interpolator&*/ interpolator,
/*ParticleSelector const&*/ [](auto const& /*part*/) { return true; },
/*GridLayout const&*/ layout);
/*ParticleRange const&*/ rangeIn, //
/*ParticleRange& */ rangeOut, //
/*Electromag const& */ em, //
/*double mass */ 1,
/*Interpolator&*/ interpolator,
/*GridLayout const&*/ layout, //
no_op, no_op);
}
}
BENCHMARK_TEMPLATE(push, /*dim=*/1, /*interp=*/1)->Unit(benchmark::kMicrosecond);
Expand Down
2 changes: 1 addition & 1 deletion tools/bench/hi5/write_particles.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

#include "benchmark/benchmark.hpp"
#include "benchmark/benchmark.h"
#define PHARE_DIAG_DOUBLES 0
#include "diagnostic/detail/h5writer.hpp"
#include "diagnostic/detail/h5_utils.hpp"
Expand Down

0 comments on commit be0eef0

Please sign in to comment.