Parallel program required to be fixed for last couple of lines due to extensive time-consuming for saving variables in 3 by 3 matrix
1 view (last 30 days)
Show older comments
parfor i=1:TotNu
a=(fy(i)*eye(3))+(Cyabs(i)*([cosa(i);cosb(i);cosc(i)]*[cosa(i) cosb(i) cosc(i)]));
b=a-Czabs(i)*([cosa(i);cosb(i);cosc(i)]*[cosa0(i) cosb0(i) cosc0(i)]);
xsj=(a*SAp(:,i)+b*[0;0;1]);
Ex=[Ex;xsj];
RondF_RondEta=[a RondF_RondEta];
RondF_RondZeta=[b RondF_RondZeta];
issues are with
RondF_RondEta=[a RondF_RondEta];
RondF_RondZeta=[b RondF_RondZeta];
where takes huge amount of time for saving. I tried to slice variable, and use parfor etc. I am looking for a solution to resolve that?
2 Comments
James Tursa
on 29 Jun 2015
Edited: James Tursa
on 29 Jun 2015
How large is TotNu? Is this the only thing you are doing inside the parfor loop?
It looks like this is basically some nD outer products and matrix-vector multiplies which could be done e.g. with the bsxfun and times functions. It will take more memory to store intermediate results (hence my question about TotNu) using the method I have in mind, but may be cleaner and faster overall than the parfor you are currently doing (parfor seems like overkill for what you have shown).
Answers (2)
Thorsten
on 29 Jun 2015
Edited: Thorsten
on 29 Jun 2015
If is more efficient if you allocate Ex, RondF_RondEta and RondF_RondZeta before the loop:
Ex = nan(1, TotNu);
RondF_RondEta = nan(1, TotNu);
RondF_RondZeta = nan(1, TotNu);
for i=1:TotNu
a = (fy(i)*eye(3))+(Cyabs(i)*([cosa(i);cosb(i);cosc(i)]*[cosa(i) cosb(i) cosc(i)]));
b = a-Czabs(i)*([cosa(i);cosb(i);cosc(i)]*[cosa0(i) cosb0(i) cosc0(i)]);
Ex(i) = (a*SAp(:,i)+b*[0;0;1]);
RondF_RondEta(i) = a;
RondF_RondZeta(i) = b;
end
James Tursa
on 29 Jun 2015
E.g., using nD operations without parfor:
cosabc = [cosa(:)'; cosb(:)'; cosc(:)'];
cos0abc = [cosa0(:)'; cosb0(:)'; cosc0(:)'];
a = bsxfun(@times,reshape(cosabc,3,1,TotNu),reshape(cosabc,1,3,TotNu));
a = bsxfun(@times,a,reshape(Cyabs,1,1,TotNu));
a = a + bsxfun(@times,eye(3),reshape(fy,1,1,TotNu));
b = bsxfun(@times,reshape(cosabc,3,1,TotNu),reshape(cos0abc,1,3,TotNu));
b = bsxfun(@times,b,reshape(Czabs,1,1,TotNu));
b = a - b;
Ex = zeros(3,TotNu);
for k=1:TotNu
Ex(:,k) = a(:,:,k) * SAp(:,k) + b(:,3,k); % nD matrix multiply
end
RondF_RondEta = reshape(a(:,:,TotNu:-1:1),3,[]);
RondF_RondZeta = reshape(b(:,:,TotNu:-1:1),3,[]);
Ex = Ex(:);
1 Comment
See Also
Categories
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!