Skip to content

Avoid thread safety issues with particle properties. #7204

Description

@bangerth

In several of the particle property plugins (and in #7161), we have member variables like this (example from elastic_stress.h):

        private:
          /**
           * Objects that are used to compute the particle property. Since the
           * object is expensive to create and is needed often it is kept as a
           * member variable. Because it is changed inside a const member function
           * (update_particle_property) it has to be mutable, but since it is
           * only used inside that function and always set before being used
           * that is not a problem. This implementation is not thread safe,
           * but it is currently not used in a threaded context.
           *
           * The first two objects provide material model in- and outputs for
           * one particle at a time; the second set for all the particles
           * in a cell at the same time. TODO use one set for both?
           */
          mutable MaterialModel::MaterialModelInputs<dim> material_inputs;
          mutable MaterialModel::MaterialModelOutputs<dim> material_outputs;
          mutable MaterialModel::MaterialModelInputs<dim> material_inputs_cell;
          mutable MaterialModel::MaterialModelOutputs<dim> material_outputs_cell

Presumably, these objects were introduced to avoid the overhead of allocating these variables as local variables. But they are not thread-safe. We should come up with a better way to do this, and @MFraters has just invented one in #7153: The class should have a ScratchSpace member variable, and then in the member functions we just take an object out of the scratch space and return it at the end of the function. ScratchSpace is thread-safe.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions