【发布时间】:2019-09-20 21:19:54
【问题描述】:
我正在尝试编写一个数学模型,它涉及在数值网格上计算特定数量数千次,其中一些模型参数会发生变化。目前,这太慢了,我正在寻找有关矢量化模型中最密集部分的建议。
为了便于阅读,我目前已经有了它的基本实现,但现在想尽可能对下面的整个代码段进行矢量化。代码段的最小示例是:
% Setup grid to evaluate and results vector
T_max = 10000;
eval_points = linspace(0, T_max, 1000);
results = zeros(size(eval_points));
% Function that is used in computation
Z_func = @(x, omega) (1./(omega.*sqrt(2*pi))).*exp( -(x.^2)./(2.*omega.*omega) );
% Random data for now, known in full problem
historic_weights = rand(1,100);
historic_times = rand(1,100);
% Fixed single parameter omega
omega = 0.5;
% Time evaluation
tic()
for eval_counter = 1:size(eval_points,2)
for historic_counter = 1:size(historic_weights,2)
temp_result = 0;
for k = 0:1:T_max
temp_result = temp_result + Z_func( eval_points(eval_counter) - historic_times(historic_counter) + 1440*floor(historic_times(historic_counter)/1440) - 1440*k, omega );
end % End of looping over k
results(eval_counter) = results(eval_counter) + historic_weights(historic_counter)*temp_result;
end % End of looping over weights
end % End of looping over evaluation points
toc()
在我的电脑上,评估只用了 60 多秒。我不希望使用并行工具箱,因为我已经在其他地方使用过,并且显示的代码段会在每个进程上调用。
如果这在 Matlab 中是不可能的,我很高兴也可以在 python 中尝试。
【问题讨论】:
标签: matlab performance vectorization