Help with using parfor (exceed matrix dimensions error)

2 views (last 30 days)
I need some help with parallel programming. I can't figure out how to correctly index variables for the parfor loop. The main thing with this code is that I need to sum some logical values (variable 'in') throughout the iteration in defined positions (given by variable 'a') inside a matrix ('grid'). The code using for loops works just fine, but takes too much time to process, as I have really a large amount of data to be processed. I don't know if I am correct, but I would like to try using parfor and running the code in a multi core computer to get the results faster. However, I don't know if its possible to use it in my code, once I have to store the results in a very specific way. If you have any suggestions, please, let me know. The code is the one above and the error that I get running it is: "Error using matlab_question (line 17 --> first parfor loop) Index exceeds matrix dimensions."
dat = cell(50000,1);
gridcm = 5;
tamx = 50;
tamy = 60;
grid = cell(50000,1);
grid2 = zeros(gridcm*(tamx),gridcm*(tamy));
for i=1:size(dat,1)
dat{i,1} = rand(5,2);
dat{i,1} = [dat{i,1};dat{i,1}(1,:)];
end
tic
parfor t = 1:size(dat,1)
minx = floor(gridcm*min(dat{t,1}(:,1)));
maxx = ceil(gridcm*max(dat{t,1}(:,1)));
miny = floor(gridcm*min(dat{t,1}(:,2)));
maxy = ceil(gridcm*max(dat{t,1}(:,2)));
x = minx:maxx;
y = miny:maxy;
xi = repmat(x,length(y),1);
xi = reshape(xi,length(x)*length(y),1);
yi = repmat(y,length(x),1)';
yi = reshape(yi,length(y)*length(x),1);
a = [xi yi];
in = inpolygon(a(:,1),a(:,2),(dat{t,1}(:,1))*gridcm,(dat{t,1}(:,2))*gridcm);
gridIni= zeros(gridcm*(tamx),gridcm*(tamy));
grid{t,1} = zeros(gridcm*(tamx),gridcm*(tamy));
grid{t,1} = grid{t,1} + gridIni(a(t,1),a(t,2)) + in(t);
end
parfor i=1:length(grid)
mat = cell2mat(grid{i,1});
grid2 = grid2+mat;
end
toc

Accepted Answer

Edric Ellis
Edric Ellis on 29 Nov 2016
I tried replacing your parfor loop with a for loop, and got an indexing error on the line
grid{t,1} = grid{t,1} + gridIni(a(t,1),a(t,2)) + in(t);
When I tried, the variable a had some zeros in it, which was causing an indexing problem...
  2 Comments
Juliana
Juliana on 29 Nov 2016
So, trying to solve the error you noticed, I got the whole thing working with parfor. However, it takes almost 3 x the time than using for loops. Any tips on this?
dat = cell(50000,1); gridcm = 5; tamx = 50; tamy = 60;
grid = cell(50000,1);
tic
parfor i=1:size(dat,1) dat{i,1} = randi([0 60],5,2); dat{i,1} = [dat{i,1};dat{i,1}(1,:)]; grid{i,1} = zeros(gridcm*(tamx),gridcm*(tamy)); end
parfor t = 1:size(dat,1)
minx = floor(gridcm*min(dat{t,1}(:,1)));
if minx == 0
minx = 1;
end
maxx = ceil(gridcm*max(dat{t,1}(:,1)));
if maxx > 250
maxx = 250;
end
miny = floor(gridcm*min(dat{t,1}(:,2)));
if miny == 0
miny = 1;
end
maxy = ceil(gridcm*max(dat{t,1}(:,2)));
if maxy > 300
maxy = 300;
end
x = minx:maxx;
y = miny:maxy;
xi = repmat(x,length(y),1);
xi = reshape(xi,length(x)*length(y),1);
yi = repmat(y,length(x),1)';
yi = reshape(yi,length(y)*length(x),1);
a = [xi yi];
in = inpolygon(a(:,1),a(:,2),(dat{t,1}(:,1))*gridcm,(dat{t,1}(:,2))*gridcm);
gridIni= zeros(gridcm*(tamx),gridcm*(tamy));
grid{t,1} = zeros(gridcm*(tamx),gridcm*(tamy));
grid{t,1} = grid{t,1} + gridIni(a(t,1),a(t,2)) + in(t);
end
grid2 = zeros(gridcm*(tamx),gridcm*(tamy));
parfor i=1:length(grid) mat = cell2mat(grid(i,1)); grid2 = grid2+mat;
end toc
Edric Ellis
Edric Ellis on 5 Dec 2016
There are several reasons why parfor doesn't always offer speedup. Note that running in parfor always incurs some overhead, even if you open the parallel pool up front. The first reason is that MATLAB itself is intrinsically multithreaded for many operations. If the thing you're trying to run in parfor is already multithreaded by MATLAB (and you're using the "local" cluster), then the parfor overheads will mean that things slow down. The second most common reason is that if your parfor loop requires a large amount of data transfer, this can slow things down too. You can use ticBytes and tocBytes to work out how much data is being transferred.

Sign in to comment.

More Answers (0)

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!