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
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
AA 2014년 12월 7일
Kevin is a 1x60 matrix and q is a 100x 61 cell array. Each cell in q consists of matrices with million rows
AA
AA 2014년 12월 7일
I changed the first line to parfor x = 1:100 and now the calculation is even slower. I thought parfor accelerates a calc on matlab but in my case it slows it down. Plz help.
Sean de Wolski
Sean de Wolski 2014년 12월 8일
s is preallocated right?
Matt J
Matt J 2014년 12월 9일
편집: Matt J 2014년 12월 9일
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개)

Matt J
Matt J 2014년 12월 8일
편집: Matt J 2014년 12월 8일

1 개 추천

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
Matt J 2014년 12월 8일

1 개 추천

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
AA 2014년 12월 8일
Error using cat Dimensions of matrices being concatenated are not consistent.
All columns of the matrices in the cell array q have 60 columns yet I still get the error.
Matt J
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.
Matt J
Matt J 2014년 12월 9일
편집: Matt J 2014년 12월 9일
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
AA 2014년 12월 9일
No, I saved the file and it is only around 5 gigabytes big. The columns of the matrices in the cell array are all the same but the rows of the matrices change even though the rows of the matrices across the same row in the cell array are the same.
Matt J
Matt J 2014년 12월 9일
편집: Matt J 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
AA 2014년 12월 9일
I am sorry for not being able to clarify myself. The original cell array had matrices with a few million rows. Then I reshaped the cell array and now it looks like this (see image)
</matlabcentral/answers/uploaded_files/22327/Untitled.png> The cell array is 61 columns wide and has 100 rows.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

AA
2014년 12월 7일

댓글:

AA
2014년 12월 9일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by