Problem using parfor for reading variable sized chunks of data into a larger pre-allocated container
Show older comments
Hi,
I have an problem where I have pre-allocated a large matrix or vector, a, and where I will read data blocks from a large number of files that will be inserted at the right indexes in a. The different files, and resulting blocks, will typically have different size.
A simple example:
a = zeros(15,1); % pre-allocated vector
b = [1,10;11,15]; % each row contains the start to stop index for each block
parfor i = 1:size(b,1)
a(b(i,1):b(i,2),:) = i*ones((b(i,2)-b(i,1))+1,1);
end
With 'for' instead of 'parfor' it works as intended.
I have been reading several sites like https://www.mathworks.com/help/parallel-computing/troubleshoot-variables-in-parfor-loops.html and https://www.mathworks.com/matlabcentral/answers/126515-usage-of-sturctures-inside-parfor but I cannot wrap my head around my problem above.
Any tips or solutions which don't reduce the performance I am trying to obtain by using parfor in the first place?
Thanks,
Oyvind
Accepted Answer
More Answers (0)
Categories
Find more on Parallel for-Loops (parfor) 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!