[WIP] Reduce allocations in aspect - #7262
Conversation
f33e503 to
0302b9f
Compare
0302b9f to
0c9222d
Compare
| */ | ||
| bool use_background_field_for_heat_production_averaging; | ||
|
|
||
| Utilities::ScratchSpace<std::vector<double>> volume_fractions_space; |
There was a problem hiding this comment.
Let's come up with a common name for these scratch space objects that we can use everywhere. I would suggest volume_fraction_scratch_space. I'm ok with other names, but "scratch" should be in there somewhere.
| #ifndef _aspect_material_model_rheology_visco_plastic_h | ||
| #define _aspect_material_model_rheology_visco_plastic_h | ||
|
|
||
| #include "aspect/utilities.h" |
| calculate_isostrain_viscosities ( const MaterialModel::MaterialModelInputs<dim> &in, | ||
| const unsigned int i, | ||
| const std::vector<double> &volume_fractions, | ||
| IsostrainViscosities &output_parameters, | ||
| const std::vector<double> &phase_function_values = std::vector<double>(), | ||
| const std::vector<unsigned int> &n_phase_transitions_per_composition = | ||
| std::vector<unsigned int>()) const; |
There was a problem hiding this comment.
| calculate_isostrain_viscosities ( const MaterialModel::MaterialModelInputs<dim> &in, | |
| const unsigned int i, | |
| const std::vector<double> &volume_fractions, | |
| IsostrainViscosities &output_parameters, | |
| const std::vector<double> &phase_function_values = std::vector<double>(), | |
| const std::vector<unsigned int> &n_phase_transitions_per_composition = | |
| std::vector<unsigned int>()) const; | |
| calculate_isostrain_viscosities (const MaterialModel::MaterialModelInputs<dim> &in, | |
| const unsigned int i, | |
| const std::vector<double> &volume_fractions, | |
| IsostrainViscosities &output_parameters, | |
| const std::vector<double> &phase_function_values = {}, | |
| const std::vector<unsigned int> &n_phase_transitions_per_composition = | |
| {}) const; |
| * This function calculates viscosities assuming that all the compositional fields | ||
| * experience the same strain rate (isostrain). | ||
| * If @p n_phase_transitions_per_composition points to a vector of | ||
| * unsigned integers this is considered the number of phase transitions | ||
| * for each compositional field and viscosity will be first computed on | ||
| * each phase and then averaged for each compositional field. | ||
| */ | ||
| void | ||
| calculate_isostrain_viscosities ( const MaterialModel::MaterialModelInputs<dim> &in, |
There was a problem hiding this comment.
You'll want to document what the difference to the previous function is. Or perhaps document it there and deprecate it.
| * For multicomponent material models: Given a vector of compositional | ||
| * field values of length N, of which M indices correspond to mass or | ||
| * volume fractions, this function returns a vector of fractions | ||
| * of length M+1, corresponding to the fraction of a ``background | ||
| * material'' as the first entry, and fractions for each of the input | ||
| * fields as the following entries. The returned vector will sum to one. | ||
| * If the sum of the compositional_fields is greater than | ||
| * one, we assume that there is no background field (i.e., that field value | ||
| * is zero). Otherwise, the difference between the sum of the compositional | ||
| * fields and 1.0 is assumed to be the amount of the background field. | ||
| * This function makes no assumptions about the units of the | ||
| * compositional field values; for example, they could correspond to | ||
| * mass or volume fractions. |
There was a problem hiding this comment.
same thing here and perhaps in other places below
| const std::vector<double> volume_fractions = MaterialUtilities::compute_only_composition_fractions(in.composition[0], | ||
| this->introspection().chemical_composition_field_indices(), | ||
| this->get_parameters().minimum_composition_fraction); | ||
| static Utilities::ScratchSpace<std::vector<double>> volume_fractions_space; |
There was a problem hiding this comment.
Make this into a member variable of the class.
| * as documented in evaluate. | ||
| */ | ||
| const IsostrainViscosities isostrain_viscosities = rheology->calculate_isostrain_viscosities(in, 0, volume_fractions, phase_function_values, phase_function.n_phase_transitions_for_each_composition()); | ||
| typename aspect::Utilities::ScratchSpace<IsostrainViscosities>::ScopedScratchObject scoped_isostrain_viscosities_space(isostrain_viscosities_space); |
There was a problem hiding this comment.
| typename aspect::Utilities::ScratchSpace<IsostrainViscosities>::ScopedScratchObject scoped_isostrain_viscosities_space(isostrain_viscosities_space); | |
| aspect::Utilities::ScratchSpace<IsostrainViscosities>::ScopedScratchObject scoped_isostrain_viscosities_space(isostrain_viscosities_space); |
And perhaps in other places below that I won't mark up.
| plastic_yielding = isostrain_viscosities.composition_yielding[std::distance(volume_fractions.begin(), max_composition)]; | ||
| plastic_yielding = isostrain_viscosities.composition_yielding[std::distance(volume_fractions.cbegin(), max_composition)]; |
There was a problem hiding this comment.
Not wrong, but necessary?
| if (this->introspection().compositional_name_exists("noninitial_plastic_strain") && plastic_yielding == true) | ||
| if (compositional_name_exists_noninitial_plastic_strain && plastic_yielding == true) |
There was a problem hiding this comment.
Yes, nice. These are good improvements too. I would take these changes here as a separate uncontroversial pull request. (Or as part of this here, your choice.)
| prm.print_parameters(prm_out, ParameterHandler::PRM); | ||
| //prm.print_parameters(prm_out, ParameterHandler::PRM); | ||
|
|
||
| std::ofstream json_out ((parameters.output_directory + "parameters.json")); | ||
| AssertThrow (json_out, | ||
| ExcMessage (std::string("Could not open file <") + | ||
| parameters.output_directory + "parameters.json>.")); | ||
| prm.print_parameters(json_out, ParameterHandler::JSON); | ||
| //prm.print_parameters(json_out, ParameterHandler::JSON); |
There was a problem hiding this comment.
You'll have to undo these changes.
This is a work in progess. The code itself hasn't really been cleaned, and still will at least require some refactors to make the usage a bit more ergonomic.
For the continental extension performance benchmark, I have been able to reduce the number of allocations from 107e6 to 64e6, as measured by heaptrack. This pull request of mostly to use the new testers to see if it actually makes a difference in the performance or not.
Before your first pull request:
For all pull requests: