Project Name: Binary Coded Layered Automata Engine (BCLAE) Language: Rust Target: High-performance, scalable cellular automata simulation License: MIT/Apache 2.0 dual license
1
2
3
4
5
6
7
8
9
10
11
12
13
┌─────────────────────────────────────────────────────────────┐
│ Application Layer │
├─────────────────────────────────────────────────────────────┤
│ CLI Interface │ Web Interface │ Native GUI │ API │
├─────────────────────────────────────────────────────────────┤
│ Simulation Engine │
├─────────────────────────────────────────────────────────────┤
│ Ant System │ Life System │ Substrate │ Renderer │
├─────────────────────────────────────────────────────────────┤
│ Compute Backend (CPU/GPU) │
├─────────────────────────────────────────────────────────────┤
│ Memory Manager │
└─────────────────────────────────────────────────────────────┘
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
bclae/
├── src/
│ ├── lib.rs // Public API
│ ├── engine/
│ │ ├── mod.rs
│ │ ├── simulation.rs // Main simulation loop
│ │ ├── config.rs // Configuration management
│ │ └── stats.rs // Performance metrics
│ ├── grid/
│ │ ├── mod.rs
│ │ ├── substrate.rs // Substrate grid management
│ │ ├── life.rs // Life grid management
│ │ ├── marked.rs // Marked cells tracking
│ │ └── inhibition.rs // Inhibition system
│ ├── ant/
│ │ ├── mod.rs
│ │ ├── ant.rs // Individual ant logic
│ │ ├── swarm.rs // Multi-ant coordination
│ │ └── rules.rs // Rule management
│ ├── compute/
│ │ ├── mod.rs
│ │ ├── cpu.rs // CPU-based computation
│ │ ├── gpu.rs // GPU acceleration (WGPU)
│ │ └── parallel.rs // Parallel processing
│ ├── render/
│ │ ├── mod.rs
│ │ ├── canvas.rs // 2D rendering
│ │ ├── colors.rs // Color management
│ │ └── export.rs // Image/video export
│ ├── io/
│ │ ├── mod.rs
│ │ ├── save_load.rs // State serialization
│ │ └── formats.rs // File format support
│ └── utils/
│ ├── mod.rs
│ ├── math.rs // Mathematical utilities
│ └── memory.rs // Memory optimization
├── benches/ // Performance benchmarks
├── examples/ // Usage examples
└── tests/ // Integration tests
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Optimized for cache efficiency and SIMD operations
pub struct SubstrateGrid {
data: Vec<u8>, // Packed color values (0-7)
width: usize,
height: usize,
stride: usize, // Aligned for SIMD
}
pub struct LifeGrid {
data: BitVec, // Bit-packed for memory efficiency
width: usize,
height: usize,
}
pub struct MarkedGrid {
data: BitVec, // Bit-packed boolean grid
width: usize,
height: usize,
}
pub struct InhibitionGrid {
data: Vec<u8>, // 0-255 inhibition strength
width: usize,
height: usize,
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#[derive(Clone, Debug)]
pub struct Ant {
pub id: u32,
pub position: Point2D<u32>,
pub direction: Direction,
pub steps: u64,
pub color: Color,
pub rule: BinaryRule,
pub activation_mask: BinaryMask,
pub activation_mode: BinaryMode,
}
#[derive(Clone, Debug)]
pub struct AntSwarm {
ants: Vec<Ant>,
spawn_mode: SpawnMode,
sync_mode: SyncMode,
spatial_index: SpatialHash, // For efficient collision detection
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SimulationConfig {
// Grid parameters
pub grid_size: (usize, usize),
pub num_colors: u8,
pub color_palette: Vec<Color>,
// Ant parameters
pub num_ants: u32,
pub ant_spawn_mode: SpawnMode,
pub ant_sync: SyncMode,
pub ant_rule: Vec<bool>,
// Life parameters
pub life_radius: u8,
pub birth_rule: u8,
pub survival_min: u8,
pub survival_max: u8,
// Activation parameters
pub activation_mask: Vec<bool>,
pub activation_mode: Vec<bool>,
pub ant_activation_radius: u8,
pub activation_probability: f32,
// Performance parameters
pub use_gpu: bool,
pub num_threads: Option<usize>,
pub chunk_size: usize,
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Structure of Arrays (SoA) for better cache performance
pub struct AntComponents {
positions: Vec<Point2D<u32>>,
directions: Vec<Direction>,
steps: Vec<u64>,
rules: Vec<BinaryRule>,
// ... other components
}
// Memory-aligned grid data
#[repr(align(64))] // Cache line alignment
pub struct AlignedGrid<T> {
data: Vec<T>,
width: usize,
height: usize,
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use std::simd::*;
impl SubstrateGrid {
// Process 32 cells at once using AVX2
pub fn update_colors_simd(&mut self, positions: &[Point2D<u32>]) {
// SIMD implementation for bulk color updates
}
}
impl LifeGrid {
// Vectorized neighbor counting
pub fn count_neighbors_simd(&self, chunk: &GridChunk) -> Vec<u8> {
// SIMD implementation for neighbor counting
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use rayon::prelude::*;
impl Simulation {
pub fn step_parallel(&mut self) {
// Phase 1: Move ants (can be parallelized by ant)
self.ants.par_iter_mut().for_each(|ant| {
self.move_ant(ant);
});
// Phase 2: Update life (divide grid into chunks)
let chunks: Vec<_> = self.create_grid_chunks();
chunks.par_iter().for_each(|chunk| {
self.update_life_chunk(chunk);
});
// Phase 3: Merge results
self.merge_chunk_results();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// WGPU compute shaders for life simulation
pub struct GpuLifeCompute {
device: wgpu::Device,
queue: wgpu::Queue,
life_pipeline: wgpu::ComputePipeline,
substrate_buffer: wgpu::Buffer,
life_buffer: wgpu::Buffer,
result_buffer: wgpu::Buffer,
}
impl GpuLifeCompute {
pub async fn compute_life_step(&self, grid_size: (u32, u32)) {
// Dispatch compute shader for life simulation
let mut encoder = self.device.create_command_encoder(&Default::default());
{
let mut compute_pass = encoder.begin_compute_pass(&Default::default());
compute_pass.set_pipeline(&self.life_pipeline);
compute_pass.set_bind_group(0, &self.bind_group, &[]);
compute_pass.dispatch_workgroups(
(grid_size.0 + 15) / 16,
(grid_size.1 + 15) / 16,
1,
);
}
self.queue.submit(std::iter::once(encoder.finish()));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
pub struct Simulation {
config: SimulationConfig,
grids: GridSystem,
ants: AntSwarm,
stats: SimulationStats,
compute_backend: Box<dyn ComputeBackend>,
}
impl Simulation {
pub fn new(config: SimulationConfig) -> Result<Self, SimulationError>;
pub fn step(&mut self) -> Result<(), SimulationError>;
pub fn run_for(&mut self, steps: u64) -> Result<(), SimulationError>;
pub fn reset(&mut self) -> Result<(), SimulationError>;
// State access
pub fn get_substrate_grid(&self) -> &SubstrateGrid;
pub fn get_life_grid(&self) -> &LifeGrid;
pub fn get_ants(&self) -> &[Ant];
pub fn get_stats(&self) -> &SimulationStats;
// Configuration
pub fn update_config(&mut self, config: SimulationConfig) -> Result<(), SimulationError>;
pub fn randomize_rules(&mut self);
// Serialization
pub fn save_state(&self, path: &Path) -> Result<(), IoError>;
pub fn load_state(&mut self, path: &Path) -> Result<(), IoError>;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
pub trait Renderer {
fn render(&mut self, simulation: &Simulation, target: &mut RenderTarget) -> Result<(), RenderError>;
fn set_viewport(&mut self, viewport: Viewport);
fn set_zoom(&mut self, zoom: f32);
}
pub struct CanvasRenderer {
canvas: Canvas,
color_cache: HashMap<u8, Color>,
cell_size: f32,
viewport: Viewport,
}
impl Renderer for CanvasRenderer {
fn render(&mut self, simulation: &Simulation, target: &mut RenderTarget) -> Result<(), RenderError> {
// Optimized rendering implementation
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
[package]
name = "bclae"
version = "0.1.0"
edition = "2021"
authors = ["Your Name <email@example.com>"]
license = "MIT OR Apache-2.0"
description = "High-performance Binary Coded Layered Automata Engine"
repository = "https://github.com/username/bclae"
[dependencies]
# Core dependencies
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
rayon = "1.7"
bitvec = "1.0"
nalgebra = "0.32"
# GPU acceleration
wgpu = { version = "0.18", optional = true }
bytemuck = { version = "1.14", optional = true }
# Rendering
image = "0.24"
palette = "0.7"
# CLI
clap = { version = "4.0", features = ["derive"], optional = true }
# Web interface
wasm-bindgen = { version = "0.2", optional = true }
web-sys = { version = "0.3", optional = true }
[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
proptest = "1.0"
[features]
default = ["gpu", "cli"]
gpu = ["wgpu", "bytemuck"]
cli = ["clap"]
web = ["wasm-bindgen", "web-sys"]
[[bench]]
name = "simulation_bench"
harness = false
[[bin]]
name = "bclae-cli"
required-features = ["cli"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ant_movement() {
let mut simulation = Simulation::new(default_config()).unwrap();
let initial_pos = simulation.get_ants()[0].position;
simulation.step().unwrap();
let new_pos = simulation.get_ants()[0].position;
assert_ne!(initial_pos, new_pos);
}
#[test]
fn test_life_evolution() {
// Test Conway's life rules
}
#[test]
fn test_serialization() {
// Test save/load functionality
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
use proptest::prelude::*;
proptest! {
#[test]
fn test_grid_bounds(
x in 0u32..1000,
y in 0u32..1000,
grid_size in 10usize..1000
) {
let grid = SubstrateGrid::new(grid_size, grid_size);
// Test that all operations respect bounds
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
use criterion::{black_box, criterion_group, criterion_main, Criterion};
fn bench_simulation_step(c: &mut Criterion) {
let mut simulation = Simulation::new(benchmark_config()).unwrap();
c.bench_function("simulation_step_1000x1000", |b| {
b.iter(|| {
simulation.step().unwrap();
})
});
}
criterion_group!(benches, bench_simulation_step);
criterion_main!(benches);
1
2
3
4
5
6
7
8
9
10
11
12
// Built-in profiling support
pub struct ProfilerConfig {
pub enable_cpu_profiling: bool,
pub enable_memory_profiling: bool,
pub sample_rate: u64,
}
impl Simulation {
pub fn enable_profiling(&mut self, config: ProfilerConfig) {
// Integration with perf, valgrind, etc.
}
}