【发布时间】:2016-10-24 16:42:41
【问题描述】:
我想做以下事情:
#include <thrust/tuple.h>
#include <tuple>
template<typename... Args>
void someFunction(void (*fp)(Args...), thrust::tuple<Args...> params) {
}
void otherFunction(int n) {
}
int main(int argc, char **argv) {
//// template argument deduction/substitution failed ////
someFunction<int>(&otherFunction, thrust::make_tuple(1));
return 0;
}
我尝试过的:
- 删除这两个参数中的一个当然会得到一个可行的解决方案。
- 当我使用模板参数在
struct中将someFunction设为静态函数时,它可以工作。但是在原始代码中someFunction是一个CUDA 内核,所以我不能这样做。还有什么想法吗? - 当我将thrust::tuple 更改为std::tuple 时它可以工作。有没有办法从 std::tuple 构造一个推力::tuple?
编辑:
为了更清楚:someFunction 和 otherFunction 是 __global__!
#include <thrust/tuple.h>
#include <tuple>
template<typename... Args>
__global__ void someFunction(void (*fp)(Args...), thrust::tuple<Args...> params) {
}
__global__ void otherFunction(int n) {
}
__constant__ void (*kfp)(int) = &otherFunction;
int testPassMain(int argc, char **argv) {
void (*h_kfp)(int);
cudaMemcpyFromSymbol(&h_kfp, kfp, sizeof(void *), 0, cudaMemcpyDeviceToHost);
someFunction<int><<<1,1>>>(h_kfp, thrust::make_tuple(1));
return 0;
}
我得到一个编译器错误:template argument deduction/substitution failed 在两个示例中。
【问题讨论】:
-
也许 与您的问题无关,但您可能会从 all standard algorithm functions 那里得到一个提示,该提示将“谓词”作为参数。他们并不真正关心函数的参数,他们只为函数提供了一个
typename模板参数。 -
如果
someFunction是一个 CUDA 内核(即一个__global__函数),为什么你没有在你的例子中配置它(在启动时)或相应地装饰它?在我看来,这个问题很不清楚。otherFunction是否可以从__global__函数调用?如果是这样,你为什么不相应地装饰它?您不能在主机代码中获取设备函数的地址,这似乎是您在这里所做的(即使您用__device__装饰了otherFunction,它仍然不会像写的那样工作) -
问题不在于从内核函数指针调用内核。我把这部分省略了,因为它有效。这是关于将带有可变参数模板的两个参数传递给全局函数时出现编译器错误。
标签: c++ c++11 cuda variadic-templates thrust