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
Viewis, 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,ClassifierContainerandClassifierDispatcher, the storage and dispatch machinery built on both.
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
Viewmirrors the same fields, each one anonika::cuda::span<T>, a non-owning{ T* data; size_t size; }pair pointing at the same (CUDA unified) memory; .view()builds aViewfrom 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):
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:
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:
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, insrc/interaction/include/exaDEM/interaction/interaction.hpp, forInteraction(particle-particle and particle-driver contacts);EXADEM_INNER_BOND_FIELDS, insrc/interaction/include/exaDEM/interaction/inner_bond_interaction.hpp, forInnerBondInteraction(see the worked example below).
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.
-
Edit the field list in
exaDEM/interaction/inner_bond_interaction.hpp: append a line withINDEX = 8(the list has 8 entries, indices0to7): -
Extend the initializer list of
InnerBondInteraction(same file) with a default value at the matching position: unlikeInteraction, which zero-initializes everything, this struct lists a default value for every field by hand. -
Use the field. It is already available through
operator[](attr::dissipated_energy)onInnerBondInteraction, onClassifierContainer<InnerBond>and on itsView. For instance, in a kernel following the pattern ofapply_interface_fracture_criterion.hpp, which readsinteraction_[attr::en]/interaction_[attr::et]the same way:
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:
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 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 (overlapdn, 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::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.