Bart.dIAs: A Python assistant for identifying parallelization opportunities
Project description
Version 0.9.1a
Bart.dIAs is an assistant for the WebGRIPP environment. Currently, it is solely an assistante for parallel programming that analyzes sequential code to identify bottlenecks and suggest parallelization strategies. It combines critical path analysis with pattern recognition to provide targeted recommendations for improving application performance.
Features
- Critical Path Analysis: Identifies performance bottlenecks in sequential code using directed acyclic graph (DAG) representation
- Parallel Pattern Recognition: Detects common parallel programming patterns in code
- Pattern-Based Code Generation: Automatically generates parallelized code for identified patterns (currently supporting Map-Reduce pattern)
- Hardware-Aware Recommendations: Provides optimization suggestions based on available system resources
- Theoretical Performance Metrics: Calculates work, span, and parallelism metrics based on Träff's "Lectures on Parallel Computing"
- Partitioning Strategy Recommendations: Suggests appropriate data and task partitioning strategies for different patterns
Critical Path Analysis
- DAG-Based Modeling: Constructs a Directed Acyclic Graph (DAG) representation of code to analyze dependencies
- Work-Span Metrics: Calculates theoretical metrics from parallel computing theory:
- Total Work (T₁): Sum of computational costs across all operations
- Critical Path Length (T∞): Longest chain of dependent operations
- Inherent Parallelism (T₁/T∞): Theoretical upper bound on speedup
- Amdahl's Law Integration: Estimates maximum theoretical speedup based on sequential fraction
- Bottleneck Identification: Pinpoints sequential code sections that limit parallelization potential
Combo Pattern Detection
- Complex Pattern Recognition: Detects and analyzes advanced patterns:
- While loops containing for loops
- For loops with recursive function calls
- Nested loops with varying depths
- Loops containing function calls that themselves contain loops
- For loops inside while loops
New in Version 0.9.1a
- Map-Reduce Pattern Implementation: Added complete support for detecting and generating parallelized code for the Map-Reduce pattern
- Hardware-Aware Code Generation: Generated code now adapts to the actual number of processors available on the system
- AST-Based Transformation: Using Python's ast module for robust code analysis and transformation
- Template-Based Code Generation: Utilizing Jinja2 templates for readable, maintainable generated code
- Support for Different Partitioning Strategies: Implemented SDP (Spatial Domain Partitioning) and SIP (Spatial Instruction Partitioning) templates
Installation
- Clone the repository:
git clone https://github.com/ratem/bart.dias.git
cd bart.dias
- Install the package:
pip install .
# Or for development mode:
pip install -e .
- Install directly from GitHub:
pip install git+https://github.com/ratem/bart.dias.git
Requirements
- Python 3.6+
- Jinja2
- NetworkX (for DAG visualization)
- Numpy
- Psutil
- Matplotlib (optional, for visualization)
Usage
Command-Line Interface (CLI)
You can use bart-dias directly from the terminal to analyze Python files:
# Analysis types: blocks, blocks_with_suggestions, critical_path, patterns, integrated
bart-dias script.py -t blocks
bart-dias script.py -t blocks_with_suggestions
bart-dias script.py -t critical_path
bart-dias script.py -t patterns
bart-dias script.py -t integrated
bart-dias script.py -t full
# Structured output (JSON for LLM consumption)
bart-dias script.py -t blocks_with_suggestions --format structured
# Line range filtering
bart-dias script.py -t blocks --start-line 50 --end-line 100
# Interactive session
bart-dias
Programmatic Usage (Python Library)
You can import bart_dias components in your own Python projects:
from bart_dias import analyze_code
import json
# Structured output — returns dict ready for LLM
result = analyze_code("script.py", analysis_type="blocks_with_suggestions", output_format="structured")
if result["ok"]:
data = result["result"]
# data["suggestions"][0]["code_suggestion"] — generated parallel code
# data["suggestions"][0]["explanation"] — why it can be parallelized
# data["suggestions"][0]["partitioning_suggestion"] — partitioning strategy
prompt = f"Analyze these parallelization opportunities:\n{json.dumps(data, indent=2)}"
# Critical path with DAG + auto-detected patterns + generated code
result = analyze_code("script.py", analysis_type="critical_path", output_format="structured")
# result["result"]["metrics"] — work, span, parallelism, Amdahl
# result["result"]["bottlenecks"][0]["code_generation"]["transformed_code"]
# result["result"]["dag_data"] — nodes, edges, critical_path_nodes
# Pattern analysis with template code generation
result = analyze_code("script.py", analysis_type="patterns", output_format="structured")
# result["result"]["identified_patterns"]["map_reduce"]["code_generation"]["transformed_code"]
# Integrated analysis (critical path + patterns + codegen)
result = analyze_code("script.py", analysis_type="integrated", output_format="structured")
# Line range filtering
result = analyze_code("script.py", analysis_type="blocks", output_format="structured", start_line=50, end_line=100)
# Markdown output (for humans/CLI)
result = analyze_code("script.py", analysis_type="blocks_with_suggestions", output_format="markdown")
print(result["result"]) # formatted Markdown string
Analysis Types
| Type | What it does | Structured output |
|---|---|---|
blocks |
Identifies parallelizable loops, functions, comprehensions | opportunities[] with type, lineno |
blocks_with_suggestions |
Same + template-generated code per block | suggestions[] with code_suggestion, explanation, partitioning_suggestion |
critical_path |
DAG metrics + bottlenecks + auto-detected patterns | metrics, bottlenecks[] with code_generation, dag_data |
patterns |
Parallel pattern recognition + codegen | identified_patterns{} with code_generation, performance_characteristics |
integrated |
Critical path + patterns combined | All of the above combined |
full |
Everything: blocks with suggestions + integrated | block_suggestions[], metrics, bottlenecks[], patterns{}, dag_data |
Architecture
Bart.dIAs consists of several key components:
Core Components
- BDiasParser: Parses Python code using AST analysis to identify parallelizable blocks and analyze dependencies
- BDiasCodeGen: Generates parallelization suggestions using Jinja2 templates for blocks
- BDiasCriticalPathAnalyzer: Performs theoretical analysis of code's inherent parallelism
- BDiasPatternAnalyzer: Parses Python code using AST analysis to identify parallelizable patterns and analyze dependencies
- BDiasPatternCodeGen: Generates parallelization suggestions using Jinja2 templates for patterns
- BDiasAssist: Provides the user interface and coordinates the analysis process
Theoretical Foundation
The critical path analysis is based on established parallel computing theory from Jesper Larsson Träff's "Lectures on Parallel Computing":
- Work-Span Model: Represents computation as a DAG where nodes are tasks and edges are dependencies
- Critical Path Analysis: Identifies the longest path of dependent operations that limits parallel execution
- Amdahl's Law: Calculates maximum theoretical speedup based on sequential fraction
- Brent's Theorem: Relates work, span, and processor count
Limitations
- Static Analysis Only: Analysis is based on code structure, not runtime behavior
- Focus on Multiprocessing: Current code generation targets Python's multiprocessing module
- Python-Specific: Analysis is designed for Python code only
- Theoretical Bounds: Critical path analysis provides theoretical upper bounds that may not be achievable in practice
Documentation
For detailed documentation, see The Docs.
License
This project is licensed under the MIT License - see the LICENSE file for details.
AI Use
This project uses AI for writing tests, docstrings, documentation, code templates, and input/output code.
Acknowledgments
- Jesper Larsson Träff for the theoretical foundation in "Lectures on Parallel Computing"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bart_dias-0.9.2.tar.gz.
File metadata
- Download URL: bart_dias-0.9.2.tar.gz
- Upload date:
- Size: 74.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e92d6e621bac7984e29a43685eed16fad191b24b29d543036c663c49c4b9e72b
|
|
| MD5 |
b01a6f8ac804b02e0e9c72a2866a7307
|
|
| BLAKE2b-256 |
f5489f7fbb447a281dad55f7fafe8697b8335f4cfb99937ceffc71d9b3b85553
|
File details
Details for the file bart_dias-0.9.2-py3-none-any.whl.
File metadata
- Download URL: bart_dias-0.9.2-py3-none-any.whl
- Upload date:
- Size: 94.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f6e50c867a5569b4624c0b11217578f5e1230799999dbdd0874ecc5f34759b3
|
|
| MD5 |
b6cd914f2401419636d8df29fdd9843f
|
|
| BLAKE2b-256 |
bde3aa4a30dc16a14f348756dd69571a430cf433d49d8a4a0e6ed89e3d9cab6a
|