accelerate 3D matrix multiptication using bsxfun

2 views (last 30 days)
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

Answers (1)

Viktor
Viktor on 6 Jul 2024
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 Comments
Viktor
Viktor on 6 Jul 2024
Edited: Walter Roberson on 6 Jul 2024
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 on 6 Jul 2024
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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!