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

回答(1 个)

Viktor
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
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
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by