Matlab Parfor function. how to use it with my current situation?

1 view (last 30 days)
Dear engineer friends in this community,
I'm having a issue with the parfor.
Actually, I have a pre-defined the function, which was not built with the idea of 'parfor'. So it is very hard (for me) to alter it to fit the 'parfor' principle. Let's note it as 'f()'.
But the good part is, this function f() need to be run for many times to acquire enough results of data. So previously, I open several matlab in my computer to run it 'parallel' to speed up the running time.
Like below,
%matlab 1
for iteration 1:5000
f();
end
%matlab 2
for iteration 5001:10000
f()
end
...
But it is not a wise way, since I know we have the 'parfor' function to automatically do this work for me. Then I look up the tutorial, the 'parfor' could divide the iterations to several parts, as long as each iteration are independent. (if my understanding is correct)
So I tried this:
parfor 1: 1000000
f();
end
But the matlab gives me error says the it violates the classifications of the variables for 'parfor'...
I am wondering, if anyone could enlight me, when we use 'parfor', is it that I have to make sure every inner for-loop is also self-independent? Why is that???
I can manually do 'parallel', why can't 'parfor' do it??

Accepted Answer

Roshan
Roshan on 13 Jan 2021
Haha, I found the answer myself... And I shared it here.
I think the issue is one of matlab or me is not that smart. So directly add a parfor outside the for-loop will not bypass the classification of variable error.
% this cannot work, because f() has variables that violating parfor function...
parfor ...
[
f()
...
]
end
But, if I pacakge this f() function, and then use parfor to call this f(). It works.
% package the f(), and then call the f() inside the parfor.
% this works.
f()
[
...
]
parfor ...
f();
end
Good day folks.
  1 Comment
Raymond Norris
Raymond Norris on 13 Jan 2021
Hi Roshan,
You've identified, what I think is, the best programming pattern when using parfor loops. Refactor your for-loop into a single unit of work (i.e. f()) and than have your parfor call that it. Refctoring isn't feasible in all cases, but when it does, it's the best pattern.
Raymond

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!