Skip to content

Latest commit

 

History

History
377 lines (273 loc) · 9.67 KB

File metadata and controls

377 lines (273 loc) · 9.67 KB

Performance Optimization Guide

This guide covers performance optimization features available in the IWO helicopter system identification project.

Table of Contents

Overview

The IWO optimization algorithm has been enhanced with several performance optimization features:

  1. Parallel Computing: Evaluate multiple cost functions simultaneously
  2. Memory Optimization: Preallocate arrays to reduce memory overhead
  3. Progress Tracking: Real-time progress bar with ETA
  4. Profiling Tools: Identify performance bottlenecks

These features can significantly reduce computation time, especially for large-scale optimizations.

Parallel Computing

Requirements

  • MATLAB R2014b or later
  • Parallel Computing Toolbox

How It Works

When enabled, the algorithm evaluates cost functions in parallel across multiple CPU cores:

  • Initialization: All initial population members evaluated in parallel
  • Offspring Evaluation: All offspring in each iteration evaluated in parallel
  • Speedup: Approximately linear with number of CPU cores (with ~70% efficiency)

Enabling Parallel Computing

Method 1: Modify Configuration File

Edit src/algorithms/iwo/IWO/config_iwo.m:

config.useParallel = true;  % Enable parallel computing

Method 2: Use Configuration Utility

% Check availability and configure
config = configure_performance('parallel', true);

Method 3: Manual Pool Management

% Start parallel pool manually before running IWO
parpool(4);  % Use 4 workers

% Run IWO (with useParallel = true in config)
load('data/experiments/best.mat')
iwo

% Shutdown pool when done
delete(gcp('nocreate'));

Expected Performance

Workers Speedup Example Time (5000 iter)
1 (serial) 1.0x 60 minutes
2 1.7x 35 minutes
4 2.8x 21 minutes
8 4.5x 13 minutes
16 7.0x 8.5 minutes

Note: Actual speedup depends on hardware, MATLAB version, and cost function complexity

Troubleshooting

Issue: "Parallel Computing Toolbox not available"

  • Solution: Install the toolbox or disable parallel mode

Issue: Parallel pool fails to start

  • Solution: Check MATLAB preferences for parallel computing settings
  • Try: parpool('local', 4) to manually start pool

Issue: Slower with parallel enabled

  • Solution: Parallel overhead may exceed benefits for small problems
  • Disable for quick optimizations (< 100 iterations)

Memory Optimization

Array Preallocation

When enabled, the algorithm preallocates arrays for offspring before generation, reducing memory fragmentation and allocation overhead.

Enable/Disable

Edit config_iwo.m:

config.preallocateOffspring = true;   % Enable (default)
config.preallocateOffspring = false;  % Disable for debugging

Benefits

  • Reduced Memory: 20-30% less peak memory usage
  • Faster Execution: 5-10% faster due to reduced allocation overhead
  • More Stable: Less memory fragmentation over long runs

Memory Usage Estimates

Population Size Parameters Estimated Peak Memory
20-40 40 ~50 MB
50-100 40 ~150 MB
100-200 40 ~400 MB

Best Practices

  1. Close unnecessary applications before long optimizations
  2. Monitor memory with configure_performance() utility
  3. Use 64-bit MATLAB for large-scale problems
  4. Clear workspace between optimization runs:
    clear all
    load('data/experiments/best.mat')
    iwo

Progress Tracking

Progress Bar Features

  • Real-time Updates: Shows current iteration and best cost
  • ETA Calculation: Estimates remaining time
  • Configurable Update Rate: Balance between feedback and performance

Configuration

% In config_iwo.m
config.showProgress = true;              % Enable/disable progress bar
config.progressUpdateInterval = 10;      % Update every N iterations

Progress Bar Display

Iteration 1450/5000 | Best: 0.234567 | ETA: 12.3 min

Disabling for Batch Processing

For automated batch processing or remote execution:

config.showProgress = false;  % No GUI progress bar

Or use configure_performance:

config = configure_performance('progress', false);

Profiling Tools

profile_iwo Utility

Identifies performance bottlenecks in the optimization algorithm.

Basic Usage

% Load data
load('data/experiments/best.mat')

% Profile 100 iterations (quick)
results = profile_iwo();

% Profile 500 iterations (more accurate)
results = profile_iwo('iterations', 500);

% Full detailed profiling
results = profile_iwo('detail', 'full', 'plot', true);

Output

=======================================================
  IWO Performance Profiling
=======================================================
Iterations: 100
Detail Level: quick
-------------------------------------------------------

Starting profiler...
Running IWO optimization (100 iterations)...

-------------------------------------------------------
Profiling complete!
Total Time: 45.23 seconds
Peak Memory: 234.56 MB
Memory Increase: 123.45 MB
-------------------------------------------------------

Top 10 Time-Consuming Functions:
Function                                  Total (s)     Self (s)      Calls
--------------------------------------------------------------------------------
Sphere                                      32.1234      28.4567        2340
lsim                                        15.2345       8.9012        2340
corrcoef                                     5.6789       5.1234       23400
...

Performance Recommendations:
- Cost function (Sphere) is a bottleneck
  → Consider enabling parallel evaluation with parfor
  → Requires Parallel Computing Toolbox
...

Interpreting Results

  • Total Time: Time spent in function and its callees
  • Self Time: Time spent in function only (not callees)
  • Calls: Number of times function was called

Focus optimization on functions with high Self Time and many Calls.

View Detailed Profile

results = profile_iwo('iterations', 500);
profview(0, results.profileInfo);  % Opens MATLAB profiler GUI

Performance Tips

General Optimization

  1. Use Parallel Computing for long optimizations (> 1000 iterations)
  2. Enable Memory Preallocation (enabled by default)
  3. Reduce Display Updates during optimization:
    config.displayInterval = 100;  % Display every 100 iterations
  4. Use Semi-log Plots for better convergence visualization:
    config.useSemilogy = true;

Hardware Considerations

  1. CPU: More cores = better parallel performance

    • Recommended: 4-8 cores for optimal cost/benefit
    • Hyperthreading provides ~20% additional speedup
  2. Memory: Ensure sufficient RAM

    • Minimum: 4 GB
    • Recommended: 8-16 GB for large populations
    • Each worker needs ~200-500 MB
  3. Disk: Fast SSD improves data loading

    • Place .mat files on SSD if available

Algorithm Tuning

Balance accuracy vs. speed by adjusting:

% Faster (lower accuracy)
config.maxIterations = 1000;
config.initialPopSize = 10;
config.maxPopSize = 20;

% Slower (higher accuracy)
config.maxIterations = 10000;
config.initialPopSize = 40;
config.maxPopSize = 80;

Monitoring Performance

% Before optimization
tic;
iwo;
totalTime = toc;
fprintf('Total time: %.2f minutes\n', totalTime / 60);

% Check final memory usage
m = memory;
fprintf('Memory used: %.2f MB\n', m.MemUsedMATLAB / 1024^2);

Benchmarking

Quick Benchmark

% Benchmark with different settings
configs = {'Serial', 'Parallel 4', 'Parallel 8'};
times = zeros(1, 3);

% Serial
config = config_iwo();
config.useParallel = false;
config.maxIterations = 100;
tic; iwo; times(1) = toc;

% Parallel 4 workers
parpool(4);
config.useParallel = true;
tic; iwo; times(2) = toc;
delete(gcp);

% Parallel 8 workers
parpool(8);
config.useParallel = true;
tic; iwo; times(3) = toc;
delete(gcp);

% Results
bar(times);
set(gca, 'XTickLabel', configs);
ylabel('Time (seconds)');
title('IWO Performance Benchmark');

Full Performance Report

% Generate comprehensive performance report
load('data/experiments/best.mat')

fprintf('System Information:\n');
fprintf('MATLAB Version: %s\n', version);
fprintf('CPU Cores: %d\n', feature('numcores'));

% Profile baseline
results_serial = profile_iwo('iterations', 500);

% Profile parallel (if available)
if license('test', 'Distrib_Computing_Toolbox')
    config = config_iwo();
    config.useParallel = true;
    results_parallel = profile_iwo('iterations', 500);

    speedup = results_serial.totalTime / results_parallel.totalTime;
    fprintf('Parallel Speedup: %.2fx\n', speedup);
end

Summary

Feature Speedup Memory Impact Complexity
Parallel Computing 2-8x +20-50 MB Low
Array Preallocation 1.05-1.1x -20-30% None
Progress Tracking -1-2% +5 MB None

Recommendation for most users:

  • Enable parallel computing (4-8 workers)
  • Keep array preallocation enabled
  • Use progress bar for interactive runs
  • Profile before major optimizations

For questions or issues, see CONTRIBUTING.md or open an issue.