ADR 0058: experimental::count_local_patterns: one driver, one engine, and the Op concept#
Status: Technical parts accepted under delegation (2026-10-21; GOVERNANCE.md, “Delegation of technical ADRs”); the new public names await the author (naming is not delegated: D9, the Open decisions table; taken on the plan’s defaults until the 0.3.0 release). The extension point stays
experimental(D4: installed, tracked, never frozen). It carries out M9b step 1 (local-patterns-experimental; the M9b acceptance criteria 1-4) under PLAN 4.1, 4.2, 5.9, 6.2 row 7, 6.4.6, 9.1-9.3, 10.2 and Appendix B. It changes no rule the author approved: the parity is exact, no gate or protocol is touched, no frozen header (hypergraph/*,update.hpp,core/*,graph/*) is edited, and the four built-in counting kernels are SASS- and resource-identical to M9a’s build.Decided: the author accepted the new public names (D9) on 2026-10-22 (GOVERNANCE.md, approvals log, 2026-10-22);
count_local_patternsstaysexperimental(the same day’s stability decision).Date: 2026-10-21
Deciders: the AI assistant, on the author’s behalf for the technical parts (M9b, step local-patterns-experimental); the names: S M Shovan (2026-10-22)
Builds on: ADR 0052 (the triad_count API), ADR 0053 (the wedge engine’s port and its Op form, c6: GO), ADR 0054 (the incremental host engine), ADR 0055 (
operators::count_delta), ADR 0057 (the 64-bit limits), ADR 0033 (the tutorial algorithms and the quoted tutorials), ADR 0023 / 0047 (the API snapshot’s tiers and the frozen headers).
Context#
PLAN 6.4.6 and Appendix B expose ESCHER-GPU’s wedge engine (countTriangles<Op>, countPairs,
countKeys) as experimental::count_local_patterns<op_t> in experimental/local_patterns.cuh,
the one public header allowed to need nvcc (PLAN 4.1), with an “add an Op” tutorial. The sketch’s
open question 2 asked how the Op concept relates to triad_count::pattern. M9a left the engine in
its Op form (c6): host-and-device operations that see 0-based ids, a rows handle and a bins handle,
with the frontier filters and the signs inside the engine, and a host engine that runs the same
operations as loops. Three constraints shaped the design: the four built-in kernels must keep M9a’s
machine code (their gates were read on it); triad_count.hpp must stay host-compilable; and a
user’s Op must run on every backend through the same update path as triad_count, so its result
composes with dyng::update.
Decision#
1. One driver, one engine#
The driver is triad_count’s problem, generalized:
local_pattern_problem<V, W, flavor_t>(cpp/src/algorithms/triad_count/problem.hpp,triad_count.cpp). Its hooks (Step 0’s live deletions, the old and new row views, the frontiers of the deleted and the ASSIGNED inserted ids, the workspace reserved before the commit, the commit, the delta with its sign checks, the stale and poison rules) are the same for every pattern. What depends on the pattern is a table,detail::local_patterns::kernels(num_bins,shape, whether it is built in, the merge limit,launch: the Op’s CUDA kernel launch, andsource: the host work of one source), and the Op’s value (its bytes), both kept in the result’s state (local_pattern_core). The flavor names the stages and the stats:triad_flavor(triad_count.*,triad_count::stats; the aliastriad_count_problem<V, W>) andlocal_patterns_flavor(count_local_patterns.*,experimental::local_patterns_stats). Both flavors are instantiated in libdyng for(int32, unweighted)and(int32, int32); users never instantiate the driver.The engine is the M9a code moved, unchanged, into installed headers: the per-Op kernels (
triangle_kernel,pair_kernel,key_kernel), the block bins and their flush, the warp-cooperative wedge loop and the exactly-once predicates (with their recorded mutation hooks) indyng/experimental/detail/local_patterns_kernels.cuhand.../local_patterns_engine.hpp; the set primitives, the rows handle and the member span indyng/experimental/local_patterns.hpp. The driver keeps what does not depend on the Op: the wedge count of the sources, its CUB scan and the one read of its total (engine/wedge.cuh,count_shape()), then calls the table’slaunch. On the host backendsoperators::count_owned(ADR 0055) partitions the sources and calls the table’ssourcethrough a type-erased item (max_pattern_binsbins per thread; the Op’s own bins handle bounds them to itsnum_bins).The four patterns are built-in Ops of that engine:
motif_op(h_motif),choose3_op(type 1),type2_op,type3_op(engine/*_ops.hpp, each with ashape), whose tables are built inbuiltin_ops.cpp(host work) andcounters.cu(kernels), pre-instantiated in libdyng, sotriad_count.hppneeds no CUDA.local_patterns::builtin_op<op_t>marks them: their device bins handle tests the sign of a bin only and adds unchecked when the Op adds 1 (M9a’s handle), their messages and requirements stay triad_count’s. A new pattern is a new Op, not a new enumerator (the sketch’s open question 2).
2. The public surface (experimental)#
<dyng/experimental/local_patterns.hpp> (host-compilable; the group experimental):
Entity |
Form |
|---|---|
|
|
|
1024 (8 KiB of shared memory per block) |
|
|
|
|
|
|
|
the set helpers on spans (one merge of the codes) |
|
the rows the engine reads of a state ( |
|
|
macros |
|
<dyng/experimental/local_patterns.cuh> (nvcc only; #error with a clear message otherwise):
Entity |
Form |
|---|---|
|
|
|
|
|
|
The shape of the API: count_local_patterns<op_t> is a class template with static compute and
update (PLAN’s name; ESCHER-GPU’s countTriangles<Op>); the Op value is fixed at
compute() and kept by the result (op()), as options::kind is: an Op’s parameters (a
threshold, a vertex) cannot change between updates, which keeps “update == recompute” true.
update() therefore takes no Op. The result is a template so that a result of one Op cannot be
updated with another (dyng::update specializes its traits per Op type).
3. The Op concept#
static constexpr int num_binsin [1,max_pattern_bins];static constexpr dyng::experimental::shape shape;the callbacks of the shape: triangles
begin_wedge(rows, a, b)andtemplate <class bins_t> triangle(rows, c, bins); pairspair(rows, a, b, bins); keyskey(rows, a, bins); andflush(bins); all DYNG_HD;trivially copyable and default-constructible: the Op value given to
compute()is the initial state of every worker (a GPU thread, a host source), which flushes once;at most
max_op_size(4,096) bytes (added at the M9b review): the value is a parameter of every counting kernel (CUDA’s kernel-parameter space is 32,764 bytes, and nvcc reported a larger Op only as “Formal parameter space overflowed” in a mangled kernel name) and every GPU thread holds a copy of it.
The ids of a call are unordered (stated at the M9b review). A full count happens to pass
a < b (and a < b < c), because it walks the out-neighbours; a frontier phase passes the changed
hyperedge as a and every neighbour as b, in either order (the closing c is always above b).
An Op’s count must be symmetric in its ids: an Op counting |a| > |b| agrees with a brute force
over i < j under compute() and is silently wrong after update() (no bin goes negative, so no
internal_error). The concept’s documentation and the tutorial say so, show the counter-example
(a test, Tutorial.AnOpThatDependsOnTheOrderOfItsIdsIsWrongAfterAnUpdate) and ask for “baseline +
deltas == recount” tests of every Op under updates.
User data (labels, a vertex set): the engine passes only the rows and the ids, so it goes in through a pointer member the caller sets, to device memory on cuda and host memory on the host backends, valid and unchanged for the result’s whole lifetime (the result keeps the Op for every update). Only a pointer-free Op runs unchanged on every backend (an open question, section 7).
detail::local_patterns::check_op<op_t>() fires one static_assert, with its own message, per
missing or ill-formed member (no cascade: each later check is skipped when its premise fails).
The callbacks are detected with the Op’s real host bins handle (host_bins<num_bins>), not a
probe type, because nvcc warns about (and with -Werror refuses) a callback instantiated for a
type no code calls it with. Compile-failure tests run each case through the host compiler
(hosted CPU CI) and, in CUDA builds, through nvcc and count_local_patterns<op_t>::compute()
itself (cpp/tests/compile_fail/local_patterns_op.cpp). The messages quote code in single
quotes: nvcc’s front end prints a backtick as ? (corrected at the M9b review; the tests match the
quotes literally, under nvcc too).
Whether a callback is DYNG_HD cannot be tested by a static_assert. nvcc reports a host-only
callback at the engine’s call, but not a __device__-only one (corrected at the M9b review:
this ADR first said nvcc reports both). nvcc diagnoses a call to a __device__ function only from
a host function that is not a template; the engine calls the host callbacks from the function
template host_source<op_t>, where nvcc compiles the call to a stub that ends the process with
exit(1). Such an Op runs on cuda, and on sequential and openmp the program ends with exit status 1
and no message. A DYNG_HD callback that calls a __device__ function is reported. The mitigation
is DYNG_EXPERIMENTAL_CHECK_HOST_CALLBACKS(op_t), used once per Op at namespace scope: it defines
a host function that is never called and calls the callbacks from a generic lambda in it (not in a
template), where nvcc does report them; it checks the concept too and adds no code that runs. The
tutorial and the API tests use it on every Op, and the compile-failure test device_only shows
nvcc reporting a __device__-only Op through it. Making the check automatic would need the engine
to call the callbacks outside a template, which it cannot (the Op is its template parameter).
What the callbacks see: 0-based live hyperedge ids and member spans of the store’s codes
(member + 1, ascending); decode() gives the vertex. No CBST key (id + 1), sentinel (the
merge’s INT_MAX) or flag reaches them: the keys of the line graph, its rows, the frontier and
the flags stay in the engine (tested by probe Ops on every backend, whose checksums equal a brute
force over the hypergraph’s own member lists). For the keys shape the engine skips the empty
row of a dead id below the bound for an Op of a user (key() is called once per live hyperedge;
the built-in type 1 adds C(0, 3) = 0 for them as in M9a). The Op never sees the frontier, the
sign or the ownership rule, and holds no view, so the subtraction cannot read G_{t+1} (I1 by
construction).
The bins of an Op of a user are bounded and checked on every backend: a bin outside
[0, num_bins) is not written and raises invalid_argument_error (on cuda through the device
error word’s internal bit, which the driver names for a user’s Op; for a built-in Op it stays
triad_count’s internal_error for a triad of no class); every add is carry-tested.
4. Requirements and contract#
The contract is triad_count’s: strong before the commit, basic after it (a poisoned result),
stale_result_error for a stale or poisoned result, invalid_argument_error for a moved-from
one, not_supported_error for an unavailable backend, and also when the header’s hand-off
version (local_patterns::abi_version) differs from the library’s. The shapes triangles and
pairs need the line graph and the vertex-to-hyperedge rows (built under automatic); keys needs
neither. Every Op of a user is refused on a hypergraph with 2,147,483,647 vertices (the public
venn3() ends a row at INT_MAX as the built-in merge does). The device phases keep M9a’s bin
layout (a stride of max(num_bins, 30)), so the built-ins’ device memory and copies are unchanged.
5. Packaging#
cmake/install.cmakeinstalls*.cuhwith the headers (also from a CPU-only build: an nvcc program then counts on the host backends). The.cuhheaders compile alone under nvcc (dyng_cuh_self_contained) and without nvcc stop at their#error; the.hppheaders are in the existing host self-containment test.Doxygen reads
*.cuh(EXTENSION_MAPPING, the CUDA qualifiers predefined away); the groupexperimental(PLAN 9.1);ci/doxygen_coverage.pyholdsdyng/experimental/to it, to the freeze’s documentation rules and to the banner “EXPERIMENTAL PUBLIC; may change in any minor release” in every@filebrief.ci/api_snapshot.pygains the tierexperimental(PLAN 5.9):dyng/experimental/*is listed in the baseline, and a difference is reported and never fails the check;--againstnever treats it as frozen.ci/cmake_consumer.shbuilds a host-only consumer of the experimental host header (CXX only) and, from a CUDA build, a user Op compiled by nvcc against the installed package (find_package(dyng), no flag of dynG’s build: the headers need no--expt-relaxed-constexpr), run on the GPU and the host backends;ci/gpu_local.sh(stepexperimental) runs it with memcheck, racecheck and synccheck over the tutorial’s and the API tests’ CUDA runs.dyng_triad_replay --counter local_patterns(CUDA builds): the built-in Ops throughcount_local_patternsbeside triad_count’s results in one commit per batch, bit for bit, and compared with ESCHER-GPU’s goldens.
7. What may change (experimental)#
Any of section 6 in a minor release, with a CHANGELOG entry: the bins handle (a typed handle per
shape; a bins.add() without the carry test for an Op that declares unit_counts), more shapes
(open wedges, for the open triads; incidence-based patterns), Ops that see the hyperedges’
weights, a device-only Op on cuda alone, a sparse or larger bin space, a side-data argument (for
example an array_view bound per backend, so that an Op with user data runs on every backend
unchanged; the M9b review’s open question), and the promotion of the
engine to cpp/src/operators/ as the operator local_patterns of PLAN 4.5.3 (with a second
in-tree user).
Consequences#
One engine: the built-in kernels are the installed templates instantiated in libdyng (
parity/results/M9b.mdsection 1: the five counting kernels’ machine code, registers, stack, shared and local memory identical to M9a’s build, byparity/triad_sass.py compare), so M9a’s gate readings stand without a re-read; the host engine runs the same per-source work throughcount_owned(no performance gate on the host backends, D3).count_ownedkeeps its two users (ADR 0055: triangle_delta and triad_count’s host engine).The tutorial “Count a new hypergraph pattern (an Op)” quotes tested code (
examples/tutorial_algorithms/local_patterns/), pinned byci/tutorial_quotes.py, which also pins the quotes of the existing tutorials.Python has no counterpart by design (
docs/developer/python_gaps.md).