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

查看:52
本文介绍了具有整数参数的 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
        }
    }
}

pow 没有在内核代码中定义吗?

Is pow not defined in kernel code?

推荐答案

您的问题是,虽然 pow 是在 CUDA 数学 API 中定义的(请参阅 这里),它不是专门用于整数参数的模板,即.没有这样的版本:

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天全站免登陆