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!