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

조회 수: 7 (최근 30일)
Roshan
Roshan 2021년 1월 13일
댓글: Raymond Norris 2021년 1월 13일
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??

채택된 답변

Roshan
Roshan 2021년 1월 13일
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
Raymond Norris
Raymond Norris 2021년 1월 13일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by