Info

This question is closed. Reopen it to edit or answer.

Using "parfor" in non constant bounds [Parallel Computing]

1 view (last 30 days)
Hi all!
Recently I started working with Matlab, and until now everything went fine. Because I'm working with giantic files, my run times have gotten out of hand. Luckily I got a lot of processors & RAM at my disposal.
At the moment I'm trying to use the "Parallel Toolbox", but I'm running into trouble. Figuring out that the function parfor could be useful, I found out that the bounds within the loops should be constant.
I need help converting my code in something suitable for parallising. To clearify this, below my code is given. The imporant part is the outer for loop: snap=1:size(A_tensor,3).
Basically I think these loops could run independantly, but still Matlab is having problems. Any suggestions what I should change to be able to parallise my code?
Thanks a bunch!
%%%Step 4: Make adjency matrices depending on snapshot size
n =length(Unique_elements);
adj = sptensor([n n size(A_tensor,3)]);
%adj = zeros(n, n, size(A_tensor,3), 'logical');
tic
for snap=1:size(A_tensor,3) % snap= snapshots in timeframe specified
for i=1:length(Unique_elements) % Go over all callers
index_caller_out = find(A_tensor(:,1,snap)==Unique_elements(i)); %Indexen of calls started
index_caller_in = find(A_tensor(:,2,snap)==Unique_elements(i)); %Indexen of calls received
for j=1:length(index_caller_out)
%J=find(Unique_elements==M.b_number(index_caller_out(j)));
J=find(Unique_elements==A_tensor(index_caller_out(j),2,snap));
adj(i,J,snap) = 1;
end
for q=1:length(index_caller_in)
%Q=find(Unique_elements==M.a_number(index_caller_in(q)));
Q=find(Unique_elements==A_tensor(index_caller_in(q),1,snap));
adj(i,Q,snap) = 1 ;
end
end
end

Answers (1)

Walter Roberson
Walter Roberson on 3 Mar 2018
Using size() as an upper bound is not a problem for parfor.
However, instead of the multiple references to A_tensor(?, ?, snap) you should take a local copy of A_tensor(:,:,snap) and use local indexing within that, writing into a local 2D copy of adj. After your j and q loops have built the local 2D copy of adj, write the local 2D copy into adj(:,:,snap) as a single step.

Community Treasure Hunt

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

Start Hunting!