From e1c79f9b8a45da711d85709bcbf85e1e9ef21596 Mon Sep 17 00:00:00 2001 From: Congyue Cui Date: Thu, 20 Feb 2025 14:49:36 -0500 Subject: [PATCH] copy assembly to benchmarks --- benchmarks/archive/stiffness1.hpp | 4 +- benchmarks/assembly.hpp | 143 ++++++++++++++++++++++++++++++ benchmarks/main.cpp | 76 ++++------------ benchmarks/stiffness.hpp | 5 +- 4 files changed, 165 insertions(+), 63 deletions(-) create mode 100644 benchmarks/assembly.hpp diff --git a/benchmarks/archive/stiffness1.hpp b/benchmarks/archive/stiffness1.hpp index 008e78cc..dadef068 100644 --- a/benchmarks/archive/stiffness1.hpp +++ b/benchmarks/archive/stiffness1.hpp @@ -30,8 +30,8 @@ constexpr static auto boundary_tag = specfem::element::boundary_tag::none; template -void compute_stiffness_interaction2(const specfem::compute::assembly &assembly, - const int &istep) { +void compute_stiffness_interaction(const specfem::compute::assembly &assembly, + const int &istep) { constexpr auto medium_tag = MediumTag; constexpr auto property_tag = PropertyTag; diff --git a/benchmarks/assembly.hpp b/benchmarks/assembly.hpp new file mode 100644 index 00000000..7ebd1f72 --- /dev/null +++ b/benchmarks/assembly.hpp @@ -0,0 +1,143 @@ +#pragma once + +#include "IO/reader.hpp" +#include "compute/boundaries/boundaries.hpp" +#include "compute/boundary_values/boundary_values.hpp" +#include "compute/compute_mesh.hpp" +#include "compute/compute_partial_derivatives.hpp" +#include "compute/coupled_interfaces/coupled_interfaces.hpp" +#include "compute/fields/fields.hpp" +#include "compute/kernels/kernels.hpp" +#include "compute/properties/interface.hpp" +#include "compute/receivers/receivers.hpp" +#include "compute/sources/sources.hpp" +#include "enumerations/display.hpp" +#include "enumerations/interface.hpp" +#include "mesh/mesh.hpp" +#include "receiver/interface.hpp" +#include "source/interface.hpp" + +namespace specfem { +/** + * @brief Compute namespace defines data structures used to store data related + * to finite element assembly. + * + * The data is organized in a manner that makes it effiecient to access when + * computing finite element compute kernels. + * + */ +namespace benchmarks { +/** + * @brief Finite element assembly data + * + */ +struct assembly { + specfem::compute::mesh mesh; ///< Properties of the assembled mesh + specfem::compute::element_types element_types; ///< Element tags for every + ///< spectral element + specfem::compute::partial_derivatives partial_derivatives; ///< Partial + ///< derivatives of + ///< the basis + ///< functions + specfem::compute::properties properties; ///< Material properties + specfem::compute::kernels kernels; ///< Frechet derivatives (Misfit kernels) + specfem::compute::sources sources; ///< Source information + specfem::compute::receivers receivers; ///< Receiver information + specfem::compute::boundaries boundaries; ///< Boundary conditions + specfem::compute::coupled_interfaces coupled_interfaces; ///< Coupled + ///< interfaces + ///< between 2 + ///< mediums + specfem::compute::fields fields; ///< Displacement, velocity, and acceleration + ///< fields + specfem::compute::boundary_values boundary_values; ///< Field values at the + ///< boundaries + + /** + * @brief Generate a finite element assembly + * + * @param mesh Finite element mesh as read from mesher + * @param quadratures Quadrature points and weights + * @param sources Source information + * @param receivers Receiver information + * @param stypes Types of seismograms + * @param t0 Start time of simulation + * @param dt Time step + * @param max_timesteps Maximum number of time steps + * @param max_sig_step Maximum number of seismogram time steps + * @param nstep_between_samples Number of time steps between output seismogram + * samples + * @param simulation Type of simulation (forward, adjoint, etc.) + * @param property_reader Reader for GLL model (skip material property + * assignment if exists) + */ + assembly( + const specfem::mesh::mesh &mesh, + const specfem::quadrature::quadratures &quadratures, + const std::vector > &sources, + const std::vector > + &receivers, + const std::vector &stypes, + const type_real t0, const type_real dt, const int max_timesteps, + const int max_sig_step, const int nsteps_between_samples, + const specfem::simulation::type simulation, + const std::shared_ptr &property_reader) { + this->mesh = { mesh.tags, mesh.control_nodes, quadratures }; + this->element_types = { this->mesh.nspec, this->mesh.ngllz, + this->mesh.ngllx, this->mesh.mapping, mesh.tags }; + this->partial_derivatives = { this->mesh }; + this->properties = { this->mesh.nspec, this->mesh.ngllz, + this->mesh.ngllx, this->element_types, + mesh.materials, property_reader != nullptr }; + this->kernels = { this->mesh.nspec, this->mesh.ngllz, this->mesh.ngllx, + this->element_types }; + this->sources = { + sources, this->mesh, this->partial_derivatives, this->element_types, t0, + dt, max_timesteps + }; + this->receivers = { this->mesh.nspec, + this->mesh.ngllz, + this->mesh.ngllz, + max_sig_step, + dt, + t0, + nsteps_between_samples, + receivers, + stypes, + this->mesh, + mesh.tags, + this->element_types }; + this->boundaries = { this->mesh.nspec, this->mesh.ngllz, + this->mesh.ngllx, mesh, + this->mesh.mapping, this->mesh.quadratures, + this->properties, this->partial_derivatives }; + this->coupled_interfaces = { mesh, + this->mesh.points, + this->mesh.quadratures, + this->partial_derivatives, + this->element_types, + this->mesh.mapping }; + this->fields = { this->mesh, this->element_types, simulation }; + this->boundary_values = { max_timesteps, this->mesh, this->element_types, + this->boundaries }; + return; + } + + /** + * @brief Maps the component of wavefield on the entire spectral element grid + * + * This field can be used to generate a plot of the wavefield + * + * @param component Component of the wavefield to map + * @return Kokkos::View + * Wavefield mapped on the entire grid. Dimensions of the view are nspec, + * ngllz, ngllx + */ + Kokkos::View + generate_wavefield_on_entire_grid( + const specfem::wavefield::simulation_field wavefield, + const specfem::wavefield::type component); +}; + +} // namespace benchmarks +} // namespace specfem diff --git a/benchmarks/main.cpp b/benchmarks/main.cpp index e00c8792..9029e268 100644 --- a/benchmarks/main.cpp +++ b/benchmarks/main.cpp @@ -1,13 +1,13 @@ #include "archive/stiffness1.hpp" +#include "assembly.hpp" #include "execute.hpp" #include "stiffness.hpp" -// #include "divide.hpp" namespace specfem { namespace benchmarks { -template -void benchmark(specfem::compute::assembly &assembly, +template +void benchmark(AssemblyType &assembly, std::shared_ptr time_scheme) { constexpr auto acoustic = specfem::element::medium_tag::acoustic; constexpr auto elastic = specfem::element::medium_tag::elastic; @@ -18,59 +18,10 @@ void benchmark(specfem::compute::assembly &assembly, const auto solver_start_time = std::chrono::system_clock::now(); - // const auto &field = assembly.fields.get_simulation_field(); - // std::cout << ">>>>" << field.acoustic.nglob << " | " << field.elastic.nglob - // << std::endl; - - // specfem::kokkos::DeviceView4d - // acoustic_field("acoustic_field", field.acoustic.nglob/ngll/ngll,ngll,ngll, - // field.acoustic.components); specfem::kokkos::DeviceView4d acoustic_field_dot_dot("acoustic_field_dot_dot", - // field.acoustic.nglob/ngll/ngll,ngll,ngll, field.acoustic.components); - - // specfem::kokkos::DeviceView4d - // elastic_field("elastic_field", field.elastic.nglob/ngll/ngll,ngll,ngll, - // field.elastic.components); specfem::kokkos::DeviceView4d elastic_field_dot_dot("elastic_field_dot_dot", - // field.elastic.nglob/ngll/ngll,ngll,ngll, field.elastic.components); - - // specfem::kokkos::DeviceView2d - // acoustic_field("acoustic_field", field.acoustic.nglob, - // field.acoustic.components); specfem::kokkos::DeviceView2d acoustic_field_dot_dot("acoustic_field_dot_dot", - // field.acoustic.nglob, field.acoustic.components); - - // specfem::kokkos::DeviceView2d - // elastic_field("elastic_field", field.elastic.nglob, - // field.elastic.components); specfem::kokkos::DeviceView2d elastic_field_dot_dot("elastic_field_dot_dot", - // field.elastic.nglob, field.elastic.components); - for (const auto [istep, dt] : time_scheme->iterate_forward()) { - // compute_stiffness_interaction(assembly, - // istep); compute_stiffness_interaction(assembly, - // istep); compute_stiffness_interaction(assembly, istep); - - // compute_stiffness_interaction2(assembly, - // istep); compute_stiffness_interaction2(assembly, istep); compute_stiffness_interaction2(assembly, istep); - - if constexpr (flag) { - compute_stiffness_interaction(assembly, - istep); - compute_stiffness_interaction(assembly, istep); - compute_stiffness_interaction(assembly, - istep); - } else { - compute_stiffness_interaction2(assembly, - istep); - compute_stiffness_interaction2(assembly, - istep); - compute_stiffness_interaction2(assembly, - istep); - } + compute_stiffness_interaction(assembly, istep); + compute_stiffness_interaction(assembly, istep); + compute_stiffness_interaction(assembly, istep); // divide_mass_matrix(assembly); // divide_mass_matrix(assembly); @@ -127,6 +78,7 @@ void run_benchmark(const YAML::Node ¶meter_dict, const std::vector > sources; const std::vector > receivers; const int nsteps = setup.get_nsteps(); + specfem::compute::assembly assembly( mesh, quadrature, sources, receivers, setup.get_seismogram_types(), setup.get_t0(), dt, nsteps, max_seismogram_time_step, @@ -134,10 +86,16 @@ void run_benchmark(const YAML::Node ¶meter_dict, setup.instantiate_property_reader()); time_scheme->link_assembly(assembly); - benchmark(assembly, time_scheme); - benchmark(assembly, time_scheme); - benchmark(assembly, time_scheme); - benchmark(assembly, time_scheme); + specfem::benchmarks::assembly assembly2( + mesh, quadrature, sources, receivers, setup.get_seismogram_types(), + setup.get_t0(), dt, nsteps, max_seismogram_time_step, + nstep_between_samples, setup.get_simulation_type(), + setup.instantiate_property_reader()); + + benchmark(assembly2, time_scheme); + benchmark(assembly, time_scheme); + benchmark(assembly2, time_scheme); + benchmark(assembly, time_scheme); std::cout << std::endl; } diff --git a/benchmarks/stiffness.hpp b/benchmarks/stiffness.hpp index 1ea399cd..f203bae9 100644 --- a/benchmarks/stiffness.hpp +++ b/benchmarks/stiffness.hpp @@ -4,6 +4,7 @@ #include "enumerations/medium.hpp" #include "enumerations/wavefield.hpp" +#include "assembly.hpp" #include "impl_chunk.hpp" #include "impl_point.hpp" #include "impl_policy.hpp" @@ -19,8 +20,8 @@ namespace benchmarks { template -void compute_stiffness_interaction(const specfem::compute::assembly &assembly, - const int &istep) { +void compute_stiffness_interaction( + const specfem::benchmarks::assembly &assembly, const int &istep) { constexpr auto medium_tag = MediumTag; constexpr auto property_tag = PropertyTag;