accelerate 3D matrix multiptication using bsxfun
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi
I have athe following code
it takes forever to run, and disable my CPU in the prosess...
I'm looking for a way to make it run better (GPU is also an option)
norm_mode is 256x256 double matrix (X,Y) image
fields is 16384x15 complex double matrix (time,mode)
in the end' I need a 3D matrix take each norm_mode image will also be a function for time and suming all the modes- 3D matrix 256x256x16384
that is the total_field
do you have any sugestions on how to make it run better?
Thanks,
Barak
function total_field = BuildSpatialField(fields,fiber, sim, others)
total_field = zeros( newSize, newSize, length(others.t) ); % total_field(X,Y,t)
h = waitbar(0, 'calculate field...');
for ii=1:others.modes
norm_mode = % load new phi
tmp = bsxfun(@times, norm_mode, reshape(fields(:,ii),1,1,[]));
total_field = total_field + tmp;
waitbar(ii/others.modes, h, ['mode ' num2str(ii) ' from ' num2str(others.modes)]);
end
close(h);
end
댓글 수: 0
답변 (1개)
Viktor
2024년 7월 6일
256x256x16384 as double array needs around 8 Gb RAM and as complex double 16 Gb. Do you have that much memory? Matlab can take forever to tell you, it ran out of memory.
댓글 수: 5
Viktor
2024년 7월 6일
편집: Walter Roberson
2024년 7월 6일
Thanks for the hint.
There is no performance difference accessing single elements in 1xn, nx1 or 1x1xn vectors.
I think i came to the conclusion since matlab uses 1xn by default.
But i found out that extracting a column vector from a matrix is faster than extracting a row vector.
Walter Roberson
2024년 7월 6일
As i understand bsxfun(@times, norm_mode, fields), it is the same as: norm_mode .* fields.
Not exactly
- bxsfun is not supported for as many datatypes as implicit expansion is supported
- historically it has been mixed as to whether bxsfun or implicit expansion is more efficient; there have been cases where bxsfun is notably more efficient, and there have been cases where implicit expansion is a bit more efficient.
So, the internal implementation is different between the two cases.
Mathworks is not putting effort into optimizing bxsfun, but is putting effort into optimizing implicit expansion.
참고 항목
카테고리
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!