MATLAB提供了一些函数,用于使用诸如0
或1
之类的通用值来预分配/初始化数组。但是,如果我们希望数组具有某个任意double
值,则有多种方法可以做到这一点,并且不清楚哪种方法更可取。
这个问题不是新问题-先前在this blog post和this answer等地方进行过讨论。但是,经验表明,软件(特别是MATLAB及其执行引擎)和硬件会随着时间而变化,因此最佳方法可能会在不同系统上有所不同。不幸的是,前面提到的资源没有提供基准代码,这可能是回答这个问题的最终方法(而且是永恒的方法)。
我正在寻找一个可以运行的基准,该基准可以告诉我在系统上使用最快的方法,考虑到我可能同时使用了“常规” double
数组和各种大小的gpuArray double
个数组。
答案 0 :(得分:6)
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!
...告诉我们在每种情况下使用的最佳方法(或等效方法)。