Skip to content

Commit

Permalink
Use parallel charge density calculation in place of charge density ca…
Browse files Browse the repository at this point in the history
…lculation in all simulations and tests
  • Loading branch information
EmilyBourne committed Feb 23, 2024
1 parent 4fa8189 commit 35e6e3c
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "params.yaml.hpp"
#include "pdi_out.yml.hpp"
#include "predcorr.hpp"
#include "quadrature.hpp"
#include "restartinitialization.hpp"
#include "singlemodeperturbinitialization.hpp"
#include "species_info.hpp"
Expand Down Expand Up @@ -223,8 +222,10 @@ int main(int argc, char** argv)
#endif
DFieldVx const quadrature_coeffs
= neumann_spline_quadrature_coefficients(gridvx, builder_vx_poisson);
Quadrature<IDimVx> const integrate_v(quadrature_coeffs);
ChargeDensityCalculator rhs(integrate_v);
auto const quadrature_coeffs_device = ddc::create_mirror_view_and_copy(
Kokkos::DefaultExecutionSpace(),
quadrature_coeffs.span_view());
ChargeDensityCalculator rhs(quadrature_coeffs_device);
FemPoissonSolverX const poisson(builder_x_poisson, spline_x_evaluator_poisson, rhs);

PredCorr const predcorr(vlasov, poisson);
Expand Down
7 changes: 4 additions & 3 deletions simulations/geometryXVx/bump_on_tail/bumpontail_fft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "params.yaml.hpp"
#include "pdi_out.yml.hpp"
#include "predcorr.hpp"
#include "quadrature.hpp"
#include "restartinitialization.hpp"
#include "singlemodeperturbinitialization.hpp"
#include "species_info.hpp"
Expand Down Expand Up @@ -213,8 +212,10 @@ int main(int argc, char** argv)

DFieldVx const quadrature_coeffs
= neumann_spline_quadrature_coefficients(gridvx, builder_vx_poisson);
Quadrature<IDimVx> const integrate_v(quadrature_coeffs);
ChargeDensityCalculator rhs(integrate_v);
auto const quadrature_coeffs_device = ddc::create_mirror_view_and_copy(
Kokkos::DefaultExecutionSpace(),
quadrature_coeffs.span_view());
ChargeDensityCalculator rhs(quadrature_coeffs_device);
FftPoissonSolver const poisson(rhs);

PredCorr const predcorr(vlasov, poisson);
Expand Down
7 changes: 4 additions & 3 deletions simulations/geometryXVx/landau/landau_fem_uniform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "params.yaml.hpp"
#include "pdi_out.yml.hpp"
#include "predcorr.hpp"
#include "quadrature.hpp"
#include "restartinitialization.hpp"
#include "singlemodeperturbinitialization.hpp"
#include "species_info.hpp"
Expand Down Expand Up @@ -222,8 +221,10 @@ int main(int argc, char** argv)
#endif
DFieldVx const quadrature_coeffs
= neumann_spline_quadrature_coefficients(gridvx, builder_vx_poisson);
Quadrature<IDimVx> const integrate_v(quadrature_coeffs);
ChargeDensityCalculator rhs(integrate_v);
auto const quadrature_coeffs_device = ddc::create_mirror_view_and_copy(
Kokkos::DefaultExecutionSpace(),
quadrature_coeffs.span_view());
ChargeDensityCalculator rhs(quadrature_coeffs_device);
FemPoissonSolverX const poisson(builder_x_poisson, spline_x_evaluator_poisson, rhs);

PredCorr const predcorr(vlasov, poisson);
Expand Down
5 changes: 2 additions & 3 deletions simulations/geometryXVx/landau/landau_fft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@

#include "bsl_advection_vx_batched.hpp"
#include "bsl_advection_x_batched.hpp"
#include "chargedensitycalculator.hpp"
#include "fftpoissonsolver.hpp"
#include "geometry.hpp"
#include "maxwellianequilibrium.hpp"
#include "neumann_spline_quadrature.hpp"
#include "paraconfpp.hpp"
#include "parallelchargedensitycalculator.hpp"
#include "params.yaml.hpp"
#include "pdi_out.yml.hpp"
#include "predcorr.hpp"
#include "quadrature.hpp"
#include "restartinitialization.hpp"
#include "singlemodeperturbinitialization.hpp"
#include "species_info.hpp"
Expand Down Expand Up @@ -217,7 +216,7 @@ int main(int argc, char** argv)
auto quadrature_coeffs_device = ddc::create_mirror_view_and_copy(
Kokkos::DefaultExecutionSpace(),
quadrature_coeffs.span_view());
ParallelChargeDensityCalculator rhs(quadrature_coeffs_device);
ChargeDensityCalculator rhs(quadrature_coeffs_device);
FftPoissonSolver const poisson(rhs);

PredCorr const predcorr(vlasov, poisson);
Expand Down
9 changes: 5 additions & 4 deletions simulations/geometryXVx/sheath/sheath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

#include "bsl_advection_vx_batched.hpp"
#include "bsl_advection_x_batched.hpp"
#include "chargedensitycalculator.hpp"
#include "collisions_intra.hpp"
#ifdef PERIODIC_RDIMX
#include "femperiodicpoissonsolver.hpp"
#else
#include "femnonperiodicpoissonsolver.hpp"
#endif
#include "Lagrange_interpolator_batched.hpp"
#include "chargedensitycalculator.hpp"
#include "fftpoissonsolver.hpp"
#include "geometry.hpp"
#include "irighthandside.hpp"
Expand All @@ -39,7 +39,6 @@
#include "paraconfpp.hpp"
#include "pdi_out.yml.hpp"
#include "predcorr.hpp"
#include "quadrature.hpp"
#include "restartinitialization.hpp"
#include "sheath.yaml.hpp"
#include "singlemodeperturbinitialization.hpp"
Expand Down Expand Up @@ -304,8 +303,10 @@ int main(int argc, char** argv)
DFieldVx const quadrature_coeffs
= neumann_spline_quadrature_coefficients(gridvx, builder_vx_poisson);

Quadrature<IDimVx> const integrate_v(quadrature_coeffs);
ChargeDensityCalculator rhs(integrate_v);
auto const quadrature_coeffs_device = ddc::create_mirror_view_and_copy(
Kokkos::DefaultExecutionSpace(),
quadrature_coeffs.span_view());
ChargeDensityCalculator rhs(quadrature_coeffs_device);
#ifdef PERIODIC_RDIMX
ddc::init_fourier_space<RDimX>(ddc::select<IDimX>(meshSpXVx));
FftPoissonSolver const poisson(rhs);
Expand Down
1 change: 0 additions & 1 deletion src/geometryXVx/poisson/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ foreach(GEOMETRY_VARIANT IN LISTS GEOMETRY_XVx_VARIANTS_LIST)

add_library("poisson_${GEOMETRY_VARIANT}" STATIC
chargedensitycalculator.cpp
parallelchargedensitycalculator.cpp
electricfield.cpp
nullpoissonsolver.cpp
)
Expand Down
66 changes: 44 additions & 22 deletions src/geometryXVx/poisson/chargedensitycalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,63 @@
#include <ddc/ddc.hpp>

#include "chargedensitycalculator.hpp"
#include "quadrature.hpp"
#include "simpson_quadrature.hpp"



ChargeDensityCalculator::ChargeDensityCalculator(Quadrature<IDimVx> const& quad) : m_quad(quad) {}
ChargeDensityCalculator::ChargeDensityCalculator(const ChunkViewType& coeffs)
: m_coefficients(coeffs)
{
}

device_t<DSpanX> ChargeDensityCalculator::operator()(
device_t<DSpanX> const rho_device,
device_t<DViewSpXVx> const allfdistribu_device) const
device_t<DSpanX> const rho,
device_t<DViewSpXVx> const allfdistribu) const
{
Kokkos::Profiling::pushRegion("ChargeDensityCalculator");
auto allfdistribu_alloc = ddc::create_mirror_view_and_copy(allfdistribu_device);
auto rho_alloc = ddc::create_mirror_view_and_copy(rho_device);
ddc::ChunkSpan const allfdistribu = allfdistribu_alloc.span_cview();
ddc::ChunkSpan const rho = rho_alloc.span_view();

DFieldVx f_vx_slice(allfdistribu.domain<IDimVx>());
IndexSp const last_kin_species = allfdistribu.domain<IDimSp>().back();
IndexSp const last_species = ddc::discrete_space<IDimSp>().charges().domain().back();
double chargedens_adiabspecies = 0.;
if (last_kin_species != last_species) {
chargedens_adiabspecies = double(charge(last_species));
}

ddc::for_each(rho.domain(), [&](IndexX const ix) {
rho(ix) = chargedens_adiabspecies;
ddc::for_each(ddc::get_domain<IDimSp>(allfdistribu), [&](IndexSp const isp) {
ddc::deepcopy(f_vx_slice, allfdistribu[isp][ix]);
// reduction over species and velocity space
Kokkos::View<const double***, Kokkos::LayoutRight> const allfdistribu_view
= allfdistribu.allocation_kokkos_view();
Kokkos::View<double*, Kokkos::LayoutRight> const rho_view = rho.allocation_kokkos_view();

ViewSp<int> const charges = ddc::host_discrete_space<IDimSp>().charges();
ViewSp<int> const kinetic_charges = charges[allfdistribu.domain<IDimSp>()];

auto charges_alloc
= create_mirror_view_and_copy(Kokkos::DefaultExecutionSpace(), kinetic_charges);

Kokkos::View<const int*, Kokkos::LayoutRight> const charges_view
= charges_alloc.span_cview().allocation_kokkos_view();

Kokkos::View<const double*, Kokkos::LayoutRight> const coef_view
= m_coefficients.allocation_kokkos_view();

std::size_t const nsp = allfdistribu_view.extent(0);
std::size_t const nx = allfdistribu_view.extent(1);
std::size_t const nvx = allfdistribu_view.extent(2);

rho(ix) += charge(isp) * m_quad(f_vx_slice.span_view());
});
});
ddc::deepcopy(rho_device, rho);
Kokkos::parallel_for(
Kokkos::TeamPolicy<>(nx, Kokkos::AUTO),
KOKKOS_LAMBDA(const Kokkos::TeamPolicy<>::member_type& team) {
const int ix = team.league_rank();
double teamSum = 0;
Kokkos::parallel_reduce(
Kokkos::TeamThreadRange(team, nvx),
[&](int const& ivx, double& sum) {
// [TO DO] Nested reduction may be possible?
for (std::size_t isp = 0; isp < nsp; isp++) {
sum += static_cast<double>(charges_view(isp)) * coef_view(ivx)
* allfdistribu_view(isp, ix, ivx);
}
},
teamSum);
rho_view(ix) = chargedens_adiabspecies + teamSum;
});
Kokkos::Profiling::popRegion();

return rho_device;
return rho;
}
14 changes: 7 additions & 7 deletions src/geometryXVx/poisson/chargedensitycalculator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
#include <geometry.hpp>

#include "ichargedensitycalculator.hpp"
#include "quadrature.hpp"
#include "simpson_quadrature.hpp"

/**
* @brief A class which computes charges density.
* @brief A class which computes charges density with Kokkos.
*
* A class which computes charges density by solving the equation:
* @f$ \int_{v} q_s f_s(x,v) dv @f$
Expand All @@ -20,15 +18,17 @@
*/
class ChargeDensityCalculator : public IChargeDensityCalculator
{
const Quadrature<IDimVx>& m_quad;
private:
using ChunkViewType = device_t<DViewVx>;
ChunkViewType m_coefficients;

public:
/**
* @brief Create a ChargeDensityCalculator object.
*
* @param quad The quadrature method which should be used.
* @param[in] coeffs
* The coefficients of the quadrature.
*/
ChargeDensityCalculator(const Quadrature<IDimVx>& quad);
explicit ChargeDensityCalculator(const ChunkViewType& coeffs);

/**
* @brief Computes the charge density rho from the distribution function.
Expand Down
65 changes: 0 additions & 65 deletions src/geometryXVx/poisson/parallelchargedensitycalculator.cpp

This file was deleted.

42 changes: 0 additions & 42 deletions src/geometryXVx/poisson/parallelchargedensitycalculator.hpp

This file was deleted.

6 changes: 4 additions & 2 deletions tests/geometryXVx/femnonperiodicpoissonsolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ TEST(FemNonPeriodicPoissonSolver, Ordering)
ddc::init_discrete_space<IDimSp>(std::move(charges), std::move(masses));

DFieldVx const quadrature_coeffs = neumann_spline_quadrature_coefficients(gridvx, builder_vx);
Quadrature<IDimVx> const integrate_v(quadrature_coeffs);
ChargeDensityCalculator rhs(integrate_v);
auto const quadrature_coeffs_device = ddc::create_mirror_view_and_copy(
Kokkos::DefaultExecutionSpace(),
quadrature_coeffs.span_view());
ChargeDensityCalculator rhs(quadrature_coeffs_device);
FemNonPeriodicPoissonSolver poisson(builder_x, spline_x_evaluator, rhs);

DFieldX electrostatic_potential(gridx);
Expand Down
6 changes: 4 additions & 2 deletions tests/geometryXVx/femperiodicpoissonsolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ TEST(FemPeriodicPoissonSolver, CosineSource)
ddc::init_discrete_space<IDimSp>(std::move(charges), std::move(masses));

DFieldVx const quadrature_coeffs = neumann_spline_quadrature_coefficients(gridvx, builder_vx);
Quadrature<IDimVx> const integrate_v(quadrature_coeffs);
ChargeDensityCalculator rhs(integrate_v);
auto const quadrature_coeffs_device = ddc::create_mirror_view_and_copy(
Kokkos::DefaultExecutionSpace(),
quadrature_coeffs.span_view());
ChargeDensityCalculator rhs(quadrature_coeffs_device);
FemPeriodicPoissonSolver poisson(builder_x, spline_x_evaluator, rhs);

DFieldX electrostatic_potential(gridx);
Expand Down
Loading

0 comments on commit 35e6e3c

Please sign in to comment.