Skip to content

Commit 1d4f883

Browse files
committed
Merge the cohort exposure fold into the train
2 parents f7ecc1d + 4c9a6c4 commit 1d4f883

7 files changed

Lines changed: 1121 additions & 40 deletions

‎ci/cr150-source-sites.json‎

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30524,12 +30524,40 @@
3052430524
"spelling": "std :: vector < native_order :: RequestHandle >",
3052530525
"ordinal": 1
3052630526
},
30527+
{
30528+
"file": "src/source/pine_adapter.cpp",
30529+
"symbol": "positions",
30530+
"scope": [
30531+
"namespace pineforge :: source :: detail #1",
30532+
"CohortExposureFold fold_cohort_exposure ( const OriginRoster & origins , const std :: unordered_map < std :: uint64_t , double > & live_units_by_origin ) noexcept #1",
30533+
"if ( origins . members ( ) . size ( ) > 4 * live_units_by_origin . size ( ) + 16 ) #1",
30534+
"for ( const auto & unit : live_units_by_origin ) #1"
30535+
],
30536+
"cls": "assign",
30537+
"spelling": "positions = . . .",
30538+
"ordinal": 1
30539+
},
30540+
{
30541+
"file": "src/source/pine_adapter.cpp",
30542+
"symbol": "remaining",
30543+
"scope": [
30544+
"namespace pineforge :: source :: detail #1",
30545+
"CohortExposureFold fold_cohort_exposure ( const OriginRoster & origins , const std :: unordered_map < std :: uint64_t , double > & live_units_by_origin ) noexcept #1",
30546+
"if ( origins . members ( ) . size ( ) > 4 * live_units_by_origin . size ( ) + 16 ) #1",
30547+
"if ( ! abandoned ) #1"
30548+
],
30549+
"cls": "assign",
30550+
"spelling": "remaining = . . .",
30551+
"ordinal": 1
30552+
},
3052730553
{
3052830554
"file": "src/source/pine_adapter.cpp",
3052930555
"symbol": "next",
3053030556
"scope": [
30531-
":: pineforge :: source :: detail :: PositionTailDiagnostics :: pineforge :: source :: detail :: PositionTailNoDiagnostics namespace pineforge :: source #1",
30532-
"double PineExecutionAdapter :: cohort_exposure_for ( const CohortFacts & facts ) const noexcept #1",
30557+
"namespace pineforge :: source :: detail #1",
30558+
"CohortExposureFold fold_cohort_exposure ( const OriginRoster & origins , const std :: unordered_map < std :: uint64_t , double > & live_units_by_origin ) noexcept #1",
30559+
"if ( origins . members ( ) . size ( ) > 4 * live_units_by_origin . size ( ) + 16 ) #1",
30560+
"if ( static_cast < std :: uint64_t > ( origins . members ( ) . size ( ) ) > = selector_charge ) #1",
3053330561
") #1"
3053430562
],
3053530563
"cls": "assign",
@@ -30540,10 +30568,12 @@
3054030568
"file": "src/source/pine_adapter.cpp",
3054130569
"symbol": "positions",
3054230570
"scope": [
30543-
":: pineforge :: source :: detail :: PositionTailDiagnostics :: pineforge :: source :: detail :: PositionTailNoDiagnostics namespace pineforge :: source #1",
30544-
"double PineExecutionAdapter :: cohort_exposure_for ( const CohortFacts & facts ) const noexcept #1",
30571+
"namespace pineforge :: source :: detail #1",
30572+
"CohortExposureFold fold_cohort_exposure ( const OriginRoster & origins , const std :: unordered_map < std :: uint64_t , double > & live_units_by_origin ) noexcept #1",
30573+
"if ( origins . members ( ) . size ( ) > 4 * live_units_by_origin . size ( ) + 16 ) #1",
30574+
"if ( static_cast < std :: uint64_t > ( origins . members ( ) . size ( ) ) > = selector_charge ) #1",
3054530575
") #1",
30546-
"for ( const auto & unit : facts . live_units_by_origin ) #1"
30576+
"for ( const auto & unit : live_units_by_origin ) #1"
3054730577
],
3054830578
"cls": "assign",
3054930579
"spelling": "positions = . . .",
@@ -30553,10 +30583,52 @@
3055330583
"file": "src/source/pine_adapter.cpp",
3055430584
"symbol": "next",
3055530585
"scope": [
30556-
":: pineforge :: source :: detail :: PositionTailDiagnostics :: pineforge :: source :: detail :: PositionTailNoDiagnostics namespace pineforge :: source #1",
30557-
"double PineExecutionAdapter :: cohort_exposure_for ( const CohortFacts & facts ) const noexcept #1",
30586+
"namespace pineforge :: source :: detail #1",
30587+
"CohortExposureFold fold_cohort_exposure ( const OriginRoster & origins , const std :: unordered_map < std :: uint64_t , double > & live_units_by_origin ) noexcept #1",
30588+
"if ( origins . members ( ) . size ( ) > 4 * live_units_by_origin . size ( ) + 16 ) #1",
30589+
"if ( static_cast < std :: uint64_t > ( origins . members ( ) . size ( ) ) > = selector_charge ) #1",
30590+
") #1",
30591+
"for ( const auto & unit : live_units_by_origin ) #1",
30592+
"for ( const auto position : * positions ) #1",
30593+
"if ( ! have_next || position < next ) #1"
30594+
],
30595+
"cls": "assign",
30596+
"spelling": "next = . . .",
30597+
"ordinal": 1
30598+
},
30599+
{
30600+
"file": "src/source/pine_adapter.cpp",
30601+
"symbol": "next",
30602+
"scope": [
30603+
"namespace pineforge :: source :: detail #1",
30604+
"void verify_cohort_exposure ( const OriginRoster & origins , const std :: unordered_map < std :: uint64_t , double > & live_units_by_origin , double actual ) noexcept #1",
30605+
") #1"
30606+
],
30607+
"cls": "assign",
30608+
"spelling": "next = . . .",
30609+
"ordinal": 1
30610+
},
30611+
{
30612+
"file": "src/source/pine_adapter.cpp",
30613+
"symbol": "positions",
30614+
"scope": [
30615+
"namespace pineforge :: source :: detail #1",
30616+
"void verify_cohort_exposure ( const OriginRoster & origins , const std :: unordered_map < std :: uint64_t , double > & live_units_by_origin , double actual ) noexcept #1",
30617+
") #1",
30618+
"for ( const auto & unit : live_units_by_origin ) #1"
30619+
],
30620+
"cls": "assign",
30621+
"spelling": "positions = . . .",
30622+
"ordinal": 1
30623+
},
30624+
{
30625+
"file": "src/source/pine_adapter.cpp",
30626+
"symbol": "next",
30627+
"scope": [
30628+
"namespace pineforge :: source :: detail #1",
30629+
"void verify_cohort_exposure ( const OriginRoster & origins , const std :: unordered_map < std :: uint64_t , double > & live_units_by_origin , double actual ) noexcept #1",
3055830630
") #1",
30559-
"for ( const auto & unit : facts . live_units_by_origin ) #1",
30631+
"for ( const auto & unit : live_units_by_origin ) #1",
3056030632
"for ( const auto position : * positions ) #1",
3056130633
"if ( ! have_next || position < next ) #1"
3056230634
],

‎src/source/pine_adapter.cpp‎

Lines changed: 188 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <pineforge/source/pine_strategy_host.hpp>
55
#include <pineforge/compat/pine/market_admission.hpp>
66
#include <pineforge/pending_order_mirror.hpp>
7-
7+
#include "pine_cohort_exposure.hpp"
88
#include <pineforge/native_calendar.hpp>
99
#include <pineforge/timeframe.hpp>
1010

@@ -5362,37 +5362,37 @@ double PineExecutionAdapter::cohort_exposure_for(const SourceId& id) const noexc
53625362
}
53635363

53645364
double PineExecutionAdapter::cohort_exposure_for(const CohortFacts& facts) const noexcept {
5365-
double total = 0.0;
5366-
// `live_units_by_origin` is a lookup table. Quantities must follow the
5367-
// cohort's deterministic source insertion order, never its hash buckets.
5368-
// Only roster positions whose incarnation has a unit row can contribute:
5369-
// step through just those, ascending, rather than the id's whole entry
5370-
// history. Allocation-free (the pending-order readback calls this).
5371-
bool have_previous = false;
5372-
std::size_t previous = 0;
5373-
for (;;) {
5374-
bool have_next = false;
5375-
std::size_t next = 0;
5376-
double units = 0.0;
5377-
for (const auto& unit : facts.live_units_by_origin) {
5378-
const auto* positions = facts.origins.positions_of(unit.first);
5379-
if (!positions) continue;
5380-
for (const auto position : *positions) {
5381-
if (have_previous && position <= previous) continue;
5382-
if (!have_next || position < next) {
5383-
have_next = true;
5384-
next = position;
5385-
units = unit.second;
5386-
}
5387-
break;
5388-
}
5389-
}
5390-
if (!have_next) break;
5391-
if (std::isfinite(units) && units > 0.0) total += units;
5392-
have_previous = true;
5393-
previous = next;
5394-
}
5395-
return std::isfinite(total) && total > 0.0 ? total : 0.0;
5365+
PINEFORGE_LOOKUP_INDEX_WORK("cohort_exposure_call", this, run_counter_, 1);
5366+
// Quantities follow source insertion order, never the lookup table's
5367+
// hash buckets. Each retained position contributes separately.
5368+
// Repeated incarnations contribute once per occurrence, in that order.
5369+
// Allocation-free: pending-order readback calls this const noexcept member.
5370+
// Finite/positive filtering and the final clamp preserve overflow to zero.
5371+
// Reordering, regrouping or multiplying repeated units changes bits.
5372+
// Diagnostic work belongs to the returned value, never adapter state.
5373+
// The seam receives the original roster and map by const reference.
5374+
// Global positions keep their order after prefix retirement.
5375+
// A unit row without a retained position contributes no term.
5376+
const auto folded = detail::fold_cohort_exposure(facts.origins,
5377+
facts.live_units_by_origin);
5378+
PINEFORGE_LOOKUP_INDEX_WORK("cohort_exposure_step", this, run_counter_, folded.steps);
5379+
PINEFORGE_LOOKUP_INDEX_WORK("cohort_exposure_window", this, run_counter_,
5380+
facts.origins.members().size());
5381+
PINEFORGE_LOOKUP_INDEX_WORK("cohort_exposure_rows", this, run_counter_,
5382+
facts.live_units_by_origin.size());
5383+
if (folded.path == 2)
5384+
PINEFORGE_LOOKUP_INDEX_WORK("cohort_exposure_path_b", this, run_counter_, 1);
5385+
if (folded.path == 3)
5386+
PINEFORGE_LOOKUP_INDEX_WORK("cohort_exposure_b_abandoned", this, run_counter_, 1);
5387+
#ifdef PINEFORGE_COHORT_EXPOSURE_FIXTURES
5388+
detail::verify_cohort_exposure(facts.origins, facts.live_units_by_origin,
5389+
folded.total);
5390+
#endif
5391+
// The fixture oracle is independent of the selected production path.
5392+
// It checks every call, including both empty-state early returns.
5393+
// No aggregate or reservation state is updated by this read.
5394+
// Callers receive only the clamped exposure value.
5395+
return folded.total;
53965396
}
53975397

53985398
// The units of the position an exit's entry (from_entry) holds; none for an
@@ -30295,3 +30295,159 @@ std::array<std::uint64_t, 3> PineExecutionAdapter::retirement_validation_state()
3029530295
}
3029630296
#endif
3029730297
} // namespace pineforge::source
30298+
30299+
namespace pineforge::source::detail {
30300+
30301+
CohortExposureFold fold_cohort_exposure(
30302+
const OriginRoster& origins,
30303+
const std::unordered_map<std::uint64_t, double>& live_units_by_origin) noexcept {
30304+
if (live_units_by_origin.empty() || origins.members().empty()) return {0.0, 0, 0};
30305+
std::uint64_t steps = 0;
30306+
std::uint8_t path = 1;
30307+
if (origins.members().size() > 4 * live_units_by_origin.size() + 16) {
30308+
// Positions are disjoint across incarnations and ascending within
30309+
// each row. Sorting all occurrences restores the roster's order.
30310+
struct Term { std::size_t position; double units; };
30311+
Term buf[kCohortFoldStack];
30312+
std::size_t n = 0;
30313+
std::size_t mapped = 0;
30314+
bool abandoned = false;
30315+
for (const auto& unit : live_units_by_origin) {
30316+
++steps;
30317+
const auto* positions = origins.positions_of(unit.first);
30318+
if (!positions) continue;
30319+
for (const auto position : *positions) {
30320+
++steps;
30321+
++mapped;
30322+
if (n == kCohortFoldStack) {
30323+
abandoned = true;
30324+
continue;
30325+
}
30326+
buf[n++] = {position, unit.second};
30327+
}
30328+
}
30329+
if (!abandoned) {
30330+
std::sort(buf, buf + n, [](const Term& a, const Term& b) noexcept {
30331+
return a.position < b.position;
30332+
});
30333+
// Deterministic sort charge: P * ceil(log2 P), zero for P <= 1.
30334+
std::uint64_t levels = 0;
30335+
for (auto remaining = n > 0 ? n - 1 : 0; remaining; remaining >>= 1)
30336+
++levels;
30337+
steps += static_cast<std::uint64_t>(n) * levels;
30338+
double total = 0.0;
30339+
for (std::size_t i = 0; i < n; ++i) {
30340+
++steps;
30341+
const double units = buf[i].units;
30342+
if (std::isfinite(units) && units > 0.0) total += units;
30343+
}
30344+
return {std::isfinite(total) && total > 0.0 ? total : 0.0, steps, 2};
30345+
}
30346+
// The partial stack gather is discarded. Comparing this charge with
30347+
// M keeps either overflow completion bounded by the frozen selector.
30348+
const auto mapped_count = static_cast<std::uint64_t>(mapped);
30349+
const auto row_count =
30350+
static_cast<std::uint64_t>(live_units_by_origin.size());
30351+
// The selector has P + 1 passes. Each pass can inspect all R rows
30352+
// and all P positions, including positions in exhausted rows.
30353+
const auto charge_max = std::numeric_limits<std::uint64_t>::max();
30354+
const auto charge_add = [charge_max](std::uint64_t left,
30355+
std::uint64_t right) noexcept {
30356+
return right > charge_max - left ? charge_max : left + right;
30357+
};
30358+
const auto charge_mul = [charge_max](std::uint64_t left,
30359+
std::uint64_t right) noexcept {
30360+
return left != 0 && right > charge_max / left
30361+
? charge_max : left * right;
30362+
};
30363+
const auto pass_count = mapped_count == charge_max
30364+
? charge_max : mapped_count + 1;
30365+
const auto selector_charge =
30366+
charge_mul(pass_count, charge_add(row_count, mapped_count));
30367+
path = 3;
30368+
if (static_cast<std::uint64_t>(origins.members().size()) >= selector_charge) {
30369+
// Frozen selector, kept term-for-term so its acceptance-ordered
30370+
// floating-point additions remain the old fold's additions.
30371+
double total = 0.0;
30372+
bool have_previous = false;
30373+
std::size_t previous = 0;
30374+
for (;;) {
30375+
bool have_next = false;
30376+
std::size_t next = 0;
30377+
double units = 0.0;
30378+
for (const auto& unit : live_units_by_origin) {
30379+
++steps;
30380+
const auto* positions = origins.positions_of(unit.first);
30381+
if (!positions) continue;
30382+
for (const auto position : *positions) {
30383+
++steps;
30384+
if (have_previous && position <= previous) continue;
30385+
if (!have_next || position < next) {
30386+
have_next = true;
30387+
next = position;
30388+
units = unit.second;
30389+
}
30390+
break;
30391+
}
30392+
}
30393+
if (!have_next) break;
30394+
if (std::isfinite(units) && units > 0.0) total += units;
30395+
have_previous = true;
30396+
previous = next;
30397+
}
30398+
return {std::isfinite(total) && total > 0.0 ? total : 0.0, steps, path};
30399+
}
30400+
}
30401+
double total = 0.0;
30402+
for (const auto& origin : origins) {
30403+
++steps;
30404+
const auto unit = live_units_by_origin.find(origin.incarnation);
30405+
if (unit == live_units_by_origin.end()) continue;
30406+
const double units = unit->second;
30407+
if (std::isfinite(units) && units > 0.0) total += units;
30408+
}
30409+
return {std::isfinite(total) && total > 0.0 ? total : 0.0, steps, path};
30410+
}
30411+
30412+
#ifdef PINEFORGE_COHORT_EXPOSURE_FIXTURES
30413+
void verify_cohort_exposure(
30414+
const OriginRoster& origins,
30415+
const std::unordered_map<std::uint64_t, double>& live_units_by_origin,
30416+
double actual) noexcept {
30417+
// Frozen selector: deliberately independent of the production fold.
30418+
double total = 0.0;
30419+
// `live_units_by_origin` is a lookup table. Quantities must follow the
30420+
// cohort's deterministic source insertion order, never its hash buckets.
30421+
// Only roster positions whose incarnation has a unit row can contribute:
30422+
// step through just those, ascending, rather than the id's whole entry
30423+
// history. Allocation-free (the pending-order readback calls this).
30424+
bool have_previous = false;
30425+
std::size_t previous = 0;
30426+
for (;;) {
30427+
bool have_next = false;
30428+
std::size_t next = 0;
30429+
double units = 0.0;
30430+
for (const auto& unit : live_units_by_origin) {
30431+
const auto* positions = origins.positions_of(unit.first);
30432+
if (!positions) continue;
30433+
for (const auto position : *positions) {
30434+
if (have_previous && position <= previous) continue;
30435+
if (!have_next || position < next) {
30436+
have_next = true;
30437+
next = position;
30438+
units = unit.second;
30439+
}
30440+
break;
30441+
}
30442+
}
30443+
if (!have_next) break;
30444+
if (std::isfinite(units) && units > 0.0) total += units;
30445+
have_previous = true;
30446+
previous = next;
30447+
}
30448+
const double expected = std::isfinite(total) && total > 0.0 ? total : 0.0;
30449+
if (std::memcmp(&actual, &expected, sizeof expected) != 0) std::abort();
30450+
}
30451+
#endif
30452+
30453+
} // namespace pineforge::source::detail
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include <cstddef>
4+
#include <cstdint>
5+
#include <unordered_map>
6+
7+
namespace pineforge::source {
8+
class OriginRoster;
9+
10+
namespace detail {
11+
12+
constexpr std::size_t kCohortFoldStack = 128;
13+
14+
struct CohortExposureFold {
15+
double total;
16+
std::uint64_t steps;
17+
std::uint8_t path; // 0: empty, 1: roster walk, 2: sort, 3: overflow fallback.
18+
};
19+
20+
// Allocation-free, acceptance-ordered fold. Diagnostic work is returned to
21+
// the adapter wrapper so observations keep their adapter and run identity.
22+
CohortExposureFold fold_cohort_exposure(
23+
const OriginRoster& origins,
24+
const std::unordered_map<std::uint64_t, double>& live_units_by_origin) noexcept;
25+
26+
#ifdef PINEFORGE_COHORT_EXPOSURE_FIXTURES
27+
// Abort on any bit mismatch against the frozen position selector.
28+
void verify_cohort_exposure(
29+
const OriginRoster& origins,
30+
const std::unordered_map<std::uint64_t, double>& live_units_by_origin,
31+
double actual) noexcept;
32+
#endif
33+
34+
} // namespace detail
35+
} // namespace pineforge::source

0 commit comments

Comments
 (0)