ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
CircuitFromMMap.cpp File Reference

Build in-memory circuits from the mmap-backed store. More...

#include <cmath>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
#include "CircuitFromMMap.h"
#include "MMappedCircuit.h"
#include "HybridEvaluator.h"
#include "RangeCheck.h"
#include "having_semantics.hpp"
#include "semiring/BoolExpr.h"
#include "provsql_utils_cpp.h"
#include "provsql_interrupt.h"
#include <vector>
#include "miscadmin.h"
#include "provsql_shmem.h"
#include "provsql_mmap.h"
#include "provsql_utils.h"
Include dependency graph for CircuitFromMMap.cpp:

Go to the source code of this file.

Functions

static unsigned long circuit_budget ()
 What the statement has left of provsql.max_memory, in bytes, for building a circuit; 0 for no limit.
static void circuit_over_budget ()
 Stop at provsql.max_memory: the circuit to load is larger than the statement has left of it.
static void circuit_load_cancelled ()
 Stop for a cancel (or a terminate) of the statement, received while its circuit was loaded: as the evaluation loops do, by a throw that the entry point turns into PostgreSQL's own error.
static char * readCircuitReply (unsigned long &size, char message_char)
 Read the worker's answer to a 'g' / 'j' request just sent, and release the store lock.
template<typename C>
static C getCircuitFromMMap (pg_uuid_t token, char message_char)
 Read and deserialise a circuit rooted at token from the mmap worker.
BooleanCircuit getBooleanCircuit (GenericCircuit &gc, pg_uuid_t token, gate_t &gate, std::unordered_map< gate_t, gate_t > &gc_to_bc)
 Build a BooleanCircuit from an already-loaded GenericCircuit.
void propagateDNNFCertificate (const GenericCircuit &gc, const std::unordered_map< gate_t, gate_t > &gc_to_bc, BooleanCircuit &c)
 Propagate the per-gate d-DNNF certificate from gc to c.
BooleanCircuit getBooleanCircuit (pg_uuid_t token, gate_t &gate)
 Build a BooleanCircuit from the mmap store rooted at token.
static void applyLoadTimeSimplification (GenericCircuit &gc)
 Apply the universal load-time simplification passes to gc.
void applyProbabilisticSimplification (GenericCircuit &gc)
 Simplify a loaded circuit for a probability readout.
GenericCircuit getGenericCircuit (pg_uuid_t token)
 Build a GenericCircuit from the mmap store rooted at token.
static GenericCircuit getJointCircuitFromMMap (const std::vector< pg_uuid_t > &tokens)
 IPC: ship a 'j' (joint) request to the mmap worker and deserialise the returned GenericCircuit.
FormulaRegions getFormulaRegions (const std::vector< FormulaRequest > &requests, unsigned long cap, unsigned long wide_cap)
 The regions of requests, for sr_formula_rows (see FormulaRegions), with the load-time simplification applied.
GenericCircuit getJointCircuit (const std::vector< pg_uuid_t > &tokens, std::vector< gate_t > &gates)
 Multi-root variant of getJointCircuit.
GenericCircuit getJointCircuit (pg_uuid_t root_token, pg_uuid_t event_token, gate_t &root_gate, gate_t &event_gate)
 Build a GenericCircuit containing the closures of two roots, with shared subgraphs unified.

Detailed Description

Build in-memory circuits from the mmap-backed store.

Implements the free functions declared in CircuitFromMMap.h:

The internal getCircuitFromMMap<C>() template handles the IPC protocol: it sends a request through the shared-memory pipe, receives a Boost-serialised circuit blob from the background worker, and deserialises it into the appropriate circuit type.

Definition in file CircuitFromMMap.cpp.

Function Documentation

◆ applyLoadTimeSimplification()

void applyLoadTimeSimplification ( GenericCircuit & gc)
static

Apply the universal load-time simplification passes to gc.

Extracted from getGenericCircuit so the joint-circuit loader (getJointCircuit) runs the same passes on its multi-root output. Gated by the provsql.simplify_on_load GUC, identical semantics.

Definition at line 220 of file CircuitFromMMap.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ applyProbabilisticSimplification()

void applyProbabilisticSimplification ( GenericCircuit & gc)

Simplify a loaded circuit for a probability readout.

The load-time passes of getGenericCircuit / getJointCircuit read no probability, so the circuit they return keeps every tuple token and serves every consumer, the displayed circuit included. A readout of a probability or of a distribution (expectation, moments, quantiles, samples, histograms, information measures, event probabilities) may further read a mixture whose selector is certain by the probabilities of its inputs as its surviving arm; this applies that rewrite (foldDegenerateMixtures with read_probabilities) and the constant folding it enables. Gated, like the load-time passes, by provsql.simplify_on_load. Call it right after loading, before any evaluation.

Parameters
gcCircuit, rewritten in place.

Definition at line 270 of file CircuitFromMMap.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ circuit_budget()

unsigned long circuit_budget ( )
static

What the statement has left of provsql.max_memory, in bytes, for building a circuit; 0 for no limit.

The circuit store builds and serialises the circuit in the worker, a process of its own, which the budget would not see otherwise; the request carries what is left of it, and the worker refuses past it (GitHub #104).

Definition at line 47 of file CircuitFromMMap.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ circuit_load_cancelled()

void circuit_load_cancelled ( )
static

Stop for a cancel (or a terminate) of the statement, received while its circuit was loaded: as the evaluation loops do, by a throw that the entry point turns into PostgreSQL's own error.

Definition at line 72 of file CircuitFromMMap.cpp.

Here is the caller graph for this function:

◆ circuit_over_budget()

void circuit_over_budget ( )
static

Stop at provsql.max_memory: the circuit to load is larger than the statement has left of it.

Definition at line 58 of file CircuitFromMMap.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getBooleanCircuit() [1/2]

BooleanCircuit getBooleanCircuit ( GenericCircuit & gc,
pg_uuid_t token,
gate_t & gate,
std::unordered_map< gate_t, gate_t > & gc_to_bc )

Build a BooleanCircuit from an already-loaded GenericCircuit.

Variant of getBooleanCircuit() that reuses an existing GenericCircuit (typically obtained via getGenericCircuit()) and additionally exposes the GenericCircuit-to-BooleanCircuit gate mapping, so that callers can translate input-side annotations (e.g., user-supplied leaf labels) from gc to the constructed BooleanCircuit.

Parameters
gcSource generic circuit (mutated through HAVING evaluation).
tokenUUID of the root gate.
gateOutput: gate_t of the root within the returned circuit.
gc_to_bcOutput: mapping from gc input/mulinput gates to the corresponding gates in the returned BooleanCircuit.
Returns
An in-memory BooleanCircuit.

Definition at line 160 of file CircuitFromMMap.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getBooleanCircuit() [2/2]

BooleanCircuit getBooleanCircuit ( pg_uuid_t token,
gate_t & gate )

Build a BooleanCircuit from the mmap store rooted at token.

Performs a depth-first traversal of the persistent circuit starting at token, translating each gate_type to the corresponding BooleanGate and copying probabilities and info integers.

On return, gate is set to the gate_t identifier within the returned circuit that corresponds to token.

Parameters
tokenUUID of the root gate.
gateOutput: gate_t of the root within the returned circuit.
Returns
An in-memory BooleanCircuit.

Definition at line 206 of file CircuitFromMMap.cpp.

Here is the call graph for this function:

◆ getCircuitFromMMap()

template<typename C>
C getCircuitFromMMap ( pg_uuid_t token,
char message_char )
static

Read and deserialise a circuit rooted at token from the mmap worker.

Template Parameters
CCircuit type to deserialise (BooleanCircuit or GenericCircuit).
Parameters
tokenUUID of the root gate to retrieve.
message_charIPC message-type byte sent to the background worker.
Returns
Deserialised circuit of type C.

Definition at line 123 of file CircuitFromMMap.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getFormulaRegions()

FormulaRegions getFormulaRegions ( const std::vector< FormulaRequest > & requests,
unsigned long cap,
unsigned long wide_cap )

The regions of requests, for sr_formula_rows (see FormulaRegions), with the load-time simplification applied.

Parameters
requestsThe gates, or ranges of their children, asked for.
capSize, in symbols, past which a gate is named.
wide_capSize past which a named gate's row is split into one row per child.

Definition at line 377 of file CircuitFromMMap.cpp.

Here is the call graph for this function:

◆ getGenericCircuit()

GenericCircuit getGenericCircuit ( pg_uuid_t token)

Build a GenericCircuit from the mmap store rooted at token.

Equivalent to createGenericCircuit() declared in MMappedCircuit.h. Performs a depth-first traversal and copies all gate metadata (type, info1/info2, extra strings, probabilities) into the returned GenericCircuit.

Parameters
tokenUUID of the root gate.
Returns
An in-memory GenericCircuit.

Definition at line 286 of file CircuitFromMMap.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getJointCircuit() [1/2]

GenericCircuit getJointCircuit ( const std::vector< pg_uuid_t > & tokens,
std::vector< gate_t > & gates )

Multi-root variant of getJointCircuit.

Loads the union of the closures of tokens in a single in-memory GenericCircuit, with the same shared-subgraph unification and load-time simplification guarantees as the two-root form. On return gates holds the resolved gate_t of each input UUID, in the order of tokens.

Parameters
tokensRoot UUIDs (at least one).
gatesOutput: resolved gate_t per token.
Returns
An in-memory GenericCircuit.

Definition at line 436 of file CircuitFromMMap.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getJointCircuit() [2/2]

GenericCircuit getJointCircuit ( pg_uuid_t root_token,
pg_uuid_t event_token,
gate_t & root_gate,
gate_t & event_gate )

Build a GenericCircuit containing the closures of two roots, with shared subgraphs unified.

Loads the union of every gate reachable from root_token and every gate reachable from event_token in a single in-memory GenericCircuit. Because GenericCircuit's UUID-to-gate mapping is idempotent, a gate_rv (or any other gate) reachable from both roots gets exactly one gate_t – the property the conditional MC sampler needs to couple the indicator's draw with the value's draw through Sampler's per-iteration cache.

The on-load simplification passes that getGenericCircuit runs (gated by provsql.simplify_on_load) are applied to the joint circuit too, followed by applyProbabilisticSimplification: every caller of this loader is a probability or distribution readout. The passes are in-place: gate types may change, but UUID-to-gate_t mappings stay valid, so callers can resolve their two roots via getGate after the call returns.

Output parameters root_gate and event_gate are the resolved gate_t for the two input UUIDs.

Parameters
root_tokenFirst root UUID (e.g. an RV's gate).
event_tokenSecond root UUID (e.g. the conditioning gate).
root_gateOutput: gate_t for root_token in the returned circuit.
event_gateOutput: gate_t for event_token in the returned circuit.
Returns
An in-memory GenericCircuit.

Definition at line 458 of file CircuitFromMMap.cpp.

Here is the call graph for this function:

◆ getJointCircuitFromMMap()

GenericCircuit getJointCircuitFromMMap ( const std::vector< pg_uuid_t > & tokens)
static

IPC: ship a 'j' (joint) request to the mmap worker and deserialise the returned GenericCircuit.

Mirrors getCircuitFromMMap but writes the multi-root payload the worker's 'j' handler expects: 'j' Oid nb_roots {pg_uuid_t}*. The response shape is identical to 'g' – unsigned long size prefix followed by a Boost-serialised GenericCircuit.

Definition at line 327 of file CircuitFromMMap.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ propagateDNNFCertificate()

void propagateDNNFCertificate ( const GenericCircuit & gc,
const std::unordered_map< gate_t, gate_t > & gc_to_bc,
BooleanCircuit & c )

Propagate the per-gate d-DNNF certificate from gc to c.

For every gate_plus / gate_times of gc carrying the DNNF_CERT_INFO mark whose BoolExpr image in c is an OR / AND gate (the zero/one shortcuts of GenericCircuit::evaluate may map a gate to a constant instead, in which case the mark is moot), set the mark on the image, so independentEvaluation() and interpretAsDD() can exploit determinism / decomposability. Requires gc_to_bc to cover internal gates, which GenericCircuit::evaluate guarantees (it memoises every visited gate).

Parameters
gcSource generic circuit.
gc_to_bcGate mapping produced by the BoolExpr evaluation.
cTarget Boolean circuit.

Definition at line 189 of file CircuitFromMMap.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readCircuitReply()

char * readCircuitReply ( unsigned long & size,
char message_char )
static

Read the worker's answer to a 'g' / 'j' request just sent, and release the store lock.

Returns
The serialised circuit, of size bytes, to be freed by the caller; what stops the load (the budget, a cancel) is thrown.

Definition at line 86 of file CircuitFromMMap.cpp.

Here is the call graph for this function:
Here is the caller graph for this function: