Skip to content

koenig-arthur/mandelbrot

Repository files navigation

mandelbrot

Generate Mandelbrot images and zoom videos using TensorFlow with NVIDIA GPU acceleration.

mandelbrot

Contents

  • Mandlebrot.ipynb: Original notebook for interactive testing (outdated)
  • mandelbrot.py: Standalone Python script for image/video generation (CLI)
  • interactive_mandelbrot.py: Real-time interactive Mandelbrot viewer with pan/zoom controls
  • run_mandelbrot.sh: Wrapper that sets CUDA library paths and runs mandelbrot.py in the venv
  • run_interactive.sh: Wrapper that sets CUDA library paths and runs interactive_mandelbrot.py in the venv

Quick Start

# (Optional) Create and activate a virtual environment
python3 -m venv .venv
source .venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# Single image (saved to mandelbrot.png)
./run_mandelbrot.sh

# Single image with custom output
./run_mandelbrot.sh image output.png

# Zoom video with N frames (saves frames under content/tmp and video_h264.mp4)
./run_mandelbrot.sh video 1000

# Delete PNG frames after video creation (saves disk space)
./run_mandelbrot.sh video 1000 --delete-frames

# Interactive viewer (explore in real-time)
./run_interactive.sh           # Default: 800x600
./run_interactive.sh 1920 1080 # Full HD

Interactive Controls

When running the interactive viewer:

Key Action
Arrow Keys Pan (move) the view
+ or = Zoom in
- or _ Zoom out
r Reset to initial view
q or ESC Quit

Performance Optimizations

The script includes several optimizations for faster rendering:

  • XLA JIT Compilation: @tf.function(jit_compile=True) compiles the Mandelbrot loop for 2-3x GPU speedup
  • Multi-threaded I/O: Parallel image saving (4 threads by default) overlaps disk writes with GPU computation
  • GPU Warmup: Pre-compiles TensorFlow graph before timing starts
  • Memory Efficiency: Queue management prevents memory buildup during long renders
  • H.264 Encoding: Automatic ffmpeg conversion creates smaller, universally-playable videos

Expected Performance (RTX 5080, 4k):

  • Frame generation: ~0.4s per frame (2000 cycles)
  • 5000 frames: ~3 hours
  • Video encoding: ~1-2 minutes

To adjust I/O threads, edit num_threads in render_zoom_video().

Changing the Formula

By default, the script renders the classical Mandelbrot set using the formula: z = z² + c

To render different fractals, edit the iteration line in the mandelbrot_helper() function (line 76 in mandelbrot.py and line 87 in interactive_mandelbrot.py):

Formula Code
Mandelbrot (default) temp = current_values * current_values + grid_c
Mandelbrot (power 3) temp = current_values * current_values * current_values + grid_c
Mandelbrot (power 4) temp = tf.pow(current_values, 4) + grid_c
Tricorn/Mandelbar temp = tf.math.conj(current_values) * tf.math.conj(current_values) + grid_c

You may also want to adjust the center coordinates to explore different regions appropriate for each formula.

GPU Notes

  • This project uses tf-nightly[and-cuda] to support very new GPUs (e.g., RTX 50xx / compute capability 12.x).
  • The first GPU run may be slow while kernels JIT-compile for your architecture.
  • Ensure NVIDIA driver is installed (nvidia-smi works). No separate system CUDA install is required when using the and-cuda wheels.

Requirements

See requirements.txt.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors