Parallel computing of "for" loop
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Muhammad Hassaan Bin Tariq
le 29 Mar 2024
Commenté : Muhammad Hassaan Bin Tariq
le 24 Avr 2024
Hi, I am solving a program in which I use a for loop for the computation of some entities. One of the entity is being updated in each loop. I am not able to use parfor loop which gives the reason that it cannot be used in case of updating entities. Is their any other way to implement parallel computing and speed up the process?
Thanks for your response in advance.
0 commentaires
Réponse acceptée
Steven Lord
le 29 Mar 2024
Is it the case that the computation of the value for the entity in the second iteration of your loop requires the value of the entity computed during the first iteration of your loop? Something like this basic scenario?
n = 5;
x = ones(1, n);
for k = 2:n
x(k) = (x(k-1).^2)+1
end
If so you can't use parfor because your loop iterations are not independent. You must compute the value of x(4) before it is possible for you to compute the value of x(5) in the example above, for instance.
Now the question of whether or not it's possible to speed up your code is a different matter entirely. We can't offer any firm guidance on that because you haven't shown us your code, but you can use the tools for measuring and profiling your code listed on this documentation page along with Code Analyzer to try to identify bottlenecks and/or suggested improvements in your code.
3 commentaires
Edric Ellis
le 24 Avr 2024
It might still be worth sketching out your use-case. There are types of computation that can be run in parfor that appear to be dependent on previous values. These are known as "reduction" operations, like this:
maxValue = -Inf;
parfor i = 1:100
maxValue = max(maxValue, rand());
end
disp(maxValue)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!