Skip to content

Views, Interactions & Classifier

exaDEM stores most of its bulk per-element data (interactions, driver projection grids, polyhedron position backups, GPU neighbor-list bookkeeping, ...) in owning Structure-of-Arrays containers, and exposes it to the compute kernels through a lightweight, non-owning View built on onika::cuda::span. This page has three parts:

  • Views: what a View is, as a general pattern of the codebase;
  • Interactions: the interaction structs (InteractionPair, Interaction, InnerBondInteraction, PlaceholderInteraction), and how to add a field to one;
  • Classifier: Classifier, ClassifierContainer and ClassifierDispatcher, the storage and dispatch machinery built on both.
Classifier
How the classes of this page relate to each other, left to right: individual interactions (Array of Structs) are grouped by type into a Classifier, an array of per-type containers (Array of Structures of Arrays); each type is one ClassifierContainer<T> (an owning Structure of Arrays); a View exposes the same arrays without owning them; and a row of a View is rebuilt into one Interaction (or InnerBondInteraction).

Views

What is a View

View is not a single class but a convention followed by several, otherwise unrelated, owning storage classes. A class following it declares a nested struct View and a .view() method:

  • the owning class stores its data in CudaMMVector<T> (one per field, for Structure-of-Arrays layouts) or another owning container;
  • its nested View mirrors the same fields, each one an onika::cuda::span<T>, a non-owning { T* data; size_t size; } pair pointing at the same (CUDA unified) memory;
  • .view() builds a View from the current state of the owning object.

A View is trivially copyable and safe to capture by value in a parallel_for / GPU kernel functor, unlike the owning object. This is why the pattern exists: build the owning storage on the host (resize, fill, ...), then give a cheap, GPU-callable View of it to the kernel.

Owning class View
ClassifierContainer<IT> ClassifierContainer<IT>::View
DEMBackupData DEMBackupData::View
RShapeDriverGridCellIndexes RShapeDriverGridCellIndexes::View
CellStorage / CellPairStorage CellStorage::View / CellPairStorage::View
InteractionHistory InteractionHistory::View
IgnorePairsGPU IgnorePairsGPU::View

Usage example

The simplest instance of the pattern, DEMBackupData (polyhedron/backup_dem.hpp):

struct DEMBackupData {
  onika::memory::CudaMMVector<double> data_;
  onika::memory::CudaMMVector<uint32_t> index_map_;

  struct View {
    onika::cuda::span<double> data_;
    onika::cuda::span<uint32_t> index_map_;
  };

  inline View view() { return View{onika::cuda::make_span(data_), onika::cuda::make_span(index_map_)}; }
};

and how it is used: build a View from the owning object, then read/write a field through it as on the owning CudaMMVector (operator[] / .data()), without owning the memory:

1
2
3
4
5
int idx = 50;
int shift = 10;
DEMBackupData::View view = backup_dem.view();
view.index_map_[idx] = shift;
double value = view.data_[view.index_map_[idx]];

View and performance

Spans are not restrict pointers. In performance-critical loops, take the raw pointer of the field with .data() into a __restrict__ pointer, as in the example at the end of this page.

Interactions

Independently of how they are stored, interactions come in a small family of C++ structs (exaDEM/interaction/), the leftmost and rightmost boxes of the diagram above. Each of them starts with an InteractionPair pair_, which holds the topology of the interaction (who interacts with whom) and is identical for every kind of interaction; what follows pair_ holds the kind-specific physical state, which differs between a contact and an inner bond.

Struct Content
InteractionPair topology only, common to every kind: pi_, pj_ (each a ParticleSubLocation: id_, cell_, p_, sub_), type_, swap_, ghost_
Interaction pair_ + friction (Vec3d), moment (Vec3d)
InnerBondInteraction pair_ + friction (tag shared with Interaction), en, tds, et, dn0, weight, criterion (RuptureCriteria), unbroken
PlaceholderInteraction pair_ + an opaque, fixed-size byte buffer, large enough to hold the larger of Interaction / InnerBondInteraction

The kind-specific fields are not plain public members: they are read/written through operator[] with a tag of the attr namespace, I[attr::NAME], on any Interaction / InnerBondInteraction:

Vec3d f = I[attr::friction];
I[attr::moment] = Vec3d{0, 0, 0};

The same operator[](attr::NAME) also works on a whole ClassifierContainer<IT> or its View, returning the array of the field instead of a single value.

PlaceholderInteraction is the type-erased form, used whenever code holds, moves or sorts an interaction without knowing at compile time whether it is a contact or an inner bond: its buffer is reinterpreted as the real struct with .as<T>() once the kind is known (from pair_.type_).

Warning

Dump/restart files store PlaceholderInteraction as raw bytes: adding, removing or reordering fields of Interaction / InnerBondInteraction changes its memory layout and breaks the binary compatibility with the dump files containing interactions written before.

Adding an attribute to an interaction struct

Interaction, InnerBondInteraction, ClassifierContainer<IT> and ClassifierContainer<IT>::View do not declare their fields directly: each is generated from an X-macro field list by the machinery of interaction_decoration.hpp (see the large "DANGER ZONE" comment at the top of that file for all the caveats). In practice, adding a field to an interaction means adding one line to one of two lists:

  • EXADEM_INTERACTION_FIELDS, in src/interaction/include/exaDEM/interaction/interaction.hpp, for Interaction (particle-particle and particle-driver contacts);
  • EXADEM_INNER_BOND_FIELDS, in src/interaction/include/exaDEM/interaction/inner_bond_interaction.hpp, for InnerBondInteraction (see the worked example below).
1
2
3
#define EXADEM_INTERACTION_FIELDS(X)    \
  X(exanb::Vec3d, friction, 0, NEW_TAG) \
  X(exanb::Vec3d, moment, 1, NEW_TAG)

Append a line X(TYPE, NAME, INDEX, NEW_TAG), with INDEX equal to the current length of the list (2 here). The attr::NAME tag, the operator[](attr::NAME) accessor and the storage in ClassifierContainer<IT> and its View are all generated from this line.

Use NEW_TAG unless the field already exists in the other list with the same name (like friction, used by both Interaction and InnerBondInteraction): then write SHARED_TAG, so that the name is not declared twice.

Danger

INDEX must match the position of the field in the list (0, 1, 2, ...). Nothing checks it at compile time: a wrong index silently reads/writes the wrong field at runtime.

Tutorial: adding a field to InnerBondInteraction

Worked example of the recipe above: add dissipated_energy (a double accumulating the energy dissipated by a bond over time) to the inner bond interactions.

  1. Edit the field list in exaDEM/interaction/inner_bond_interaction.hpp: append a line with INDEX = 8 (the list has 8 entries, indices 0 to 7):

    #define EXADEM_INNER_BOND_FIELDS(X)         \
      X(exanb::Vec3d, friction, 0, SHARED_TAG)  \
      X(double, en, 1, NEW_TAG)                 \
      X(exanb::Vec3d, tds, 2, NEW_TAG)          \
      X(double, et, 3, NEW_TAG)                 \
      X(double, dn0, 4, NEW_TAG)                \
      X(double, weight, 5, NEW_TAG)             \
      X(RuptureCriteria, criterion, 6, NEW_TAG) \
      X(uint8_t, unbroken, 7, NEW_TAG)          \
      X(double, dissipated_energy, 8, NEW_TAG)  // <-- new field, NEW_TAG since it is not reused elsewhere
    
  2. Extend the initializer list of InnerBondInteraction (same file) with a default value at the matching position: unlike Interaction, which zero-initializes everything, this struct lists a default value for every field by hand.

    fm_ = {
        {0, 0, 0},  // friction
        0.0,        // en
        {0, 0, 0},  // tds
        0.0,        // et
        0.0,        // dn0
        1.0,        // weight
        {},         // criterion
        true,       // unbroken
        0.0         // dissipated_energy
    };
    
  3. Use the field. It is already available through operator[](attr::dissipated_energy) on InnerBondInteraction, on ClassifierContainer<InnerBond> and on its View. For instance, in a kernel following the pattern of apply_interface_fracture_criterion.hpp, which reads interaction_[attr::en] / interaction_[attr::et] the same way:

    1
    2
    3
    4
    5
    6
    // per-interaction update, e.g. in a kernel working on an InnerBondInteraction& I
    I[attr::dissipated_energy] += I[attr::en] + I[attr::et];
    
    // or, batched over a whole View (as apply_interface_fracture_criterion.hpp does for en/et):
    double* __restrict__ diss = interactions_[attr::dissipated_energy].data();
    diss[idx] += en[idx] + et[idx];
    

Danger

This will very likely break the reading of previously written restart/dump files.

Classifier, ClassifierContainer & ClassifierDispatcher

exaDEM stores every interaction (particle-particle contact, particle-driver contact, or inner bond) as a row of a per-type Structure-of-Arrays container, the three middle boxes of the diagram above: one ClassifierContainer<IT> per interaction type id, owned by a Classifier, exposed to the kernels through ClassifierContainer<IT>::View, and routed at runtime to the right compile-time InteractionType by ClassifierDispatcher. Its fields are those described in Interactions.

ClassifierContainer

ClassifierContainer<IT> is the owning storage of one interaction type id (e.g. VertexFace) of an InteractionType category (IT). It is specialized per InteractionType, because each category backs a different interaction struct, hence a different field set:

ClassifierContainer
The common fields (grey) are shared by the three specializations; the type-specific fields (green) depend on IT: friction/moment for ParticleParticle/ParticleDriver (backing Interaction), or the inner bond fields for InnerBond (backing InnerBondInteraction).
IT Common fields Type-specific fields
ParticleParticle id_i, id_j, cell_i, cell_j, p_i, p_j, sub_i, sub_j, swap, ghost friction, moment
ParticleDriver (same) friction, moment
InnerBond (same) friction, en, tds, et, dn0, weight, criterion, unbroken

Each field is accessed as an array with container[attr::field_name]. To build a container, resize/clear it and fill it with insert/copy from a std::vector<PlaceholderInteraction>; to use it in a kernel, call view() to get a ClassifierContainer<IT>::View.

Classifier

Classifier details

Classifier owns one ClassifierContainer<IT> per interaction type id, grouped into three std::vectors by category, plus a buffer for the scratch data of the contact law:

  • particles_: type ids 0 to 3, one per pair of polyhedron features (vertex-vertex, vertex-edge, vertex-face, edge-edge), std::vector<ClassifierContainer<ParticleParticle>>;
  • drivers_: type ids 4 to 12, one per pair of particle/driver features, std::vector<ClassifierContainer<ParticleDriver>>;
  • innerbonds_: type id 13, std::vector<ClassifierContainer<InnerBond>>;
  • contact_state_: per-type scratch buffers for the quantities computed by the contact law but not kept on the interaction (overlap dn, contact point, normal and tangential forces).

Given a runtime type id (0 to NTypes-1), get_data<IT>(typeID) / get_info<IT>(typeID) return the matching ClassifierContainer<IT>& (and its size for the latter); IT must be known at compile time and is usually fixed by the calling template. for_all_interactions(classifier, func, ...) loops over every type id and calls func once per non-empty container.

ClassifierDispatcher

There is a mismatch to resolve: the compute kernels are templated on the compile-time InteractionType IT (the compiler must know whether it generates code for Interaction or InnerBondInteraction, and their Views), but at runtime the code only has an integer type id, read from the neighbor list.

ClassifierDispatcher
ClassifierDispatcher::dispatch turns a runtime type id back into the matching ClassifierContainer<IT>::View, and calls func<IT>() once per interaction of this type, in parallel: one OpenMP thread (CPU) or CUDA thread (GPU) per call.

ClassifierDispatcher (aliased IDispatcher for the 3 real categories) closes this gap: given the runtime type id, it finds which of Types... it belongs to and calls func.template operator()<IT>(...) with that compile-time IT, so that the code inside func is fully statically typed again. Along with IT, it passes to func the matching View, found through a ClassifierViewAccessor: a plain-pointer, GPU-callable lookup into 3 arrays of Views, one per category.

1
2
3
4
5
6
ClassifierViewStorage wrappers(classifier);
ClassifierViewAccessor accessor = wrappers.accessor();
FunctorType func;
int typeID = 3;

IDispatcher::dispatch(typeID, accessor, func, args...);