Search code examples
matlabparallel-processingparfor

Trouble with running a parallelized script from a driver script


I'm trying to parallelize my code, and I finally got the parfor loops set up such that Matlab doesn't crash every time. However, I've now got an error that I can't seem to figure out.

I have a driver script (Driver12.m) that calls the script that I'm trying to parallelize (Worker12.m). If I run Worker12.m directly, it usually finishes with no problem. However, every time I try to run it from Driver12.m, it either 1) causes Matlab to crash, or 2) throws a strange error at me. Here's some of my code:

%Driver script
run('(path name)/Worker12.m');

%Relevant worker script snippet
parfor q=1:number_of_ranges
    timenumber = squeeze(new_TS(q,:,:));
    timenumber_shift = circshift(timenumber, [0 1]);
    for m = 1:total_working_channels
        timenumberm = timenumber(m,:);
        for n = 1:total_working_channels
            R_P(m,n,q) = mean(timenumberm.*conj(timenumber(n,:)),2);
            R_V(m,n,q) = mean(timenumberm.*conj(timenumber_shift(n,:)),2);
        end
    end
end

Outcome #1: "Matlab has encountered an unexpected error and needs to close."

Outcome #2: "An UndefinedFunction error was thrown on the workers for ''. This might be because the file containing '' is not accessible on the workers. Use addAttachedFiles(pool, files) to specify the required files to be attached. See the documentation for 'parallel.Pool/addAttachedFiles' for more details. Caused by: Undefined function or variable ""."

However, if I run Worker12.m directly, it works fine. It's only when I run it from the driver script that I get issues. Obviously, this error message from Outcome #2 isn't all that useful. Any suggestions?

Edit: So I created a toy example that reproduces an error, but now both my toy example and the original code are giving me a new, 3rd error. Here's the toy example:

%Driver script
run('parpoolexample.m')

%parpoolexample.m
clear all
new_TS = rand([1000,32,400]);
[number_of_ranges,total_working_channels,~] = size(new_TS);
R_P = zeros(total_working_channels,total_working_channels,number_of_ranges);
R_V = zeros(total_working_channels,total_working_channels,number_of_ranges);

parfor q=1:number_of_ranges
    timenumber = squeeze(new_TS(q,:,:));
    timenumber_shift = circshift(timenumber, [0 1]);
    for m = 1:total_working_channels
        timenumberm = timenumber(m,:);
        for n = 1:total_working_channels
            R_P(m,n,q) = mean(timenumberm.*conj(timenumber(n,:)),2);
            R_V(m,n,q) = mean(timenumberm.*conj(timenumber_shift(n,:)),2);
        end
    end
end

Outcome #3: "Index exceeds matrix dimensions (line 7)."

So, at the 'parfor' line, it's saying that I'm exceeding the matrix dimensions, even though I believe that should not be the case. Now I can't even get my original script to recreate Outcomes #1 or #2.


Solution

  • Don't use run with parallel language constructs like parfor and spmd. Unfortunately it doesn't work very well. Instead, use cd or addpath to let MATLAB see your script.