Increase amount of processor- and RAM used by MATLAB (parfor)
조회 수: 8 (최근 30일)
이전 댓글 표시
I'm running big calculations and simulations on a powerful computer (8 i7-cores and 12 GB RAM). But for some reason it only uses 10-13% of both RAM and CPU power. How can I increase this?
I suppose the limitation on the CPU is because it only uses 1 of the cores.
If that is the case, I suppose I should use parfor. But since I need to evaluate evaluate a 7-dimensional relation, it is not too straightforward.
An example of the kind of code it has to execute is:
for i=1:length(a)
for j=1:length(b)
for ii=1:length(c)
for jj=1:length(d)
E(i,j,ii,jj) = a(i)^2 * b(j) + c(ii) * d(jj) ^2 + a(i) * c(ii);
end
end
end
end
Can anybody help me out here? Just replacing one of the 'for' with 'parfor' does unfortunately not do the trick.
Thanks
댓글 수: 0
채택된 답변
Matt J
2012년 10월 8일
편집: Matt J
2012년 10월 8일
Vectorizing might help, although this looks like it could be a huge matrix, and therefore difficult not only to compute fast, but to store.
aterms=a(:);
bterms=b(:).';
cterms=reshape(c,1,1,[]);
dterms=reshape(d.^2,1,1,1,[]);
E1=bsxfun(@times,aterms.^2,bterms);
E2=bsxfun(@times,aterms,cterms);
E3=bsxfun(@times,dterms,cterms);
E=bsxfun(@plus,E1,E2);
E=bsxfun(@plus,E,E3);
댓글 수: 5
Matt J
2012년 10월 10일
I need to map the behavior of the energy at the different angles as a function of these 5 tunable parameters.
OK, but map them for what purpose? For visualization? How are you going to make a 4D plot?
추가 답변 (2개)
Bradley Steel
2012년 10월 8일
편집: Bradley Steel
2012년 10월 8일
There are multiple ways to improve this; I'm not certain if you're already doing this, so two improvements without parallelisation:
- preallocate space for EOR
- vectorise the expressions where possible, eg:
A=repmat(reshape(a,[],1,1,1),[1 length(b) length(c) length(d)]);
B=repmat(reshape(b,1,[],1,1),[length(a) 1 length(c) length(d)]);
C=repmat(reshape(c,1,1,[],1),[length(a) length(b) 1 length(d)]);
D=repmat(reshape(d,1,1,1,[]),[length(a) length(b) length(c) 1]);
E2 = A.^2.*B + C.*D.^2 + A.*C;
Within the parallel toolbox, turning the inner loop into a forloop should run, but is likely to be slower due to memory overhead. The one you want to parellise is probably the outermost loop, but as set it won't be run because MATLAB doesn't know what values j,ii,jj hold when it creates the forloop. An alternative would be:
E=struct('x',[]);
parfor i=1:length(a)
E(i).x = zeros(length(b),length(c),length(d));
for j=1:length(b)
for ii=1:length(c)
for jj=1:length(d)
E(i).x(j,ii,jj) = a(i)^2 * b(j) + c(ii) * d(jj) ^2 + a(i) * c(ii);
end
end
end
end
You then need to turn the structure E back into the matrix you need. Without testing I would expect this to still be substantially slower than the vectorised version above, although it may be you have some cases which you cannot vectorise.
댓글 수: 2
Image Analyst
2012년 10월 8일
If you have a matrix E(dim1, dim2,...) I believe it iterates dim1 first, then increments dim 2, etc. We know that MATLAB goes down rows (the first column in a 2D matrix) in the first column, before it moves over to the next column to go down rows in that column. So if you have large matrices, you might get some speedup by inverting the order of your loops so that dim1 is the inner most loop, dim2 is the next inner loop, etc. Might be worth a try to see if it makes it faster.
for jj=1:length(d)
for ii=1:length(c)
for j=1:length(b)
for i=1:length(a)
E(i,j,ii,jj) = a(i)^2 * b(j) + c(ii) * d(jj) ^2 + a(i) * c(ii);
end
end
end
end
Another thing to try is to increase your priority of MATLAB via the task list (type control-shift-Escape to bring up the task list, right-click on MATLAB process), though this might not help if you have lots of idle time and no other program is competing for CPU time.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!