Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[WIP] Reduce allocations in aspect - #7262

Draft
MFraters wants to merge 2 commits into
geodynamics:mainfrom
MFraters:reduce_allocations_in_aspect
Draft

[WIP] Reduce allocations in aspect#7262
MFraters wants to merge 2 commits into
geodynamics:mainfrom
MFraters:reduce_allocations_in_aspect

Conversation

@MFraters

Copy link
Copy Markdown
Member

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:

@MFraters
MFraters force-pushed the reduce_allocations_in_aspect branch from f33e503 to 0302b9f Compare July 31, 2026 15:01
@MFraters
MFraters force-pushed the reduce_allocations_in_aspect branch from 0302b9f to 0c9222d Compare July 31, 2026 15:04
*/
bool use_background_field_for_heat_production_averaging;

Utilities::ScratchSpace<std::vector<double>> volume_fractions_space;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use <...>

Comment on lines +222 to +228
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;

Comment on lines +214 to +222
* 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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll want to document what the difference to the previous function is. Or perhaps document it there and deprecate it.

Comment on lines +382 to +394
* 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Comment on lines -255 to +269
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)];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not wrong, but necessary?

Comment on lines -183 to +211
if (this->introspection().compositional_name_exists("noninitial_plastic_strain") && plastic_yielding == true)
if (compositional_name_exists_noninitial_plastic_strain && plastic_yielding == true)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

Comment thread source/simulator/core.cc
Comment on lines -593 to +599
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll have to undo these changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants