下面是一个玩具示例,我把它放在一起,探索使用CPU加速执行的parfoor函数。然而,即使在查看了并行文档之后,我仍然对如何升级它以在我的GPU (Nvidia 980ti)上运行感到困惑。
非常感谢任何关于如何更新此代码以在GPU上运行的指示。
干杯。
% toy example--monte carlo estimation of pi using for loops
tic;
N = 1000000000;
hitcounter = 0;
for i = 1:N
x = rand;
y = rand;
if ( y < sqrt(1-x*x) )
hitcounter = hitcounter + 1;
end
end
disp(hitcounter/N*4)
toc;
% toy example--monte carlo estimation of pi using parfor loops
tic;
N = 1000000000;
hitcounter = 0;
parfor i = 1:N
x = rand;
y = rand;
if ( y < sqrt(1-x*x) )
hitcounter = hitcounter + 1;
end
end
disp(hitcounter/N*4)
toc;发布于 2017-08-17 15:45:40
你需要做的主要事情是向量化你的代码--这总是一个好主意,特别是在GPU上。然后,您只需使用rand的尾随参数直接在GPU上构建x和y。
N = 1000000;
x = rand(1, N, 'gpuArray');
y = rand(1, N, 'gpuArray');
pi_est = sum(y < sqrt(1 - x.*x)) / N * 4;请注意,我缩小了N,以使其适合图形处理器。如果你想使用更高的GPU值运行-我建议添加一个外部循环,本质上是以“块”的形式执行计算,以适应N的有限内存。
https://stackoverflow.com/questions/45646470
复制相似问题