Nested for loop to parfor
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I have nested for loops.After I convert the outer for loop to parfor (I get no compilation errors), the program doesn't even go to the first step in the parfor loop until after 2.5 hours or so. Can someone please tell me why?
I tried various configurations where the number of broadcast variables were small and contained no large arrays but still the same problem. The program structure is as follows:
Variable initializations (around 10)
Matrix initializations (all to zeros, some of size 15*15, some 225*225) % Even if these matrices are initialized as local matrices inside the parfor loop it makes no difference.
parfor m=1:200
del(m)=some_value_depending on m
m %doesn't even print this for 2.5 hours! :(
for v=1:800
H=[some 15*15 matrix with some elements depending on m and some
others on v, but none of them depend on both simultaneously]
for i=1:225
for j=1:225
M=[225*225 matrix computed from some operations on H]
end
end
for k=1:15
some one line operation on M giving 2 matrices W and S
end
B=Inverse(W)*S -- size is 15*1;
A(m,:)=B;
Few lines of code operating on A(m)
end
end
plot(different values)
As you can see, there are no functions, but there are several large matrices within the loops. Will writing each loop as a function make it faster? If so, can someone please explain why?
Also, can someone tell how to use profile command for parallel computations if at all?
I am stuck for the past few weeks-please help.
Thank You
Subramanian
2 commentaires
Richard Brown
le 11 Avr 2012
I take it that the nested i,j loop just creates one entry of M at a time, not a 225x225 matrix each iteration.
Also, why are you using inverse to solve linear system, why not W\S? And have you initialised specifically del and A?
Réponses (2)
Ken Atwell
le 10 Avr 2012
How long does this loop take to run before converting the 'for' to 'parlor'? Are you running multiple local MATLAB workers on your computer, or connecting to a cluster?
Here are a few things that could be an explanation (assuming local workers -- if a cluster is involved, you may need to consult with its admin):
- Make sure you run 'matlabpool open' before the parlor loop (sorry if this sounds obvious, but it is a common pitfall).
- While the code is running, run the Task Manager (on Windows), Activity Monitor (Mac) or similar tool and look at the CPU usage and memory usage. CPU usage will probably be spiked at 100% ("good"), but if memory is also spiked, the computer may be trashing (overly-relying on virtual memory, which will almost certainly overwhelm and benefit from parlor).
- Run the code without 'parlor' and consult the Task Manager again for CPU usage. It is possible that the natural multithreading in MATLAB already doing a reasonable job, so there is little more to gain by switching to 'parfor'
Konrad Malkowski
le 11 Avr 2012
Are the matrices preallocated, or are they allocated dynamically at run time by the inner for loops? If matrices are not preallocated, try doing that.
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!