Help with using parfor (exceed matrix dimensions error)
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Juliana
am 28 Nov. 2016
Kommentiert: Edric Ellis
am 5 Dez. 2016
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
0 Kommentare
Akzeptierte Antwort
Edric Ellis
am 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 Kommentare
Edric Ellis
am 5 Dez. 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!