两个几乎相同的调用,一个有效,一个失败
我有这些模板函数,可以在带有 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 的 。我收到错误:
错误:没有函数模板“cmax”的实例与参数列表匹配
参数类型为:(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***/
}
I have these template functions for use inline on device with 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);};
In the code I have
cmin(z[i],y[j])-cmax(x[i],z[j])
for int arrays x,y,and z. I get the error:
error: no instance of function template "cmax" matches the argument list
argument types are: (int, int)
I get the error for cmax but not cmin. If I replace the cmax line with
#define cmax(a,b) ((a)>(b))?(a):(b)
that works just fine, but I don't want #defines, they are problematic. What the heck is going on here?
EDIT:
here is the full calling function. times is 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***/
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不合法。您无法将文字
0
绑定到int&
引用(但可以绑定到const int&
引用)。is not legal. You can't bind the literal
0
to anint&
reference (but you can to aconst int&
reference).如果
x
或z
是一个const
数组,它们的元素类型将为const int
,这是不可转换的到int&
。尝试使用:
如果
T
始终是像int
这样的原始类型,您甚至可以按值传递参数:编辑: @aschepler 有权回答。
If either
x
orz
is aconst
array, their element type will beconst int
, which is not convertible toint&
.Try with:
If
T
is always a primitive type likeint
, you can even pass the parameters by value:EDIT: @aschepler has the right answer.
如果您的函数也采用引用作为参数,则在返回引用时应小心。您可能会返回对临时对象的引用!就像这样:
这对于 int 和 float 来说可能没问题,但对于非 POD 来说是危险的。
You should be careful in returning a reference, if your functions also take references as arguments. You might return a reference to a temporary! Like in:
which is probably ok for
int
andfloat
, but dangerous for non-PODs.请尝试颠倒定义顺序。
cmax 然后 cmmin。那么输出是什么?
Try to reverse the definition order please.
cmax then cmin. What is the outpout then ?
您的 cmax 和 cmin 对元素进行非常量引用。也许你的数组被声明为 const ?
很难说,因为示例不完整。
Your cmax and cmin are taking non-const reference to the elements. Maybe your arrays are declared as const?
Hard to tell, because the example is not complete.
也许您已经有一些活动的
定义
,它们会污染您的命名空间?尝试重命名cmin
和cmax
,或#undef cmin
和#undef cmax。或者运行g++ -E
来查看去宏化的代码。或者添加
::
命名空间说明符:无论如何,你只需要define中的所有
()
即可。更好:而且您可能也不需要模板函数的内联。
Maybe you already have some active
defines
which pollute you namespace?Try renamingcmin
andcmax
, or#undef cmin
and#undef cmax
. Or rung++ -E
to see the de-macrofied code.Or add
::
namespace specifier:Anyway, you only need all the
()
in defines. Nicer:And you probably do not need the
inline
for a template function neither.为了澄清我对第一个问题的评论:是的,如果 ona 只是传递来自参数的引用,那么应该小心。这是一个完整的说明:
后面是
输出是:
在某些机器上,我猜它甚至可能段错误。在
Thing
的析构函数中,我将data
重置为0
,但人们很容易想象更多。因此,当我们执行 BAD
refmin
调用时,我们返回对临时对象的引用。它在;
之后被销毁。因此,当我们尝试输出&x
时,它已经消失了。To clarify my comment on the initial question: Yes, one should be careful if ona just passes through a reference from a parameter. Here is a complete illustration:
followed by
The output is:
On some machines it might even segfault, I guess. In
Thing
s destructor I resetdata
to0
, but one could easily imagine more there.So, when we do the BAD
refmin
call, we return a reference to a temporary. Which is destroyed after the;
. So, when we try to output&x
, it's already gone.