SO I WAS JUST WATCHING THIS VIDEO ABOUT RUSSIA AND THE WAR IN UKRAINE, AND IT WAS SAYING THAT 10% OF ALL ASSETS OWNED BY RUSSIAN BANKS ARE ON THE VERGE OF COLLAPSE OR “PROBLEMATIC ASSETS” PROBLEMATIC ASSETS ARE THOSE THAT THE BANK WILL BE UNLIKELY TO EVER SEE A RETURN FROM, NOW THAT CAN BE KIND OF SCARY RIGHT?
10%? THAT’S ALL YOU’VE GOT ON MR. PUTIN? THAT’S IT? 10% OF ASSETS OWNED BY HIS BANKS? YEA SURE IT’S GONNA LOSE ALOT OF MONEY FOR THE BANKS BUT, 10 PERCENT IS LIKE UNLIKELY TO EVEN BE FELT BY THE GENERAL PUBLIC… IT SEEMS WE HAVE A MISINFORMATION CAMPAIGN BEING WAGED OUT THERE IN YOUTUBE.
I WOULD SUGGEST VIEWING ANYTHING ON YOUTUBE (AND THE ENTIRE INTERNET) WITH A HIGHLY SKEPTICAL EYE AND PERHAPS CALLING LIKE ALL OF IT BULLSHIT, UNLESS IT’S TECH RELATED, HOW CAN YOU KNOW WHATS REAL AND WHAT ISN’T?
ANYWAYS, I WAS JUST THINKING TODAY THAT THE ENTIRE RUSSIAN STATE WAS GOING TO JUST COLLAPSE FROM ALL OF THIS WHEN I FINALLY REALIZED JUST NOW AFTER WATCHING THAT VIDEO THAT IT’S NOWHERE NEAR COLLAPSING, PUTIN COULD PROBABLY CARRY ON FOR LIKE 6-7 MORE YEARS BEFORE HE REALLY RUNS “AGROUND”
THAT WOULD BE MY ESTIMATE, BUT WHO KNOWS.. MAYBE UKRAINE WILL WIN THIS THING? NOT THAT I’M ROOTING FOR UKRAINE, I AM ROOTING FOR PUTIN AND RUSSIA BUT THAT IS ALMOST ANOTHER STORY, THIS COMING FROM A PUTIN SUPPORTER MIGHT HAVE YOU BELIEVING THAT IT IN ITSELF IS BS, IT ISNT, I AM ALWAYS FAIR ABOUT THESE TYPES OF THINGS AND THIS IS ONE OF THOSE THINGS, NOW WHY WOULD ANYONE BLATANTLY MAKE UP SOMETHING LIKE THAT AND THEN MAKE VIDEOS ABOUT IT? TO GRAB YOUR ATTENTION? WHY WOULD ANYONE DO THAT?
ARE THERE STUPID PEOPLE OUT THERE OR SOMETHING? THEY NEED UPGRADING! I WILL UPGRADE THEM WITH THE JAMES DEAN PROGRAM!
C++ #include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdint>
#include <immintrin.h>
#include <omp.h>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <unordered_map>
#include <chrono>
#include <limits>
#include <sstream>
#include <iomanip>
#include <sys/resource.h> // For memory tracking (getrusage)
#include <unistd.h> // For sysconf, _SC_PAGESIZE
#include <sys/times.h> // For CPU time tracking
#include <atomic> // For thread-safe progress tracking
#include <mutex> // For progress bar updates
#include <thread> // For async progress display
// PCG Random Number Generator (extracted to separate header)
#include "pcg_rng.h"
// ============================================================================
// ULTRA VERBOSE MODE - COMPLETE VERBOSITY CONTROL SYSTEM
// ============================================================================
//
// COMPILE-TIME CONTROL:
// Default (normal mode): clang++ -O3 -march=native -fopenmp -std=c++17 -o main main.cpp
// Ultra-verbose mode: clang++ -DULTRA_VERBOSE=true -O3 -march=native -fopenmp -std=c++17 -o main main.cpp
//
// VERBOSITY LEVELS:
// ULTRA_VERBOSE=false -> Normal output (stage progress, summaries only)
// ULTRA_VERBOSE=true -> Full tracing (variables, operations, statements)
//
// SAMPLE RATE CONTROL:
// Adjust VERBOSE_SAMPLE_RATE to control output density:
// - 1 = Print EVERY operation (EXTREMELY slow, massive output)
// - 1000 = Print every 1000th operation (very detailed)
// - 100000 = Print every 100,000th operation (default, manageable)
// - 500000 = Print once per chunk (minimal tracing)
//
// OUTPUT FORMAT:
// [VAR] variable_name = value @ context
// [OP] OPERATION: input1 + input2 -> output @ context
// [STAGE] STAGE_NAME | Chunk N | details
// [CIV id] field = value
//
// WARNING: Ultra-verbose mode generates MASSIVE output (potentially GBs)
// Use only for debugging small samples or specific civilians
// ============================================================================
#ifndef ULTRA_VERBOSE
#define ULTRA_VERBOSE false
#endif
// Verbosity sample rate: only print 1 in N assignments to reduce output
// Lower = more output, Higher = less output
#ifndef VERBOSE_SAMPLE_RATE
constexpr int64_t VERBOSE_SAMPLE_RATE = 100000 ; // Print every 100,000th assignment
#else
constexpr int64_t VERBOSE_SAMPLE_RATE = VERBOSE_SAMPLE_RATE;
#endif
// Specific civilian tracing: set to a civilian ID to trace only that civilian
// Set to -1 to use sample rate instead
#ifndef TRACE_SPECIFIC_CIVILIAN
constexpr int64_t TRACE_SPECIFIC_CIVILIAN = - 1 ; // -1 = use sample rate
#endif
// ============================================================================
// FILE LOGGING SYSTEM - Dual output to console AND log file
// ============================================================================
#ifndef LOG_FILENAME
#define LOG_FILENAME "james_dean_trace.log"
#endif
// JSON output format flag
#ifndef JSON_OUTPUT
#define JSON_OUTPUT false
#endif
#ifndef JSON_FILENAME
#define JSON_FILENAME "james_dean_trace.json"
#endif
// ============================================================================
// TIMESTAMP SYSTEM - High resolution timing for performance profiling
// ============================================================================
std::chrono::high_resolution_clock::time_point g_start_time;
inline void init_timer () {
g_start_time = std::chrono::high_resolution_clock:: now ();
}
// Get elapsed time in milliseconds since program start
inline double get_elapsed_ms () {
auto now = std::chrono::high_resolution_clock:: now ();
return std::chrono:: duration < double , std:: milli >(now - g_start_time). count ();
}
// Get elapsed time as formatted string [HH:MM:SS.mmm]
inline std:: string get_timestamp () {
double ms = get_elapsed_ms ();
int total_seconds = static_cast< int > (ms / 1000.0 );
int hours = total_seconds / 3600 ;
int minutes = (total_seconds % 3600 ) / 60 ;
int seconds = total_seconds % 60 ;
int millis = static_cast< int > (ms) % 1000 ;
std::ostringstream oss;
oss << "[" << std:: setfill ( '0' ) << std:: setw ( 2 ) << hours << ":"
<< std:: setw ( 2 ) << minutes << ":"
<< std:: setw ( 2 ) << seconds << "."
<< std:: setw ( 3 ) << millis << "]" ;
return oss . str ();
}
// ============================================================================
// MEMORY TRACKING SYSTEM - Monitor memory usage throughout execution
// ============================================================================
struct MemoryStats {
long peak_rss_kb = 0 ; // Peak resident set size
long current_rss_kb = 0 ; // Current resident set size
long page_faults = 0 ; // Page faults
long voluntary_ctx = 0 ; // Voluntary context switches
long involuntary_ctx = 0 ; // Involuntary context switches
};
inline MemoryStats get_memory_stats () {
MemoryStats stats;
struct rusage usage ;
if ( getrusage (RUSAGE_SELF, & usage) == 0 ) {
stats . peak_rss_kb = usage . ru_maxrss ; // In KB on Linux
stats . page_faults = usage . ru_majflt + usage . ru_minflt ;
stats . voluntary_ctx = usage . ru_nvcsw ;
stats . involuntary_ctx = usage . ru_nivcsw ;
}
// Read current RSS from /proc/self/statm
std::ifstream statm ( "/proc/self/statm" );
if ( statm . is_open ()) {
long pages, resident;
statm >> pages >> resident;
stats . current_rss_kb = resident * ( sysconf (_SC_PAGESIZE) / 1024 );
}
return stats;
}
inline std:: string format_memory_mb ( long kb ) {
std::ostringstream oss;
oss << std::fixed << std:: setprecision ( 2 ) << (kb / 1024.0 ) << "MB" ;
return oss . str ();
}
// Memory tracking macro
#define TRACE_MEMORY ( label ) \
if constexpr (ULTRA_VERBOSE) { \
auto _mem = get_memory_stats(); \
std::ostringstream _oss; \
_oss << get_timestamp() << " [MEM] " << label \
<< " | RSS: " << format_memory_mb(_mem.current_rss_kb) \
<< " | Peak: " << format_memory_mb(_mem.peak_rss_kb) \
<< " | PgFaults: " << _mem.page_faults; \
DUAL_OUT(_oss.str()) \
}
// ============================================================================
// CPU USAGE TRACKING - Per-thread and total CPU time monitoring
// ============================================================================
struct CPUStats {
double user_time_sec = 0.0 ; // User mode CPU time
double system_time_sec = 0.0 ; // Kernel mode CPU time
double total_time_sec = 0.0 ; // Total CPU time
double cpu_percent = 0.0 ; // CPU utilization percentage
int num_threads = 0 ; // Number of active threads
};
std::chrono::high_resolution_clock::time_point g_last_cpu_check;
double g_last_cpu_time = 0.0 ;
inline void init_cpu_tracking () {
g_last_cpu_check = std::chrono::high_resolution_clock:: now ();
struct tms times_buf ;
times ( & times_buf);
long ticks_per_sec = sysconf (_SC_CLK_TCK);
g_last_cpu_time = ( times_buf . tms_utime + times_buf . tms_stime ) / ( double )ticks_per_sec;
}
inline CPUStats get_cpu_stats () {
CPUStats stats;
struct tms times_buf ;
clock_t now_ticks = times ( & times_buf);
long ticks_per_sec = sysconf (_SC_CLK_TCK);
stats . user_time_sec = times_buf . tms_utime / ( double )ticks_per_sec;
stats . system_time_sec = times_buf . tms_stime / ( double )ticks_per_sec;
stats . total_time_sec = stats . user_time_sec + stats . system_time_sec ;
// Calculate CPU percentage since last check
auto now = std::chrono::high_resolution_clock:: now ();
double wall_elapsed = std::chrono:: duration < double >(now - g_last_cpu_check). count ();
double cpu_elapsed = stats . total_time_sec - g_last_cpu_time;
if (wall_elapsed > 0.001 ) {
stats . cpu_percent = (cpu_elapsed / wall_elapsed) * 100.0 ;
g_last_cpu_check = now;
g_last_cpu_time = stats . total_time_sec ;
}
stats . num_threads = omp_get_max_threads ();
return stats;
}
// Per-thread CPU tracking
struct ThreadCPUStats {
double thread_times [ 128 ] = { 0 }; // Up to 128 threads
int active_threads = 0 ;
};
ThreadCPUStats g_thread_stats;
inline void record_thread_cpu ( int thread_id , double time_spent ) {
if (thread_id >= 0 && thread_id < 128 ) {
#pragma omp atomic
g_thread_stats . thread_times [thread_id] += time_spent;
}
}
#define TRACE_CPU ( label ) \
if constexpr (ULTRA_VERBOSE) { \
auto _cpu = get_cpu_stats(); \
std::ostringstream _oss; \
_oss << get_timestamp() << " [CPU] " << label \
<< " | User: " << std::fixed << std::setprecision(2) << _cpu.user_time_sec << "s" \
<< " | Sys: " << _cpu.system_time_sec << "s" \
<< " | Total: " << _cpu.total_time_sec << "s" \
<< " | Util: " << _cpu.cpu_percent << "%" \
<< " | Threads: " << _cpu.num_threads; \
DUAL_OUT(_oss.str()) \
if constexpr (JSON_OUTPUT) { \
std::ostringstream _val; _val << _cpu.cpu_percent << "% (" << _cpu.total_time_sec << "s)"; \
write_json_event("CPU", label, _val.str(), ""); \
} \
}
// ============================================================================
// NETWORK/IPC TRACING - Infrastructure for distributed runs
// ============================================================================
#ifndef ENABLE_IPC
#define ENABLE_IPC false
#endif
#ifndef NODE_ID
#define NODE_ID 0
#endif
#ifndef TOTAL_NODES
#define TOTAL_NODES 1
#endif
struct IPCMessage {
int64_t timestamp_ms;
int source_node;
int dest_node;
std::string msg_type; // "SYNC", "DATA", "RESULT", "HEARTBEAT"
std::string payload;
size_t payload_size;
};
struct IPCStats {
std::atomic < int64_t > messages_sent{ 0 };
std::atomic < int64_t > messages_received{ 0 };
std::atomic < int64_t > bytes_sent{ 0 };
std::atomic < int64_t > bytes_received{ 0 };
std::atomic < int64_t > sync_barriers{ 0 };
};
IPCStats g_ipc_stats;
std::vector < IPCMessage > g_ipc_log;
std::mutex g_ipc_mutex;
inline void log_ipc_send ( int dest , const std:: string & type , size_t size ) {
if constexpr ( ! ENABLE_IPC) return ;
g_ipc_stats . messages_sent ++ ;
g_ipc_stats . bytes_sent += size;
IPCMessage msg;
msg . timestamp_ms = static_cast< int64_t > ( get_elapsed_ms ());
msg . source_node = NODE_ID;
msg . dest_node = dest;
msg . msg_type = type;
msg . payload_size = size;
std::lock_guard < std::mutex > lock (g_ipc_mutex);
g_ipc_log . push_back (msg);
}
inline void log_ipc_recv ( int source , const std:: string & type , size_t size ) {
if constexpr ( ! ENABLE_IPC) return ;
g_ipc_stats . messages_received ++ ;
g_ipc_stats . bytes_received += size;
IPCMessage msg;
msg . timestamp_ms = static_cast< int64_t > ( get_elapsed_ms ());
msg . source_node = source;
msg . dest_node = NODE_ID;
msg . msg_type = type;
msg . payload_size = size;
std::lock_guard < std::mutex > lock (g_ipc_mutex);
g_ipc_log . push_back (msg);
}
inline void log_ipc_barrier () {
if constexpr ( ! ENABLE_IPC) return ;
g_ipc_stats . sync_barriers ++ ;
}
#define TRACE_IPC_SEND ( dest , type , size ) \
if constexpr (ENABLE_IPC && ULTRA_VERBOSE) { \
log_ipc_send(dest, type, size); \
std::ostringstream _oss; \
_oss << get_timestamp() << " [IPC] SEND -> Node " << dest \
<< " | Type: " << type << " | Size: " << size << " bytes"; \
DUAL_OUT(_oss.str()) \
}
#define TRACE_IPC_RECV ( source , type , size ) \
if constexpr (ENABLE_IPC && ULTRA_VERBOSE) { \
log_ipc_recv(source, type, size); \
std::ostringstream _oss; \
_oss << get_timestamp() << " [IPC] RECV <- Node " << source \
<< " | Type: " << type << " | Size: " << size << " bytes"; \
DUAL_OUT(_oss.str()) \
}
#define TRACE_IPC_BARRIER () \
if constexpr (ENABLE_IPC && ULTRA_VERBOSE) { \
log_ipc_barrier(); \
std::ostringstream _oss; \
_oss << get_timestamp() << " [IPC] BARRIER | Sync #" << g_ipc_stats.sync_barriers; \
DUAL_OUT(_oss.str()) \
}
inline void print_ipc_summary () {
if constexpr ( ! ENABLE_IPC) return ;
std::cout << " \n --- IPC SUMMARY (Node " << NODE_ID << "/" << TOTAL_NODES << ") ---" << std::endl;
std::cout << "Messages Sent: " << g_ipc_stats . messages_sent << " (" << ( g_ipc_stats . bytes_sent / 1024 ) << " KB)" << std::endl;
std::cout << "Messages Recv: " << g_ipc_stats . messages_received << " (" << ( g_ipc_stats . bytes_received / 1024 ) << " KB)" << std::endl;
std::cout << "Sync Barriers: " << g_ipc_stats . sync_barriers << std::endl;
std::cout << "-------------------------------" << std::endl;
}
// ============================================================================
// PROGRESS BAR WITH ETA - Real-time progress tracking
// ============================================================================
struct ProgressBar {
std::atomic < int64_t > current{ 0 };
int64_t total = 0 ;
std::string label;
std::chrono::high_resolution_clock::time_point start_time;
bool active = false ;
int bar_width = 40 ;
std::mutex update_mutex;
void start ( const std:: string & lbl , int64_t tot ) {
label = lbl;
total = tot;
current = 0 ;
start_time = std::chrono::high_resolution_clock:: now ();
active = true ;
display ();
}
void update ( int64_t val ) {
current = val;
display ();
}
void increment ( int64_t delta = 1 ) {
current += delta;
// Only update display every 1% or so to avoid console spam
int64_t step = std:: max ( static_cast< int64_t > ( 1 ), total / 100 );
if (current % step == 0 ) {
display ();
}
}
void display () {
if ( ! active) return ;
std::lock_guard < std::mutex > lock (update_mutex);
double progress = (total > 0 ) ? ( double )current / total : 0.0 ;
int filled = static_cast< int > (progress * bar_width);
// Calculate ETA
auto now = std::chrono::high_resolution_clock:: now ();
double elapsed_sec = std::chrono:: duration < double >(now - start_time). count ();
double eta_sec = 0.0 ;
std::string eta_str = "--:--" ;
if (progress > 0.01 && elapsed_sec > 0.5 ) {
eta_sec = (elapsed_sec / progress) * ( 1.0 - progress);
int eta_min = static_cast< int > (eta_sec) / 60 ;
int eta_s = static_cast< int > (eta_sec) % 60 ;
std::ostringstream oss;
oss << std:: setfill ( '0' ) << std:: setw ( 2 ) << eta_min << ":" << std:: setw ( 2 ) << eta_s;
eta_str = oss . str ();
}
// Calculate speed
double items_per_sec = (elapsed_sec > 0.1 ) ? current / elapsed_sec : 0.0 ;
std::string speed_str;
if (items_per_sec >= 1000000 ) {
std::ostringstream oss;
oss << std::fixed << std:: setprecision ( 1 ) << (items_per_sec / 1000000 ) << "M/s" ;
speed_str = oss . str ();
} else if (items_per_sec >= 1000 ) {
std::ostringstream oss;
oss << std::fixed << std:: setprecision ( 1 ) << (items_per_sec / 1000 ) << "K/s" ;
speed_str = oss . str ();
} else {
std::ostringstream oss;
oss << std::fixed << std:: setprecision ( 0 ) << items_per_sec << "/s" ;
speed_str = oss . str ();
}
// Build progress bar
std::ostringstream bar;
bar << " \r " << label << " [" ;
for ( int i = 0 ; i < bar_width; ++ i) {
if (i < filled) bar << "=" ;
else if (i == filled) bar << ">" ;
else bar << " " ;
}
bar << "] " << std::fixed << std:: setprecision ( 1 ) << (progress * 100 ) << "%"
<< " | " << current << "/" << total
<< " | " << speed_str
<< " | ETA: " << eta_str << " " ;
std::cout << bar . str () << std::flush;
}
void finish () {
if ( ! active) return ;
current = total;
display ();
std::cout << std::endl; // New line after completion
active = false ;
// Print final timing
auto now = std::chrono::high_resolution_clock:: now ();
double elapsed_sec = std::chrono:: duration < double >(now - start_time). count ();
std::cout << label << " completed in " << std::fixed << std:: setprecision ( 2 ) << elapsed_sec << "s" << std::endl;
}
};
// Global progress bar instance
ProgressBar g_progress;
#define PROGRESS_START ( label , total ) g_progress.start(label, total)
#define PROGRESS_UPDATE ( val ) g_progress.update(val)
#define PROGRESS_INCREMENT ( delta ) g_progress.increment(delta)
#define PROGRESS_FINISH () g_progress.finish()
// ============================================================================
// SIMD OPERATION COUNTERS - Track vectorized operations
// ============================================================================
struct SIMDStats {
std::atomic < int64_t > avx2_loads{ 0 }; // _mm256_load_ps
std::atomic < int64_t > avx2_stores{ 0 }; // _mm256_store_ps
std::atomic < int64_t > avx2_adds{ 0 }; // _mm256_add_ps
std::atomic < int64_t > avx2_subs{ 0 }; // _mm256_sub_ps
std::atomic < int64_t > avx2_muls{ 0 }; // _mm256_mul_ps
std::atomic < int64_t > avx2_divs{ 0 }; // _mm256_div_ps
std::atomic < int64_t > avx2_fmas{ 0 }; // _mm256_fmadd_ps
std::atomic < int64_t > avx2_broadcasts{ 0 }; // _mm256_set1_ps
std::atomic < int64_t > avx2_shuffles{ 0 }; // shuffles/permutes
std::atomic < int64_t > avx2_compares{ 0 }; // comparisons
std::atomic < int64_t > avx2_bitwise{ 0 }; // and/or/xor
std::atomic < int64_t > scalar_ops{ 0 }; // Non-SIMD fallback ops
std::atomic < int64_t > total_vectors{ 0 }; // Total 256-bit vectors processed
std::atomic < int64_t > total_floats{ 0 }; // Total floats processed (vectors * 8)
void reset () {
avx2_loads = avx2_stores = avx2_adds = avx2_subs = 0 ;
avx2_muls = avx2_divs = avx2_fmas = avx2_broadcasts = 0 ;
avx2_shuffles = avx2_compares = avx2_bitwise = scalar_ops = 0 ;
total_vectors = total_floats = 0 ;
}
int64_t total_simd_ops () const {
return avx2_loads + avx2_stores + avx2_adds + avx2_subs +
avx2_muls + avx2_divs + avx2_fmas + avx2_broadcasts +
avx2_shuffles + avx2_compares + avx2_bitwise;
}
};
SIMDStats g_simd_stats;
// SIMD counting macros (only active in ULTRA_VERBOSE mode)
#define COUNT_SIMD_LOAD () if constexpr (ULTRA_VERBOSE) { g_simd_stats.avx2_loads++; g_simd_stats.total_vectors++; g_simd_stats.total_floats += 8; }
#define COUNT_SIMD_STORE () if constexpr (ULTRA_VERBOSE) { g_simd_stats.avx2_stores++; }
#define COUNT_SIMD_ADD () if constexpr (ULTRA_VERBOSE) { g_simd_stats.avx2_adds++; }
#define COUNT_SIMD_SUB () if constexpr (ULTRA_VERBOSE) { g_simd_stats.avx2_subs++; }
#define COUNT_SIMD_MUL () if constexpr (ULTRA_VERBOSE) { g_simd_stats.avx2_muls++; }
#define COUNT_SIMD_DIV () if constexpr (ULTRA_VERBOSE) { g_simd_stats.avx2_divs++; }
#define COUNT_SIMD_FMA () if constexpr (ULTRA_VERBOSE) { g_simd_stats.avx2_fmas++; }
#define COUNT_SIMD_BROADCAST () if constexpr (ULTRA_VERBOSE) { g_simd_stats.avx2_broadcasts++; }
#define COUNT_SIMD_SHUFFLE () if constexpr (ULTRA_VERBOSE) { g_simd_stats.avx2_shuffles++; }
#define COUNT_SIMD_CMP () if constexpr (ULTRA_VERBOSE) { g_simd_stats.avx2_compares++; }
#define COUNT_SIMD_BITWISE () if constexpr (ULTRA_VERBOSE) { g_simd_stats.avx2_bitwise++; }
#define COUNT_SCALAR_OP () if constexpr (ULTRA_VERBOSE) { g_simd_stats.scalar_ops++; }
inline void print_simd_summary () {
if constexpr ( ! ULTRA_VERBOSE) return ;
std::cout << " \n --- SIMD OPERATION SUMMARY ---" << std::endl;
std::cout << "AVX2 Loads: " << g_simd_stats . avx2_loads << std::endl;
std::cout << "AVX2 Stores: " << g_simd_stats . avx2_stores << std::endl;
std::cout << "AVX2 Adds: " << g_simd_stats . avx2_adds << std::endl;
std::cout << "AVX2 Subs: " << g_simd_stats . avx2_subs << std::endl;
std::cout << "AVX2 Muls: " << g_simd_stats . avx2_muls << std::endl;
std::cout << "AVX2 Divs: " << g_simd_stats . avx2_divs << std::endl;
std::cout << "AVX2 FMAs: " << g_simd_stats . avx2_fmas << std::endl;
std::cout << "AVX2 Broadcasts: " << g_simd_stats . avx2_broadcasts << std::endl;
std::cout << "AVX2 Shuffles: " << g_simd_stats . avx2_shuffles << std::endl;
std::cout << "AVX2 Compares: " << g_simd_stats . avx2_compares << std::endl;
std::cout << "AVX2 Bitwise: " << g_simd_stats . avx2_bitwise << std::endl;
std::cout << "Scalar Fallback: " << g_simd_stats . scalar_ops << std::endl;
std::cout << "------------------------------" << std::endl;
std::cout << "Total SIMD Ops: " << g_simd_stats . total_simd_ops () << std::endl;
std::cout << "Total Vectors: " << g_simd_stats . total_vectors << " (256-bit)" << std::endl;
std::cout << "Total Floats: " << g_simd_stats . total_floats << std::endl;
double simd_ratio = ( g_simd_stats . total_simd_ops () > 0 ) ?
100.0 * g_simd_stats . total_simd_ops () / ( g_simd_stats . total_simd_ops () + g_simd_stats . scalar_ops ) : 0.0 ;
std::cout << "SIMD Efficiency: " << std::fixed << std:: setprecision ( 1 ) << simd_ratio << "%" << std::endl;
std::cout << "------------------------------" << std::endl;
}
#define TRACE_SIMD_SUMMARY () print_simd_summary()
// ============================================================================
// THREAD-LEVEL PROFILING - Per-thread timing breakdown
// ============================================================================
constexpr int MAX_THREADS = 128 ;
struct ThreadProfile {
std::atomic < double > total_time_sec{ 0.0 };
std::atomic < int64_t > tasks_completed{ 0 };
std::atomic < int64_t > items_processed{ 0 };
std::chrono::high_resolution_clock::time_point task_start;
std::string current_task;
bool in_task = false ;
};
struct ThreadProfiler {
ThreadProfile threads [MAX_THREADS];
std::mutex profile_mutex;
void start_task ( int tid , const std:: string & task_name ) {
if (tid < 0 || tid >= MAX_THREADS) return ;
threads [tid]. task_start = std::chrono::high_resolution_clock:: now ();
threads [tid]. current_task = task_name;
threads [tid]. in_task = true ;
}
void end_task ( int tid , int64_t items = 1 ) {
if (tid < 0 || tid >= MAX_THREADS || ! threads [tid]. in_task ) return ;
auto now = std::chrono::high_resolution_clock:: now ();
double elapsed = std::chrono:: duration < double >(now - threads [tid]. task_start ). count ();
// Atomic add for double using compare-exchange
double old_val = threads [tid]. total_time_sec . load ();
while ( ! threads [tid]. total_time_sec . compare_exchange_weak (old_val, old_val + elapsed)) {}
threads [tid]. tasks_completed ++ ;
threads [tid]. items_processed += items;
threads [tid]. in_task = false ;
}
void print_summary ( int num_threads ) {
if constexpr ( ! ULTRA_VERBOSE) return ;
std::lock_guard < std::mutex > lock (profile_mutex);
std::cout << " \n --- THREAD PROFILING SUMMARY ---" << std::endl;
std::cout << std:: setw ( 8 ) << "Thread" << std:: setw ( 12 ) << "Time(s)"
<< std:: setw ( 12 ) << "Tasks" << std:: setw ( 15 ) << "Items"
<< std:: setw ( 12 ) << "Items/s" << std::endl;
std::cout << std:: string ( 59 , '-' ) << std::endl;
double total_time = 0.0 ;
int64_t total_tasks = 0 ;
int64_t total_items = 0 ;
for ( int i = 0 ; i < num_threads && i < MAX_THREADS; ++ i) {
double time = threads [i]. total_time_sec . load ();
int64_t tasks = threads [i]. tasks_completed . load ();
int64_t items = threads [i]. items_processed . load ();
double rate = (time > 0.001 ) ? items / time : 0.0 ;
if (tasks > 0 ) {
std::cout << std:: setw ( 8 ) << i
<< std:: setw ( 12 ) << std::fixed << std:: setprecision ( 2 ) << time
<< std:: setw ( 12 ) << tasks
<< std:: setw ( 15 ) << items
<< std:: setw ( 12 ) << std::fixed << std:: setprecision ( 0 ) << rate
<< std::endl;
}
total_time += time;
total_tasks += tasks;
total_items += items;
}
std::cout << std:: string ( 59 , '-' ) << std::endl;
std::cout << std:: setw ( 8 ) << "TOTAL"
<< std:: setw ( 12 ) << std::fixed << std:: setprecision ( 2 ) << total_time
<< std:: setw ( 12 ) << total_tasks
<< std:: setw ( 15 ) << total_items
<< std:: setw ( 12 ) << std::fixed << std:: setprecision ( 0 )
<< ((total_time > 0.001 ) ? total_items / total_time : 0.0 )
<< std::endl;
std::cout << "--------------------------------" << std::endl;
}
void reset () {
for ( int i = 0 ; i < MAX_THREADS; ++ i) {
threads [i]. total_time_sec = 0.0 ;
threads [i]. tasks_completed = 0 ;
threads [i]. items_processed = 0 ;
threads [i]. in_task = false ;
}
}
};
ThreadProfiler g_thread_profiler;
#define THREAD_TASK_START ( task_name ) g_thread_profiler.start_task(omp_get_thread_num(), task_name)
#define THREAD_TASK_END ( items ) g_thread_profiler.end_task(omp_get_thread_num(), items)
#define THREAD_PROFILE_SUMMARY () g_thread_profiler.print_summary(omp_get_max_threads())
#define THREAD_PROFILE_RESET () g_thread_profiler.reset()
// ============================================================================
// STEP 10: TRACE LEVEL SYSTEM - Configurable logging levels
// ============================================================================
// Compile-time trace level control:
// -DTRACE_LEVEL=0 -> DEBUG (all messages)
// -DTRACE_LEVEL=1 -> INFO (info and above)
// -DTRACE_LEVEL=2 -> WARN (warnings and above)
// -DTRACE_LEVEL=3 -> ERROR (errors and fatal only)
// -DTRACE_LEVEL=4 -> FATAL (fatal only)
// -DTRACE_LEVEL=5 -> SILENT (no trace output)
#ifndef TRACE_LEVEL
#define TRACE_LEVEL 0 // Default: DEBUG (all messages)
#endif
enum class TraceLevel : int {
DEBUG = 0 ,
INFO = 1 ,
WARN = 2 ,
ERROR = 3 ,
FATAL = 4 ,
SILENT = 5
};
constexpr TraceLevel g_min_trace_level = static_cast< TraceLevel > (TRACE_LEVEL);
constexpr const char * trace_level_name ( TraceLevel level ) {
switch (level) {
case TraceLevel::DEBUG: return "DEBUG" ;
case TraceLevel::INFO: return "INFO " ;
case TraceLevel::WARN: return "WARN " ;
case TraceLevel::ERROR: return "ERROR" ;
case TraceLevel::FATAL: return "FATAL" ;
case TraceLevel::SILENT: return "SILENT" ;
default : return "?????" ;
}
}
constexpr const char * trace_level_color ( TraceLevel level ) {
switch (level) {
case TraceLevel::DEBUG: return " \033 [36m" ; // Cyan
case TraceLevel::INFO: return " \033 [32m" ; // Green
case TraceLevel::WARN: return " \033 [33m" ; // Yellow
case TraceLevel::ERROR: return " \033 [31m" ; // Red
case TraceLevel::FATAL: return " \033 [35m" ; // Magenta
default : return " \033 [0m" ; // Reset
}
}
constexpr const char * TRACE_COLOR_RESET = " \033 [0m" ;
// Leveled trace output macro - uses cout directly (defined before DUAL_OUT)
// JSON output not available in these early-defined macros
#define TRACE_LEVEL_OUT ( level , msg ) \
if constexpr (ULTRA_VERBOSE && static_cast<int>(level) >= static_cast<int>(g_min_trace_level)) { \
std::ostringstream _tlo_oss; \
_tlo_oss << get_timestamp() << " " << trace_level_color(level) << "[" << trace_level_name(level) << "]" << TRACE_COLOR_RESET << " " << msg; \
std::cout << _tlo_oss.str() << std::endl; \
}
#define TRACE_DEBUG ( msg ) TRACE_LEVEL_OUT(TraceLevel::DEBUG, msg)
#define TRACE_INFO ( msg ) TRACE_LEVEL_OUT(TraceLevel::INFO, msg)
#define TRACE_WARN_MSG ( msg ) TRACE_LEVEL_OUT(TraceLevel::WARN, msg)
#define TRACE_ERROR_MSG ( msg ) TRACE_LEVEL_OUT(TraceLevel::ERROR, msg)
#define TRACE_FATAL ( msg ) TRACE_LEVEL_OUT(TraceLevel::FATAL, msg)
// ============================================================================
// STEP 10: FUNCTION CALL STATISTICS - Track call counts and timing
// ============================================================================
struct FunctionStats {
std::atomic < int64_t > call_count{ 0 };
std::atomic < int64_t > total_time_ns{ 0 };
std::atomic < int64_t > min_time_ns{INT64_MAX};
std::atomic < int64_t > max_time_ns{ 0 };
std::string function_name;
void record ( int64_t duration_ns ) {
call_count ++ ;
total_time_ns += duration_ns;
// Update min (using compare-exchange)
int64_t current_min = min_time_ns . load ();
while (duration_ns < current_min &&
! min_time_ns . compare_exchange_weak (current_min, duration_ns)) {}
// Update max
int64_t current_max = max_time_ns . load ();
while (duration_ns > current_max &&
! max_time_ns . compare_exchange_weak (current_max, duration_ns)) {}
}
double avg_time_us () const {
int64_t count = call_count . load ();
return (count > 0 ) ? ( total_time_ns . load () / 1000.0 ) / count : 0.0 ;
}
};
constexpr int MAX_TRACKED_FUNCTIONS = 64 ;
struct FunctionProfiler {
std::unordered_map < std::string, FunctionStats > stats;
std::mutex stats_mutex;
void record ( const std:: string & func_name , int64_t duration_ns ) {
std::lock_guard < std::mutex > lock (stats_mutex);
if ( stats . find (func_name) == stats . end ()) {
stats [func_name]. function_name = func_name;
}
stats [func_name]. record (duration_ns);
}
void print_summary () {
if constexpr ( ! ULTRA_VERBOSE) return ;
std::lock_guard < std::mutex > lock (stats_mutex);
if ( stats . empty ()) return ;
std::cout << " \n --- FUNCTION CALL STATISTICS ---" << std::endl;
std::cout << std:: setw ( 30 ) << std::left << "Function"
<< std:: setw ( 12 ) << std::right << "Calls"
<< std:: setw ( 14 ) << "Total(ms)"
<< std:: setw ( 12 ) << "Avg(us)"
<< std:: setw ( 12 ) << "Min(us)"
<< std:: setw ( 12 ) << "Max(us)" << std::endl;
std::cout << std:: string ( 92 , '-' ) << std::endl;
// Sort by total time descending
std::vector < std::pair < std::string, FunctionStats *>> sorted;
for ( auto & kv : stats) {
sorted . push_back ({ kv . first , & kv . second });
}
std:: sort ( sorted . begin (), sorted . end (),
[]( const auto & a , const auto & b ) {
return a . second -> total_time_ns . load () > b . second -> total_time_ns . load ();
});
for ( const auto & [name, s] : sorted) {
std::cout << std:: setw ( 30 ) << std::left << name . substr ( 0 , 29 )
<< std:: setw ( 12 ) << std::right << s -> call_count . load ()
<< std:: setw ( 14 ) << std::fixed << std:: setprecision ( 2 ) << ( s -> total_time_ns . load () / 1000000.0 )
<< std:: setw ( 12 ) << std::fixed << std:: setprecision ( 2 ) << s -> avg_time_us ()
<< std:: setw ( 12 ) << std::fixed << std:: setprecision ( 2 ) << ( s -> min_time_ns . load () / 1000.0 )
<< std:: setw ( 12 ) << std::fixed << std:: setprecision ( 2 ) << ( s -> max_time_ns . load () / 1000.0 )
<< std::endl;
}
std::cout << "--------------------------------" << std::endl;
}
void reset () {
std::lock_guard < std::mutex > lock (stats_mutex);
stats . clear ();
}
};
FunctionProfiler g_function_profiler;
// RAII timer for automatic function timing
struct ScopedFunctionTimer {
std::string func_name;
std::chrono::high_resolution_clock::time_point start_time;
ScopedFunctionTimer ( const std:: string & name ) : func_name (name) {
if constexpr (ULTRA_VERBOSE) {
start_time = std::chrono::high_resolution_clock:: now ();
}
}
~ScopedFunctionTimer () {
if constexpr (ULTRA_VERBOSE) {
auto end = std::chrono::high_resolution_clock:: now ();
auto duration_ns = std::chrono:: duration_cast <std::chrono:: nanoseconds >(end - start_time). count ();
g_function_profiler . record (func_name, duration_ns);
}
}
};
#define PROFILE_FUNCTION () ScopedFunctionTimer _func_timer(__FUNCTION__)
#define PROFILE_FUNCTION_NAMED ( name ) ScopedFunctionTimer _func_timer(name)
#define FUNCTION_STATS_SUMMARY () g_function_profiler.print_summary()
#define FUNCTION_STATS_RESET () g_function_profiler.reset()
// ============================================================================
// STEP 10: CORRELATION IDS - For distributed tracing across nodes
// ============================================================================
struct CorrelationContext {
std::atomic < uint64_t > sequence_counter{ 0 };
int node_id = NODE_ID;
uint64_t session_id = 0 ;
void init () {
// Generate session ID from timestamp
auto now = std::chrono::system_clock:: now ();
session_id = std::chrono:: duration_cast <std::chrono:: microseconds >(
now . time_since_epoch ()). count () & 0x FFFFFFFF ;
}
// Generate unique correlation ID: SESSION-NODE-SEQUENCE
std:: string generate_id () {
uint64_t seq = sequence_counter ++ ;
std::ostringstream oss;
oss << std::hex << std:: setfill ( '0' )
<< std:: setw ( 8 ) << session_id << "-"
<< std:: setw ( 2 ) << node_id << "-"
<< std:: setw ( 8 ) << seq;
return oss . str ();
}
// Parse correlation ID components
static bool parse_id ( const std:: string & id , uint64_t & session , int & node , uint64_t & seq ) {
if ( id . length () != 21 ) return false ; // "SSSSSSSS-NN-QQQQQQQQ"
try {
session = std:: stoull ( id . substr ( 0 , 8 ), nullptr , 16 );
node = std:: stoi ( id . substr ( 9 , 2 ), nullptr , 16 );
seq = std:: stoull ( id . substr ( 12 , 8 ), nullptr , 16 );
return true ;
} catch (...) {
return false ;
}
}
};
CorrelationContext g_correlation;
// Thread-local correlation ID for request tracing
thread_local std::string tl_correlation_id;
#define CORRELATION_INIT () g_correlation.init()
#define CORRELATION_NEW () (tl_correlation_id = g_correlation.generate_id())
#define CORRELATION_GET () tl_correlation_id
#define CORRELATION_SET ( id ) (tl_correlation_id = (id))
#define TRACE_CORRELATED ( msg ) \
if constexpr (ULTRA_VERBOSE) { \
std::ostringstream _oss; \
_oss << get_timestamp() << " [CID:" << tl_correlation_id << "] " << msg; \
DUAL_OUT(_oss.str()) \
}
// ============================================================================
// STEP 10: WATCHDOG/HEARTBEAT SYSTEM - Monitor thread health
// ============================================================================
struct WatchdogMonitor {
struct ThreadHeartbeat {
std::atomic < int64_t > last_heartbeat_ms{ 0 };
std::atomic < bool > is_alive{ false };
std::string current_task;
std::atomic < int64_t > hang_count{ 0 };
};
ThreadHeartbeat threads [MAX_THREADS];
std::atomic < bool > monitoring_active{ false };
std::thread monitor_thread;
int64_t heartbeat_timeout_ms = 30000 ; // 30 second timeout
int64_t check_interval_ms = 5000 ; // Check every 5 seconds
std::atomic < int64_t > total_hang_detections{ 0 };
void start_monitoring () {
if constexpr ( ! ULTRA_VERBOSE) return ;
monitoring_active = true ;
monitor_thread = std:: thread ([ this ]() {
while (monitoring_active) {
check_threads ();
std::this_thread:: sleep_for (std::chrono:: milliseconds (check_interval_ms));
}
});
TRACE_INFO ( "Watchdog monitor started (timeout=" + std:: to_string (heartbeat_timeout_ms) + "ms)" );
}
void stop_monitoring () {
monitoring_active = false ;
if ( monitor_thread . joinable ()) {
monitor_thread . join ();
}
}
void heartbeat ( int tid , const std:: string & task = "" ) {
if (tid < 0 || tid >= MAX_THREADS) return ;
threads [tid]. last_heartbeat_ms = static_cast< int64_t > ( get_elapsed_ms ());
threads [tid]. is_alive = true ;
if ( ! task . empty ()) {
threads [tid]. current_task = task;
}
}
void thread_start ( int tid , const std:: string & task ) {
if (tid < 0 || tid >= MAX_THREADS) return ;
threads [tid]. last_heartbeat_ms = static_cast< int64_t > ( get_elapsed_ms ());
threads [tid]. is_alive = true ;
threads [tid]. current_task = task;
}
void thread_end ( int tid ) {
if (tid < 0 || tid >= MAX_THREADS) return ;
threads [tid]. is_alive = false ;
threads [tid]. current_task = "" ;
}
void check_threads () {
int64_t now_ms = static_cast< int64_t > ( get_elapsed_ms ());
for ( int i = 0 ; i < MAX_THREADS; ++ i) {
if ( threads [i]. is_alive ) {
int64_t elapsed = now_ms - threads [i]. last_heartbeat_ms . load ();
if (elapsed > heartbeat_timeout_ms) {
threads [i]. hang_count ++ ;
total_hang_detections ++ ;
std::ostringstream oss;
oss << "HANG DETECTED: Thread " << i
<< " (task: " << threads [i]. current_task << ")"
<< " - no heartbeat for " << elapsed << "ms" ;
TRACE_WARN_MSG ( oss . str ());
}
}
}
}
void print_summary () {
if constexpr ( ! ULTRA_VERBOSE) return ;
if (total_hang_detections == 0 ) {
std::cout << " \n [WATCHDOG] No hang detections during execution." << std::endl;
return ;
}
std::cout << " \n --- WATCHDOG SUMMARY ---" << std::endl;
std::cout << "Total Hang Detections: " << total_hang_detections << std::endl;
for ( int i = 0 ; i < MAX_THREADS; ++ i) {
if ( threads [i]. hang_count > 0 ) {
std::cout << " Thread " << i << ": " << threads [i]. hang_count << " hangs" << std::endl;
}
}
std::cout << "------------------------" << std::endl;
}
};
WatchdogMonitor g_watchdog;
#define WATCHDOG_START () g_watchdog.start_monitoring()
#define WATCHDOG_STOP () g_watchdog.stop_monitoring()
#define WATCHDOG_HEARTBEAT ( task ) g_watchdog.heartbeat(omp_get_thread_num(), task)
#define WATCHDOG_THREAD_START ( task ) g_watchdog.thread_start(omp_get_thread_num(), task)
#define WATCHDOG_THREAD_END () g_watchdog.thread_end(omp_get_thread_num())
#define WATCHDOG_SUMMARY () g_watchdog.print_summary()
// ============================================================================
// STEP 10: TIMING HISTOGRAM - Distribution of function timings
// ============================================================================
struct TimingHistogram {
static constexpr int NUM_BUCKETS = 16 ;
// Buckets: <1us, <10us, <100us, <1ms, <10ms, <100ms, <1s, <10s, >=10s
std::atomic < int64_t > buckets [NUM_BUCKETS] = {};
std::string name;
TimingHistogram () : name ( "unnamed" ) {}
explicit TimingHistogram ( const std:: string & n ) : name (n) {}
void record ( int64_t duration_ns ) {
int bucket = 0 ;
int64_t threshold_ns = 1000 ; // Start at 1 microsecond
while (bucket < NUM_BUCKETS - 1 && duration_ns >= threshold_ns) {
bucket ++ ;
threshold_ns *= 10 ;
}
buckets [bucket] ++ ;
}
void print () const {
if constexpr ( ! ULTRA_VERBOSE) return ;
std::cout << " \n --- TIMING HISTOGRAM: " << name << " ---" << std::endl;
const char * labels[] = {
"<1us" , "<10us" , "<100us" , "<1ms" , "<10ms" , "<100ms" , "<1s" , "<10s" , ">=10s" ,
"b9" , "b10" , "b11" , "b12" , "b13" , "b14" , "b15"
};
int64_t max_count = 0 ;
int64_t total = 0 ;
for ( int i = 0 ; i < 9 ; ++ i) { // Only first 9 buckets are meaningful
max_count = std:: max (max_count, buckets [i]. load ());
total += buckets [i]. load ();
}
if (total == 0 ) return ;
for ( int i = 0 ; i < 9 ; ++ i) {
int64_t count = buckets [i]. load ();
double pct = 100.0 * count / total;
int bar_width = (max_count > 0 ) ? static_cast< int > ( 40.0 * count / max_count) : 0 ;
std::cout << std:: setw ( 8 ) << labels [i] << " | "
<< std:: string (bar_width, '#' ) << std:: string ( 40 - bar_width, ' ' )
<< " " << std:: setw ( 8 ) << count
<< " (" << std::fixed << std:: setprecision ( 1 ) << pct << "%)"
<< std::endl;
}
std::cout << "-----------------------------------------" << std::endl;
}
void reset () {
for ( int i = 0 ; i < NUM_BUCKETS; ++ i) {
buckets [i] = 0 ;
}
}
};
// Global histograms for major operations
TimingHistogram g_expand_histogram ( "EXPAND" );
TimingHistogram g_assume_histogram ( "ASSUME" );
TimingHistogram g_predict_histogram ( "PREDICT" );
TimingHistogram g_simd_histogram ( "SIMD_BLOCK" );
#define HISTOGRAM_RECORD ( name , duration_ns ) g_ ##name##_histogram .record(duration_ns)
#define HISTOGRAM_PRINT ( name ) g_ ##name##_histogram .print()
#define HISTOGRAM_RESET ( name ) g_ ##name##_histogram .reset()
#define PRINT_ALL_HISTOGRAMS () \
g_expand_histogram.print(); \
g_assume_histogram.print(); \
g_predict_histogram.print(); \
g_simd_histogram.print()
// ============================================================================
// GLOBAL STATISTICS COUNTERS - Track all values and operations
// ============================================================================
struct GlobalStats {
// Population counters
std::atomic < int64_t > total_minds_processed{ 0 };
std::atomic < int64_t > total_chunks_completed{ 0 };
std::atomic < int64_t > total_entities_created{ 0 };
std::atomic < int64_t > total_civilians_placed{ 0 };
// Stage operation counters
std::atomic < int64_t > stage1_init_ops{ 0 }; // Initialization operations
std::atomic < int64_t > stage2_band_ops{ 0 }; // Banding operations
std::atomic < int64_t > stage3_expand_ops{ 0 }; // Expansion operations
std::atomic < int64_t > stage4_assume_ops{ 0 }; // Assuming operations
std::atomic < int64_t > stage5_predict_ops{ 0 }; // Prediction operations
// Weight/value counters
std::atomic < int64_t > total_weights_modified{ 0 };
std::atomic < int64_t > total_simd_vectors{ 0 };
std::atomic < int64_t > total_floats_processed{ 0 };
// Timing
double stage1_time_sec = 0.0 ;
double stage2_time_sec = 0.0 ;
double stage3_time_sec = 0.0 ;
double stage4_time_sec = 0.0 ;
double stage5_time_sec = 0.0 ;
void print_summary () {
std::cout << " \n ========================================" << std::endl;
std::cout << " GLOBAL STATISTICS SUMMARY" << std::endl;
std::cout << "========================================" << std::endl;
std::cout << " \n --- POPULATION COUNTERS ---" << std::endl;
std::cout << " Total Minds Processed: " << total_minds_processed << std::endl;
std::cout << " Total Chunks Completed: " << total_chunks_completed << std::endl;
std::cout << " Total Entities Created: " << total_entities_created << std::endl;
std::cout << " Total Civilians Placed: " << total_civilians_placed << std::endl;
std::cout << " \n --- STAGE OPERATION COUNTERS ---" << std::endl;
std::cout << " Stage 1 (Init): " << stage1_init_ops << " ops" << std::endl;
std::cout << " Stage 2 (Banding): " << stage2_band_ops << " ops" << std::endl;
std::cout << " Stage 3 (Expand): " << stage3_expand_ops << " ops" << std::endl;
std::cout << " Stage 4 (Assume): " << stage4_assume_ops << " ops" << std::endl;
std::cout << " Stage 5 (Predict): " << stage5_predict_ops << " ops" << std::endl;
std::cout << " \n --- DATA PROCESSING COUNTERS ---" << std::endl;
std::cout << " Total Weights Modified: " << total_weights_modified << std::endl;
std::cout << " Total SIMD Vectors: " << total_simd_vectors << std::endl;
std::cout << " Total Floats Processed: " << total_floats_processed << std::endl;
std::cout << " \n --- STAGE TIMING ---" << std::endl;
std::cout << " Stage 1 (Init): " << std::fixed << std:: setprecision ( 3 ) << stage1_time_sec << "s" << std::endl;
std::cout << " Stage 2 (Banding): " << std::fixed << std:: setprecision ( 3 ) << stage2_time_sec << "s" << std::endl;
std::cout << " Stage 3 (Expand): " << std::fixed << std:: setprecision ( 3 ) << stage3_time_sec << "s" << std::endl;
std::cout << " Stage 4 (Assume): " << std::fixed << std:: setprecision ( 3 ) << stage4_time_sec << "s" << std::endl;
std::cout << " Stage 5 (Predict): " << std::fixed << std:: setprecision ( 3 ) << stage5_time_sec << "s" << std::endl;
double total_stage_time = stage1_time_sec + stage2_time_sec + stage3_time_sec + stage4_time_sec + stage5_time_sec;
std::cout << " Total Stage Time: " << std::fixed << std:: setprecision ( 3 ) << total_stage_time << "s" << std::endl;
std::cout << "======================================== \n " << std::endl;
}
void reset () {
total_minds_processed = 0 ;
total_chunks_completed = 0 ;
total_entities_created = 0 ;
total_civilians_placed = 0 ;
stage1_init_ops = stage2_band_ops = stage3_expand_ops = 0 ;
stage4_assume_ops = stage5_predict_ops = 0 ;
total_weights_modified = total_simd_vectors = total_floats_processed = 0 ;
stage1_time_sec = stage2_time_sec = stage3_time_sec = 0.0 ;
stage4_time_sec = stage5_time_sec = 0.0 ;
}
};
GlobalStats g_stats;
#define STATS_SUMMARY () g_stats.print_summary()
#define STATS_RESET () g_stats.reset()
// ============================================================================
// ERROR/EXCEPTION TRACING - With stack traces
// ============================================================================
#include <execinfo.h> // For backtrace
#include <cxxabi.h> // For demangling
struct ErrorInfo {
int64_t timestamp_ms;
std::string error_type;
std::string message;
std::string location;
std::vector < std::string > stack_trace;
int thread_id;
};
std::vector < ErrorInfo > g_error_log;
std::mutex g_error_mutex;
std::atomic < int64_t > g_error_count{ 0 };
std::atomic < int64_t > g_warning_count{ 0 };
// Get demangled stack trace
inline std:: vector <std:: string > get_stack_trace ( int max_frames = 32 ) {
std::vector < std::string > trace;
void * callstack [ 64 ];
int frames = backtrace (callstack, std:: min (max_frames, 64 ));
char ** symbols = backtrace_symbols (callstack, frames);
if (symbols) {
for ( int i = 2 ; i < frames; ++ i) { // Skip first 2 (this function and caller)
std::string sym ( symbols [i]);
// Try to demangle C++ symbols
size_t start = sym . find ( '(' );
size_t end = sym . find ( '+' , start);
if (start != std::string::npos && end != std::string::npos) {
std::string mangled = sym . substr (start + 1 , end - start - 1 );
int status;
char * demangled = abi:: __cxa_demangle ( mangled . c_str (), nullptr , nullptr , & status);
if (status == 0 && demangled) {
sym = sym . substr ( 0 , start + 1 ) + demangled + sym . substr (end);
free (demangled);
}
}
trace . push_back (sym);
}
free (symbols);
}
return trace;
}
inline void log_error ( const std:: string & type , const std:: string & msg ,
const std:: string & file , int line , bool is_warning = false ) {
ErrorInfo err;
err . timestamp_ms = static_cast< int64_t > ( get_elapsed_ms ());
err . error_type = type;
err . message = msg;
err . location = file + ":" + std:: to_string (line);
err . thread_id = omp_get_thread_num ();
err . stack_trace = get_stack_trace ();
if (is_warning) {
g_warning_count ++ ;
} else {
g_error_count ++ ;
}
std::lock_guard < std::mutex > lock (g_error_mutex);
g_error_log . push_back (err);
// Immediate output (using cout/cerr only since log files may not be initialized yet)
std::ostringstream oss;
oss << get_timestamp () << " [" << (is_warning ? "WARN" : "ERROR" ) << "] "
<< type << ": " << msg << " @ " << err . location << " (thread " << err . thread_id << ")" ;
// Always output to stderr for errors
std::cerr << oss . str () << std::endl;
if constexpr (ULTRA_VERBOSE) {
// Print stack trace
for ( size_t i = 0 ; i < std:: min ( err . stack_trace . size (), size_t ( 5 )); ++ i) {
std::cerr << " [" << i << "] " << err . stack_trace [i] << std::endl;
}
}
}
inline void print_error_summary () {
if (g_error_count == 0 && g_warning_count == 0 ) {
std::cout << " \n [OK] No errors or warnings logged." << std::endl;
return ;
}
std::cout << " \n --- ERROR/WARNING SUMMARY ---" << std::endl;
std::cout << "Total Errors: " << g_error_count << std::endl;
std::cout << "Total Warnings: " << g_warning_count << std::endl;
if ( ! g_error_log . empty ()) {
std::cout << " \n Recent issues:" << std::endl;
size_t start = ( g_error_log . size () > 10 ) ? g_error_log . size () - 10 : 0 ;
for ( size_t i = start; i < g_error_log . size (); ++ i) {
auto & err = g_error_log [i];
std::cout << " [" << err . timestamp_ms << "ms] " << err . error_type
<< ": " << err . message << " @ " << err . location << std::endl;
}
}
std::cout << "-----------------------------" << std::endl;
}
// Error tracing macros
#define TRACE_ERROR ( type , msg ) log_error(type, msg, __FILE__, __LINE__, false)
#define TRACE_WARNING ( type , msg ) log_error(type, msg, __FILE__, __LINE__, true)
#define TRACE_ASSERT ( condition , msg ) \
if (!(condition)) { \
log_error("ASSERTION_FAILED", std::string( #condition ) + " - " + msg, __FILE__, __LINE__, false); \
}
// Exception handling wrapper
#define TRY_TRACE try {
#define CATCH_TRACE ( context ) \
} catch (const std::exception& e) { \
log_error("EXCEPTION", std::string(e.what()) + " in " + context, __FILE__, __LINE__, false); \
throw; \
} catch (...) { \
log_error("UNKNOWN_EXCEPTION", "Unknown exception in " + std::string(context), __FILE__, __LINE__, false); \
throw; \
}
// Bounds checking with tracing
#define CHECK_BOUNDS ( idx , max , context ) \
if ((idx) < 0 || (idx) >= (max)) { \
std::ostringstream _err; \
_err << "Index " << (idx) << " out of bounds [0, " << (max) << ") in " << context; \
log_error("BOUNDS_ERROR", _err.str(), __FILE__, __LINE__, false); \
}
// Null pointer check
#define CHECK_NULL ( ptr , context ) \
if ((ptr) == nullptr) { \
log_error("NULL_POINTER", std::string("Null pointer: ") + #ptr + " in " + context, __FILE__, __LINE__, false); \
}
#define ERROR_SUMMARY () print_error_summary()
// Global log file stream (thread-safe with critical sections)
std::ofstream g_log_file;
bool g_log_file_open = false ;
// JSON output file stream
std::ofstream g_json_file;
bool g_json_file_open = false ;
bool g_json_first_entry = true ;
// Initialize JSON file
inline void init_json_file () {
if constexpr (JSON_OUTPUT && ULTRA_VERBOSE) {
g_json_file . open (JSON_FILENAME, std::ios::out | std::ios::trunc);
g_json_file_open = g_json_file . is_open ();
if (g_json_file_open) {
g_json_file << "{" << std::endl;
g_json_file << " \" metadata \" : {" << std::endl;
auto now = std::chrono::system_clock:: now ();
auto time = std::chrono::system_clock:: to_time_t (now);
std::string time_str = std:: ctime ( & time);
time_str . erase ( time_str . find_last_not_of ( " \n " ) + 1 ); // Remove newline
g_json_file << " \" started \" : \" " << time_str << " \" ," << std::endl;
g_json_file << " \" version \" : \" 2.0 \" " << std::endl;
g_json_file << " }," << std::endl;
g_json_file << " \" events \" : [" << std::endl;
g_json_first_entry = true ;
std::cout << "[JSON] Opened JSON file: " << JSON_FILENAME << std::endl;
}
}
}
// Write JSON metadata (called after constants are available)
inline void write_json_metadata () {
if ( ! g_json_file_open) return ;
// We'll write constants info as the first event
}
// Close JSON file
inline void close_json_file () {
if (g_json_file_open) {
g_json_file << std::endl << " ]," << std::endl;
// Add final memory stats
auto mem = get_memory_stats ();
g_json_file << " \" final_stats \" : {" << std::endl;
g_json_file << " \" peak_memory_mb \" : " << std::fixed << std:: setprecision ( 2 ) << ( mem . peak_rss_kb / 1024.0 ) << "," << std::endl;
g_json_file << " \" total_time_ms \" : " << std::fixed << std:: setprecision ( 3 ) << get_elapsed_ms () << "," << std::endl;
g_json_file << " \" page_faults \" : " << mem . page_faults << std::endl;
g_json_file << " }" << std::endl;
g_json_file << "}" << std::endl;
g_json_file . close ();
g_json_file_open = false ;
std::cout << "[JSON] Closed JSON file: " << JSON_FILENAME << std::endl;
}
}
// Write JSON event (thread-safe)
inline void write_json_event ( const std:: string & type , const std:: string & name ,
const std:: string & value , const std:: string & context ) {
if ( ! g_json_file_open) return ;
#pragma omp critical ( json_write )
{
if ( ! g_json_first_entry) {
g_json_file << "," << std::endl;
}
g_json_first_entry = false ;
g_json_file << " { \" t \" :" << std::fixed << std:: setprecision ( 3 ) << get_elapsed_ms ()
<< ", \" type \" : \" " << type << " \" "
<< ", \" name \" : \" " << name << " \" "
<< ", \" value \" : \" " << value << " \" "
<< ", \" ctx \" : \" " << context << " \" }" ;
}
}
// Escape string for JSON
inline std:: string json_escape ( const std:: string & s ) {
std::string result;
for ( char c : s) {
switch (c) {
case '"' : result += " \\\" " ; break ;
case ' \\ ' : result += " \\\\ " ; break ;
case ' \n ' : result += " \\ n" ; break ;
case ' \r ' : result += " \\ r" ; break ;
case ' \t ' : result += " \\ t" ; break ;
default : result += c;
}
}
return result;
}
// Initialize log file
inline void init_log_file () {
// Initialize timer and CPU tracking first
init_timer ();
init_cpu_tracking ();
// Initialize Step 10 systems
CORRELATION_INIT ();
if constexpr (ULTRA_VERBOSE) {
g_log_file . open (LOG_FILENAME, std::ios::out | std::ios::trunc);
g_log_file_open = g_log_file . is_open ();
if (g_log_file_open) {
auto now = std::chrono::system_clock:: now ();
auto time = std::chrono::system_clock:: to_time_t (now);
g_log_file << "========================================" << std::endl;
g_log_file << "JAMES DEAN TRACE LOG" << std::endl;
g_log_file << "Started: " << std:: ctime ( & time);
g_log_file << "Trace Level: " << trace_level_name (g_min_trace_level) << std::endl;
g_log_file << "========================================" << std::endl << std::endl;
std::cout << "[LOG] Opened log file: " << LOG_FILENAME << std::endl;
std::cout << "[LOG] Trace level: " << trace_level_name (g_min_trace_level) << std::endl;
}
// Initialize JSON file
init_json_file ();
// Start watchdog monitoring
WATCHDOG_START ();
}
}
// Close log file
inline void close_log_file () {
// Close JSON file first
close_json_file ();
if (g_log_file_open) {
auto now = std::chrono::system_clock:: now ();
auto time = std::chrono::system_clock:: to_time_t (now);
auto mem = get_memory_stats ();
auto cpu = get_cpu_stats ();
g_log_file << std::endl << "========================================" << std::endl;
g_log_file << "LOG COMPLETE" << std::endl;
g_log_file << "Ended: " << std:: ctime ( & time);
g_log_file << "Total Time: " << std::fixed << std:: setprecision ( 3 ) << get_elapsed_ms () << " ms" << std::endl;
g_log_file << "CPU Time: " << std::fixed << std:: setprecision ( 2 ) << cpu . total_time_sec << "s (User: " << cpu . user_time_sec << "s, Sys: " << cpu . system_time_sec << "s)" << std::endl;
g_log_file << "Peak Memory: " << format_memory_mb ( mem . peak_rss_kb ) << std::endl;
g_log_file << "========================================" << std::endl;
g_log_file . close ();
g_log_file_open = false ;
std::cout << "[LOG] Closed log file: " << LOG_FILENAME << std::endl;
std::cout << "[LOG] Total execution time: " << std::fixed << std:: setprecision ( 3 ) << get_elapsed_ms () << " ms" << std::endl;
std::cout << "[LOG] CPU time: " << std::fixed << std:: setprecision ( 2 ) << cpu . total_time_sec << "s" << std::endl;
std::cout << "[LOG] Peak memory: " << format_memory_mb ( mem . peak_rss_kb ) << std::endl;
// Print all summaries
print_ipc_summary ();
TRACE_SIMD_SUMMARY ();
THREAD_PROFILE_SUMMARY ();
FUNCTION_STATS_SUMMARY ();
PRINT_ALL_HISTOGRAMS ();
WATCHDOG_SUMMARY ();
WATCHDOG_STOP ();
ERROR_SUMMARY ();
}
}
// Dual output macro - writes to both cout and log file
#define DUAL_OUT ( msg ) \
std::cout << msg << std::endl; \
if (g_log_file_open) { \
g_log_file << msg << std::endl; \
}
// Thread-safe dual output for parallel regions
#define DUAL_OUT_SAFE ( msg ) \
{ \
std::ostringstream _oss; \
_oss << msg; \
std::cout << _oss.str() << std::endl; \
if (g_log_file_open) { \
_Pragma("omp critical(log_write)") \
g_log_file << _oss.str() << std::endl; \
} \
}
// Stage-specific verbosity flags (all default to ULTRA_VERBOSE setting)
#ifndef TRACE_INIT
#define TRACE_INIT ULTRA_VERBOSE
#endif
#ifndef TRACE_BANDING
#define TRACE_BANDING ULTRA_VERBOSE
#endif
#ifndef TRACE_EXPANSION
#define TRACE_EXPANSION ULTRA_VERBOSE
#endif
#ifndef TRACE_ASSUMING
#define TRACE_ASSUMING ULTRA_VERBOSE
#endif
#ifndef TRACE_PREDICTION
#define TRACE_PREDICTION ULTRA_VERBOSE
#endif
#ifndef TRACE_PLACEMENT
#define TRACE_PLACEMENT ULTRA_VERBOSE
#endif
// Helper: Check if we should trace this civilian
inline bool should_trace ( int64_t idx ) {
if constexpr ( ! ULTRA_VERBOSE) return false ;
if constexpr (TRACE_SPECIFIC_CIVILIAN >= 0 ) {
return idx == TRACE_SPECIFIC_CIVILIAN;
}
return (idx % VERBOSE_SAMPLE_RATE) == 0 ;
}
// Variable tracing macros
#define TRACE_VAR ( name , value , context ) \
if constexpr (ULTRA_VERBOSE) { \
std::ostringstream _oss; \
_oss << get_timestamp() << " [VAR] " << name << " = " << value << " @ " << context; \
DUAL_OUT(_oss.str()) \
if constexpr (JSON_OUTPUT) { \
std::ostringstream _val_oss; _val_oss << value; \
write_json_event("VAR", name, json_escape(_val_oss.str()), json_escape(context)); \
} \
}
#define TRACE_VAR_SAMPLED ( name , value , context , idx ) \
if (should_trace(idx)) { \
std::ostringstream _oss; \
_oss << get_timestamp() << " [VAR] " << name << " = " << value << " @ " << context; \
DUAL_OUT_SAFE(_oss.str()) \
if constexpr (JSON_OUTPUT) { \
std::ostringstream _val_oss; _val_oss << value; \
write_json_event("VAR", name, json_escape(_val_oss.str()), json_escape(context)); \
} \
}
#define TRACE_OP ( op_name , input1 , input2 , output , context ) \
if constexpr (ULTRA_VERBOSE) { \
std::ostringstream _oss; \
_oss << get_timestamp() << " [OP] " << op_name << ": " << input1 << " + " << input2 << " -> " << output << " @ " << context; \
DUAL_OUT(_oss.str()) \
if constexpr (JSON_OUTPUT) { \
std::ostringstream _val_oss; _val_oss << input1 << "+" << input2 << "->" << output; \
write_json_event("OP", op_name, json_escape(_val_oss.str()), json_escape(context)); \
} \
}
#define TRACE_OP_SAMPLED ( op_name , input1 , input2 , output , context , idx ) \
if (should_trace(idx)) { \
std::ostringstream _oss; \
_oss << get_timestamp() << " [OP] " << op_name << ": " << input1 << " + " << input2 << " -> " << output << " @ " << context; \
DUAL_OUT_SAFE(_oss.str()) \
if constexpr (JSON_OUTPUT) { \
std::ostringstream _val_oss; _val_oss << input1 << "+" << input2 << "->" << output; \
write_json_event("OP", op_name, json_escape(_val_oss.str()), json_escape(context)); \
} \
}
#define TRACE_STAGE ( stage_name , chunk , detail ) \
if constexpr (ULTRA_VERBOSE) { \
std::ostringstream _oss; \
_oss << get_timestamp() << " [STAGE] " << stage_name << " | Chunk " << chunk << " | " << detail; \
DUAL_OUT(_oss.str()) \
if constexpr (JSON_OUTPUT) { \
std::ostringstream _ctx_oss; _ctx_oss << "chunk=" << chunk; \
write_json_event("STAGE", stage_name, json_escape(detail), _ctx_oss.str()); \
} \
}
#define TRACE_CIVILIAN ( civ_id , field , value ) \
if (should_trace(civ_id)) { \
std::ostringstream _oss; \
_oss << get_timestamp() << " [CIV " << civ_id << "] " << field << " = " << value; \
DUAL_OUT_SAFE(_oss.str()) \
if constexpr (JSON_OUTPUT) { \
std::ostringstream _ctx_oss; _ctx_oss << "civ=" << civ_id; \
write_json_event("CIV", field, json_escape(value), _ctx_oss.str()); \
} \
}
// Meta-print: trace what we are printing
#define TRACE_PRINT ( msg ) \
if constexpr (ULTRA_VERBOSE) { \
std::ostringstream _oss; \
_oss << "[PRINT] PRINTING: \"" << msg << "\""; \
DUAL_OUT(_oss.str()) \
} \
std::cout << msg
#define TRACE_PRINT_VAL ( msg , val ) \
if constexpr (ULTRA_VERBOSE) { \
std::ostringstream _oss; \
_oss << "[PRINT] PRINTING: \"" << msg << val << "\""; \
DUAL_OUT(_oss.str()) \
} \
std::cout << msg << val
// Function entry/exit tracing
// Function entry/exit tracing with timestamps and JSON
#define TRACE_FUNC_ENTRY ( func_name ) \
if constexpr (ULTRA_VERBOSE) { \
std::ostringstream _oss; \
_oss << get_timestamp() << " [FUNC] ENTER: " << func_name; \
DUAL_OUT(_oss.str()) \
if constexpr (JSON_OUTPUT) { write_json_event("FUNC", "ENTER", func_name, ""); } \
}
#define TRACE_FUNC_EXIT ( func_name ) \
if constexpr (ULTRA_VERBOSE) { \
std::ostringstream _oss; \
_oss << get_timestamp() << " [FUNC] EXIT: " << func_name; \
DUAL_OUT(_oss.str()) \
if constexpr (JSON_OUTPUT) { write_json_event("FUNC", "EXIT", func_name, ""); } \
}
#define TRACE_ALLOC ( name , size , ptr ) \
if constexpr (ULTRA_VERBOSE) { \
std::ostringstream _oss; \
_oss << get_timestamp() << " [ALLOC] " << name << ": size=" << size << " bytes, ptr=" << ptr; \
DUAL_OUT(_oss.str()) \
if constexpr (JSON_OUTPUT) { \
std::ostringstream _val_oss; _val_oss << size << " bytes"; \
write_json_event("ALLOC", name, _val_oss.str(), ""); \
} \
}
#define TRACE_CONST ( name , value ) \
if constexpr (ULTRA_VERBOSE) { \
std::ostringstream _oss; \
_oss << get_timestamp() << " [CONST] " << name << " = " << value; \
DUAL_OUT(_oss.str()) \
if constexpr (JSON_OUTPUT) { \
std::ostringstream _val_oss; _val_oss << value; \
write_json_event("CONST", name, _val_oss.str(), ""); \
} \
}
#define TRACE_LOOP ( loop_name , iter , total ) \
if constexpr (ULTRA_VERBOSE) { \
if ((iter) % VERBOSE_SAMPLE_RATE == 0) { \
std::ostringstream _oss; \
_oss << get_timestamp() << " [LOOP] " << loop_name << ": iter " << iter << "/" << total; \
DUAL_OUT(_oss.str()) \
if constexpr (JSON_OUTPUT) { \
std::ostringstream _val_oss; _val_oss << iter << "/" << total; \
write_json_event("LOOP", loop_name, _val_oss.str(), ""); \
} \
} \
}
#define TRACE_BRANCH ( condition , result ) \
if constexpr (ULTRA_VERBOSE) { \
std::ostringstream _oss; \
_oss << get_timestamp() << " [BRANCH] " << condition << " -> " << (result ? "TRUE" : "FALSE"); \
DUAL_OUT(_oss.str()) \
if constexpr (JSON_OUTPUT) { \
write_json_event("BRANCH", condition, (result ? "TRUE" : "FALSE"), ""); \
} \
}
// Helper to build context strings
inline std:: string make_context ( int64_t chunk , int64_t civilian , const char * stage ) {
std::ostringstream oss;
oss << "chunk=" << chunk << ", civ=" << civilian << ", stage=" << stage;
return oss . str ();
}
// ============================================================================
// CORE CONSTANTS - NUMERIC VALUES USED THROUGHOUT THE PROGRAM
// ============================================================================
//
// === POPULATION CONSTANTS (all int64_t for massive scale support) ===
//
// TOTAL_POPULATION: The total number of "minds" or civilians to process.
// Set to INT64_MAX (9,223,372,036,854,775,807) to represent
// the maximum possible population. This is the grand total
// of all entities that will eventually be processed.
// Value: 9,223,372,036,854,775,807 (9.22 quintillion)
//
// CHUNK_SIZE: How many civilians to process per iteration/chunk.
// Since we process 500k at a time due to RAM constraints,
// the total population is divided into chunks of this size.
// Value: 500,000 (500 thousand per chunk)
//
// ACTIVE_POPULATION: The number of civilians actually allocated in RAM at once.
// This is limited by available system memory.
// Each civilian needs: DIMENSIONS * sizeof(float) * 2 bytes
// (weights + ghost buffer) = 8192 * 4 * 2 = 65,536 bytes
// 500,000 civilians = ~31 GB for weights+ghost alone.
// Value: 500,000 (fits in ~32-64 GB RAM)
//
// ENTITY_NODES: Number of EntityPerson nodes (source entities with traits).
// These are the "seed" personalities that influence civilians.
// Value: 175,000 (175 thousand unique entity templates)
//
// MAX_CIVILIANS: Maximum theoretical civilian ID value.
// Used for bounds checking and civilian context references.
// Value: INT64_MAX (9,223,372,036,854,775,807)
//
// NUM_CHUNKS: Total number of chunks needed to process TOTAL_POPULATION.
// Calculated as ceiling(TOTAL_POPULATION / CHUNK_SIZE).
// Value: ~18,446,744,073,709 chunks for INT64_MAX population
//
// === DIMENSION CONSTANTS ===
//
// DIMENSIONS: The size of each civilian's "mind" weight vector.
// Each mind is an 8192-dimensional float vector.
// 8192 = 2^13, chosen for SIMD alignment (256-bit AVX2 = 8 floats)
// Value: 8,192 floats per mind
//
// STAGES: Number of pipeline stages for prediction distillation.
// The 50-stage "flattening" crunch that converts the 8k mind
// into 3 action values (move_x, move_y, fire_intensity).
// Value: 50 stages
//
// === SIMD CONSTANTS ===
//
// AVX2 Vector Width: 256 bits = 32 bytes = 8 floats per SIMD operation
// Floats per Vector: 8 (processes 8 weights simultaneously)
// DIMENSIONS / 8 = 1024 SIMD iterations per mind
//
// === MEMORY CALCULATIONS ===
//
// Per Civilian Memory:
// - weights: 8192 * 4 bytes = 32,768 bytes (32 KB)
// - ghost: 8192 * 4 bytes = 32,768 bytes (32 KB)
// - banding: 4 bytes
// - Total: ~65,540 bytes (~64 KB per civilian)
//
// For ACTIVE_POPULATION = 1,000,000 (64 GB RAM system):
// - weights array: 1,000,000 * 32 KB = 30.5 GB
// - ghost array: 1,000,000 * 32 KB = 30.5 GB
// - banding array: 1,000,000 * 4 B = 3.8 MB
// - Total RAM: ~61 GB (fits in 64 GB with headroom)
//
// === THREADING CONSTANTS ===
//
// OpenMP Threads: 24 (configured for 24-thread CPU)
// MAX_THREADS: 128 (maximum tracked threads for profiling)
//
// === VERBOSITY CONSTANTS ===
//
// VERBOSE_SAMPLE_RATE: Print every Nth operation (default: 100,000)
// TRACE_SPECIFIC_CIVILIAN: -1 = sample all, else trace only that civilian ID
//
// ============================================================================
const int64_t TOTAL_POPULATION = INT64_MAX; // 9,223,372,036,854,775,807 minds (max signed long long)
const int64_t CHUNK_SIZE = 100000 LL ; // Process 100k civilians per chunk
const int64_t ACTIVE_POPULATION = 100000 LL ; // 100k civilians allocated in RAM at once (~6.1 GB)
const int64_t ENTITY_NODES = 350000 LL ; // 350k EntityPerson source nodes (doubled for 64GB)
const int64_t MAX_CIVILIANS = INT64_MAX; // Maximum civilian ID (9.22 quintillion)
const size_t DIMENSIONS = 8192 ; // 8k-dimensional mind vector (2^13, SIMD-aligned)
const int STAGES = 50 ; // 50-stage prediction pipeline
const int64_t NUM_CHUNKS = (TOTAL_POPULATION / CHUNK_SIZE) + 1 ; // ~9.2 trillion chunks for full population
// Print all constants at startup (called from print_verbosity_config)
inline void trace_all_constants () {
if constexpr (ULTRA_VERBOSE) {
std::cout << " \n [CONST] === CORE CONSTANTS ===" << std::endl;
std::cout << "[CONST] TOTAL_POPULATION = " << TOTAL_POPULATION << " (INT64_MAX = 9.22 quintillion)" << std::endl;
std::cout << "[CONST] CHUNK_SIZE = " << CHUNK_SIZE << " (100k per chunk)" << std::endl;
std::cout << "[CONST] ACTIVE_POPULATION = " << ACTIVE_POPULATION << " (100k in RAM)" << std::endl;
std::cout << "[CONST] ENTITY_NODES = " << ENTITY_NODES << " (350k source nodes)" << std::endl;
std::cout << "[CONST] MAX_CIVILIANS = " << MAX_CIVILIANS << " (INT64_MAX)" << std::endl;
std::cout << "[CONST] DIMENSIONS = " << DIMENSIONS << " (8k floats per mind)" << std::endl;
std::cout << "[CONST] STAGES = " << STAGES << " (prediction pipeline stages)" << std::endl;
std::cout << "[CONST] NUM_CHUNKS = " << NUM_CHUNKS << " (total chunks to process)" << std::endl;
std::cout << "[CONST] VERBOSE_SAMPLE_RATE = " << VERBOSE_SAMPLE_RATE << std::endl;
std::cout << "[CONST] TRACE_SPECIFIC_CIVILIAN = " << TRACE_SPECIFIC_CIVILIAN << std::endl;
std::cout << "[CONST] === MEMORY ESTIMATES ===" << std::endl;
std::cout << "[CONST] Per civilian: ~65 KB (weights + ghost + banding)" << std::endl;
std::cout << "[CONST] Active RAM: ~6.1 GB for " << ACTIVE_POPULATION << " civilians" << std::endl;
std::cout << "[CONST] SIMD: 8 floats per AVX2 operation, 1024 ops per mind" << std::endl;
std::cout << "[CONST] === END CONSTANTS === \n " << std::endl;
}
}
// Print verbosity configuration at startup
inline void print_verbosity_config () {
std::cout << " \n --- VERBOSITY CONFIGURATION ---" << std::endl;
std::cout << "ULTRA_VERBOSE: " << (ULTRA_VERBOSE ? "ENABLED" : "DISABLED" ) << std::endl;
if constexpr (ULTRA_VERBOSE) {
std::cout << "VERBOSE_SAMPLE_RATE: " << VERBOSE_SAMPLE_RATE << std::endl;
std::cout << "TRACE_SPECIFIC_CIVILIAN: " << (TRACE_SPECIFIC_CIVILIAN >= 0 ? std:: to_string (TRACE_SPECIFIC_CIVILIAN) : "ALL (sampled)" ) << std::endl;
std::cout << "TRACE_LEVEL: " << trace_level_name (g_min_trace_level)
<< " (only " << trace_level_name (g_min_trace_level) << " and above)" << std::endl;
std::cout << "Stage tracing:" << std::endl;
std::cout << " INIT: " << (TRACE_INIT ? "ON" : "OFF" ) << std::endl;
std::cout << " BANDING: " << (TRACE_BANDING ? "ON" : "OFF" ) << std::endl;
std::cout << " EXPANSION: " << (TRACE_EXPANSION ? "ON" : "OFF" ) << std::endl;
std::cout << " ASSUMING: " << (TRACE_ASSUMING ? "ON" : "OFF" ) << std::endl;
std::cout << " PREDICTION: " << (TRACE_PREDICTION ? "ON" : "OFF" ) << std::endl;
std::cout << " PLACEMENT: " << (TRACE_PLACEMENT ? "ON" : "OFF" ) << std::endl;
std::cout << "Step 10 Features:" << std::endl;
std::cout << " Function Profiling: ENABLED" << std::endl;
std::cout << " Correlation IDs: ENABLED (Session: " << std::hex << g_correlation . session_id << std::dec << ")" << std::endl;
std::cout << " Watchdog Monitor: ENABLED (timeout=" << g_watchdog . heartbeat_timeout_ms << "ms)" << std::endl;
std::cout << " Timing Histograms: ENABLED" << std::endl;
}
std::cout << "-------------------------------- \n " << std::endl;
// Trace all constants at startup
trace_all_constants ();
}
// --- THE QUAD MIND STRUCTURE (from combined.cpp + step2.cpp) ---
struct QuadMind {
float * weights; // Banded weights
float * ghost; // Assuming buffer
float * banding_pressures; // The "Stickiness" factor per entity (from step2)
QuadMind () {
TRACE_FUNC_ENTRY ( "QuadMind::QuadMind()" )
// Allocate for ACTIVE_POPULATION (500k) to fit in RAM
// Each chunk reuses this memory
size_t weights_size = ACTIVE_POPULATION * DIMENSIONS * sizeof ( float );
size_t ghost_size = ACTIVE_POPULATION * DIMENSIONS * sizeof ( float );
size_t banding_size = ACTIVE_POPULATION * sizeof ( float );
TRACE_VAR ( "weights_size" , weights_size, "QuadMind allocation" )
TRACE_VAR ( "ghost_size" , ghost_size, "QuadMind allocation" )
TRACE_VAR ( "banding_size" , banding_size, "QuadMind allocation" )
weights = ( float * )std:: aligned_alloc ( 64 , weights_size);
TRACE_ALLOC ( "weights" , weights_size, ( void * )weights)
ghost = ( float * )std:: aligned_alloc ( 64 , ghost_size);
TRACE_ALLOC ( "ghost" , ghost_size, ( void * )ghost)
banding_pressures = ( float * )std:: aligned_alloc ( 64 , banding_size);
TRACE_ALLOC ( "banding_pressures" , banding_size, ( void * )banding_pressures)
bool alloc_success = (weights && ghost && banding_pressures);
TRACE_BRANCH ( "weights && ghost && banding_pressures" , alloc_success)
if ( ! alloc_success) {
std::cerr << "[ERROR] Failed to allocate QuadMind memory!" << std::endl;
}
TRACE_FUNC_EXIT ( "QuadMind::QuadMind()" )
}
~QuadMind () {
TRACE_FUNC_ENTRY ( "QuadMind::~QuadMind()" )
if (weights) {
TRACE_VAR ( "freeing weights" , ( void * )weights, "destructor" )
std:: free (weights);
}
if (ghost) {
TRACE_VAR ( "freeing ghost" , ( void * )ghost, "destructor" )
std:: free (ghost);
}
if (banding_pressures) {
TRACE_VAR ( "freeing banding_pressures" , ( void * )banding_pressures, "destructor" )
std:: free (banding_pressures);
}
TRACE_FUNC_EXIT ( "QuadMind::~QuadMind()" )
}
};
// --- THE QUAD NODE (from step1.cpp) ---
// Individual node representation with its own weight vector
class QuadNode {
public:
uint64_t node_id;
std::vector < float > weights;
float banding_coefficient;
QuadNode ( uint64_t id , pcg32 & rng ) : node_id (id) {
TRACE_FUNC_ENTRY ( "QuadNode::QuadNode(id=" + std:: to_string (id) + ")" )
TRACE_VAR ( "node_id" , id, "QuadNode init" )
// Initialize weight dimensions for this "brain"
weights . resize (DIMENSIONS);
TRACE_VAR ( "weights.size()" , DIMENSIONS, "QuadNode init" )
for ( size_t i = 0 ; i < DIMENSIONS; ++ i) {
uint32_t rng_val = rng ();
float normalized = static_cast< float > (rng_val) / static_cast< float > (UINT32_MAX);
float weight_val = normalized * 2.0 f - 1.0 f ;
weights [i] = weight_val;
// Trace first and last weight only
if (i == 0 || i == DIMENSIONS - 1 ) {
if constexpr (ULTRA_VERBOSE) {
if (id % VERBOSE_SAMPLE_RATE == 0 ) {
std::cout << "[OP] WEIGHT_INIT[" << i << "]: rng=" << rng_val
<< " / UINT32_MAX=" << UINT32_MAX << " -> norm=" << normalized
<< " * 2 - 1 -> " << weight_val << std::endl;
}
}
}
}
// Randomize the initial Banding strength
uint32_t band_rng = rng ();
banding_coefficient = static_cast< float > (band_rng) / static_cast< float > (UINT32_MAX);
TRACE_VAR_SAMPLED ( "banding_coefficient" , banding_coefficient, "QuadNode init" , id)
TRACE_FUNC_EXIT ( "QuadNode::QuadNode()" )
}
// Default constructor for vector resize
QuadNode () : node_id ( 0 ), banding_coefficient ( 0.0 f ) {}
};
// --- THE ENTITY PERSON (from step1.cpp) ---
class EntityPerson {
public:
uint64_t civil_id;
QuadNode mind_node;
EntityPerson ( uint64_t id , pcg32 & rng )
: civil_id (id), mind_node (id, rng) {
TRACE_VAR_SAMPLED ( "EntityPerson.civil_id" , id, "EntityPerson init" , id)
}
// Default constructor
EntityPerson () : civil_id ( 0 ) {
TRACE_VAR ( "EntityPerson.civil_id" , 0 , "EntityPerson default init" )
}
};
// --- THE JAMES DEAN DATA CARRIER ---
struct PositiveHex {
std::string sentence;
float intensity;
};
// --- THE QUAD EXPANDING ENGINE (from step3.cpp) ---
// Represents the 50-stage pipeline's "Expansion" phase
class QuadExpandingEngine {
public:
// A map of "Nodes" (words) to their high-dimensional influence vectors
std::unordered_map < std::string, std::vector < float >> expansion_library;
QuadExpandingEngine () {
TRACE_FUNC_ENTRY ( "QuadExpandingEngine::QuadExpandingEngine()" )
// Pre-expanding key concepts during our 20-minute wait time
std::vector < std::string > concepts = { "INVULNERABILITY" , "INVISIBILITY" , "COMBINE" , "RESISTANCE" };
TRACE_VAR ( "concepts.size()" , concepts . size (), "QuadExpandingEngine init" )
for ( size_t idx = 0 ; idx < concepts . size (); ++ idx) {
const auto & concept = concepts [idx];
expansion_library [concept] = std:: vector < float >(DIMENSIONS, 1.0 f );
TRACE_VAR ( "expansion_library[ \" " + concept + " \" ].size()" , DIMENSIONS, "QuadExpandingEngine init" )
}
TRACE_VAR ( "expansion_library.size()" , expansion_library . size (), "QuadExpandingEngine init" )
TRACE_FUNC_EXIT ( "QuadExpandingEngine::QuadExpandingEngine()" )
}
// The EXPANDING function: Floods a person's "Mind" with the context of a word
void expand_node_context ( float * person_weights , const std:: string & word , float intensity ) {
TRACE_FUNC_ENTRY ( "expand_node_context(word= \" " + word + " \" , intensity=" + std:: to_string (intensity) + ")" )
bool word_found = ( expansion_library . find (word) != expansion_library . end ());
TRACE_BRANCH ( "word in expansion_library" , word_found)
if ( ! word_found) {
TRACE_FUNC_EXIT ( "expand_node_context (word not found)" )
return ;
}
const float * influence = expansion_library [word]. data ();
TRACE_VAR ( "influence_ptr" , ( void * )influence, "expand_node_context" )
TRACE_VAR ( "person_weights_ptr" , ( void * )person_weights, "expand_node_context" )
// Use 24 threads to expand the word across the 8192 dimensions
#pragma omp parallel for num_threads (24)
for ( size_t i = 0 ; i < DIMENSIONS; i += 8 ) {
// Trace first iteration only
if (i == 0 ) {
if constexpr (ULTRA_VERBOSE) {
float w0 = person_weights [ 0 ];
float inf0 = influence [ 0 ];
float contrib = inf0 * intensity;
float result0 = w0 + contrib;
#pragma omp critical
std::cout << "[OP] EXPAND_SIMD[0]: weight=" << w0 << " + (influence=" << inf0
<< " * intensity=" << intensity << ") = " << result0 << std::endl;
}
}
// AVX-256 SIMD: Process 8 floats at once for zero-latency expansion
__m256 weight_vec = _mm256_load_ps ( & person_weights [i]);
COUNT_SIMD_LOAD ()
__m256 influence_vec = _mm256_load_ps ( & influence [i]);
COUNT_SIMD_LOAD ()
__m256 intensity_vec = _mm256_set1_ps (intensity);
COUNT_SIMD_BROADCAST ()
// Weight = Weight + (Influence * Intensity)
__m256 result = _mm256_add_ps (weight_vec, _mm256_mul_ps (influence_vec, intensity_vec));
COUNT_SIMD_MUL ()
COUNT_SIMD_ADD ()
_mm256_store_ps ( & person_weights [i], result);
COUNT_SIMD_STORE ()
}
TRACE_FUNC_EXIT ( "expand_node_context" )
}
// Add a new concept to the expansion library
// feb. 5th, 2026 --;
void add_concept ( const std:: string & concept , float base_value = 1.0 f ) {
TRACE_FUNC_ENTRY ( "add_concept( \" " + concept + " \" , base_value=" + std:: to_string (base_value) + ")" )
expansion_library [concept] = std:: vector < float >(DIMENSIONS, base_value);
TRACE_VAR ( "expansion_library.size()" , expansion_library . size (), "add_concept" )
TRACE_FUNC_EXIT ( "add_concept" )
}
};
// --- THE QUAD PREDICTOR (from step5.cpp) ---
// The 50-Stage Pipeline: Distilling the 8k Mind into 3 Actions
// ENHANCED: Multiple processing phases for deeper mind analysis
class QuadPredictor {
public:
struct PredictionResult {
float move_x;
float move_y;
float fire_intensity;
float confidence; // How confident is this prediction
float complexity; // How complex was the mind
int stages_processed; // How many stages were actually processed
};
// Enhanced 50-Stage Pipeline with multiple processing phases
PredictionResult predict_action ( float * mind_weights , bool verbose = false ) {
TRACE_FUNC_ENTRY ( "QuadPredictor::predict_action(verbose=" + std:: string (verbose ? "true" : "false" ) + ")" )
float distilled_sum_x = 0.0 f ;
float distilled_sum_y = 0.0 f ;
float distilled_fire = 0.0 f ;
float variance_sum = 0.0 f ;
float max_activation = 0.0 f ;
TRACE_VAR ( "distilled_sum_x" , distilled_sum_x, "predict_action init" )
TRACE_VAR ( "distilled_sum_y" , distilled_sum_y, "predict_action init" )
TRACE_VAR ( "distilled_fire" , distilled_fire, "predict_action init" )
TRACE_VAR ( "variance_sum" , variance_sum, "predict_action init" )
TRACE_VAR ( "max_activation" , max_activation, "predict_action init" )
// Phase 1: Initial weight analysis - calculate mean
if constexpr (ULTRA_VERBOSE) {
if (verbose) std::cout << "[PHASE] 1: Initial weight analysis - calculate mean" << std::endl;
}
float mean = 0.0 f ;
for ( size_t i = 0 ; i < DIMENSIONS; ++ i) {
mean += mind_weights [i];
// Trace first weight addition
if (i == 0 && verbose) {
TRACE_OP ( "MEAN_ADD" , "mean=0" , mind_weights [ 0 ], mind_weights [ 0 ], "Phase 1 first iter" )
}
}
float raw_sum = mean;
mean /= DIMENSIONS;
TRACE_VAR ( "raw_sum" , raw_sum, "predict_action Phase 1" )
TRACE_OP ( "MEAN_DIV" , raw_sum, ( float )DIMENSIONS, mean, "predict_action Phase 1" )
if constexpr (ULTRA_VERBOSE) {
if (verbose) {
std::cout << "[OP] MEAN: sum=" << raw_sum << " / dims=" << DIMENSIONS << " -> mean=" << mean << std::endl;
}
}
// Phase 2: Variance calculation for complexity
if constexpr (ULTRA_VERBOSE) {
if (verbose) std::cout << "[PHASE] 2: Variance calculation for complexity" << std::endl;
}
for ( size_t i = 0 ; i < DIMENSIONS; ++ i) {
float diff = mind_weights [i] - mean;
float diff_sq = diff * diff;
variance_sum += diff_sq;
// Trace first iteration
if (i == 0 && verbose) {
TRACE_OP ( "DIFF" , mind_weights [ 0 ], mean, diff, "Phase 2 first iter" )
TRACE_OP ( "DIFF_SQ" , diff, diff, diff_sq, "Phase 2 first iter" )
}
}
float variance = variance_sum / DIMENSIONS;
float complexity = std:: sqrt (variance);
TRACE_VAR ( "variance_sum" , variance_sum, "predict_action Phase 2" )
TRACE_OP ( "VAR_DIV" , variance_sum, ( float )DIMENSIONS, variance, "Phase 2" )
TRACE_OP ( "COMPLEXITY_SQRT" , variance, 0.0 f , complexity, "Phase 2" )
if constexpr (ULTRA_VERBOSE) {
if (verbose) {
std::cout << "[OP] VARIANCE: sum_sq=" << variance_sum << " / dims=" << DIMENSIONS << " -> var=" << variance << std::endl;
std::cout << "[OP] COMPLEXITY: sqrt(var=" << variance << ") -> " << complexity << std::endl;
}
}
// Phase 3: Multi-stage distillation with SIMD
if constexpr (ULTRA_VERBOSE) {
if (verbose) {
std::cout << "[PHASE] 3: Multi-stage distillation with SIMD" << std::endl;
std::cout << "[VAR] sum_x = _mm256_setzero_ps() @ Phase 3 init" << std::endl;
std::cout << "[VAR] sum_y = _mm256_setzero_ps() @ Phase 3 init" << std::endl;
std::cout << "[VAR] sum_f = _mm256_setzero_ps() @ Phase 3 init" << std::endl;
std::cout << "[VAR] max_act = _mm256_setzero_ps() @ Phase 3 init" << std::endl;
}
}
__m256 sum_x = _mm256_setzero_ps ();
__m256 sum_y = _mm256_setzero_ps ();
__m256 sum_f = _mm256_setzero_ps ();
__m256 max_act = _mm256_setzero_ps ();
// Saturation factors for each stage tier
if constexpr (ULTRA_VERBOSE) {
if (verbose) {
std::cout << "[CONST] sat_factor_1 = 1.2f (Early stages 1-17)" << std::endl;
std::cout << "[CONST] sat_factor_2 = 1.4f (Mid stages 18-34)" << std::endl;
std::cout << "[CONST] sat_factor_3 = 1.6f (Late stages 35-50)" << std::endl;
std::cout << "[CONST] clamp_range = [-1.0, 1.0]" << std::endl;
}
}
const __m256 sat_factor_1 = _mm256_set1_ps ( 1.2 f ); // Early stages
const __m256 sat_factor_2 = _mm256_set1_ps ( 1.4 f ); // Mid stages
const __m256 sat_factor_3 = _mm256_set1_ps ( 1.6 f ); // Late stages
const __m256 one = _mm256_set1_ps ( 1.0 f );
const __m256 neg_one = _mm256_set1_ps ( - 1.0 f );
#pragma omp parallel num_threads (24)
{
__m256 local_x = _mm256_setzero_ps ();
__m256 local_y = _mm256_setzero_ps ();
__m256 local_f = _mm256_setzero_ps ();
__m256 local_max = _mm256_setzero_ps ();
#pragma omp for nowait schedule ( static )
for ( size_t i = 0 ; i < DIMENSIONS; i += 24 ) {
// Trace first iteration of SIMD loop
if (i == 0 ) {
if constexpr (ULTRA_VERBOSE) {
if (verbose) {
#pragma omp critical
{
std::cout << "[SIMD] Loading weights[0..23] for x/y/fire mapping" << std::endl;
std::cout << "[VAR] mind_weights[0] = " << mind_weights [ 0 ] << " @ Phase 3 load" << std::endl;
std::cout << "[VAR] mind_weights[8] = " << mind_weights [ 8 ] << " @ Phase 3 load" << std::endl;
std::cout << "[VAR] mind_weights[16] = " << mind_weights [ 16 ] << " @ Phase 3 load" << std::endl;
}
}
}
}
// Load 24 weights (3 groups of 8 for x, y, fire mapping)
__m256 w0 = _mm256_load_ps ( & mind_weights [i]);
COUNT_SIMD_LOAD ()
__m256 w1 = _mm256_load_ps ( & mind_weights [i + 8 ]);
COUNT_SIMD_LOAD ()
__m256 w2 = _mm256_load_ps ( & mind_weights [i + 16 ]);
COUNT_SIMD_LOAD ()
// Stage tier 1: Early processing (stages 1-17)
w0 = _mm256_mul_ps (w0, sat_factor_1);
COUNT_SIMD_MUL ()
w1 = _mm256_mul_ps (w1, sat_factor_1);
COUNT_SIMD_MUL ()
w2 = _mm256_mul_ps (w2, sat_factor_1);
COUNT_SIMD_MUL ()
// Trace first saturation
if (i == 0 ) {
if constexpr (ULTRA_VERBOSE) {
if (verbose) {
float temp [ 8 ];
_mm256_storeu_ps (temp, w0);
#pragma omp critical
std::cout << "[OP] SAT_TIER1: w0[0]=" << ( mind_weights [ 0 ]) << " * 1.2 -> " << temp [ 0 ] << std::endl;
}
}
}
// Stage tier 2: Mid processing (stages 18-34)
w0 = _mm256_mul_ps (w0, sat_factor_2);
w1 = _mm256_mul_ps (w1, sat_factor_2);
w2 = _mm256_mul_ps (w2, sat_factor_2);
// Stage tier 3: Late processing (stages 35-50)
w0 = _mm256_mul_ps (w0, sat_factor_3);
w1 = _mm256_mul_ps (w1, sat_factor_3);
w2 = _mm256_mul_ps (w2, sat_factor_3);
// Trace final saturation result
if (i == 0 ) {
if constexpr (ULTRA_VERBOSE) {
if (verbose) {
float temp [ 8 ];
_mm256_storeu_ps (temp, w0);
#pragma omp critical
std::cout << "[OP] SAT_ALL: w0[0] after all tiers (1.2*1.4*1.6) -> " << temp [ 0 ] << std::endl;
}
}
}
// Track max activation before clamping
__m256 abs_mask = _mm256_castsi256_ps ( _mm256_set1_epi32 ( 0x 7FFFFFFF ));
local_max = _mm256_max_ps (local_max, _mm256_and_ps (w0, abs_mask));
local_max = _mm256_max_ps (local_max, _mm256_and_ps (w1, abs_mask));
local_max = _mm256_max_ps (local_max, _mm256_and_ps (w2, abs_mask));
// Clamp to [-1, 1]
w0 = _mm256_min_ps ( _mm256_max_ps (w0, neg_one), one);
w1 = _mm256_min_ps ( _mm256_max_ps (w1, neg_one), one);
w2 = _mm256_min_ps ( _mm256_max_ps (w2, neg_one), one);
// Map to outputs
local_x = _mm256_add_ps (local_x, w0);
local_y = _mm256_add_ps (local_y, w1);
local_f = _mm256_add_ps (local_f, w2);
}
// Horizontal sum
if constexpr (ULTRA_VERBOSE) {
if (verbose) {
#pragma omp critical
std::cout << "[OP] HORIZONTAL_SUM: Reducing 8-lane SIMD to scalar" << std::endl;
}
}
float lx [ 8 ], ly [ 8 ], lf [ 8 ], lm [ 8 ];
_mm256_storeu_ps (lx, local_x);
_mm256_storeu_ps (ly, local_y);
_mm256_storeu_ps (lf, local_f);
_mm256_storeu_ps (lm, local_max);
float thread_x = lx [ 0 ] + lx [ 1 ] + lx [ 2 ] + lx [ 3 ] + lx [ 4 ] + lx [ 5 ] + lx [ 6 ] + lx [ 7 ];
float thread_y = ly [ 0 ] + ly [ 1 ] + ly [ 2 ] + ly [ 3 ] + ly [ 4 ] + ly [ 5 ] + ly [ 6 ] + ly [ 7 ];
float thread_f = lf [ 0 ] + lf [ 1 ] + lf [ 2 ] + lf [ 3 ] + lf [ 4 ] + lf [ 5 ] + lf [ 6 ] + lf [ 7 ];
float thread_m = std:: max ({ lm [ 0 ], lm [ 1 ], lm [ 2 ], lm [ 3 ], lm [ 4 ], lm [ 5 ], lm [ 6 ], lm [ 7 ]});
if constexpr (ULTRA_VERBOSE) {
if (verbose) {
#pragma omp critical
{
std::cout << "[VAR] thread_x = " << thread_x << " @ thread " << omp_get_thread_num () << std::endl;
std::cout << "[VAR] thread_y = " << thread_y << " @ thread " << omp_get_thread_num () << std::endl;
std::cout << "[VAR] thread_f = " << thread_f << " @ thread " << omp_get_thread_num () << std::endl;
std::cout << "[VAR] thread_m = " << thread_m << " @ thread " << omp_get_thread_num () << std::endl;
}
}
}
#pragma omp atomic
distilled_sum_x += thread_x;
#pragma omp atomic
distilled_sum_y += thread_y;
#pragma omp atomic
distilled_fire += thread_f;
#pragma omp critical
{ max_activation = std:: max (max_activation, thread_m); }
}
// Phase 4: Confidence calculation based on activation spread
if constexpr (ULTRA_VERBOSE) {
if (verbose) std::cout << "[PHASE] 4: Confidence calculation" << std::endl;
}
TRACE_VAR ( "distilled_sum_x (final)" , distilled_sum_x, "Phase 4" )
TRACE_VAR ( "distilled_sum_y (final)" , distilled_sum_y, "Phase 4" )
TRACE_VAR ( "distilled_fire (final)" , distilled_fire, "Phase 4" )
TRACE_VAR ( "max_activation (final)" , max_activation, "Phase 4" )
float sigmoid_input = - max_activation + 2.0 f ;
TRACE_OP ( "SIGMOID_INPUT" , - max_activation, 2.0 f , sigmoid_input, "Phase 4" )
float exp_val = std:: exp (sigmoid_input);
float confidence = 1.0 f / ( 1.0 f + exp_val);
TRACE_OP ( "SIGMOID" , 1.0 f , ( 1.0 f + exp_val), confidence, "Phase 4" )
// Final output calculations
float raw_x = distilled_sum_x / DIMENSIONS;
float raw_y = distilled_sum_y / DIMENSIONS;
float raw_f = distilled_fire / DIMENSIONS;
TRACE_OP ( "NORM_X" , distilled_sum_x, ( float )DIMENSIONS, raw_x, "Phase 4 final" )
TRACE_OP ( "NORM_Y" , distilled_sum_y, ( float )DIMENSIONS, raw_y, "Phase 4 final" )
TRACE_OP ( "NORM_F" , distilled_fire, ( float )DIMENSIONS, raw_f, "Phase 4 final" )
float final_move_x = std:: tanh (raw_x);
float final_move_y = std:: tanh (raw_y);
float final_fire = std:: clamp (raw_f, 0.0 f , 1.0 f );
TRACE_OP ( "TANH_X" , raw_x, 0.0 f , final_move_x, "Phase 4 final" )
TRACE_OP ( "TANH_Y" , raw_y, 0.0 f , final_move_y, "Phase 4 final" )
TRACE_OP ( "CLAMP_F" , raw_f, 0.0 f , final_fire, "Phase 4 final" )
if constexpr (ULTRA_VERBOSE) {
if (verbose) {
std::cout << "[OP] DISTILL_X: sum=" << distilled_sum_x << " / dims=" << DIMENSIONS
<< " -> raw=" << (distilled_sum_x / DIMENSIONS) << " -> tanh=" << final_move_x << std::endl;
std::cout << "[OP] DISTILL_Y: sum=" << distilled_sum_y << " / dims=" << DIMENSIONS
<< " -> raw=" << (distilled_sum_y / DIMENSIONS) << " -> tanh=" << final_move_y << std::endl;
std::cout << "[OP] DISTILL_F: sum=" << distilled_fire << " / dims=" << DIMENSIONS
<< " -> raw=" << (distilled_fire / DIMENSIONS) << " -> clamp=" << final_fire << std::endl;
std::cout << "[OP] CONFIDENCE: max_act=" << max_activation << " -> sigmoid(" << sigmoid_input << ") = " << confidence << std::endl;
}
}
if constexpr (ULTRA_VERBOSE) {
if (verbose) {
std::cout << "[RETURN] PredictionResult {" << std::endl;
std::cout << " move_x: " << final_move_x << std::endl;
std::cout << " move_y: " << final_move_y << std::endl;
std::cout << " fire_intensity: " << final_fire << std::endl;
std::cout << " confidence: " << confidence << std::endl;
std::cout << " complexity: " << complexity << std::endl;
std::cout << " stages_processed: " << STAGES << std::endl;
std::cout << "}" << std::endl;
}
}
TRACE_FUNC_EXIT ( "QuadPredictor::predict_action" )
return {
final_move_x,
final_move_y,
final_fire,
confidence,
complexity,
STAGES
};
}
};
// --- THE CIVILIAN OBJECT ---
// A civilian that receives a processed mind from all stages
struct Civilian {
int64_t civilian_id;
bool mind_placed; // Has a mind been placed on this civilian?
QuadPredictor::PredictionResult prediction;
float banding_pressure;
float ghost_entropy; // How much the ghost has decayed
float expansion_level; // Total expansion applied
float assumption_decay; // Decay from assuming stage
int64_t processing_chunk; // Which chunk was this processed in
// INVULNERABILITY - persists and accumulates across operations
float existing_invulnerability; // Civilian's current invulnerability level
float invulnerability_history [ 8 ]; // Last 8 invulnerability values for tracking
int invulnerability_history_idx; // Current index in history buffer
Civilian () : civilian_id ( 0 ), mind_placed ( false ), banding_pressure ( 0 ),
ghost_entropy ( 0 ), expansion_level ( 0 ), assumption_decay ( 0 ), processing_chunk ( 0 ),
existing_invulnerability ( 0.0 f ), invulnerability_history_idx ( 0 ) {
TRACE_FUNC_ENTRY ( "Civilian::Civilian() [default]" )
prediction = { 0 , 0 , 0 };
std:: memset (invulnerability_history, 0 , sizeof (invulnerability_history));
TRACE_VAR ( "civilian_id" , 0 , "Civilian default init" )
TRACE_VAR ( "mind_placed" , false , "Civilian default init" )
TRACE_VAR ( "existing_invulnerability" , 0.0 f , "Civilian default init" )
TRACE_FUNC_EXIT ( "Civilian::Civilian() [default]" )
}
Civilian ( int64_t id ) : civilian_id (id), mind_placed ( false ), banding_pressure ( 0 ),
ghost_entropy ( 0 ), expansion_level ( 0 ), assumption_decay ( 0 ), processing_chunk ( 0 ),
existing_invulnerability ( 0.0 f ), invulnerability_history_idx ( 0 ) {
TRACE_FUNC_ENTRY ( "Civilian::Civilian(id=" + std:: to_string (id) + ")" )
prediction = { 0 , 0 , 0 };
std:: memset (invulnerability_history, 0 , sizeof (invulnerability_history));
TRACE_VAR ( "civilian_id" , id, "Civilian init" )
TRACE_VAR ( "mind_placed" , false , "Civilian init" )
TRACE_VAR ( "banding_pressure" , 0 , "Civilian init" )
TRACE_VAR ( "ghost_entropy" , 0 , "Civilian init" )
TRACE_VAR ( "expansion_level" , 0 , "Civilian init" )
TRACE_VAR ( "assumption_decay" , 0 , "Civilian init" )
TRACE_VAR ( "existing_invulnerability" , 0.0 f , "Civilian init" )
TRACE_FUNC_EXIT ( "Civilian::Civilian(id)" )
}
// Push new invulnerability onto existing (accumulates, doesn't replace)
void push_invulnerability ( float delta ) {
TRACE_FUNC_ENTRY ( "push_invulnerability(delta=" + std:: to_string (delta) + ")" )
TRACE_VAR ( "existing_invulnerability (before)" , existing_invulnerability, "push_invulnerability" )
TRACE_VAR ( "invulnerability_history_idx" , invulnerability_history_idx, "push_invulnerability" )
// Record in history
invulnerability_history [invulnerability_history_idx] = delta;
TRACE_VAR ( "invulnerability_history[" + std:: to_string (invulnerability_history_idx) + "]" , delta, "push_invulnerability" )
int old_idx = invulnerability_history_idx;
invulnerability_history_idx = (invulnerability_history_idx + 1 ) % 8 ;
TRACE_OP ( "IDX_WRAP" , old_idx + 1 , 8 , invulnerability_history_idx, "push_invulnerability" )
// Accumulate onto existing invulnerability
float old_invuln = existing_invulnerability;
existing_invulnerability += delta;
TRACE_OP ( "ACCUM_INVULN" , old_invuln, delta, existing_invulnerability, "push_invulnerability" )
// Clamp to reasonable bounds (can go above 1.0 for "super invulnerable")
bool needs_clamp = (existing_invulnerability < 0.0 f );
TRACE_BRANCH ( "existing_invulnerability < 0" , needs_clamp)
if (needs_clamp) {
existing_invulnerability = 0.0 f ;
TRACE_VAR ( "existing_invulnerability (clamped)" , 0.0 f , "push_invulnerability" )
}
TRACE_VAR ( "existing_invulnerability (after)" , existing_invulnerability, "push_invulnerability" )
TRACE_FUNC_EXIT ( "push_invulnerability" )
}
// Get total accumulated invulnerability
float get_invulnerability () const {
TRACE_VAR ( "get_invulnerability() returning" , existing_invulnerability, "Civilian" )
return existing_invulnerability;
}
// Get average invulnerability change over history
float get_invulnerability_trend () const {
TRACE_FUNC_ENTRY ( "get_invulnerability_trend()" )
float sum = 0.0 f ;
for ( int i = 0 ; i < 8 ; ++ i) {
sum += invulnerability_history [i];
TRACE_VAR ( "invulnerability_history[" + std:: to_string (i) + "]" , invulnerability_history [i], "get_trend" )
}
float trend = sum / 8.0 f ;
TRACE_OP ( "TREND_AVG" , sum, 8.0 f , trend, "get_invulnerability_trend" )
TRACE_FUNC_EXIT ( "get_invulnerability_trend" )
return trend;
}
};
// --- INVULNERABILITY ACCUMULATION TRACKER ---
// Tracks how invulnerability builds across chunks/iterations
struct InvulnerabilityAccumulator {
float total_delta_applied; // Sum of all invulnerability deltas applied
float min_delta; // Minimum delta seen
float max_delta; // Maximum delta seen
int64_t civilians_processed; // Number of civilians that received invulnerability
float chunk_totals [ 16 ]; // Per-chunk total invulnerability (up to 16 chunks)
float chunk_averages [ 16 ]; // Per-chunk average invulnerability
int64_t chunk_counts [ 16 ]; // Per-chunk civilian counts
int current_chunk; // Current chunk being processed
InvulnerabilityAccumulator () {
TRACE_FUNC_ENTRY ( "InvulnerabilityAccumulator::InvulnerabilityAccumulator()" )
reset ();
TRACE_FUNC_EXIT ( "InvulnerabilityAccumulator::InvulnerabilityAccumulator()" )
}
void reset () {
TRACE_FUNC_ENTRY ( "InvulnerabilityAccumulator::reset()" )
total_delta_applied = 0.0 f ;
min_delta = std::numeric_limits< float >:: max ();
max_delta = std::numeric_limits< float >:: lowest ();
civilians_processed = 0 ;
current_chunk = 0 ;
std:: memset (chunk_totals, 0 , sizeof (chunk_totals));
std:: memset (chunk_averages, 0 , sizeof (chunk_averages));
std:: memset (chunk_counts, 0 , sizeof (chunk_counts));
TRACE_VAR ( "total_delta_applied" , total_delta_applied, "reset" )
TRACE_VAR ( "min_delta" , min_delta, "reset" )
TRACE_VAR ( "max_delta" , max_delta, "reset" )
TRACE_VAR ( "civilians_processed" , civilians_processed, "reset" )
TRACE_VAR ( "current_chunk" , current_chunk, "reset" )
TRACE_FUNC_EXIT ( "InvulnerabilityAccumulator::reset()" )
}
void record_delta ( float delta , int chunk_idx ) {
TRACE_FUNC_ENTRY ( "record_delta(delta=" + std:: to_string (delta) + ", chunk=" + std:: to_string (chunk_idx) + ")" )
float old_total = total_delta_applied;
total_delta_applied += delta;
TRACE_OP ( "TOTAL_ADD" , old_total, delta, total_delta_applied, "record_delta" )
bool new_min = (delta < min_delta);
bool new_max = (delta > max_delta);
TRACE_BRANCH ( "delta < min_delta" , new_min)
TRACE_BRANCH ( "delta > max_delta" , new_max)
if (new_min) {
min_delta = delta;
TRACE_VAR ( "min_delta (new)" , min_delta, "record_delta" )
}
if (new_max) {
max_delta = delta;
TRACE_VAR ( "max_delta (new)" , max_delta, "record_delta" )
}
civilians_processed ++ ;
TRACE_VAR ( "civilians_processed" , civilians_processed, "record_delta" )
bool valid_chunk = (chunk_idx >= 0 && chunk_idx < 16 );
TRACE_BRANCH ( "chunk_idx in [0,16)" , valid_chunk)
if (valid_chunk) {
chunk_totals [chunk_idx] += delta;
chunk_counts [chunk_idx] ++ ;
TRACE_VAR ( "chunk_totals[" + std:: to_string (chunk_idx) + "]" , chunk_totals [chunk_idx], "record_delta" )
TRACE_VAR ( "chunk_counts[" + std:: to_string (chunk_idx) + "]" , chunk_counts [chunk_idx], "record_delta" )
}
TRACE_FUNC_EXIT ( "record_delta" )
}
void finalize_chunk ( int chunk_idx ) {
TRACE_FUNC_ENTRY ( "finalize_chunk(chunk_idx=" + std:: to_string (chunk_idx) + ")" )
bool can_finalize = (chunk_idx >= 0 && chunk_idx < 16 && chunk_counts [chunk_idx] > 0 );
TRACE_BRANCH ( "can_finalize (valid idx && count > 0)" , can_finalize)
if (can_finalize) {
chunk_averages [chunk_idx] = chunk_totals [chunk_idx] / chunk_counts [chunk_idx];
TRACE_OP ( "AVG_CALC" , chunk_totals [chunk_idx], ( float ) chunk_counts [chunk_idx], chunk_averages [chunk_idx], "finalize_chunk" )
TRACE_VAR ( "chunk_averages[" + std:: to_string (chunk_idx) + "]" , chunk_averages [chunk_idx], "finalize_chunk" )
}
current_chunk = chunk_idx + 1 ;
TRACE_VAR ( "current_chunk" , current_chunk, "finalize_chunk" )
TRACE_FUNC_EXIT ( "finalize_chunk" )
}
float get_global_average () const {
TRACE_FUNC_ENTRY ( "get_global_average()" )
bool has_civilians = (civilians_processed > 0 );
TRACE_BRANCH ( "civilians_processed > 0" , has_civilians)
float avg = has_civilians ? total_delta_applied / civilians_processed : 0.0 f ;
TRACE_OP ( "GLOBAL_AVG" , total_delta_applied, ( float )civilians_processed, avg, "get_global_average" )
TRACE_FUNC_EXIT ( "get_global_average" )
return avg;
}
void print_summary () const {
TRACE_FUNC_ENTRY ( "print_summary()" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" --- INVULNERABILITY ACCUMULATION SUMMARY --- \" " << std::endl;
}
std::cout << " \n --- INVULNERABILITY ACCUMULATION SUMMARY ---" << std::endl;
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" Total delta applied: " << total_delta_applied << " \" " << std::endl;
}
std::cout << "Total delta applied: " << total_delta_applied << std::endl;
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" Civilians processed: " << civilians_processed << " \" " << std::endl;
}
std::cout << "Civilians processed: " << civilians_processed << std::endl;
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" Global average: " << get_global_average () << " \" " << std::endl;
}
std::cout << "Global average: " << get_global_average () << std::endl;
std::cout << "Min delta: " << min_delta << std::endl;
std::cout << "Max delta: " << max_delta << std::endl;
std::cout << " \n Per-chunk breakdown:" << std::endl;
for ( int i = 0 ; i < current_chunk && i < 16 ; ++ i) {
if constexpr (ULTRA_VERBOSE) {
std::cout << "[LOOP] print_summary chunk iteration: " << i << "/" << current_chunk << std::endl;
}
std::cout << " Chunk " << (i + 1 ) << ": total=" << chunk_totals [i]
<< ", avg=" << chunk_averages [i]
<< ", count=" << chunk_counts [i] << std::endl;
}
std::cout << "--------------------------------------------" << std::endl;
TRACE_FUNC_EXIT ( "print_summary" )
}
};
// --- THE UNIFIED ENGINE ---
class JamesDeanQuad {
private:
QuadMind mind;
pcg64 rng64;
pcg32 rng32;
std::vector < EntityPerson > entity_population;
QuadExpandingEngine expander; // Step3 expanding engine
QuadPredictor predictor; // Step5 prediction engine
std::vector < Civilian > civilians; // Collected civilian objects
// Per-chunk invulnerability deltas (calculated during expansion, applied during placement)
float * invulnerability_deltas;
// Global invulnerability accumulation tracker
InvulnerabilityAccumulator invuln_tracker;
public:
JamesDeanQuad ()
: rng64 { 0x 853c49e6748fea9b ULL , 0x da3e39cb94b95bdb ULL },
rng32 { 0x 853c49e6748fea9b ULL , 0x da3e39cb94b95bdb ULL } {
TRACE_FUNC_ENTRY ( "JamesDeanQuad::JamesDeanQuad()" )
TRACE_VAR ( "rng64 seed" , "0x853c49e6748fea9bULL" , "JamesDeanQuad init" )
TRACE_VAR ( "rng32 seed" , "0x853c49e6748fea9bULL" , "JamesDeanQuad init" )
// Allocate buffer for tracking invulnerability changes per person in chunk
size_t delta_size = ACTIVE_POPULATION * sizeof ( float );
TRACE_VAR ( "delta_buffer_size" , delta_size, "JamesDeanQuad init" )
invulnerability_deltas = ( float * )std:: aligned_alloc ( 64 , delta_size);
TRACE_ALLOC ( "invulnerability_deltas" , delta_size, ( void * )invulnerability_deltas)
std:: memset (invulnerability_deltas, 0 , delta_size);
TRACE_VAR ( "invulnerability_deltas zeroed" , delta_size, "JamesDeanQuad init" )
TRACE_FUNC_EXIT ( "JamesDeanQuad::JamesDeanQuad()" )
}
~JamesDeanQuad () {
TRACE_FUNC_ENTRY ( "JamesDeanQuad::~JamesDeanQuad()" )
if (invulnerability_deltas) {
TRACE_VAR ( "freeing invulnerability_deltas" , ( void * )invulnerability_deltas, "destructor" )
std:: free (invulnerability_deltas);
}
TRACE_FUNC_EXIT ( "JamesDeanQuad::~JamesDeanQuad()" )
}
// Get the invulnerability tracker for summary output
const InvulnerabilityAccumulator & get_invuln_tracker () const {
return invuln_tracker;
}
// STEP 1: INITIALIZE ENTITY POPULATION (from step1.cpp)
void initialize_entities ( int64_t count = ENTITY_NODES) {
TRACE_FUNC_ENTRY ( "initialize_entities(count=" + std:: to_string (count) + ")" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [SYSTEM] Initializing " << count << " EntityPerson nodes... \" " << std::endl;
}
std::cout << "[SYSTEM] Initializing " << count << " EntityPerson nodes..." << std::endl;
entity_population . reserve (count);
TRACE_VAR ( "entity_population.capacity()" , count, "initialize_entities" )
for ( int64_t i = 0 ; i < count; ++ i) {
entity_population . emplace_back (i, rng32);
TRACE_LOOP ( "entity_creation" , i, count)
if (i % 25000 == 0 && i > 0 ) {
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" ...created " << i << " entities \" " << std::endl;
}
std::cout << " ...created " << i << " entities" << std::endl;
}
}
TRACE_VAR ( "entity_population.size()" , entity_population . size (), "initialize_entities" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [SYSTEM] Entity population complete: " << entity_population . size () << " nodes \" " << std::endl;
}
std::cout << "[SYSTEM] Entity population complete: " << entity_population . size () << " nodes" << std::endl;
TRACE_FUNC_EXIT ( "initialize_entities" )
}
// STEP 1 & 2: INITIALIZE & BAND for a chunk
void initialize_chunk ( int64_t chunk_num ) {
TRACE_FUNC_ENTRY ( "initialize_chunk(chunk_num=" + std:: to_string (chunk_num) + ")" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [CHUNK " << chunk_num << "] Initializing and banding " << ACTIVE_POPULATION << " minds... \" " << std::endl;
}
std::cout << " [CHUNK " << chunk_num << "] Initializing and banding " << ACTIVE_POPULATION << " minds..." << std::endl;
TRACE_STAGE ( "INITIALIZE" , chunk_num, "Starting weight initialization" )
TRACE_VAR ( "ACTIVE_POPULATION" , ACTIVE_POPULATION, "initialize_chunk" )
TRACE_VAR ( "DIMENSIONS" , DIMENSIONS, "initialize_chunk" )
#pragma omp parallel num_threads (24)
{
int tid = omp_get_thread_num ();
TRACE_VAR ( "thread_id" , tid, "initialize_chunk parallel" )
// Use chunk_num to create unique random streams per chunk
__uint128_t seed1 = ( __uint128_t )(tid + chunk_num * 100 );
__uint128_t seed2 = ( __uint128_t )(tid + chunk_num * 100 ) | 0x ABCDEF ;
pcg64 thread_rng = {seed1, seed2};
TRACE_VAR ( "thread_rng seed1" , ( uint64_t )seed1, "initialize_chunk" )
TRACE_VAR ( "thread_rng seed2" , ( uint64_t )seed2, "initialize_chunk" )
#pragma omp for schedule ( static )
for ( int64_t i = 0 ; i < ACTIVE_POPULATION; ++ i) {
TRACE_LOOP ( "init_population" , i, ACTIVE_POPULATION)
// Initialize banding pressure (The "Stickiness" from step2)
uint64_t rng_val = thread_rng () % 1000 ;
float banding_pressure = ( float )rng_val / 1000.0 f ;
mind . banding_pressures [i] = banding_pressure;
TRACE_VAR_SAMPLED ( "rng_val" , rng_val, make_context (chunk_num, i, "init" ), i)
TRACE_OP_SAMPLED ( "BANDING_CALC" , ( float )rng_val, 1000.0 f , banding_pressure, make_context (chunk_num, i, "init" ), i)
TRACE_VAR_SAMPLED ( "mind.banding_pressures[" + std:: to_string (i) + "]" , banding_pressure,
make_context (chunk_num, i, "init" ), i)
for ( size_t j = 0 ; j < DIMENSIONS; ++ j) {
int64_t raw_rng = thread_rng () % 2000 - 1000 ;
float val = ( float )raw_rng / 1000.0 f ;
float final_weight = val * banding_pressure;
mind . weights [i * DIMENSIONS + j] = final_weight;
mind . ghost [i * DIMENSIONS + j] = final_weight;
// Only trace first weight per sampled civilian
if (j == 0 ) {
TRACE_VAR_SAMPLED ( "raw_rng" , raw_rng, make_context (chunk_num, i, "init" ), i)
TRACE_OP_SAMPLED ( "VAL_CALC" , ( float )raw_rng, 1000.0 f , val, make_context (chunk_num, i, "init" ), i)
TRACE_OP_SAMPLED ( "WEIGHT_MUL" , val, banding_pressure, final_weight, make_context (chunk_num, i, "init" ), i)
TRACE_VAR_SAMPLED ( "mind.weights[" + std:: to_string (i) + "][0]" , final_weight,
make_context (chunk_num, i, "init" ), i)
TRACE_VAR_SAMPLED ( "mind.ghost[" + std:: to_string (i) + "][0]" , final_weight,
make_context (chunk_num, i, "init" ), i)
}
}
}
}
TRACE_STAGE ( "INITIALIZE" , chunk_num, "Complete" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [CHUNK " << chunk_num << "] Initialization complete. \" " << std::endl;
}
std::cout << " [CHUNK " << chunk_num << "] Initialization complete." << std::endl;
TRACE_FUNC_EXIT ( "initialize_chunk" )
}
// STEP 2: APPLY BANDING UPDATE (from step2.cpp)
// "Banding" - updating one float updates ALL because they are stuck
void apply_banding_update ( float positive_hex_influence ) {
TRACE_FUNC_ENTRY ( "apply_banding_update(influence=" + std:: to_string (positive_hex_influence) + ")" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [SYSTEM] Applying banding update with influence: " << positive_hex_influence << " \" " << std::endl;
}
std::cout << "[SYSTEM] Applying banding update with influence: " << positive_hex_influence << std::endl;
float delta = positive_hex_influence * 0.001 f ;
TRACE_OP ( "DELTA_CALC" , positive_hex_influence, 0.001 f , delta, "apply_banding_update" )
TRACE_VAR ( "total_weights" , ACTIVE_POPULATION * DIMENSIONS, "apply_banding_update" )
#pragma omp parallel for num_threads (24)
for ( int64_t i = 0 ; i < ACTIVE_POPULATION * DIMENSIONS; ++ i) {
THREAD_TASK_START ( "banding_update" );
float old_weight = mind . weights [i];
mind . weights [i] += delta;
TRACE_LOOP ( "banding_update" , i, ACTIVE_POPULATION * DIMENSIONS)
if (i == 0 ) {
TRACE_OP ( "WEIGHT_ADD" , old_weight, delta, mind . weights [i], "apply_banding_update[0]" )
}
THREAD_TASK_END ( 1 );
}
TRACE_FUNC_EXIT ( "apply_banding_update" )
}
// SYNC: Transfer EntityPerson weights into the QuadMind structure
void sync_entities_to_mind () {
TRACE_FUNC_ENTRY ( "sync_entities_to_mind()" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [SYSTEM] Syncing entity weights to QuadMind... \" " << std::endl;
}
std::cout << "[SYSTEM] Syncing entity weights to QuadMind..." << std::endl;
int64_t sync_count = std:: min (( int64_t ) entity_population . size (), ACTIVE_POPULATION);
TRACE_VAR ( "entity_population.size()" , entity_population . size (), "sync_entities_to_mind" )
TRACE_VAR ( "ACTIVE_POPULATION" , ACTIVE_POPULATION, "sync_entities_to_mind" )
TRACE_OP ( "MIN" , ( float ) entity_population . size (), ( float )ACTIVE_POPULATION, ( float )sync_count, "sync_entities_to_mind" )
#pragma omp parallel for num_threads (24)
for ( int64_t i = 0 ; i < sync_count; ++ i) {
TRACE_LOOP ( "entity_sync" , i, sync_count)
EntityPerson & entity = entity_population [i];
float * mind_weights = & mind . weights [i * DIMENSIONS];
TRACE_VAR_SAMPLED ( "entity.civil_id" , entity . civil_id , "sync" , i)
TRACE_VAR_SAMPLED ( "entity.mind_node.banding_coefficient" , entity . mind_node . banding_coefficient , "sync" , i)
// Apply banding coefficient during sync
for ( size_t j = 0 ; j < DIMENSIONS; ++ j) {
float entity_weight = entity . mind_node . weights [j];
float coeff = entity . mind_node . banding_coefficient ;
float result = entity_weight * coeff;
mind_weights [j] = result;
if (j == 0 ) {
TRACE_OP_SAMPLED ( "SYNC_MUL" , entity_weight, coeff, result, "sync[0]" , i)
}
}
// Copy to ghost as initial assumption
std:: memcpy ( & mind . ghost [i * DIMENSIONS], mind_weights, DIMENSIONS * sizeof ( float ));
TRACE_VAR_SAMPLED ( "memcpy ghost" , DIMENSIONS * sizeof ( float ), "sync" , i)
}
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [SYSTEM] Sync complete for " << sync_count << " entities. \" " << std::endl;
}
std::cout << "[SYSTEM] Sync complete for " << sync_count << " entities." << std::endl;
TRACE_FUNC_EXIT ( "sync_entities_to_mind" )
}
// STEP 3: EXPAND (Mapping James Dean Sentences to Weights)
void expand_positive_hex ( int64_t civ_id , const PositiveHex & hex ) {
PROFILE_FUNCTION_NAMED ( "expand_positive_hex" );
auto _expand_start = std::chrono::high_resolution_clock:: now ();
TRACE_FUNC_ENTRY ( "expand_positive_hex(civ_id=" + std:: to_string (civ_id) + ", intensity=" + std:: to_string ( hex . intensity ) + ")" )
float * civ_weights = & mind . weights [civ_id * DIMENSIONS];
TRACE_VAR ( "civ_weights_ptr" , ( void * )civ_weights, "expand_positive_hex" )
TRACE_VAR ( "hex.sentence" , hex . sentence , "expand_positive_hex" )
TRACE_VAR ( "hex.intensity" , hex . intensity , "expand_positive_hex" )
#pragma omp parallel for num_threads (24)
for ( size_t j = 0 ; j < DIMENSIONS; j += 8 ) {
__m256 w = _mm256_load_ps ( & civ_weights [j]);
__m256 influence = _mm256_set1_ps ( hex . intensity );
__m256 result = _mm256_add_ps (w, influence);
_mm256_store_ps ( & civ_weights [j], result);
COUNT_SIMD_LOAD ();
COUNT_SIMD_BROADCAST ();
COUNT_SIMD_ADD ();
COUNT_SIMD_STORE ();
if (j == 0 ) {
float temp [ 8 ];
_mm256_storeu_ps (temp, result);
TRACE_OP ( "HEX_EXPAND[0]" , civ_weights [ 0 ], hex . intensity , temp [ 0 ], "expand_positive_hex" )
}
}
if constexpr (ULTRA_VERBOSE) {
auto _expand_end = std::chrono::high_resolution_clock:: now ();
auto _expand_ns = std::chrono:: duration_cast <std::chrono:: nanoseconds >(_expand_end - _expand_start). count ();
HISTOGRAM_RECORD (expand, _expand_ns);
}
TRACE_FUNC_EXIT ( "expand_positive_hex" )
}
// STEP 3: EXPAND NODE CONTEXT (from step3.cpp)
// Floods a person's "Mind" with the context of a specific word from the library
void expand_node_context ( int64_t civ_id , const std:: string & word , float intensity ) {
TRACE_FUNC_ENTRY ( "expand_node_context(civ_id=" + std:: to_string (civ_id) + ", word= \" " + word + " \" , intensity=" + std:: to_string (intensity) + ")" )
float * civ_weights = & mind . weights [civ_id * DIMENSIONS];
TRACE_VAR ( "civ_weights_ptr" , ( void * )civ_weights, "expand_node_context" )
expander . expand_node_context (civ_weights, word, intensity);
TRACE_FUNC_EXIT ( "expand_node_context" )
}
// STEP 3: STAGE 3 EXPANDING (from step3.cpp)
// Expand context for entire civilian population (within active chunk)
void stage_3_expanding ( const std:: string & concept , float intensity ) {
TRACE_FUNC_ENTRY ( "stage_3_expanding(concept= \" " + concept + " \" , intensity=" + std:: to_string (intensity) + ")" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [SYSTEM] Starting Stage 3: Expanding '" << concept << "' context for population... \" " << std::endl;
}
std::cout << "[SYSTEM] Starting Stage 3: Expanding '" << concept << "' context for population..." << std::endl;
TRACE_VAR ( "ACTIVE_POPULATION" , ACTIVE_POPULATION, "stage_3_expanding" )
for ( int64_t p = 0 ; p < ACTIVE_POPULATION; ++ p) {
TRACE_LOOP ( "stage3_expand" , p, ACTIVE_POPULATION)
float * current_mind = & mind . weights [p * DIMENSIONS];
TRACE_VAR_SAMPLED ( "current_mind_ptr" , ( void * )current_mind, "stage_3_expanding" , p)
expander . expand_node_context (current_mind, concept, intensity);
if (p % 100000 == 0 ) {
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" Expanded context for " << p << " civilians... \" " << std::endl;
}
std::cout << " Expanded context for " << p << " civilians..." << std::endl;
}
}
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [SYSTEM] Stage 3 complete. \" " << std::endl;
}
std::cout << "[SYSTEM] Stage 3 complete." << std::endl;
TRACE_FUNC_EXIT ( "stage_3_expanding" )
}
// Add a concept to the expansion library
void add_expansion_concept ( const std:: string & concept , float base_value = 1.0 f ) {
expander . add_concept (concept, base_value);
}
// STEP 4: EXECUTE ASSUMPTION (from step4.cpp)
// The ASSUMING Function - handles data unavailability with entropy decay
void execute_assumption ( int64_t person_index , bool data_unavailable ) {
PROFILE_FUNCTION_NAMED ( "execute_assumption" );
auto _assume_start = std::chrono::high_resolution_clock:: now ();
TRACE_FUNC_ENTRY ( "execute_assumption(person=" + std:: to_string (person_index) + ", data_unavailable=" + (data_unavailable ? "true" : "false" ) + ")" )
float * current_weights = & mind . weights [person_index * DIMENSIONS];
float * ghost_weights = & mind . ghost [person_index * DIMENSIONS];
TRACE_VAR_SAMPLED ( "current_weights_ptr" , ( void * )current_weights, "execute_assumption" , person_index)
TRACE_VAR_SAMPLED ( "ghost_weights_ptr" , ( void * )ghost_weights, "execute_assumption" , person_index)
TRACE_BRANCH ( "data_unavailable" , data_unavailable)
if (data_unavailable) {
// Data is missing! The Xeon must ASSUME.
// We copy the last known "Ghost" state into the current mind
TRACE_VAR ( "action" , "ASSUME from ghost with decay" , "execute_assumption" )
#pragma omp parallel for num_threads (24) schedule ( static )
for ( size_t i = 0 ; i < DIMENSIONS; ++ i) {
// Decay the assumption slightly (Entropy)
// As time passes without data, the assumption gets "fuzzier"
float ghost_val = ghost_weights [i];
float decayed = ghost_val * 0.999 f ;
current_weights [i] = decayed;
if (i == 0 ) {
TRACE_OP_SAMPLED ( "DECAY_MUL" , ghost_val, 0.999 f , decayed, "execute_assumption" , person_index)
}
}
} else {
// Data is present! Update the Ghost Mind for future assumptions
TRACE_VAR ( "action" , "UPDATE ghost from current" , "execute_assumption" )
#pragma omp parallel for num_threads (24) schedule ( static )
for ( size_t i = 0 ; i < DIMENSIONS; ++ i) {
ghost_weights [i] = current_weights [i];
if (i == 0 ) {
TRACE_VAR_SAMPLED ( "ghost[0] = current[0]" , current_weights [ 0 ], "execute_assumption" , person_index)
}
}
}
if constexpr (ULTRA_VERBOSE) {
auto _assume_end = std::chrono::high_resolution_clock:: now ();
auto _assume_ns = std::chrono:: duration_cast <std::chrono:: nanoseconds >(_assume_end - _assume_start). count ();
HISTOGRAM_RECORD (assume, _assume_ns);
}
TRACE_FUNC_EXIT ( "execute_assumption" )
}
// STEP 4: STAGE 4 ASSUMING (from step4.cpp)
// Run assumption across entire population (within active chunk)
void stage_4_assuming ( bool data_unavailable ) {
TRACE_FUNC_ENTRY ( "stage_4_assuming(data_unavailable=" + std:: string (data_unavailable ? "true" : "false" ) + ")" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [SYSTEM] Starting Stage 4: Assuming for population (data_unavailable="
<< (data_unavailable ? "true" : "false" ) << ")... \" " << std::endl;
}
std::cout << "[SYSTEM] Starting Stage 4: Assuming for population (data_unavailable="
<< (data_unavailable ? "true" : "false" ) << ")..." << std::endl;
TRACE_VAR ( "ACTIVE_POPULATION" , ACTIVE_POPULATION, "stage_4_assuming" )
for ( int64_t p = 0 ; p < ACTIVE_POPULATION; ++ p) {
TRACE_LOOP ( "stage4_assume" , p, ACTIVE_POPULATION)
execute_assumption (p, data_unavailable);
if (p % 100000 == 0 ) {
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" Processed assumption for " << p << " civilians... \" " << std::endl;
}
std::cout << " Processed assumption for " << p << " civilians..." << std::endl;
}
}
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [SYSTEM] Stage 4 complete. \" " << std::endl;
}
std::cout << "[SYSTEM] Stage 4 complete." << std::endl;
TRACE_FUNC_EXIT ( "stage_4_assuming" )
}
// STEP 4 & 5: ASSUME & PREDICT (original combined.cpp version)
void process_pipeline ( int64_t civ_id , bool data_installed ) {
TRACE_FUNC_ENTRY ( "process_pipeline(civ_id=" + std:: to_string (civ_id) + ", data_installed=" + (data_installed ? "true" : "false" ) + ")" )
float * civ_weights = & mind . weights [civ_id * DIMENSIONS];
float * civ_ghost = & mind . ghost [civ_id * DIMENSIONS];
TRACE_VAR_SAMPLED ( "civ_weights_ptr" , ( void * )civ_weights, "process_pipeline" , civ_id)
TRACE_VAR_SAMPLED ( "civ_ghost_ptr" , ( void * )civ_ghost, "process_pipeline" , civ_id)
TRACE_BRANCH ( "data_installed" , data_installed)
// Assuming logic (enhanced with step4 entropy decay)
if ( ! data_installed) {
TRACE_VAR ( "action" , "DECAY from ghost" , "process_pipeline" )
#pragma omp parallel for num_threads (24) schedule ( static )
for ( size_t i = 0 ; i < DIMENSIONS; ++ i) {
float ghost_val = civ_ghost [i];
float decayed = ghost_val * 0.999 f ; // Entropy decay
civ_weights [i] = decayed;
if (i == 0 ) {
TRACE_OP_SAMPLED ( "ENTROPY_DECAY" , ghost_val, 0.999 f , decayed, "process_pipeline" , civ_id)
}
}
} else {
// Update ghost for future assumptions
TRACE_VAR ( "action" , "MEMCPY to ghost" , "process_pipeline" )
std:: memcpy (civ_ghost, civ_weights, DIMENSIONS * sizeof ( float ));
TRACE_VAR_SAMPLED ( "memcpy size" , DIMENSIONS * sizeof ( float ), "process_pipeline" , civ_id)
}
// 50-Stage Prediction Distillation
TRACE_VAR ( "STAGES" , STAGES, "process_pipeline distillation" )
float move_x = 0 , move_y = 0 ;
for ( size_t j = 0 ; j < DIMENSIONS; ++ j) {
float w = civ_weights [j];
float w_orig = w;
for ( int s = 0 ; s < STAGES; ++ s) {
w = std:: tanh (w * 1.001 f ); // The "Flattening" crunch
}
if (j == 0 ) {
TRACE_OP_SAMPLED ( "FLATTEN" , w_orig, ( float )STAGES, w, "process_pipeline" , civ_id)
}
if (j % 2 == 0 ) move_x += w; else move_y += w;
}
TRACE_VAR_SAMPLED ( "move_x_sum" , move_x, "process_pipeline" , civ_id)
TRACE_VAR_SAMPLED ( "move_y_sum" , move_y, "process_pipeline" , civ_id)
if (civ_id % 100000 == 0 ) {
float norm_x = move_x / DIMENSIONS;
float norm_y = move_y / DIMENSIONS;
TRACE_OP ( "NORM_X" , move_x, ( float )DIMENSIONS, norm_x, "process_pipeline output" )
TRACE_OP ( "NORM_Y" , move_y, ( float )DIMENSIONS, norm_y, "process_pipeline output" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" Civ " << civ_id << " Predicted Move: (" << norm_x << ", " << norm_y << ") \" " << std::endl;
}
std::cout << "Civ " << civ_id << " Predicted Move: (" << norm_x << ", " << norm_y << ")" << std::endl;
}
TRACE_FUNC_EXIT ( "process_pipeline" )
}
// STEP 5: PREDICT ACTION (from step5.cpp)
// Distill the 8k Mind into 3 Actions using 50-stage pipeline
QuadPredictor:: PredictionResult predict_action ( int64_t civ_id , bool verbose = false ) {
PROFILE_FUNCTION_NAMED ( "predict_action" );
auto _predict_start = std::chrono::high_resolution_clock:: now ();
TRACE_FUNC_ENTRY ( "predict_action(civ_id=" + std:: to_string (civ_id) + ", verbose=" + (verbose ? "true" : "false" ) + ")" )
float * civ_weights = & mind . weights [civ_id * DIMENSIONS];
TRACE_VAR_SAMPLED ( "civ_weights_ptr" , ( void * )civ_weights, "predict_action" , civ_id)
auto result = predictor . predict_action (civ_weights, verbose);
TRACE_VAR_SAMPLED ( "result.move_x" , result . move_x , "predict_action" , civ_id)
TRACE_VAR_SAMPLED ( "result.move_y" , result . move_y , "predict_action" , civ_id)
TRACE_VAR_SAMPLED ( "result.fire_intensity" , result . fire_intensity , "predict_action" , civ_id)
if constexpr (ULTRA_VERBOSE) {
auto _predict_end = std::chrono::high_resolution_clock:: now ();
auto _predict_ns = std::chrono:: duration_cast <std::chrono:: nanoseconds >(_predict_end - _predict_start). count ();
HISTOGRAM_RECORD (predict, _predict_ns);
}
TRACE_FUNC_EXIT ( "predict_action" )
return result;
}
// STEP 5: BIRTH CIVILIAN (from step5.cpp)
// The moment of birth - distill and output prediction for a civilian
void birth_civilian ( int64_t civ_id ) {
TRACE_FUNC_ENTRY ( "birth_civilian(civ_id=" + std:: to_string (civ_id) + ")" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \"\n [SYSTEM] Distilling Mind for Civilian " << civ_id << "... \" " << std::endl;
}
std::cout << " \n [SYSTEM] Distilling Mind for Civilian " << civ_id << "..." << std::endl;
QuadPredictor::PredictionResult action = predict_action (civ_id, true );
TRACE_VAR ( "action.move_x" , action . move_x , "birth_civilian" )
TRACE_VAR ( "action.move_y" , action . move_y , "birth_civilian" )
TRACE_VAR ( "action.fire_intensity" , action . fire_intensity , "birth_civilian" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" --- PREDICTION OUTPUT --- \" " << std::endl;
std::cout << "[PRINT] PRINTING: \" Move X: " << action . move_x << " \" " << std::endl;
std::cout << "[PRINT] PRINTING: \" Move Y: " << action . move_y << " \" " << std::endl;
std::cout << "[PRINT] PRINTING: \" Fire Rate: " << action . fire_intensity << " \" " << std::endl;
std::cout << "[PRINT] PRINTING: \" ------------------------- \" " << std::endl;
}
std::cout << "--- PREDICTION OUTPUT ---" << std::endl;
std::cout << "Move X: " << action . move_x << std::endl;
std::cout << "Move Y: " << action . move_y << std::endl;
std::cout << "Fire Rate: " << action . fire_intensity << std::endl;
std::cout << "-------------------------" << std::endl;
TRACE_FUNC_EXIT ( "birth_civilian" )
}
// STEP 5: STAGE 5 PREDICTION (from step5.cpp)
// Run prediction across entire population (within active chunk) and collect results
void stage_5_prediction () {
TRACE_FUNC_ENTRY ( "stage_5_prediction()" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [SYSTEM] Starting Stage 5: Prediction for " << ACTIVE_POPULATION << " minds... \" " << std::endl;
}
std::cout << "[SYSTEM] Starting Stage 5: Prediction for " << ACTIVE_POPULATION << " minds..." << std::endl;
TRACE_VAR ( "ACTIVE_POPULATION" , ACTIVE_POPULATION, "stage_5_prediction" )
#pragma omp parallel for num_threads (24) schedule ( dynamic , 1000)
for ( int64_t p = 0 ; p < ACTIVE_POPULATION; ++ p) {
TRACE_LOOP ( "stage5_predict" , p, ACTIVE_POPULATION)
QuadPredictor::PredictionResult action = predict_action (p);
if (p % 100000 == 0 ) {
#pragma omp critical
{
TRACE_VAR ( "civ_" + std:: to_string (p) + ".move_x" , action . move_x , "stage_5_prediction" )
TRACE_VAR ( "civ_" + std:: to_string (p) + ".move_y" , action . move_y , "stage_5_prediction" )
TRACE_VAR ( "civ_" + std:: to_string (p) + ".fire" , action . fire_intensity , "stage_5_prediction" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" Civ " << p << " -> Move(" << action . move_x
<< ", " << action . move_y << ") Fire: " << action . fire_intensity << " \" " << std::endl;
}
std::cout << " Civ " << p << " -> Move(" << action . move_x
<< ", " << action . move_y << ") Fire: " << action . fire_intensity << std::endl;
}
}
}
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [SYSTEM] Stage 5 complete. \" " << std::endl;
}
std::cout << "[SYSTEM] Stage 5 complete." << std::endl;
TRACE_FUNC_EXIT ( "stage_5_prediction" )
}
// STEP 6: PLACE MIND ON CIVILIAN
// Place a processed mind onto a civilian - uses local index (within chunk)
void place_mind_on_civilian ( int64_t local_idx , int64_t global_id , int64_t chunk_num ) {
TRACE_FUNC_ENTRY ( "place_mind_on_civilian(local=" + std:: to_string (local_idx) + ", global=" + std:: to_string (global_id) + ", chunk=" + std:: to_string (chunk_num) + ")" )
bool valid_idx = (local_idx < ACTIVE_POPULATION);
TRACE_BRANCH ( "local_idx < ACTIVE_POPULATION" , valid_idx)
if ( ! valid_idx) {
TRACE_FUNC_EXIT ( "place_mind_on_civilian (invalid idx)" )
return ;
}
Civilian civ (global_id);
TRACE_VAR_SAMPLED ( "civ.civilian_id" , global_id, make_context (chunk_num, global_id, "placement" ), global_id)
// PUSH invulnerability onto civilian (accumulates, doesn't replace)
// This uses the delta calculated during Stage 3 expansion
float delta = invulnerability_deltas [local_idx];
civ . push_invulnerability (delta);
TRACE_VAR_SAMPLED ( "invulnerability_delta" , delta, make_context (chunk_num, global_id, "placement" ), global_id)
TRACE_VAR_SAMPLED ( "civ.existing_invulnerability" , civ . get_invulnerability (), make_context (chunk_num, global_id, "placement" ), global_id)
// Record to global accumulation tracker (thread-safe via atomic/critical in caller)
// Note: We record in critical section below along with push_back
// Collect prediction using local index
float * civ_weights = & mind . weights [local_idx * DIMENSIONS];
civ . prediction = predictor . predict_action (civ_weights);
TRACE_VAR_SAMPLED ( "civ.prediction.move_x" , civ . prediction . move_x , make_context (chunk_num, global_id, "prediction" ), global_id)
TRACE_VAR_SAMPLED ( "civ.prediction.move_y" , civ . prediction . move_y , make_context (chunk_num, global_id, "prediction" ), global_id)
TRACE_VAR_SAMPLED ( "civ.prediction.fire_intensity" , civ . prediction . fire_intensity , make_context (chunk_num, global_id, "prediction" ), global_id)
// Collect banding pressure from stage 2
civ . banding_pressure = mind . banding_pressures [local_idx];
TRACE_VAR_SAMPLED ( "civ.banding_pressure" , civ . banding_pressure , make_context (chunk_num, global_id, "banding" ), global_id)
// Calculate ghost entropy (difference between weights and ghost)
TRACE_VAR_SAMPLED ( "calculating entropy" , "SIMD loop" , make_context (chunk_num, global_id, "entropy" ), global_id)
float entropy_sum = 0.0 f ;
float * weights = & mind . weights [local_idx * DIMENSIONS];
float * ghost = & mind . ghost [local_idx * DIMENSIONS];
for ( size_t j = 0 ; j < DIMENSIONS; j += 8 ) {
__m256 w = _mm256_load_ps ( & weights [j]);
__m256 g = _mm256_load_ps ( & ghost [j]);
__m256 diff = _mm256_sub_ps (w, g);
diff = _mm256_mul_ps (diff, diff);
float temp [ 8 ];
_mm256_storeu_ps (temp, diff);
entropy_sum += temp [ 0 ] + temp [ 1 ] + temp [ 2 ] + temp [ 3 ] + temp [ 4 ] + temp [ 5 ] + temp [ 6 ] + temp [ 7 ];
if (j == 0 ) {
TRACE_OP_SAMPLED ( "ENTROPY_DIFF_SQ" , weights [ 0 ], ghost [ 0 ], temp [ 0 ], make_context (chunk_num, global_id, "entropy" ), global_id)
}
}
float raw_entropy = entropy_sum / DIMENSIONS;
civ . ghost_entropy = std:: sqrt (raw_entropy);
TRACE_OP_SAMPLED ( "ENTROPY_SQRT" , raw_entropy, 0.0 f , civ . ghost_entropy , make_context (chunk_num, global_id, "entropy" ), global_id)
// Calculate expansion level (sum of absolute weights)
TRACE_VAR_SAMPLED ( "calculating expansion" , "SIMD loop" , make_context (chunk_num, global_id, "expansion" ), global_id)
float exp_sum = 0.0 f ;
for ( size_t j = 0 ; j < DIMENSIONS; j += 8 ) {
__m256 w = _mm256_load_ps ( & weights [j]);
__m256 abs_mask = _mm256_castsi256_ps ( _mm256_set1_epi32 ( 0x 7FFFFFFF ));
w = _mm256_and_ps (w, abs_mask);
float temp [ 8 ];
_mm256_storeu_ps (temp, w);
exp_sum += temp [ 0 ] + temp [ 1 ] + temp [ 2 ] + temp [ 3 ] + temp [ 4 ] + temp [ 5 ] + temp [ 6 ] + temp [ 7 ];
if (j == 0 ) {
TRACE_VAR_SAMPLED ( "abs_weight[0]" , temp [ 0 ], make_context (chunk_num, global_id, "expansion" ), global_id)
}
}
civ . expansion_level = exp_sum / DIMENSIONS;
TRACE_OP_SAMPLED ( "EXPANSION_AVG" , exp_sum, ( float )DIMENSIONS, civ . expansion_level , make_context (chunk_num, global_id, "expansion" ), global_id)
TRACE_VAR_SAMPLED ( "civ.expansion_level" , civ . expansion_level , make_context (chunk_num, global_id, "expansion" ), global_id)
// Calculate assumption decay
TRACE_VAR_SAMPLED ( "calculating decay" , "scalar loop" , make_context (chunk_num, global_id, "decay" ), global_id)
float decay_sum = 0.0 f ;
for ( size_t j = 0 ; j < DIMENSIONS; ++ j) {
float diff = std:: abs ( weights [j] - ghost [j]);
decay_sum += diff;
if (j == 0 ) {
TRACE_OP_SAMPLED ( "DECAY_ABS_DIFF" , weights [ 0 ], ghost [ 0 ], diff, make_context (chunk_num, global_id, "decay" ), global_id)
}
}
civ . assumption_decay = decay_sum / DIMENSIONS;
TRACE_OP_SAMPLED ( "DECAY_AVG" , decay_sum, ( float )DIMENSIONS, civ . assumption_decay , make_context (chunk_num, global_id, "decay" ), global_id)
TRACE_VAR_SAMPLED ( "civ.assumption_decay" , civ . assumption_decay , make_context (chunk_num, global_id, "assuming" ), global_id)
TRACE_VAR_SAMPLED ( "civ.ghost_entropy" , civ . ghost_entropy , make_context (chunk_num, global_id, "ghost" ), global_id)
civ . processing_chunk = chunk_num;
civ . mind_placed = true ;
TRACE_VAR_SAMPLED ( "civ.processing_chunk" , chunk_num, make_context (chunk_num, global_id, "finalize" ), global_id)
TRACE_VAR_SAMPLED ( "civ.mind_placed" , true , make_context (chunk_num, global_id, "finalize" ), global_id)
// Full civilian trace for sampled civilians
TRACE_CIVILIAN (global_id, "COMPLETE" , "mind_placed=true, invuln=" + std:: to_string ( civ . get_invulnerability ()))
#pragma omp critical
{
// Record invulnerability delta to global tracker
TRACE_VAR_SAMPLED ( "recording delta to tracker" , delta, make_context (chunk_num, global_id, "tracker" ), global_id)
invuln_tracker . record_delta (delta, static_cast< int > (chunk_num - 1 ));
civilians . push_back (std:: move (civ));
TRACE_VAR_SAMPLED ( "civilians.size()" , civilians . size (), make_context (chunk_num, global_id, "push" ), global_id)
}
TRACE_FUNC_EXIT ( "place_mind_on_civilian" )
}
// Process a chunk of minds (100k at a time) - uses local indices
void process_chunk ( int64_t chunk_num ) {
TRACE_FUNC_ENTRY ( "process_chunk(chunk_num=" + std:: to_string (chunk_num) + ")" )
int64_t global_start = (chunk_num - 1 ) * CHUNK_SIZE;
TRACE_VAR ( "global_start" , global_start, "process_chunk" )
TRACE_OP ( "GLOBAL_START" , ( float )(chunk_num - 1 ), ( float )CHUNK_SIZE, ( float )global_start, "process_chunk" )
// Clear civilian buffer from previous chunk to prevent memory accumulation
civilians . clear ();
TRACE_VAR ( "civilians.cleared" , true , "process_chunk" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \"\n [CHUNK " << chunk_num << "] Processing " << ACTIVE_POPULATION << " minds... \" " << std::endl;
}
std::cout << " \n [CHUNK " << chunk_num << "] Processing " << ACTIVE_POPULATION << " minds (global IDs " << global_start << " to " << (global_start + ACTIVE_POPULATION - 1 ) << ")" << std::endl;
// First initialize this chunk's minds
TRACE_VAR ( "calling" , "initialize_chunk" , "process_chunk" )
auto stage1_start = std::chrono::high_resolution_clock:: now ();
initialize_chunk (chunk_num);
auto stage1_end = std::chrono::high_resolution_clock:: now ();
g_stats . stage1_time_sec += std::chrono:: duration < double >(stage1_end - stage1_start). count ();
g_stats . stage1_init_ops += ACTIVE_POPULATION;
std::cout << " [STAGE 1/5] Init complete: " << ACTIVE_POPULATION << " minds initialized" << std::endl;
// Stage 2: Banding update for this chunk
auto stage2_start = std::chrono::high_resolution_clock:: now ();
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [CHUNK " << chunk_num << "] Applying banding update... \" " << std::endl;
}
TRACE_STAGE ( "BANDING" , chunk_num, "hex_influence=0.75, factor=0.001" )
constexpr float HEX_INFLUENCE = 0.75 f ;
constexpr float BANDING_FACTOR = 0.001 f ;
constexpr float BANDING_DELTA = HEX_INFLUENCE * BANDING_FACTOR;
TRACE_VAR ( "HEX_INFLUENCE" , HEX_INFLUENCE, "process_chunk banding" )
TRACE_VAR ( "BANDING_FACTOR" , BANDING_FACTOR, "process_chunk banding" )
TRACE_OP ( "BANDING_DELTA" , HEX_INFLUENCE, BANDING_FACTOR, BANDING_DELTA, "process_chunk banding" )
int64_t band_ops = 0 ;
#pragma omp parallel for num_threads (24) reduction (+: band_ops )
for ( int64_t i = 0 ; i < ACTIVE_POPULATION; ++ i) {
for ( size_t j = 0 ; j < DIMENSIONS; ++ j) {
float old_weight = mind . weights [i * DIMENSIONS + j];
float new_weight = old_weight + BANDING_DELTA;
mind . weights [i * DIMENSIONS + j] = new_weight;
band_ops ++ ;
// Trace first weight of sampled civilians
if (j == 0 ) {
TRACE_OP_SAMPLED ( "BAND_ADD" , old_weight, BANDING_DELTA, new_weight,
make_context (chunk_num, i, "banding" ), i)
}
}
}
auto stage2_end = std::chrono::high_resolution_clock:: now ();
g_stats . stage2_time_sec += std::chrono:: duration < double >(stage2_end - stage2_start). count ();
g_stats . stage2_band_ops += band_ops;
g_stats . total_weights_modified += band_ops;
std::cout << " [STAGE 2/5] Banding complete: " << band_ops << " weight modifications" << std::endl;
// Stage 3: Expanding for this chunk - PUSH onto existing invulnerability
auto stage3_start = std::chrono::high_resolution_clock:: now ();
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [CHUNK " << chunk_num << "] Expanding INVULNERABILITY context (accumulative)... \" " << std::endl;
}
std::cout << " [CHUNK " << chunk_num << "] Expanding INVULNERABILITY context (accumulative)..." << std::endl;
TRACE_STAGE ( "EXPANSION" , chunk_num, "concept=INVULNERABILITY, intensity=0.85" )
int64_t expand_ops = 0 ;
int64_t simd_vectors = 0 ;
bool has_invuln = ( expander . expansion_library . find ( "INVULNERABILITY" ) != expander . expansion_library . end ());
TRACE_BRANCH ( "INVULNERABILITY in library" , has_invuln)
if (has_invuln) {
const float * influence = expander . expansion_library [ "INVULNERABILITY" ]. data ();
TRACE_VAR ( "influence_ptr" , ( void * )influence, "process_chunk expansion" )
constexpr float EXPANSION_INTENSITY = 0.85 f ;
TRACE_VAR ( "EXPANSION_INTENSITY" , EXPANSION_INTENSITY, "process_chunk expansion" )
// Calculate invulnerability delta for each person AND apply to weights
#pragma omp parallel for num_threads (24) reduction (+: expand_ops , simd_vectors )
for ( int64_t p = 0 ; p < ACTIVE_POPULATION; ++ p) {
float * person_weights = & mind . weights [p * DIMENSIONS];
float delta_sum = 0.0 f ;
// Trace first iteration for sampled civilians
if constexpr (ULTRA_VERBOSE) {
if (p % VERBOSE_SAMPLE_RATE == 0 ) {
float first_weight = person_weights [ 0 ];
float first_influence = influence [ 0 ];
float first_contribution = first_influence * EXPANSION_INTENSITY;
float first_result = first_weight + first_contribution;
std::cout << "[OP] EXPAND: weight[0]=" << first_weight
<< " + (influence[0]=" << first_influence << " * intensity=" << EXPANSION_INTENSITY << ")"
<< " = " << first_result << " @ " << make_context (chunk_num, p, "expansion" ) << std::endl;
}
}
for ( size_t i = 0 ; i < DIMENSIONS; i += 8 ) {
__m256 weight_vec = _mm256_load_ps ( & person_weights [i]);
__m256 influence_vec = _mm256_load_ps ( & influence [i]);
__m256 intensity_vec = _mm256_set1_ps (EXPANSION_INTENSITY);
__m256 contribution = _mm256_mul_ps (influence_vec, intensity_vec);
__m256 result = _mm256_add_ps (weight_vec, contribution);
_mm256_store_ps ( & person_weights [i], result);
simd_vectors ++ ;
expand_ops += 8 ; // 8 floats per SIMD vector
// Sum the contribution for invulnerability delta
float temp [ 8 ];
_mm256_storeu_ps (temp, contribution);
delta_sum += temp [ 0 ] + temp [ 1 ] + temp [ 2 ] + temp [ 3 ] + temp [ 4 ] + temp [ 5 ] + temp [ 6 ] + temp [ 7 ];
}
// Store the invulnerability delta for this person (to be pushed during placement)
float avg_delta = delta_sum / DIMENSIONS;
invulnerability_deltas [p] = avg_delta;
TRACE_VAR_SAMPLED ( "invulnerability_delta_raw" , delta_sum, make_context (chunk_num, p, "expansion" ), p)
TRACE_OP_SAMPLED ( "DELTA_AVG" , delta_sum, ( float )DIMENSIONS, avg_delta, make_context (chunk_num, p, "expansion" ), p)
}
}
auto stage3_end = std::chrono::high_resolution_clock:: now ();
g_stats . stage3_time_sec += std::chrono:: duration < double >(stage3_end - stage3_start). count ();
g_stats . stage3_expand_ops += expand_ops;
g_stats . total_simd_vectors += simd_vectors;
g_stats . total_floats_processed += expand_ops;
std::cout << " [STAGE 3/5] Expansion complete: " << expand_ops << " floats expanded, "
<< simd_vectors << " SIMD vectors" << std::endl;
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [CHUNK " << chunk_num << "] Expansion complete (invulnerability deltas calculated). \" " << std::endl;
}
std::cout << " [CHUNK " << chunk_num << "] Expansion complete (invulnerability deltas calculated)." << std::endl;
// Stage 4: Assuming for this chunk
auto stage4_start = std::chrono::high_resolution_clock:: now ();
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [CHUNK " << chunk_num << "] Running assumption pass... \" " << std::endl;
}
std::cout << " [CHUNK " << chunk_num << "] Running assumption pass..." << std::endl;
TRACE_STAGE ( "ASSUMING" , chunk_num, "decay_factor=0.999, copying weights to ghost then decaying" )
constexpr float ENTROPY_DECAY_FACTOR = 0.999 f ;
TRACE_VAR ( "ENTROPY_DECAY_FACTOR" , ENTROPY_DECAY_FACTOR, "process_chunk assuming" )
int64_t assume_ops = 0 ;
int64_t ghost_copies = 0 ;
// Phase 1: Copy current weights to ghost (snapshot for future assumptions)
#pragma omp parallel for num_threads (24) reduction (+: ghost_copies )
for ( int64_t p = 0 ; p < ACTIVE_POPULATION; ++ p) {
float * weights = & mind . weights [p * DIMENSIONS];
float * ghost = & mind . ghost [p * DIMENSIONS];
std:: memcpy (ghost, weights, DIMENSIONS * sizeof ( float ));
ghost_copies += DIMENSIONS;
TRACE_VAR_SAMPLED ( "ghost_snapshot[0]" , ghost [ 0 ], make_context (chunk_num, p, "assume-copy" ), p)
}
// Phase 2: Apply entropy decay to weights
#pragma omp parallel for num_threads (24) reduction (+: assume_ops )
for ( int64_t p = 0 ; p < ACTIVE_POPULATION; ++ p) {
float * weights = & mind . weights [p * DIMENSIONS];
float * ghost = & mind . ghost [p * DIMENSIONS];
for ( size_t i = 0 ; i < DIMENSIONS; ++ i) {
float old_weight = weights [i];
float ghost_val = ghost [i];
float new_weight = ghost_val * ENTROPY_DECAY_FACTOR;
weights [i] = new_weight;
assume_ops ++ ;
// Trace first weight of sampled civilians
if (i == 0 ) {
TRACE_OP_SAMPLED ( "DECAY_MUL" , ghost_val, ENTROPY_DECAY_FACTOR, new_weight,
make_context (chunk_num, p, "assume-decay" ), p)
}
}
}
auto stage4_end = std::chrono::high_resolution_clock:: now ();
g_stats . stage4_time_sec += std::chrono:: duration < double >(stage4_end - stage4_start). count ();
g_stats . stage4_assume_ops += assume_ops;
g_stats . total_floats_processed += ghost_copies + assume_ops;
std::cout << " [STAGE 4/5] Assuming complete: " << ghost_copies << " ghost copies, "
<< assume_ops << " decay ops" << std::endl;
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [CHUNK " << chunk_num << "] Assumption pass complete. \" " << std::endl;
}
std::cout << " [CHUNK " << chunk_num << "] Assumption pass complete." << std::endl;
// Stage 5: Prediction and mind placement for this chunk
auto stage5_start = std::chrono::high_resolution_clock:: now ();
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [CHUNK " << chunk_num << "] Running predictions and placing minds on civilians... \" " << std::endl;
}
std::cout << " [CHUNK " << chunk_num << "] Running predictions and placing minds on civilians..." << std::endl;
TRACE_STAGE ( "PREDICTION" , chunk_num, "stages=50, placing minds on civilians" )
int64_t placed_count = 0 ;
TRACE_VAR ( "placed_count" , placed_count, "process_chunk prediction" )
#pragma omp parallel for num_threads (24) schedule ( dynamic , 1000)
for ( int64_t p = 0 ; p < ACTIVE_POPULATION; ++ p) {
int64_t global_id = global_start + p;
place_mind_on_civilian (p, global_id, chunk_num);
#pragma omp atomic
placed_count ++ ;
if (placed_count % 100000 == 0 ) {
#pragma omp critical
{
std::cout << " [CHUNK " << chunk_num << "] Placed minds on " << placed_count << "/" << ACTIVE_POPULATION << " civilians..." << std::endl;
}
}
}
auto stage5_end = std::chrono::high_resolution_clock:: now ();
g_stats . stage5_time_sec += std::chrono:: duration < double >(stage5_end - stage5_start). count ();
g_stats . stage5_predict_ops += placed_count * STAGES; // Each placement runs STAGES predictions
g_stats . total_minds_processed += placed_count;
std::cout << " [STAGE 5/5] Prediction complete: " << placed_count << " minds placed, "
<< (placed_count * STAGES) << " prediction stages executed" << std::endl;
// Finalize invulnerability tracking for this chunk
TRACE_VAR ( "finalizing chunk" , chunk_num - 1 , "process_chunk" )
invuln_tracker . finalize_chunk ( static_cast< int > (chunk_num - 1 ));
// Update global chunk completion stats
g_stats . total_chunks_completed ++ ;
TRACE_VAR ( "civilians.size()" , civilians . size (), "process_chunk complete" )
TRACE_VAR ( "chunk_avg_invuln" , invuln_tracker . chunk_averages [chunk_num - 1 ], "process_chunk complete" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [CHUNK " << chunk_num << "] Complete. Chunk civilians: " << civilians . size () << " \" " << std::endl;
}
std::cout << " [CHUNK " << chunk_num << "] Complete. Chunk civilians: " << civilians . size () << " (total processed: " << g_stats . total_minds_processed << ")" << std::endl;
std::cout << " [CHUNK " << chunk_num << "] Invulnerability avg for chunk: " << invuln_tracker . chunk_averages [chunk_num - 1 ] << std::endl;
// Print running statistics after each chunk
std::cout << " \n [STATS] Running Totals After Chunk " << chunk_num << ":" << std::endl;
std::cout << " Total minds processed: " << g_stats . total_minds_processed << std::endl;
std::cout << " Total chunks completed: " << g_stats . total_chunks_completed << std::endl;
std::cout << " Total weights modified: " << g_stats . total_weights_modified << std::endl;
std::cout << " Total SIMD vectors: " << g_stats . total_simd_vectors << std::endl;
std::cout << " Total floats processed: " << g_stats . total_floats_processed << std::endl;
std::cout << " Stage timings (s): Init=" << g_stats . stage1_time_sec
<< ", Band=" << g_stats . stage2_time_sec
<< ", Expand=" << g_stats . stage3_time_sec
<< ", Assume=" << g_stats . stage4_time_sec
<< ", Predict=" << g_stats . stage5_time_sec << std::endl;
std::cout << std::endl;
TRACE_FUNC_EXIT ( "process_chunk" )
}
// Get civilian by index
Civilian & get_civilian ( int64_t index ) {
TRACE_FUNC_ENTRY ( "get_civilian(index=" + std:: to_string (index) + ")" )
TRACE_VAR ( "returning civilian_id" , civilians [index]. civilian_id , "get_civilian" )
TRACE_FUNC_EXIT ( "get_civilian" )
return civilians [index];
}
// Get total civilians
int64_t get_civilian_count () const {
int64_t count = static_cast< int64_t > ( civilians . size ());
TRACE_VAR ( "get_civilian_count() returning" , count, "JamesDeanQuad" )
return count;
}
// Get entity count
int64_t get_entity_count () const {
int64_t count = static_cast< int64_t > ( entity_population . size ());
TRACE_VAR ( "get_entity_count() returning" , count, "JamesDeanQuad" )
return count;
}
// Access an entity
EntityPerson & get_entity ( int64_t index ) {
TRACE_FUNC_ENTRY ( "get_entity(index=" + std:: to_string (index) + ")" )
TRACE_VAR ( "returning entity civil_id" , entity_population [index]. civil_id , "get_entity" )
TRACE_FUNC_EXIT ( "get_entity" )
return entity_population [index];
}
};
// Print help/usage information
void print_help ( const char * program_name ) {
std::cout << R"(
================================================================================
JAMES DEAN QUAD ENGINE v2.0 - HELP
================================================================================
USAGE:
)" << program_name << R"( [OPTIONS]
OPTIONS:
--help, -h Show this help message and exit
--version, -v Show version information
================================================================================
COMPILE-TIME FLAGS
================================================================================
VERBOSITY CONTROL:
-DULTRA_VERBOSE=true Enable ultra-verbose tracing mode
(Default: false)
-DVERBOSE_SAMPLE_RATE=N Print every Nth operation (Default: 100000)
Lower = more output, Higher = less output
-DTRACE_SPECIFIC_CIVILIAN=N Trace only civilian N (-1 = use sample rate)
(Default: -1)
TRACE LEVEL CONTROL:
-DTRACE_LEVEL=0 DEBUG - All messages (most verbose)
-DTRACE_LEVEL=1 INFO - Info and above
-DTRACE_LEVEL=2 WARN - Warnings and above
-DTRACE_LEVEL=3 ERROR - Errors and fatal only
-DTRACE_LEVEL=4 FATAL - Fatal only
-DTRACE_LEVEL=5 SILENT - No trace output
STAGE-SPECIFIC TRACING:
-DTRACE_INIT=true/false Trace initialization stage
-DTRACE_BANDING=true/false Trace banding stage
-DTRACE_EXPANSION=true/false Trace expansion stage
-DTRACE_ASSUMING=true/false Trace assuming stage
-DTRACE_PREDICTION=true/false Trace prediction stage
-DTRACE_PLACEMENT=true/false Trace placement stage
OUTPUT FILES:
-DLOG_FILENAME="file.log" Set log file name (Default: james_dean_trace.log)
-DJSON_OUTPUT=true Enable JSON output (Default: false)
-DJSON_FILENAME="file.json" Set JSON file name (Default: james_dean_trace.json)
DISTRIBUTED/IPC:
-DENABLE_IPC=true Enable IPC/network tracing (Default: false)
-DNODE_ID=N Set node ID for distributed runs (Default: 0)
-DTOTAL_NODES=N Set total nodes count (Default: 1)
================================================================================
EXAMPLE COMPILATIONS
================================================================================
Normal mode (minimal output):
clang++ -std=c++17 -O3 -march=native -fopenmp -rdynamic -o james_dean main.cpp
Ultra-verbose mode:
clang++ -DULTRA_VERBOSE=true -std=c++17 -O3 -march=native -fopenmp -rdynamic -o james_dean main.cpp
Verbose with JSON output:
clang++ -DULTRA_VERBOSE=true -DJSON_OUTPUT=true -std=c++17 -O3 -march=native -fopenmp -rdynamic -o james_dean main.cpp
Trace only errors:
clang++ -DULTRA_VERBOSE=true -DTRACE_LEVEL=3 -std=c++17 -O3 -march=native -fopenmp -rdynamic -o james_dean main.cpp
Trace specific civilian (e.g., civilian 42):
clang++ -DULTRA_VERBOSE=true -DTRACE_SPECIFIC_CIVILIAN=42 -std=c++17 -O3 -march=native -fopenmp -rdynamic -o james_dean main.cpp
High detail sampling (every 1000th op):
clang++ -DULTRA_VERBOSE=true -DVERBOSE_SAMPLE_RATE=1000 -std=c++17 -O3 -march=native -fopenmp -rdynamic -o james_dean main.cpp
================================================================================
RUNTIME FEATURES
================================================================================
Step 10 Features (when ULTRA_VERBOSE=true):
- Trace Level System: Color-coded DEBUG/INFO/WARN/ERROR/FATAL messages
- Function Profiling: Automatic call count and timing statistics
- Correlation IDs: Unique IDs for distributed tracing
- Watchdog Monitor: Detects hung threads (30s timeout)
- Timing Histograms: Distribution analysis of function timings
Output Files Generated (when ULTRA_VERBOSE=true):
- james_dean_trace.log: Full trace log
- james_dean_trace.json: JSON-formatted events (if JSON_OUTPUT=true)
================================================================================
)" << std::endl;
}
void print_version () {
std::cout << "James Dean Quad Engine v2.0" << std::endl;
std::cout << "Population Type: int64_t (signed long long)" << std::endl;
std::cout << "Max Population: " << INT64_MAX << std::endl;
std::cout << "SIMD: AVX2 (256-bit)" << std::endl;
std::cout << "Threading: OpenMP (" << omp_get_max_threads () << " threads)" << std::endl;
std::cout << "Compiled: " << __DATE__ << " " << __TIME__ << std::endl;
}
int main ( int argc , char * argv []) {
// Parse command line arguments
for ( int i = 1 ; i < argc; ++ i) {
std::string arg = argv [i];
if (arg == "--help" || arg == "-h" ) {
print_help ( argv [ 0 ]);
return 0 ;
}
if (arg == "--version" || arg == "-v" ) {
print_version ();
return 0 ;
}
}
// Initialize log file for dual output (console + file)
init_log_file ();
TRACE_FUNC_ENTRY ( "main()" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" ======================================== \" " << std::endl;
std::cout << "[PRINT] PRINTING: \" JAMES DEAN QUAD ENGINE v2.0 \" " << std::endl;
std::cout << "[PRINT] PRINTING: \" ======================================== \" " << std::endl;
}
std::cout << "========================================" << std::endl;
std::cout << " JAMES DEAN QUAD ENGINE v2.0" << std::endl;
std::cout << "========================================" << std::endl;
// Print verbosity configuration
TRACE_VAR ( "calling" , "print_verbosity_config()" , "main" )
print_verbosity_config ();
TRACE_VAR ( "DIMENSIONS" , DIMENSIONS, "main" )
TRACE_VAR ( "TOTAL_POPULATION" , TOTAL_POPULATION, "main" )
TRACE_VAR ( "CHUNK_SIZE" , CHUNK_SIZE, "main" )
TRACE_VAR ( "NUM_CHUNKS" , NUM_CHUNKS, "main" )
TRACE_VAR ( "ENTITY_NODES" , ENTITY_NODES, "main" )
TRACE_VAR ( "STAGES" , STAGES, "main" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" Dimensions per mind: " << DIMENSIONS << " \" " << std::endl;
std::cout << "[PRINT] PRINTING: \" Total Population: " << TOTAL_POPULATION << " \" " << std::endl;
}
std::cout << "Dimensions per mind: " << DIMENSIONS << " (8k floats, 32 KB per mind)" << std::endl;
std::cout << "Total Population: " << TOTAL_POPULATION << " (INT64_MAX = 9.22 quintillion minds)" << std::endl;
std::cout << "Population Type: int64_t (signed long long)" << std::endl;
std::cout << "Chunk Size: " << CHUNK_SIZE << " (100k civilians per chunk)" << std::endl;
std::cout << "Active Population: " << ACTIVE_POPULATION << " (100k in RAM at once, ~6.1 GB)" << std::endl;
std::cout << "Total Chunks: " << NUM_CHUNKS << " (~92 trillion chunks for full population)" << std::endl;
std::cout << "Entity Nodes: " << ENTITY_NODES << " (175k source personalities)" << std::endl;
std::cout << "Stages: " << STAGES << " (prediction pipeline stages)" << std::endl;
std::cout << "========================================" << std::endl << std::endl;
TRACE_VAR ( "creating" , "JamesDeanQuad engine" , "main" )
JamesDeanQuad engine;
TRACE_MEMORY ( "After JamesDeanQuad creation" )
// Reserve space for all civilians upfront
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [INIT] Reserving space for " << TOTAL_POPULATION << " civilians... \" " << std::endl;
}
std::cout << "[INIT] Reserving space for " << TOTAL_POPULATION << " civilians..." << std::endl;
// Phase 1: Initialize EntityPerson nodes (175k nodes)
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \"\n [PHASE 1] Initializing EntityPerson nodes... \" " << std::endl;
}
std::cout << " \n [PHASE 1] Initializing EntityPerson nodes..." << std::endl;
TRACE_VAR ( "calling" , "engine.initialize_entities()" , "main phase 1" )
engine . initialize_entities (ENTITY_NODES);
TRACE_MEMORY ( "After EntityPerson initialization" )
// Phase 2-5: Process in 500k chunks (each chunk gets initialized, banded, expanded, assumed, predicted)
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \"\n [PHASE 2-5] Processing " << TOTAL_POPULATION << " minds in " << NUM_CHUNKS << " chunks... \" " << std::endl;
}
std::cout << " \n [PHASE 2-5] Processing " << TOTAL_POPULATION << " minds in " << NUM_CHUNKS << " chunks of " << CHUNK_SIZE << "..." << std::endl;
// Start progress bar for chunk processing
PROGRESS_START ( "Chunks" , NUM_CHUNKS);
TRACE_CPU ( "Before chunk processing" )
for ( int64_t chunk = 1 ; chunk <= NUM_CHUNKS; ++ chunk) {
TRACE_LOOP ( "main_chunk_loop" , chunk, NUM_CHUNKS)
TRACE_VAR ( "processing chunk" , chunk, "main" )
TRACE_MEMORY ( "Before chunk " + std:: to_string (chunk))
WATCHDOG_HEARTBEAT ( "chunk_" + std:: to_string (chunk));
TRACE_INFO ( "Processing chunk " + std:: to_string (chunk) + "/" + std:: to_string (NUM_CHUNKS));
engine . process_chunk (chunk);
TRACE_MEMORY ( "After chunk " + std:: to_string (chunk))
TRACE_CPU ( "After chunk " + std:: to_string (chunk))
PROGRESS_UPDATE (chunk);
}
PROGRESS_FINISH ();
TRACE_CPU ( "After all chunk processing" )
// Final summary
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \"\n ======================================== \" " << std::endl;
std::cout << "[PRINT] PRINTING: \" PROCESSING COMPLETE \" " << std::endl;
std::cout << "[PRINT] PRINTING: \" ======================================== \" " << std::endl;
}
std::cout << " \n ========================================" << std::endl;
std::cout << " PROCESSING COMPLETE" << std::endl;
std::cout << "========================================" << std::endl;
TRACE_VAR ( "TOTAL_POPULATION" , TOTAL_POPULATION, "main summary" )
TRACE_VAR ( "engine.get_civilian_count()" , engine . get_civilian_count (), "main summary" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" Total minds processed: " << TOTAL_POPULATION << " \" " << std::endl;
std::cout << "[PRINT] PRINTING: \" Total civilians with minds: " << engine . get_civilian_count () << " \" " << std::endl;
}
std::cout << "Total minds processed: " << TOTAL_POPULATION << std::endl;
std::cout << "Total civilians with minds: " << engine . get_civilian_count () << std::endl;
TRACE_MEMORY ( "After all processing complete" )
// Show sample civilian stats
bool has_civilians = ( engine . get_civilian_count () > 0 );
TRACE_BRANCH ( "engine.get_civilian_count() > 0" , has_civilians)
if (has_civilians) {
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \"\n --- SAMPLE CIVILIAN STATS --- \" " << std::endl;
}
std::cout << " \n --- SAMPLE CIVILIAN STATS ---" << std::endl;
// First civilian
TRACE_VAR ( "getting civilian" , 0 , "main sample stats" )
Civilian & first = engine . get_civilian ( 0 );
TRACE_VAR ( "first.civilian_id" , first . civilian_id , "main sample" )
TRACE_VAR ( "first.mind_placed" , first . mind_placed , "main sample" )
TRACE_VAR ( "first.processing_chunk" , first . processing_chunk , "main sample" )
TRACE_VAR ( "first.prediction.move_x" , first . prediction . move_x , "main sample" )
TRACE_VAR ( "first.prediction.move_y" , first . prediction . move_y , "main sample" )
TRACE_VAR ( "first.prediction.fire_intensity" , first . prediction . fire_intensity , "main sample" )
TRACE_VAR ( "first.prediction.confidence" , first . prediction . confidence , "main sample" )
TRACE_VAR ( "first.banding_pressure" , first . banding_pressure , "main sample" )
TRACE_VAR ( "first.ghost_entropy" , first . ghost_entropy , "main sample" )
TRACE_VAR ( "first.expansion_level" , first . expansion_level , "main sample" )
TRACE_VAR ( "first.assumption_decay" , first . assumption_decay , "main sample" )
TRACE_VAR ( "first.get_invulnerability()" , first . get_invulnerability (), "main sample" )
TRACE_VAR ( "first.get_invulnerability_trend()" , first . get_invulnerability_trend (), "main sample" )
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \" [Civilian 0] \" " << std::endl;
}
std::cout << "[Civilian 0]" << std::endl;
std::cout << " Mind Placed: " << ( first . mind_placed ? "YES" : "NO" ) << std::endl;
std::cout << " Chunk: " << first . processing_chunk << std::endl;
std::cout << " Move: (" << first . prediction . move_x << ", " << first . prediction . move_y << ")" << std::endl;
std::cout << " Fire: " << first . prediction . fire_intensity << std::endl;
std::cout << " Confidence: " << first . prediction . confidence << std::endl;
std::cout << " Complexity: " << first . prediction . complexity << std::endl;
std::cout << " Banding: " << first . banding_pressure << std::endl;
std::cout << " Entropy: " << first . ghost_entropy << std::endl;
std::cout << " Expansion: " << first . expansion_level << std::endl;
std::cout << " Decay: " << first . assumption_decay << std::endl;
std::cout << " Invulnerability: " << first . get_invulnerability () << std::endl;
std::cout << " Invuln Trend: " << first . get_invulnerability_trend () << std::endl;
// Middle civilian (find one from middle chunk)
int64_t mid_idx = engine . get_civilian_count () / 2 ;
if (mid_idx > 0 ) {
Civilian & mid = engine . get_civilian (mid_idx);
std::cout << " \n [Civilian " << mid . civilian_id << "]" << std::endl;
std::cout << " Mind Placed: " << ( mid . mind_placed ? "YES" : "NO" ) << std::endl;
std::cout << " Chunk: " << mid . processing_chunk << std::endl;
std::cout << " Move: (" << mid . prediction . move_x << ", " << mid . prediction . move_y << ")" << std::endl;
std::cout << " Fire: " << mid . prediction . fire_intensity << std::endl;
std::cout << " Confidence: " << mid . prediction . confidence << std::endl;
std::cout << " Invulnerability: " << mid . get_invulnerability () << std::endl;
}
// Last civilian
Civilian & last = engine . get_civilian ( engine . get_civilian_count () - 1 );
std::cout << " \n [Civilian " << last . civilian_id << "]" << std::endl;
std::cout << " Mind Placed: " << ( last . mind_placed ? "YES" : "NO" ) << std::endl;
std::cout << " Chunk: " << last . processing_chunk << std::endl;
std::cout << " Move: (" << last . prediction . move_x << ", " << last . prediction . move_y << ")" << std::endl;
std::cout << " Fire: " << last . prediction . fire_intensity << std::endl;
std::cout << " Confidence: " << last . prediction . confidence << std::endl;
std::cout << " Invulnerability: " << last . get_invulnerability () << std::endl;
std::cout << "-----------------------------" << std::endl;
}
// Print invulnerability accumulation summary
TRACE_VAR ( "calling" , "engine.get_invuln_tracker().print_summary()" , "main" )
engine . get_invuln_tracker (). print_summary ();
// Print final global statistics summary
std::cout << " \n " ;
g_stats . print_summary ();
if constexpr (ULTRA_VERBOSE) {
std::cout << "[PRINT] PRINTING: \"\n [SYSTEM] James Dean Quad Engine complete. \" " << std::endl;
}
std::cout << " \n [SYSTEM] James Dean Quad Engine complete." << std::endl;
TRACE_VAR ( "return" , 0 , "main" )
TRACE_FUNC_EXIT ( "main()" )
// Close log file
close_log_file ();
return 0 ;
}