Increase amount of processor- and RAM used by MATLAB (parfor)
3 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Matt J
on 8 Oct 2012
Edited: Matt J
on 8 Oct 2012
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 Comments
Matt J
on 10 Oct 2012
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?
More Answers (2)
Bradley Steel
on 8 Oct 2012
Edited: Bradley Steel
on 8 Oct 2012
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 Comments
Image Analyst
on 8 Oct 2012
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.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!