An automatic parallelization tool that converts sequential C++ image processing code to OpenMP-parallelized code dynamically.
This project demonstrates automatic parallelization of sequential C++ code using OpenMP. It includes:
- Sequential Implementation (
codebase.cpp) - Original sequential image processing pipeline - Auto-Parallelizer (
auto_parallelizer.py) - Python tool that automatically analyzes and parallelizes code - Parallel Implementation (
codebase_parallel.cpp) - Auto-generated OpenMP parallelized version
The auto-parallelizer automatically:
- Detects parallelizable for-loops in C++ code
- Identifies reduction operations (e.g., sum accumulation)
- Generates appropriate OpenMP pragmas (
#pragma omp parallel for) - Handles reduction clauses for parallel accumulation
- Adds OpenMP header includes
- Produces compilation-ready parallel code
./run_comparison.shThis script will:
- Run the auto-parallelizer to generate parallel code
- Compile both sequential and parallel versions
- Run benchmarks on both versions
- Display timing comparisons
python3 auto_parallelizer.py codebase.cpp codebase_parallel.cppThe tool will analyze the sequential code and generate a report showing which loops were parallelized.
g++ codebase.cpp -o image_processor_sequentialg++ -fopenmp codebase_parallel.cpp -o image_processor_parallel# Sequential version
./image_processor_sequential 2048 2048
# Parallel version
./image_processor_parallel 2048 2048The code implements a typical image processing pipeline with 5 operations:
- Generate Synthetic Data - Creates test RGB image data
- RGB to Grayscale Conversion - Converts color image to grayscale
- Calculate Average - Computes average grayscale value (uses reduction)
- Brightness Adjustment - Adjusts image brightness
- Threshold Application - Applies binary threshold
All operations except data generation are automatically parallelized by the tool.
- Identifies standard C-style for loops with integer iterators
- Analyzes loop bounds and iteration patterns
- Checks for data dependencies
- Detects array access patterns
- Identifies I/O operations (not parallelizable)
- Detects break/continue statements
- Automatically identifies accumulation patterns (e.g.,
sum += ...) - Generates appropriate
reduction(+:var)clauses
- Inserts pragmas at correct positions
- Maintains code formatting and indentation
- Handles loop iterator scope correctly
- Avoids redundant private variable declarations
Both executables accept optional image dimensions:
./image_processor_sequential [width] [height]
./image_processor_parallel [width] [height]Default: 1024x1024 pixels
- C++ Compiler: g++ with OpenMP support
- Python: Python 3.x
- Operating System: Linux, macOS, or Windows with MinGW
The parallel version typically achieves significant speedup on multi-core systems. Speedup depends on:
- Number of CPU cores
- Image size (larger images benefit more)
- Memory bandwidth
- Thread scheduling overhead
Typical speedup: 2-4x on quad-core systems
.
├── codebase.cpp # Sequential implementation
├── codebase_parallel.cpp # Auto-generated parallel version
├── auto_parallelizer.py # Auto-parallelization tool
├── run_comparison.sh # Automated test script
├── README.md # This file
└── CSE355 Project Specification.pdf # Project specification
- Parsing: Reads the C++ source file line by line
- Function Detection: Identifies function boundaries for context
- Loop Detection: Uses regex to find for-loop patterns
- Dependency Analysis:
- Checks for array access patterns
- Identifies reduction operations
- Detects non-parallelizable constructs
- Code Generation:
- Adds
#include <omp.h>header - Inserts OpenMP pragmas before parallelizable loops
- Maintains correct indentation and formatting
- Adds
- Report Generation: Provides detailed analysis of parallelization
Auto-Parallelization Report
==================================================
Input file: codebase.cpp
Output file: codebase_parallel.cpp
Parallelized 5 out of 5 loops:
✓ Loop 1 in function 'generateImageData' (line 32)
✓ Loop 2 in function 'convertToGrayscale' (line 42)
✓ Loop 3 in function 'calculateAverageGray' (line 53)
- Reduction operations: [('sum', '+')]
✓ Loop 4 in function 'adjustBrightness' (line 62)
✓ Loop 5 in function 'applyThreshold' (line 73)
==================================================
Parallel code generated successfully!
Potential improvements to the auto-parallelizer:
- Support for nested loops
- More sophisticated dependency analysis
- Detection of other reduction operations (min, max, multiplication)
- Support for parallel sections and tasks
- Loop scheduling clauses (static, dynamic, guided)
- SIMD vectorization hints
Educational project for CSE355 - Theory of Computation
Auto-Parallelization Image Processing Project