accelerate 3D matrix multiptication using bsxfun
显示 更早的评论
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
2024-7-6
0 个投票
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 个评论
barak messica
2024-7-6
As i understand bsxfun(@times, norm_mode, fields), it is the same as: norm_mode .* fields.
To improve the loop you can do several things.
- bring "fields" into the right shape before entering the for loop.
- use the keyword parfor instead of for
- use gpuArray(X)
- waitbar can slow down your loop significantly
Stephen23
2024-7-6
"matlab prefers 1xn vectors rather than 1x1xn or nx1"
Where is this preference documented?
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!