我只是尝试在我的cuda代码中添加trust :: sort,但是nvcc告诉我:
type_traits.h(322): error C2660: 'test' : function does not take 1 arguments
type_traits.h(322): error C2866:'thrust::detail::tt_detail::is_convertible_sfinae<From,To>::value' : a const static data member of a managed type must be initialized at the point of declaration
type_traits.h(355): error C2057: expected constant expressiontype_traits.h(363): error C2975: '__v' : invalid template argument for 'thrust::detail::integral_constant', expected compile-time constant expression
type_traits.h(363): error C2975: '__v' : invalid template argument for 'thrust::detail::integral_constant', expected compile-time constant expression
我已经搜索过了,但似乎没有人像我一样得到同样的问题
我的代码中有关推力的部分:
#include <thrust\sort.h>
struct prepare_struct
{
float xp;
float yp;
float zp;
float xs;
float ys;
float zs;
float sep;
int idSrc_idEve;
};
int compare_sort(prepare_struct &a, prepare_struct &b){ return a.sep > b.sep;}
void func(...){
...
prepare_struct* sPos_d;
checkCudaErrors( cudaMalloc((void**)&sPos_d, n*sizeof(prepare_struct) ) );
//a kernel that will fill sPos_d
thrust::sort(sPos_d, sPos_d + n, compare_sort);
...
}
如果我删除thrust :: sort(),它可以编译而没有错误
我尝试过push :: device_vector,但它会得到同样的错误
而且raw_pointer_cast()也会得到相同的错误消息
这是推力或nvcc内的错误吗?
或我的代码中有什么问题?
环境:
win7 x64 vs 2010 cuda 5.0 sm_20
device_vector版本:
#include <thrust/device_vector.h>
void func(...){
...
thrust::device_vector<prepare_struct> sPos_dv(n_src_sta);
prepare_struct* sPos_d = thrust::raw_pointer_cast(sPos_dv.data());
//a kernel that will fill sPos_d
thrust::sort(sPos_dv.begin(),sPos_dv.end(),compare_sort);
...
}
答案 0 :(得分:1)
在Stack Overflow上撰写帖子时,请确保提供 Short, Self Contained, Correct (Compilable), Example (又名SSCCE)。这将使其他试图帮助您的成员的生活更轻松,并且它还可以帮助您找到代码中的实际错误。如果您的代码不可编译,请提供演示编译问题的示例。
至于你的计划,必须有一些你没有告诉我们的事情。你试图实现的目标很容易被Thrust处理,没有任何错误。这是您的程序的修改(和完成)版本:
#include <thrust/sort.h>
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
struct prepare_struct
{
float xp;
float yp;
float zp;
float xs;
float ys;
float zs;
float sep;
int idSrc_idEve;
};
struct prepare_struct_compare {
__host__ __device__
bool operator() (const prepare_struct& a, const prepare_struct& b)
{
return a.sep < b.sep;
}
};
void initialize(thrust::host_vector<prepare_struct>& v)
{
for(size_t i = 0; i < v.size(); i++)
v[i].sep = v.size() - i;
}
void print(const thrust::host_vector<prepare_struct>& v)
{
for(size_t i = 0; i < v.size(); i++)
std::cout << " " << v[i].sep;
std::cout << "\n";
}
int main ()
{
const int n = 10;
// Initialize vector of prepare_struct on host
thrust::host_vector<prepare_struct> vec_h(n);
initialize(vec_h);
std::cout << "Initial vector:" << std::endl;
print(vec_h);
// Copy vector to device
thrust::device_vector<prepare_struct> vec_d = vec_h;
// Sort on device
thrust::sort (vec_d.begin(), vec_d.end(), prepare_struct_compare());
// Copy result back to host
thrust::host_vector<prepare_struct> res_h = vec_d;
std::cout << "Final vector:" << std::endl;
print(res_h);
}
运行此程序会给我们:
Initial vector:
10 9 8 7 6 5 4 3 2 1
Final vector:
1 2 3 4 5 6 7 8 9 10