Why doesn't MATLAB support 'cpuArray' for some array creation functions?
2 次查看(过去 30 天)
显示 更早的评论
For example, we can create a GPU array by code:
rand(3, 'single', 'gpuArray')
Unfortunately, this code will fail if it is run on a PC without a CUDA graphics card. If we can pass 'cpuArray' to rand, we can easily write a MATLAB program for both CPU and GPU computings. E.g.,
useGpu = 0
if(useGpu)
arrayType = 'gpuArray';
else
arrayType = 'cpuArray';
end
% The program ...
A = rand(3, 'single', arrayType);
B = rand(3, 'single', arrayType);
C = A*B;
(Unfortunately, this example doesn't work for current MATLAB...)
Otherwise, we have to write this code:
useGpu = 0
if(useGpu)
A = rand(3, 'single', 'gpuArray');
B = rand(3, 'single', 'gpuArray');
else
A = rand(3, 'single');
B = rand(3, 'single');
end
C = A*B;
Could MathWorks add 'cpuArray' to most array creation functions in the future?
0 个评论
采纳的回答
Edric Ellis
2018-1-17
Another workaround is to use a cell array.
if useGpu
arg = {'gpuArray'};
else
arg = {};
end
A = rand(3, 'single', arg{:});
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 GPU Computing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!