Using classified variables in Parallel for loops in Matlab

82 Views Asked by At

Hi I am trying to run a code for image reconstruction of 2D projections using the iradon transform. My aim to reduce the time significantly for which I am trying to using parallel for loop in my local cluster profile of my laptop. But I seem to get an error which I am finding it difficult to debug-

Error: The variable data in a parfor cannot be classified.

My code-

clc;
close all;
clear all;

tic
% projection_length = input('Define projection length (px) = ');
projection_length = 4100;  % which means our ouput will have 4100 cross sectional images of the capillary tube

parfor q = 1:projection_length

  for i = 1:5  % typically 500, since we take 500 projections around the capillary tube 0.72 deg inc
      if length(num2str(i)) == 1
           data(q,:, i) = imread(['pre00' num2str(i) '.tif'], 'PixelRegion', {[1 1600], [q q]});
      elseif length(num2str(i)) == 2
           data(q,:, i) = imread(['pre0' num2str(i) '.tif'], 'PixelRegion', {[1 1600], [q q]});
      elseif length(num2str(i)) == 3
           data(q,:, i) = imread(['pre' num2str(i) '.tif'], 'PixelRegion', {[1 1600], [q q]});
      end

      disp(['Analyzing projection ' num2str(q)  ' of ' num2str(projection_length) ', Angle ' num2str(i) '...']);
  end
  H = iradon( data(q,:,:), 0.72, 'Hann', 0.8, 1600);
end
toc
1

There are 1 best solutions below

3
On

Firstly, I would recommend using sprintf to build the filename, like so:

fname = sprintf('pre%03d.tif', i);
data(q, :, i) = imread(fname, ...);

This will correctly zero-pad fname.

The problem with data is that you're indexing it in different ways, and unfortunately parfor cannot understand that correctly. (This should show up as a "red" warning in the MATLAB editor, which can sometimes give you better information about parfor problems). I would be tempted to fix the problem something like this:

parfor q = 1:projection_length
    tmpData = zeros(N, numFiles); % insert correct value of N here
    for i = 1:numFiles
        fname = ...;
        tmpData(:, i) = imread(fname, ...);
    end
    data(q, :, :) = tmpData;
    H = iradon(tmpData, ...);
end

I'm not sure what you're trying to do with H - parfor will treat that as a temporary variable, and the value will not be available after the loop.