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

3 vues (au cours des 30 derniers jours)
Roshan
Roshan le 13 Jan 2021
Commenté : Raymond Norris le 13 Jan 2021
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??

Réponse acceptée

Roshan
Roshan le 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 commentaire
Raymond Norris
Raymond Norris le 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by