Is the pile-up of ones into the elements of a matrix parallelizable by parfor?
Show older comments
Hello,
Is there any way to do the following pile-up in matrix A using parfor?
A = zeros(1,J);
for i=1:n
j = f(i); % f returns index j = 1,2,...J
A(j) = A(j)+1;
end
Thanks in advance, Abi
Accepted Answer
More Answers (1)
Assuming that f() is the most time-consuming part:
C = cell(1, n);
parfor iC = 1:n
C{iC} = f(iC);
end
A = zeros(1,J);
for iC = 1:n
index = C{iC};
A(index) = A(index) + 1;
end
Unfortunately I cannot test this, but collecting the results of f() in a cell should work, while A(j)=A(j)+1 cannot be performed inside a PARFOR due to a concurrent data access to the values of A.
3 Comments
Abi mehranian
on 19 Feb 2013
You could actually split the accumarray computation using PARFOR, but that means broadcasting multiple copies of A from the labs. For large J, I don't know if it would be worth it. My example below involves the cell-splitting utility MAT2TILES, available here
j=ones(n,2);
parfor i=1:n
j(i,1) = f(i); % f returns index j = 1,2,...J
end
m=matlabpool('size');
chunksize=ceil(J/m);
jC = mat2tiles(j,[chunksize, inf]);
parfor i=1:length(jC)
A=A+accumarray(jC{i},1,[J,1]);
end
Abi mehranian
on 20 Feb 2013
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!