matlab if statements with CUDA

前端 未结 4 1417
鱼传尺愫
鱼传尺愫 2021-01-20 01:59

I have the following matlab code:

randarray = gpuArray(rand(N,1));
N = 1000;

tic
g=0;
for i=1:N

    if randarray(i)>10
        g=g+1;
    end

end
toc

         


        
4条回答
  •  失恋的感觉
    2021-01-20 02:45

    Using MATLAB R2011b and Parallel Computing Toolbox on a now rather old GPU (Tesla C1060), here's what I see:

    >> g = 100*parallel.gpu.GPUArray.rand(1, 1000);
    >> tic, sum(g>10); toc
    Elapsed time is 0.000474 seconds.
    

    Operating on scalar elements of a gpuArray one at a time is always going to be slow, so using the sum method is much quicker.

提交回复
热议问题