具有整数参数的CUDA pow函数 [英] CUDA pow function with integer arguments

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

问题描述

我是CUDA的新用户,无法理解我做错了什么。

I'm new in CUDA, and cannot understand what I'm doing wrong.

我试图计算它在数组中具有id的对象的距离,数组中的轴x和数组中的轴y为每个对象寻找邻居

I'm trying to calculate the distance of object it has id in array, axis x in array and axis y in array to find neighbors for each object

__global__ 
void dist(int *id_d, int *x_d, int *y_d, 
              int *dist_dev, int dimBlock, int i)
{
    int idx = threadIdx.x + blockIdx.x*blockDim.x;

    while(idx < dimBlock){
        int i;
        for(i= 0; i< dimBlock; i++){
            if (idx == i)continue;
            dist_dev[idx] = pow(x_d[idx] - x_d[i], 2) + pow(y_d[idx] - y_d[i], 2); // error here
        }
    }
}

code> pow 在内核代码中未定义

Is pow not defined in kernel code?

推荐答案

code> pow 是在CUDA math API中定义的(请参阅 here ),它不是专用于整数参数的模板,即。没有这样的版本:

Your problem is that while pow is defined in the CUDA math API (see here), it is not template specialised for integer arguments, ie. there is no version like this:

__device__ ​ int pow ( int  x, int  y ) 

这是为什么你会得到一个错误。您将需要显式地将基本参数强制转换为浮点类型,如下所示:

This is why you are getting an error. You will need to explicitly cast the base argument to a floating point type like this:

dist_dev[idx] = pow((double)(x_d[idx] - x_d[i]), 2.0) + 
                    pow((double)(y_d[idx] - y_d[i]), 2.0); 

说的话,在你的示例中使用双精度浮点指数对于整数平方将很差效率的观点。最好使用整数乘法代替执行计算:

Having said that, using double precision floating point exponential in your example for a integer square will be poor from an efficiency point of view. It would be preferable to perform the calculation using integer multiplication instead:

int dx = x_d[idx] - x_d[i];
int dy = y_d[idx] - y_d[i];
dist_dev[idx] = (dx * dx) + (dy * dy); 

这篇关于具有整数参数的CUDA pow函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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