Masan
Masan

Reputation: 345

Is it possible to use use `parfor` for parallel computing in Matlab in these codes?

I am using parfor for parallel computing in Matlab. I am not familiar with this command. If that is possible, please look at my code below and tell me if I can write it with parfor. These errors and warnings are appear in Matlab Editor:

Upvotes: 0

Views: 469

Answers (2)

Edric
Edric

Reputation: 25160

The problem here is that it we can see that you're not using Dat in a way that is order-dependent, but the static analysis machinery of parfor cannot deduce that because of the way you're assigning into it. I think you can work around this by instead creating a whole new Dat for each iteration of the loop, like so:

Dat = struct('normXpj', rand(10,1), 'InitialGuess', 3);
normXpj = rand(10);
parfor idx = 1:10
    tmpDat = struct('normXpj', normXpj(:,idx), 'InitialGuess', Dat.InitialGuess);
    % use 'tmpDat'
    disp(tmpDat);
end

Upvotes: 2

LowPolyCorgi
LowPolyCorgi

Reputation: 5188

The answer is no, unfortunately. At line:

Dat.normXpj = normXpj(pj,:);

you assign a value to Dat.normXpj, but you have to know that in a parfor loop there can be multiple iterations executing at the same time. So what value should be used for Dat.normXpj ? Matlab cannot decide, hence your error.

More generally, your code looks quite messy. I suppose you want to use parfor to increase execution speed. Probably a more efficient option would be to use the profiler (see profile) to detect the bottlenecks in your code, and apply a correction if that's possible.

Best,

Upvotes: 1

Related Questions