srdatalog.ir.codegen.cuda.main_file¶
Main C++ file emission — the top-level compile unit.
Port of src/srdatalog/codegen.nim:codegenFixpointRuleSets (the parts
that build mir_cpp_str). The main file ties all the per-rule
JIT batch files together into one compilable source containing:
Relation typedefs (AST::RelationSchema per relation)
Database blueprint (AST::Database<rels…>)
Device DB aliases (SemiNaiveDatabase<Blueprint, DeviceRelationType>)
GPU runtime #includes
JitRunner_
forward declaration structs (from complete_runner’s decloutput)Empty namespace
_Plans {} (placeholder, was TMP-only) <Ruleset>_Runnerstruct with:using DB =
_DB load_data() (generates CSV input loading for every relation with
input_file)Per-step
step_N(db, max_iterations)methods — each is just the content ofgen_step_bodyfor that steprun(db, max_iterations) dispatcher that calls step_N with chrono timing + console log, plus print_size stats for every relation tagged
print_size=True
Top-level entry: gen_main_file_content(...) returns the full string.
Scope caveats:
Skips the Nim-only
cpp_str = ...AST::Fixpoint<...>AST dump (removed from Nim too — the comment at codegen.nim:148 confirms)Canonical index tracking for print_size stats is a simplified port (uses relation’s declared canonical index when available; falls back to default column order otherwise).
User-level
datalog_db(<BlueprintName>, <DBCode>)emission is caller responsibility — not part of this module.
Module Contents¶
Functions¶
|
|
DB blueprint + DeviceDB alias inlined into each batch file so
|
|
Emit an |
|
Device DB aliases + GPU runtime includes + JitRunner forward
declaration structs. |
|
Emit the |
|
Emit the full main-file string — mirrors Nim’s |
|
Top-of-file preamble needed for main.cpp to compile as its own TU. In Nim’s pipeline these using-directives come from the .nim driver; for the standalone Python path we emit them explicitly. |
|
Emit |
|
Emit the Runner::run() out-of-line definition in its own TU. Contains the step_0..step_N dispatch + print_size block. |
|
Emit |
|
Non-template Runner struct DECLARATION (for main.cpp). |
|
Schema definitions inlined into each |
|
Emit a standalone .cpp for one step_N out-of-line definition. |
|
Emit ONE large .cpp that contains everything a project needs —
preamble, schemas, DB alias, every JitRunner_X struct body, the
host-side |
API¶
- srdatalog.ir.codegen.cuda.main_file.gen_db_alias(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl]) str[source]¶
using <Ruleset>_DB = AST::Database<...>;— matches the Nim genRelCpp emission (codegen.nim:224).
- srdatalog.ir.codegen.cuda.main_file.gen_db_type_alias_for_batch(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl]) str[source]¶
DB blueprint + DeviceDB alias inlined into each batch file so
JitRunner_<rule>::DBresolves. Mirrors thedbTypeAliasNim passes toJitBatchManager.setDbTypeAlias.ruleset_namehere is the ext_db name (e.g. “TrianglePlan_DB”), not the bare ruleset; the resulting types are<ruleset>_Blueprintand<ruleset>_DeviceDB.
- srdatalog.ir.codegen.cuda.main_file.gen_extern_c_shim(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl]) str[source]¶
Emit an
extern "C"shim the Python ctypes loader can call.Exposes (all C-ABI, return 0 on success / nonzero on error):
srdatalog_init()— init CUDAsrdatalog_load_csv(rel, path)— load_from_file for one relationsrdatalog_run(max_iters)— copy-to-device + _Runner::runsrdatalog_shutdown()— free host DBsrdatalog_size(rel_name)— FULL_VER canonical index size
The shim uses a file-scope
HostDB*holding the live SemiNaiveDatabase so Python can stage data via multipleload_csvcalls beforerun.
- srdatalog.ir.codegen.cuda.main_file.gen_kernel_decls_block(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl], runner_decls: dict[str, str], cache_dir_hint: str, standalone_order: bool = False) str[source]¶
Device DB aliases + GPU runtime includes + JitRunner forward declaration structs.
runner_decls[rule_name]is thedeclstring returned bygen_complete_runner— mirrors Nim’s collecting ofruleDecls[ruleName]in codegen.nim:357.
- srdatalog.ir.codegen.cuda.main_file.gen_load_data_method(decls: list[srdatalog.ir.hir.types.RelationDecl]) str[source]¶
Emit the
load_datastatic template method — reads CSVs for every relation withinput_fileset.
- srdatalog.ir.codegen.cuda.main_file.gen_main_file_content(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl], mir_program: srdatalog.ir.mir.types.Program, step_bodies: list[str], runner_decls: dict[str, str], cache_dir_hint: str = '<jit-cache>', canonical_indices: dict[str, list[int]] | None = None, jit_batch_count: int = 0, emit_preamble: bool = False, extra_index_headers: list[str] | None = None, decl_only_runner: bool = False) str[source]¶
Emit the full main-file string — mirrors Nim’s
mir_cpp_str.Structure: 0. (opt)
#include "srdatalog.h"+using namespacedirectives (emit_preamble=True, default — needed for the standalone Python path where main.cpp is a compilable TU. Pass False to match the pure Nim fragment byte-for-byte.)Relation typedefs + DB alias (
genRelCpp)using namespace SRDatalog::mir::dsl;Kernel-decls block (device DB + GPU includes + JitRunner fwd decls)
namespace <Ruleset>_Plans { }(empty for JIT target)<Ruleset>_Runnerstruct// ======== JIT File-Based Compilation ========summary
- srdatalog.ir.codegen.cuda.main_file.gen_main_file_preamble() str[source]¶
Top-of-file preamble needed for main.cpp to compile as its own TU. In Nim’s pipeline these using-directives come from the .nim driver; for the standalone Python path we emit them explicitly.
- srdatalog.ir.codegen.cuda.main_file.gen_relation_typedefs(decls: list[srdatalog.ir.hir.types.RelationDecl]) str[source]¶
Emit
using <Name> = AST::RelationSchema<decltype("<Name>"_s), <Semiring>, std::tuple<<types>>>;for each relation decl. Used in the main-file scope whereusing namespace SRDatalog::ASTmakes the unqualifiedAST::resolve.Appends a 4th template argument for relations with a non-default
index_type(e.g.Device2LevelIndexfor fat-fact points-to sets).
- srdatalog.ir.codegen.cuda.main_file.gen_run_dispatcher_file(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl], runner_decls: dict[str, str], mir_program: srdatalog.ir.mir.types.Program, canonical_indices: dict[str, list[int]] | None = None, extra_index_headers: list[str] | None = None) str[source]¶
Emit the Runner::run() out-of-line definition in its own TU. Contains the step_0..step_N dispatch + print_size block.
- srdatalog.ir.codegen.cuda.main_file.gen_runner_struct(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl], mir_program: srdatalog.ir.mir.types.Program, step_bodies: list[str], canonical_indices: dict[str, list[int]] | None = None) str[source]¶
Emit
<Ruleset>_Runner— the main orchestrator struct.Args: ruleset_name: base name (e.g. “TrianglePlan”) decls: relation declarations (for load_data + print_size) mir_program: output of compile_to_mir step_bodies: one per step, the full
template <typename DB> static void step_N(...)function body as emitted by gen_step_body. canonical_indices: optional relation→column-order map for print_size stats. If omitted, falls back to default order.
- srdatalog.ir.codegen.cuda.main_file.gen_runner_struct_declonly(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl], mir_program: srdatalog.ir.mir.types.Program) str[source]¶
Non-template Runner struct DECLARATION (for main.cpp).
All step_N and run() become
static void step_N(DB& db, size_t);with the concreteDBtypedef resolved to<Ruleset>_DB_DeviceDB. Bodies live in separate shard files.
- srdatalog.ir.codegen.cuda.main_file.gen_schema_definitions_for_batch(decls: list[srdatalog.ir.hir.types.RelationDecl]) str[source]¶
Schema definitions inlined into each
jit_batch_N.cpp. Mirrors Nim’scollectSchemaDefinitionsoutput: fully-qualifiedSRDatalog::AST::RelationSchema(nousing namespaceumbrella in the batch file scope) plus theLiteralsusing-directive for the_sUDL.Pass this to
cache.JitBatchManager.set_schema_definitions(...)/write_jit_project(schema_definitions=...).
- srdatalog.ir.codegen.cuda.main_file.gen_step_shard_file(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl], runner_decls: dict[str, str], mir_program: srdatalog.ir.mir.types.Program, step_bodies: list[str], step_idx: int, extra_index_headers: list[str] | None = None) str[source]¶
Emit a standalone .cpp for one step_N out-of-line definition.
Each shard repeats the schema/DB/GPU-runtime preamble so it is a valid TU — same cost as the batch files’ self-contained layout. The gain is parallelism: N shards compile alongside the batches instead of serializing through main.cpp.
- srdatalog.ir.codegen.cuda.main_file.gen_unity_main_file_content(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl], mir_program: srdatalog.ir.mir.types.Program, step_bodies: list[str], runner_decls: dict[str, str], per_rule_runner_bodies: list[tuple[str, str]], *, canonical_indices: dict[str, list[int]] | None = None, extra_index_headers: list[str] | None = None) str[source]¶
Emit ONE large .cpp that contains everything a project needs — preamble, schemas, DB alias, every JitRunner_X struct body, the host-side
_Runnerwith step bodies, and the extern “C” shim.Compiling this single TU is much faster than compiling main.cpp + N batch files when PCH is unavailable, because
srdatalog.h(the ~500 KB boost/hana/RMM header stack) parses ONCE instead of N times. For doop that’s ~100s → ~20s on cold compile.