diff --git a/simulations/geometryXVx/bump_on_tail/bumpontail_fem_uniform.cpp b/simulations/geometryXVx/bump_on_tail/bumpontail_fem_uniform.cpp index 3bbdb49f7..a597147f8 100644 --- a/simulations/geometryXVx/bump_on_tail/bumpontail_fem_uniform.cpp +++ b/simulations/geometryXVx/bump_on_tail/bumpontail_fem_uniform.cpp @@ -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" @@ -223,8 +222,10 @@ int main(int argc, char** argv) #endif DFieldVx const quadrature_coeffs = neumann_spline_quadrature_coefficients(gridvx, builder_vx_poisson); - Quadrature 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); diff --git a/simulations/geometryXVx/bump_on_tail/bumpontail_fft.cpp b/simulations/geometryXVx/bump_on_tail/bumpontail_fft.cpp index 72a3b1533..87f120139 100644 --- a/simulations/geometryXVx/bump_on_tail/bumpontail_fft.cpp +++ b/simulations/geometryXVx/bump_on_tail/bumpontail_fft.cpp @@ -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" @@ -213,8 +212,10 @@ int main(int argc, char** argv) DFieldVx const quadrature_coeffs = neumann_spline_quadrature_coefficients(gridvx, builder_vx_poisson); - Quadrature 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); diff --git a/simulations/geometryXVx/landau/landau_fem_uniform.cpp b/simulations/geometryXVx/landau/landau_fem_uniform.cpp index 7bb94a531..c88d96d1c 100644 --- a/simulations/geometryXVx/landau/landau_fem_uniform.cpp +++ b/simulations/geometryXVx/landau/landau_fem_uniform.cpp @@ -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" @@ -222,8 +221,10 @@ int main(int argc, char** argv) #endif DFieldVx const quadrature_coeffs = neumann_spline_quadrature_coefficients(gridvx, builder_vx_poisson); - Quadrature 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); diff --git a/simulations/geometryXVx/landau/landau_fft.cpp b/simulations/geometryXVx/landau/landau_fft.cpp index 6bcaaf4ee..d7740d817 100644 --- a/simulations/geometryXVx/landau/landau_fft.cpp +++ b/simulations/geometryXVx/landau/landau_fft.cpp @@ -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" @@ -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); diff --git a/simulations/geometryXVx/sheath/sheath.cpp b/simulations/geometryXVx/sheath/sheath.cpp index 2f1f0745d..82fa213be 100644 --- a/simulations/geometryXVx/sheath/sheath.cpp +++ b/simulations/geometryXVx/sheath/sheath.cpp @@ -20,7 +20,6 @@ #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" @@ -28,6 +27,7 @@ #include "femnonperiodicpoissonsolver.hpp" #endif #include "Lagrange_interpolator_batched.hpp" +#include "chargedensitycalculator.hpp" #include "fftpoissonsolver.hpp" #include "geometry.hpp" #include "irighthandside.hpp" @@ -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" @@ -304,8 +303,10 @@ int main(int argc, char** argv) DFieldVx const quadrature_coeffs = neumann_spline_quadrature_coefficients(gridvx, builder_vx_poisson); - Quadrature 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(ddc::select(meshSpXVx)); FftPoissonSolver const poisson(rhs); diff --git a/src/geometryXVx/poisson/CMakeLists.txt b/src/geometryXVx/poisson/CMakeLists.txt index 24b046ea0..6ac8b4585 100644 --- a/src/geometryXVx/poisson/CMakeLists.txt +++ b/src/geometryXVx/poisson/CMakeLists.txt @@ -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 ) diff --git a/src/geometryXVx/poisson/chargedensitycalculator.cpp b/src/geometryXVx/poisson/chargedensitycalculator.cpp index be8920b24..3a376a9b1 100644 --- a/src/geometryXVx/poisson/chargedensitycalculator.cpp +++ b/src/geometryXVx/poisson/chargedensitycalculator.cpp @@ -3,24 +3,17 @@ #include #include "chargedensitycalculator.hpp" -#include "quadrature.hpp" -#include "simpson_quadrature.hpp" - - -ChargeDensityCalculator::ChargeDensityCalculator(Quadrature const& quad) : m_quad(quad) {} +ChargeDensityCalculator::ChargeDensityCalculator(const ChunkViewType& coeffs) + : m_coefficients(coeffs) +{ +} device_t ChargeDensityCalculator::operator()( - device_t const rho_device, - device_t const allfdistribu_device) const + device_t const rho, + device_t 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()); IndexSp const last_kin_species = allfdistribu.domain().back(); IndexSp const last_species = ddc::discrete_space().charges().domain().back(); double chargedens_adiabspecies = 0.; @@ -28,16 +21,45 @@ device_t ChargeDensityCalculator::operator()( chargedens_adiabspecies = double(charge(last_species)); } - ddc::for_each(rho.domain(), [&](IndexX const ix) { - rho(ix) = chargedens_adiabspecies; - ddc::for_each(ddc::get_domain(allfdistribu), [&](IndexSp const isp) { - ddc::deepcopy(f_vx_slice, allfdistribu[isp][ix]); + // reduction over species and velocity space + Kokkos::View const allfdistribu_view + = allfdistribu.allocation_kokkos_view(); + Kokkos::View const rho_view = rho.allocation_kokkos_view(); + + ViewSp const charges = ddc::host_discrete_space().charges(); + ViewSp const kinetic_charges = charges[allfdistribu.domain()]; + + auto charges_alloc + = create_mirror_view_and_copy(Kokkos::DefaultExecutionSpace(), kinetic_charges); + + Kokkos::View const charges_view + = charges_alloc.span_cview().allocation_kokkos_view(); + + Kokkos::View 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(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; } diff --git a/src/geometryXVx/poisson/chargedensitycalculator.hpp b/src/geometryXVx/poisson/chargedensitycalculator.hpp index 0b60174fb..d644a0464 100644 --- a/src/geometryXVx/poisson/chargedensitycalculator.hpp +++ b/src/geometryXVx/poisson/chargedensitycalculator.hpp @@ -7,11 +7,9 @@ #include #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$ @@ -20,15 +18,17 @@ */ class ChargeDensityCalculator : public IChargeDensityCalculator { - const Quadrature& m_quad; +private: + using ChunkViewType = device_t; + 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& quad); + explicit ChargeDensityCalculator(const ChunkViewType& coeffs); /** * @brief Computes the charge density rho from the distribution function. diff --git a/src/geometryXVx/poisson/parallelchargedensitycalculator.cpp b/src/geometryXVx/poisson/parallelchargedensitycalculator.cpp deleted file mode 100644 index 033597232..000000000 --- a/src/geometryXVx/poisson/parallelchargedensitycalculator.cpp +++ /dev/null @@ -1,65 +0,0 @@ -// SPDX-License-Identifier: MIT - -#include - -#include "parallelchargedensitycalculator.hpp" - -ParallelChargeDensityCalculator::ParallelChargeDensityCalculator(const ChunkViewType& coeffs) - : m_coefficients(coeffs) -{ -} - -device_t ParallelChargeDensityCalculator::operator()( - device_t const rho, - device_t const allfdistribu) const -{ - Kokkos::Profiling::pushRegion("ChargeDensityCalculator"); - IndexSp const last_kin_species = allfdistribu.domain().back(); - IndexSp const last_species = ddc::discrete_space().charges().domain().back(); - double chargedens_adiabspecies = 0.; - if (last_kin_species != last_species) { - chargedens_adiabspecies = double(charge(last_species)); - } - - // reduction over species and velocity space - Kokkos::View const allfdistribu_view - = allfdistribu.allocation_kokkos_view(); - Kokkos::View const rho_view = rho.allocation_kokkos_view(); - - ViewSp const charges = ddc::host_discrete_space().charges(); - ViewSp const kinetic_charges = charges[allfdistribu.domain()]; - - auto charges_alloc - = create_mirror_view_and_copy(Kokkos::DefaultExecutionSpace(), kinetic_charges); - - Kokkos::View const charges_view - = charges_alloc.span_cview().allocation_kokkos_view(); - - Kokkos::View 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); - - 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(charges_view(isp)) * coef_view(ivx) - * allfdistribu_view(isp, ix, ivx); - } - }, - teamSum); - rho_view(ix) = chargedens_adiabspecies + teamSum; - }); - Kokkos::Profiling::popRegion(); - - return rho; -} diff --git a/src/geometryXVx/poisson/parallelchargedensitycalculator.hpp b/src/geometryXVx/poisson/parallelchargedensitycalculator.hpp deleted file mode 100644 index a8d821240..000000000 --- a/src/geometryXVx/poisson/parallelchargedensitycalculator.hpp +++ /dev/null @@ -1,42 +0,0 @@ -// SPDX-License-Identifier: MIT - -#pragma once - -#include - -#include - -#include "ichargedensitycalculator.hpp" - -/** - * @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$ - * where @f$ q_s @f$ is the charge of the species @f$ s @f$ and - * @f$ f_s(x,v) @f$ is the distribution function. - */ -class ParallelChargeDensityCalculator : public IChargeDensityCalculator -{ -private: - using ChunkViewType = device_t; - ChunkViewType m_coefficients; - -public: - /** - * @brief Create a ParallelChargeDensityCalculator object. - * @param[in] coeffs - * The coefficients of the quadrature. - */ - explicit ParallelChargeDensityCalculator(const ChunkViewType& coeffs); - - /** - * @brief Computes the charge density rho from the distribution function. - * @param[in, out] rho - * @param[in] allfdistribu - * - * @return rho The charge density. - */ - device_t operator()(device_t rho, device_t allfdistribu) - const final; -}; diff --git a/tests/geometryXVx/femnonperiodicpoissonsolver.cpp b/tests/geometryXVx/femnonperiodicpoissonsolver.cpp index 5742a19c7..472c579c0 100644 --- a/tests/geometryXVx/femnonperiodicpoissonsolver.cpp +++ b/tests/geometryXVx/femnonperiodicpoissonsolver.cpp @@ -70,8 +70,10 @@ TEST(FemNonPeriodicPoissonSolver, Ordering) ddc::init_discrete_space(std::move(charges), std::move(masses)); DFieldVx const quadrature_coeffs = neumann_spline_quadrature_coefficients(gridvx, builder_vx); - Quadrature 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); diff --git a/tests/geometryXVx/femperiodicpoissonsolver.cpp b/tests/geometryXVx/femperiodicpoissonsolver.cpp index d744722c0..d6a9145ae 100644 --- a/tests/geometryXVx/femperiodicpoissonsolver.cpp +++ b/tests/geometryXVx/femperiodicpoissonsolver.cpp @@ -66,8 +66,10 @@ TEST(FemPeriodicPoissonSolver, CosineSource) ddc::init_discrete_space(std::move(charges), std::move(masses)); DFieldVx const quadrature_coeffs = neumann_spline_quadrature_coefficients(gridvx, builder_vx); - Quadrature 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); diff --git a/tests/geometryXVx/fftpoissonsolver.cpp b/tests/geometryXVx/fftpoissonsolver.cpp index bc59529e8..6631fe4e5 100644 --- a/tests/geometryXVx/fftpoissonsolver.cpp +++ b/tests/geometryXVx/fftpoissonsolver.cpp @@ -12,8 +12,6 @@ #include "fftpoissonsolver.hpp" #include "geometry.hpp" #include "neumann_spline_quadrature.hpp" -#include "parallelchargedensitycalculator.hpp" -#include "quadrature.hpp" #include "species_info.hpp" TEST(FftPoissonSolver, CosineSource) @@ -63,8 +61,10 @@ TEST(FftPoissonSolver, CosineSource) ddc::init_discrete_space(std::move(charges), std::move(masses)); DFieldVx const quadrature_coeffs = neumann_spline_quadrature_coefficients(gridvx, builder_vx); - Quadrature 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 poisson(rhs); DFieldX electrostatic_potential(gridx); @@ -152,7 +152,7 @@ TEST(FftPoissonSolver, CosineSourceParallel) device_t quadrature_coeffs_device(quadrature_coeffs.domain()); ddc::deepcopy(quadrature_coeffs_device, quadrature_coeffs); - ParallelChargeDensityCalculator rhs(quadrature_coeffs_device); + ChargeDensityCalculator rhs(quadrature_coeffs_device); FftPoissonSolver poisson(rhs); DFieldX electrostatic_potential(gridx);