matlab if语句与CUDA [英] matlab if statements with CUDA

查看:186
本文介绍了matlab if语句与CUDA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下matlab代码:

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

tic
g = 0;
for i = 1:N

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

end
toc

secondrandarray = rand(N,1);
g = 0;

tic
for i = 1:N

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

end
toc



已用时间为0.221710秒。
已用时间为0.000012秒。

1)为什么if子句在GPU上这么慢?



2)我该如何解决这个限制?



感谢

解决方案

这通常是一件坏事,无论你在cpu或gpu。 / p>

以下是执行您正在查看的操作的好方法。

  N = 1000; 
randarray = gpuArray(100 * rand(N,1));
tic
g = nnz(randarray> 10);
toc



我没有PCT,我无法验证这是否真的有效



但是,如果您有 Jacket ,您一定能够做到以下。

  N = 1000; 
randarray = gdouble(100 * rand(N,1));
tic
g = nnz(randarray> 10);
toc

全面披露:我是开发Jacket的工程师之一。


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

secondrandarray = rand(N,1);
 g=0;

 tic 
for i=1:N

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

end
toc



Elapsed time is 0.221710 seconds.
Elapsed time is 0.000012 seconds.

1) Why is the if clause so slow on the GPU? It is slowing down all my attempts at optimisation

2) What can I do to get around this limitation?

Thanks

解决方案

This is typically a bad thing to do no matter if you are doing it on the cpu or the gpu.

The following would be a good way to do the operation you are looking at.

N = 1000;
randarray = gpuArray(100 * rand(N,1));
tic
g = nnz(randarray > 10);
toc

I do not have PCT and I can not verify if this actually works (number of functions supported on GPU are fairly limited).

However if you had Jacket, you would definitely be able to do the following.

N = 1000;
randarray = gdouble(100 * rand(N, 1));
tic
g = nnz(randarray > 10);
toc

Full disclosure: I am one of the engineers developing Jacket.

这篇关于matlab if语句与CUDA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆