gpt4 book ai didi

arrays - 初始化非标准值的 double 组的最快方法

转载 作者:太空宇宙 更新时间:2023-11-03 19:54:23 24 4
gpt4 key购买 nike

MATLAB 提供了用于预分配/初始化具有常见值的数组的函数,例如 01 .但是,如果我们希望数组有一些任意double,有多种方法可以实现,但哪一种更可取并不明显。

这个问题并不新鲜 - 之前曾在 this blog post 等地方讨论过在this answer .然而,经验表明软件(特别是 MATLAB 及其执行引擎)和硬件会随着时间而变化,因此最佳方法可能在不同的系统上会有所不同。不幸的是,上述来源不提供基准测试代码,这可能是回答这个问题的最终(和永恒)方式。

我正在寻找一个我可以运行的基准测试,它会告诉我在我的系统上使用最快的方法,考虑到我可能同时使用“常规”double 各种大小的数组和 gpuArray double 数组。

最佳答案

function allocationBenchmark(arrSz)
if nargin < 1
arrSz = 1000;
end

%% RAM
t = [];
disp('--------------- Allocations in RAM ---------------')
t(end+1) = timeit(@()v1(arrSz), 1);
t(end+1) = timeit(@()v2(arrSz), 1);
t(end+1) = timeit(@()v3(arrSz), 1);
t(end+1) = timeit(@()v4(arrSz), 1);
t(end+1) = timeit(@()v5(arrSz), 1);
t(end+1) = timeit(@()v6(arrSz), 1);
t(end+1) = timeit(@()v7(arrSz), 1);
t = 1E3 * t; % conversion to msec
disp(t); disp(" ");
[~,I] = min(t);
disp("Conclusion: method #" + I + " is the fastest on the CPU!"); disp(" ");

%% VRAM
if gpuDeviceCount == 0, return; end
t = [];
disp('--------------- Allocations in VRAM --------------')
t(end+1) = NaN; % impossible (?) to run v1 on the gpu
t(end+1) = gputimeit(@()v2gpu(arrSz), 1);
t(end+1) = gputimeit(@()v3gpu(arrSz), 1);
t(end+1) = gputimeit(@()v4gpu(arrSz), 1);
t(end+1) = gputimeit(@()v5gpu(arrSz), 1);
t(end+1) = gputimeit(@()v6gpu(arrSz), 1);
t(end+1) = gputimeit(@()v7gpu(arrSz), 1);
t = 1E3 * t; % conversion to msec
disp(t); disp(" ");
[~,I] = min(t);
disp("Conclusion: method #" + I + " is the fastest on the GPU!");

end

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% RAM
function out = v1(M)
% Indexing on the undefined matrix with assignment:
out(1:M, 1:M) = pi;
end

function out = v2(M)
% Indexing on the target value using the `ones` function:
scalar = pi;
out = scalar(ones(M));
end

function out = v3(M)
% Using the `zeros` function with addition:
out = zeros(M, M) + pi;
end

function out = v4(M)
% Using the `repmat` function:
out = repmat(pi, [M, M]);
end

function out = v5(M)
% Using the ones function with multiplication:
out = ones(M) .* pi;
end

function out = v6(M)
% Default initialization with full assignment:
out = zeros(M);
out(:) = pi;
end

function out = v7(M)
% Using the `repelem` function:
out = repelem(pi,M,M);
end

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% VRAM
function out = v2gpu(M)
scalar = gpuArray(pi);
out = scalar(gpuArray.ones(M));
end

function out = v3gpu(M)
out = gpuArray.zeros(M, M) + gpuArray(pi);
end

function out = v4gpu(M)
out = repmat(gpuArray(pi), [M, M]);
end

function out = v5gpu(M)
out = gpuArray.ones(M) .* gpuArray(pi);
end

function out = v6gpu(M)
% Default initialization with full assignment:
out = gpuArray.zeros(M);
out(:) = gpuArray(pi);
end

function out = v7gpu(M)
% Using the `repelem` function:
out = repelem(gpuArray(pi),M,M);
end

运行上面的代码(例如输入 5000)会产生以下结果:

--------------- Allocations in RAM ---------------
110.4832 328.1685 48.7895 47.9652 108.8930 93.0481 47.9037

Conclusion: method #7 is the fastest on the CPU!

--------------- Allocations in VRAM --------------
NaN 37.0322 17.9096 14.2873 17.7377 16.1386 16.6330

Conclusion: method #4 is the fastest on the GPU!

... 它告诉我们在每种情况下使用的最佳(或等效)方法。

关于arrays - 初始化非标准值的 double 组的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54538503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com