accelerating a code via parfor
이전 댓글 표시
Is there any ways to speed up the code below? It is too slow on my computer? When I type in parpool, is that enough to use all the cores of my i5 CPU for calculating this code or must I insert a parfor code into my code below. If so, how do I insert a parfor code in the code below? Thanks
for x = 1:100
for y = 1:61
b = abs(bsxfun(@minus,q{x,y},kevin))
averagesAcrossColumns = mean(b, 2);
b = abs(bsxfun(@minus,b,averagesAcrossColumns));
b = sum(b, 2);
s{x,y} = b;
end
end
댓글 수: 5
Henrik
2014년 12월 7일
Can you give some more information? What are the dimensions of q and kevin? Maybe give a full working example of your code to play with?
It might be possible to do some other optimizations.
I'm not too good on making parfor run, but try changing the inner loop to parfor and see if MATLAB complains.
AA
2014년 12월 7일
AA
2014년 12월 7일
Sean de Wolski
2014년 12월 8일
s is preallocated right?
Kevin is a 1x60 matrix and q is a 100x 61 cell array.
The memory consumed by a 100x61 cell array, each containing a double precision matrix of size 1e6 x 60 is about 2.6 TB! The only way you could be storing such a matrix on any normal computer is if some of the q{x,y} are shared duplicates of each other. If that's true, you could speed things up by getting rid of the duplicates.
답변 (2개)
There is no need to have a double loop. See if the following improves things. If not, tell us more about the contents of each q{x,y}, in particular how many columns it has.
s=cell(size(q));
parfor i=1:numel(q)
b = abs(bsxfun(@minus,q{i},kevin))
averagesAcrossColumns = mean(b, 2);
b = abs(bsxfun(@minus,b,averagesAcrossColumns));
b = sum(b, 2);
s{i} = b;
end
Matt J
2014년 12월 8일
If all q{x,y} have the same number of columns, you don't need a loop at all,
b = abs(bsxfun(@minus,cat(3,q{:}),kevin))
averagesAcrossColumns = mean(b, 2);
b = abs(bsxfun(@minus,b,averagesAcrossColumns));
b = sum(b, 2);
s=reshape(num2cell(b,1),size(q));
댓글 수: 6
AA
2014년 12월 8일
Matt J
2014년 12월 9일
AA Commented:
It doesnt work. If I was to buy a graphics card supporting cuda how much faster would my parfor command become? Right now I need more than an hour to run this code because the cell array has matrices with millions of columns.
Graphics cards have no effect on parfor. You could use gpuArray to speed things up, but first you need to explain the 2.6 TB RAM that your q array is consuming (see My Comment).
If you pad the rows of each q(x,y) so that they are all the same size, the code should work. The following test version (with fewer rows than you mention) runs for me in about 7 sec.
q(1:100,1:61)={rand(1e3,60)};
kevin=rand(1,60);
tic;
b = abs(bsxfun(@minus,cat(3,q{:}),kevin));
averagesAcrossColumns = mean(b, 2);
b = abs(bsxfun(@minus,b,averagesAcrossColumns));
b = sum(b, 2);
s=reshape(num2cell(b,1),size(q));
toc
AA
2014년 12월 9일
You said in your comment here "Each cell in q consists of matrices with million rows". If that's true, then q must contain a minimum of 1e6*60*100*61 values. In doubles, that is 2.6TB. So, you should correct that comment so we understand what's really going on.
Anyway, you haven't commented on my other Answer (with parfor). You also haven't commented on the possibility of padding all q(x,y) to have the same number of rows.
A GPU solution is starting to sound doubtful. You will need lots of RAM (<5GB) on the card and it will take a long time to transmit the data to the card.
AA
2014년 12월 9일
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!