如何在一个方法中两次调用一个函数来编译cuda代码?
我正在尝试编译这段代码:如何在一个方法中两次调用一个函数来编译cuda代码?,cuda,ptx,Cuda,Ptx,我正在尝试编译这段代码: struct foo { unsigned long long x0; }; //__device__ __noinline__ foo bar(foo a, foo b){ // << try this __device__ foo bar(foo a, foo b){ foo r; asm( ".reg .u64 my_cool_var;\n\t" "add.cc.u64 %0,
struct foo {
unsigned long long x0;
};
//__device__ __noinline__ foo bar(foo a, foo b){ // << try this
__device__ foo bar(foo a, foo b){
foo r;
asm(
".reg .u64 my_cool_var;\n\t"
"add.cc.u64 %0, %1, %2;\n\t"
: "=l"(r.x0)
: "l"(a.x0)
"l"(b.x0)
);
return r;
}
__device__ foo func_with2call(foo x, foo y){
foo res = bar(x, y);
foo iy = bar(x, res);
return iy;
}
__global__ void cuda_test1(foo *a, foo *b, foo *r) {
*r = func_with2call(*a, *b);
}
我知道,这是代码内联的问题。例如,如果我使用
\uuu noinline\uuu
属性编译bar
函数,则不会出现错误。有没有一种方法可以保持内联(除了用不同的内部变量名复制bar
函数代码外),但仍然调用bar
函数两次?此限制将在中讨论。您可以通过强制每个定义进入其自己的范围来绕过它,例如:
__device__ foo bar(foo a, foo b){
foo r;
asm(
"{.reg .u64 my_cool_var;\n\t"
"add.cc.u64 %0, %1, %2;\n\t"
"}"
: "=l"(r.x0)
: "l"(a.x0)
"l"(b.x0)
);
return r;
}
这将在没有冲突的情况下安全地进行内联
__device__ foo bar(foo a, foo b){
foo r;
asm(
"{.reg .u64 my_cool_var;\n\t"
"add.cc.u64 %0, %1, %2;\n\t"
"}"
: "=l"(r.x0)
: "l"(a.x0)
"l"(b.x0)
);
return r;
}