c++ - 两个几乎相同的调用,一个有效,一个失败

标签 c++ templates cuda

我有这些模板函数可以在带有 cuda 的设备上内联使用

template <class T> __device__ inline T& cmin(T&a,T&b){return (a<b)?(a):(b);};
template <class T> __device__ inline T& cmax(T&a,T&b){return (a>b)?(a):(b);};

在我的代码中

cmin(z[i],y[j])-cmax(x[i],z[j])

对于 int 数组 x、y 和 z。我收到错误:

error: no instance of function template "cmax" matches the argument list

      argument types are: (int, int)

我收到 cmax 错误,但没有收到 cmin 错误。如果我用

替换 cmax 行
#define cmax(a,b) ((a)>(b))?(a):(b)

工作得很好,但我不想要#defines,它们有问题。这到底是怎么回事?

编辑: 这是完整的调用函数。 times 是 typedef int。

__global__ void compute_integral_y_isums(times * admit, times * discharge, times * Mx, times * isums, ar_size N){
    // computes the sums for each j
    // blocks on j,
    // threads on i since we will be summing over i.
    // sumation over J should be handled by either a different kernel or on the cpu.
    ar_index tid = threadIdx.x;
    ar_index i = blockIdx.x;                       // summing for patient i 
    ar_index j = threadIdx.x; // across other patients j
    __shared__ times cache[threadsPerBlock];

  times Iy = 0;
    while(j<N){
        // R code:  max(0,min(Mx[i],d3[j,'Discharge.time'])-max(d3[i,'Admission.time'],Mx[j]))
        times imin = cmin(Mx[i],discharge[j]);
        times imax = cmax(admit[i],Mx[j]);
        Iy += cmax(0,imin-imax);
        j += blockDim.x;
    }
    cache[tid] = Iy;

    __syncthreads(); 
    // reduce 
    /***REMOVED***/
}

最佳答案

Iy += cmax(0,imin-imax);

不合法。您不能将文字 0 绑定(bind)到 int& 引用(但可以绑定(bind)到 const int& 引用)。

关于c++ - 两个几乎相同的调用,一个有效,一个失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4054458/

相关文章:

templates - 如何限制 Mercurial 日志的大小?

c - 我只看到主机的 "world hello"而不是设备

cuda - 如何使用CUDA 8.0 nvprof剖析OpenCL应用程序

c++ - CMake 找不到 CUDA : "Could not find cmake module file: CMakeDetermineCUDACompiler.cmake"

c++ - C++ 中的基本信号处理

c++ - 使用自定义转换运算符向上转换到模板类

c++ - 成员右值引用和对象生命周期

c++ - 当文本位于编辑控件中时,GetWindowTextLength 返回 0

java - 此代码出现运行时错误与编译错误的原因

c++ - 我在这里误用了模板吗?