How to slice the nested for loop to divide up the work for cluster/multi-core processors?
Show older comments
I have the following code with four nested for loops that calls the f() function during each iteration. V is a Ns by 3 by Nt matrix and f() inputs four 3-element vectors but not matrices. I was wondering if there is any way I can slice this nested for loops or somehow use ndgrid() so that I can use cluster to speed up the process.
Any suggestions will be appreciated.
for m = 1:Nt-1
for n = m+1:Nt
parfor i = 1:Ns
for j = 1:Ns
d(i,j)= f(V(i,:,m),V(i+1,:,m),V(j,:,n),V(j+1,:,n));
end
end
D(m,n) = min(min(d));
end
disp(['This is No. ',num2str(m),' tube']);
end
Answers (1)
Edric Ellis
on 8 Apr 2013
Edited: Edric Ellis
on 8 Apr 2013
That PARFOR loop does work, although as you observe 'V' is not currently sliced. Whether or not you can do much to the structure of this program depends on how you can modify your function 'f' to be vectorized. My first suggestion would be to replace the PARFOR loop with a FOR loop, and run the MATLAB profiler on your code to see where time is being spent.
If 'V' is large and constant, you might get some speedup by using my Worker Object Wrapper which helps when you need the same data inside several PARFOR loops. See here for the code, which the lets you do this
VWrapper = WorkerObjWrapper(V)
for ...
for ...
parfor ...
Vtmp = VWrapper.Value;
for ...
d(i,j) = f(Vtmp(...), ...);
end
end
end
end
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!