Skip to content

pyk92/Mandelbrot-Explorer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mandelbrot Explorer

A high-performance C library for generating and rendering fractal sets (Mandelbrot, Julia, and Newton fractals) with multiple SIMD and parallelization optimizations.

Features

  • Efficient Fractal Rendering: Optimized implementations for Mandelbrot, Julia, and Newton fractals
  • SIMD Optimizations:
    • SSE2 intrinsics for vectorized computations
    • AVX2 intrinsics for enhanced performance on modern CPUs
  • Multithreading: OpenMP-based parallelization for multi-core utilization
  • Border-Based Rendering: Smart boundary detection algorithm using deque-based BFS to minimize unnecessary computation
  • Interactive Visualization: Real-time SDL2-based viewer for exploring fractals
  • Multiple Precision Levels: Standard double precision and extended __float128 precision

Requirements

Build Dependencies

  • CMake >= 3.16
  • C11-compatible compiler (GCC, Clang, or MSVC)
  • SDL2 (only required if building test binaries)
  • OpenMP (optional, for multithreading support)

System Features

  • SSE2 instructions (standard on modern x86_64 CPUs)
  • AVX2 instructions (optional, for maximum performance on supported CPUs)

Installation

On Ubuntu/Debian:

sudo apt-get install build-essential cmake libsdl2-dev libomp-dev pkg-config

On macOS:

brew install cmake sdl2 libomp

On Windows:

  • Install Visual Studio with MSVC or use MinGW-w64
  • Install SDL2 development libraries
  • Ensure CMake is in your PATH

Building

Basic Setup

Create a build directory and configure with CMake:

mkdir build
cd build
cmake ..

Build Options

The following CMake options are available:

Option Default Description
ENABLE_AVX2 ON Enable AVX2 optimizations (auto-detected if supported)
BUILD_TESTS OFF Build test and example binaries
TEST_REALTIME OFF Build interactive real-time viewer
TEST_BENCHMARK OFF Build benchmark utility
TEST_VIDEO OFF Build video/image generation utility

Building the Static Library

By default, only the static library is built:

cd build
cmake ..
make

This creates libmandelbrot_core.a which can be linked into your own projects.

Building Test Binaries

To build specific test programs, enable the corresponding options:

Real-Time Interactive Viewer

The interactive viewer allows you to explore fractals in real-time with mouse and keyboard controls:

cd build
cmake .. -DBUILD_TESTS=ON -DTEST_REALTIME=ON
make
./test_realtime

Controls:

  • Mouse Scroll: Zoom in/out
  • Left Click + Drag: Pan the view
  • Right Click (on Mandelbrot): Generate Julia set for the clicked point
  • P: Increase iteration count
  • M: Decrease iteration count
  • ESC: Return to Mandelbrot set

Benchmark Utility

The benchmark tool measures rendering performance with different optimization combinations (SSE2, AVX2, multithreading):

cd build
cmake .. -DBUILD_TESTS=ON -DTEST_BENCHMARK=ON
make
./test_benchmark

Output Example:

Borderbrot rendered in 3.456789 s
Borderbrot with sse2 rendered in 1.234567 s
Borderbrot with avx2 rendered in 0.789012 s
Borderbrot with multithreading rendered in 0.654321 s
Borderbrot with multithreading and avx2 rendered in 0.123456 s

Video/Image Generation

The video utility generates high-resolution fractal images or video frames:

cd build
cmake .. -DBUILD_TESTS=ON -DTEST_VIDEO=ON
make
./test_video

Building All Tests

To build all test binaries at once:

cd build
cmake .. -DBUILD_TESTS=ON -DTEST_REALTIME=ON -DTEST_BENCHMARK=ON -DTEST_VIDEO=ON
make

Or simply:

cd build
cmake .. -DBUILD_TESTS=ON
make

Library Usage

Linking Against the Library

In your CMakeLists.txt:

find_package(mandelbrot_explorer REQUIRED)
target_link_libraries(your_target PRIVATE mandelbrot_core)

Or by adding the subdirectory:

add_subdirectory(path/to/mandelbrot_explorer)
target_include_directories(your_target PRIVATE path/to/mandelbrot_explorer/include)
target_link_libraries(your_target PRIVATE mandelbrot_core)

Basic Usage Example

#include "mandelbrot.h"
#include "palette.h"

int main() {
    int width = 1920, height = 1080;
    int max_iterations = 6000;
    int *output = malloc(width * height * sizeof(int));
    int *palette = malloc(max_iterations * sizeof(int));
    
    // Fill palette with colors
    fill_plte1(palette, max_iterations);
    
    // Render Mandelbrot set with optimizations
    mandelbrot(
        output,           // output buffer
        width, height,    // dimensions
        max_iterations,   // iteration limit
        -0.7, 0.0,       // dx, dy (offset)
        2.5,             // zoom level
        palette,         // color palette
        MANDELBROT_AVX2 | MANDELBROT_MULTITHREADING | MANDELBROT_FILL  // flags
    );
    
    // Use output buffer (width*height ARGB color values)
    
    free(output);
    free(palette);
    return 0;
}

Rendering Flags

Available flags for mandelbrot() and julia():

  • MANDELBROT_MULTITHREADING: Use all available CPU cores via OpenMP
  • MANDELBROT_SSE2: Enable SSE2 vector optimizations (always attempted if available)
  • MANDELBROT_AVX2: Enable AVX2 vector optimizations (requires AVX2 support)
  • MANDELBROT_SMOOTH: Enable smooth coloring for better gradients
  • MANDELBROT_FILL: Fill uncalculated areas intelligently

Supported Fractals

Mandelbrot Set

void mandelbrot(int *out, unsigned int width, unsigned int height, 
                unsigned int max, double dx, double dy, double zoom, 
                int *plte, int flags);

Julia Set

void julia(int *out, unsigned int width, unsigned int height, 
           unsigned int max, double dx, double dy, double zoom, 
           int *plte, int flags, double x0, double y0);

Newton Fractal

void newton(int *out, unsigned int width, unsigned int height, 
            unsigned int max, double dx, double dy, double zoom, 
            int flags, double complex z1, double complex z2, double complex z3);

High-Precision Mandelbrot (using __float128)

void precisebrot(int *out, unsigned int width, unsigned int height, 
                 unsigned int max, __float128 dx, __float128 dy, 
                 __float128 zoom, int *plte, int flags);

Project Structure

mandelbrot_explorer/
├── include/                 # Public headers
│   ├── deque.h             # Double-ended queue data structure
│   ├── julia.h             # Julia set generation
│   ├── mandelbrot.h        # Mandelbrot and variants
│   ├── newton.h            # Newton fractal
│   └── palette.h           # Color palette utilities
├── src/                    # Core implementation
│   ├── mandelbrot.c        # Mandelbrot algorithm with optimizations
│   ├── julia.c             # Julia set implementation
│   ├── newton.c            # Newton fractal implementation
│   ├── deque.c             # Deque data structure
│   ├── palette.c           # Palette generation
│   └── precisebrot.c       # High-precision variants
├── test/                   # Test and example programs
│   ├── realtime.c          # Interactive SDL2 viewer
│   ├── benchmark.c         # Performance benchmarking
│   ├── video.c             # High-res image/video generation
│   └── foto.c              # Additional image utilities
├── CMakeLists.txt          # Build configuration
└── README.md               # This file

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published