Skip to content

vl3c/Cellatons

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

66 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Cellatons β€” Cellular Automata in Mojo πŸ”₯

High-performance cellular automata implementations in Mojo, featuring CPU (SIMD-vectorized) and native GPU execution paths. Four sub-projects cover 1D, 2D, 3D, and 4D automata with real-time visualization and comprehensive benchmarking.

Sub-Projects

1. Row Cellular Automata (1D)

Rule-based 1D automata (Rule 30, 110, 254, etc.) with pattern evolution from a single center cell. Supports massive grids (20KΓ—10K) for deep pattern exploration.

2. Grid Game of Life (2D)

Classic 2D simultaneous-update automaton with real-time pygame visualization at 1440p. Interactive controls for pause, reset, and GPU/CPU mode switching.

3. Cube Automaton (3D, GPU-only)

26-neighbor 3D cellular automaton (B6/S567) running on native GPU kernels. Renders a rotating voxel cube with a transparent gray grid on black background.

4. Hypercube Automaton (4D, GPU-only)

80-neighbor 4D extension of the cube rule (B6/S567) over WΓ—ZΓ—YΓ—X (default 32Γ—32Γ—32Γ—32). GPU-only with multiple render modes: single W slice, max-intensity projection, tiled slices, and render-off; modes cycle with a toggle key.


Features

  • Mojo-native implementation: Grid logic, rule engines, renderers, and compute modules organized into clean packages
  • Multiple execution modes:
    • Sequential CPU
    • Parallel cells on CPU (multi-threaded)
    • Parallel grids on CPU
    • SIMD-vectorized CPU (AVX-512, 64-cell vectors)
    • CuPy GPU (via Python interop)
    • Native Mojo GPU kernels
  • Real-time visualization: pygame-powered viewers with FPS counter and control overlays
  • PNG export: Optional Pillow-based image generation for Row CA patterns
  • Comprehensive benchmarking: Detailed timing stats (min/max/avg/stddev) with report generation
  • Extensive test suites: 70+ unit tests covering grid operations, rules, CPU/GPU equivalence

Requirements

  • Linux environment (on Windows, run under WSL2)
  • Pixi for dependency management
  • Mojo 0.26+ (MAX nightly channel)
  • CUDA-capable GPU (optional, for GPU acceleration)

Dependencies (managed by Pixi)

Package Version
mojo β‰₯0.26.1
pillow β‰₯12.0
cupy β‰₯13.6
pygame β‰₯2.5

Repository Structure

.
β”œβ”€β”€ shared/                      # Shared utilities
β”‚   β”œβ”€β”€ common.mojo              # Global config (WIDTH, HEIGHT, flags)
β”‚   β”œβ”€β”€ benchmark.mojo           # Benchmark suite utilities
β”‚   β”œβ”€β”€ logger.mojo              # Timestamped logging
β”‚   └── gpu_timing_result.mojo   # GPU timing stats struct
β”‚
β”œβ”€β”€ row/                       # 1D Row cellular automata
β”‚   β”œβ”€β”€ main.mojo                # Entry point (benchmark all modes)
β”‚   β”œβ”€β”€ grid.mojo                # Grid struct + CPU/GPU generation
β”‚   β”œβ”€β”€ rule.mojo                # Rule definition (3-neighbor patterns)
β”‚   β”œβ”€β”€ rule_container.mojo      # Pre-defined rules (30, 110, 254)
β”‚   β”œβ”€β”€ gpu_kernels.mojo         # Native Mojo GPU kernels
β”‚   β”œβ”€β”€ renderer.mojo            # PNG export via Pillow
β”‚   β”œβ”€β”€ renderer/                # Interactive pygame viewer
β”‚   β”‚   β”œβ”€β”€ base.mojo            # Renderer configuration
β”‚   β”‚   β”œβ”€β”€ python_module.mojo   # Mojo-Python bridge
β”‚   β”‚   └── viewer.py            # Python-side rendering
β”‚   β”œβ”€β”€ run_tests.mojo           # row test runner (Mojo + Python)
β”‚   └── tests/                   # 40+ unit tests
β”‚
β”œβ”€β”€ grid/                      # Grid Game of Life (2D)
β”‚   β”œβ”€β”€ main.mojo                # Entry point (live viewer)
β”‚   β”œβ”€β”€ run_benchmark.mojo       # Dedicated benchmark runner
β”‚   β”œβ”€β”€ grid.mojo                # Double-buffered 2D grid
β”‚   β”œβ”€β”€ rules.mojo               # B3/S23 rule implementation
β”‚   β”œβ”€β”€ cpu_compute.mojo         # SIMD-vectorized CPU compute
β”‚   β”œβ”€β”€ gpu_compute.mojo         # Persistent GPU buffer management
β”‚   β”œβ”€β”€ gpu_kernels.mojo         # Native Mojo GPU kernels
β”‚   β”œβ”€β”€ renderer/                # pygame real-time renderer
β”‚   β”‚   β”œβ”€β”€ base.mojo            # Display initialization
β”‚   β”‚   β”œβ”€β”€ python_module.mojo   # Mojo-Python bridge
β”‚   β”‚   └── viewer.py            # Zero-copy numpy rendering
β”‚   β”œβ”€β”€ benchmark/               # Stats & reporting
β”‚   β”‚   β”œβ”€β”€ stats.mojo           # Statistical calculations
β”‚   β”‚   β”œβ”€β”€ result.mojo          # Result aggregation
β”‚   β”‚   └── report.mojo          # Console/file reports
β”‚   └── tests/                   # 70+ unit tests
β”‚
β”œβ”€β”€ generated/                   # PNG outputs
β”œβ”€β”€ pixi.toml / pixi.lock        # Environment definition
└── README.md

Quick Start

1. Install Pixi

curl -fsSL https://pixi.sh/install.sh | sh

2. Install Dependencies

pixi install

3. Run Grid Game of Life (Interactive)

pixi run mojo grid/main.mojo

Controls:

  • SPACE β€” Pause/Resume
  • R β€” Reset (new random grid)
  • G β€” Toggle GPU/CPU mode
  • Q / ESC β€” Quit

4. Run Row CA Viewer (Interactive)

pixi run mojo row/renderer/main.mojo

Visualizes the 20KΓ—10K grid playback with pygame at 60 FPS. Use this for live viewing; pause/resume with SPACE, reset with R, quit with Q/ESC.

5. Run Row CA Benchmark

pixi run mojo row/main.mojo

Runs all execution modes on a 20KΓ—10K grid and prints timing comparison.

6. Run Grid Benchmark

pixi run mojo grid/run_benchmark.mojo

Runs 10,000 generations at 4K resolution, outputs detailed report to grid_benchmark.txt.

7. Run cube Automaton (GPU-only)

pixi run mojo cube/main.mojo

Controls: SPACE β€” Pause/Resume, R β€” Reset, Q/ESC β€” Quit

8. Run hypercube Automaton (4D, GPU-only)

pixi run mojo hypercube/main.mojo

Controls: SPACE β€” Pause/Resume, R β€” Reset, T β€” Cycle render mode (Slice β†’ Max β†’ Tiled β†’ Off), [ / ] β€” Move W slice (Slice mode), Q/ESC β€” Quit

Windows (WSL2) one-liner β€” run inside WSL, pointing to your repo:

wsl -e bash -lc "cd /mnt/c/Path/To/Cellatons && pixi run mojo cube/main.mojo"

Running Tests

# All Row tests + renderer tests
pixi run mojo row/run_tests.mojo

# Grid Game of Life tests
pixi run mojo grid/run_tests.mojo

# Cube automaton tests
pixi run mojo cube/run_tests.mojo

# Hypercube automaton tests (GPU not required for grid checks)
pixi run mojo hypercube/run_tests.mojo

Configuration

Row CA (shared/common.mojo)

alias WIDTH: Int = 20_000       # Grid width (cells)
alias HEIGHT: Int = 10_000      # Grid height (rows/generations)
alias PIXELS_PER_CELL: Int = 2  # PNG scale factor
alias RENDER_PNGS: Bool = False # Enable PNG generation

Grid CA (grid/grid.mojo)

alias DISPLAY_WIDTH: Int = 2560   # Display width (1440p)
alias DISPLAY_HEIGHT: Int = 1440  # Display height
alias INITIAL_DENSITY: Float64 = 0.15  # Random fill density

Performance

Both sub-projects leverage:

  • AVX-512 SIMD: 64-byte aligned buffers for vectorized processing
  • Double-buffering: Ping-pong buffers for zero-copy generation swaps
  • Native GPU kernels: Mojo's GPU programming model for direct CUDA execution
  • Zero-copy rendering: numpy views over Mojo memory for efficient Python interop

Typical performance on RTX-class GPUs:

  • Row CA: 200M+ cells generated in <1s
  • grid: 8M+ cells/generation at 60+ FPS (GPU mode)

License

MIT

About

Cellular automata (high-performance CPU and GPU implementations)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors