2017-03-06 54 views
0

我试图插入一个值到第三的位置,使用推力host_vector。插入host_vector使用推力

static thrust::host_vector <int *> bins; int * p; bins.insert(3, 1, p);

但我得到的错误:

error: no instance of overloaded function "thrust::host_vector<T, Alloc>::insert [with T=int *, Alloc=std::allocator<int *>]" matches the argument list argument types are: (int, int, int *) object type is: thrust::host_vector<int *, std::allocator<int *>>

有没有人见过这个,我怎么能解决这个问题?我想使用矢量将信息传递给GPU。我本来试图用向量的向量来表示数据持有不同数量的空间细胞,但据悉,这是不可能的推力。所以不是,我使用的保持我的数据,由空间细胞排序的矢量bins(第一3个值可能对应于所述第一小区,在接下来的2到第二小区时,在下一个0至第三小区等) 。保持的值是指向颗粒,并且表示在空间细胞粒子数(没有在运行时间之前已知的)。

+1

如果更换'通过你的代码不能正常工作st :: host_vector'与'std :: vector',所以我不知道为什么你认为它会与推力工作。第一个参数应该是一个迭代器。所以,如果你改变'bins.insert(3,1,P);''来bins.insert(bins.begin()+ 3,1,P);'我认为它应该编译你。您可能想研究标准向量插入[documentation](http://www.cplusplus.com/reference/vector/vector/insert/)。 –

+1

顺便说一句,我想你会发现指针,而难以奏效的一个载体,但不是你问的问题。而不是一个指向粒子的向量,只是使用一个粒子向量,这是我的建议。 –

回答

1

正如在评论中指出,thrust::host_vector直接仿照std::vector和您要使用的操作需要的位置参数,这就是为什么你会得到一个编译错误的迭代。你可以看到这一点,如果你咨询相关文件:

http://en.cppreference.com/w/cpp/container/vector/insert https://thrust.github.io/doc/classthrust_1_1host__vector.html#a9bb7c8e26ee8c10c5721b584081ae065

代码的完整工作示例代码片段你显示应该是这样的:

#include <iostream> 
#include <thrust/host_vector.h> 

int main() 
{ 
    thrust::host_vector <int *> bins(10, reinterpret_cast<int *>(0)); 
    int * p = reinterpret_cast<int *>(0xdeadbeef); 
    bins.insert(bins.begin()+3, 1, p); 

    auto it = bins.begin(); 
    for(int i=0; it != bins.end(); ++it, i++) { 
     int* v = *it; 
     std::cout << i << " " << v << std::endl; 
    } 
    return 0; 
} 

注意,这需要将C++ 11个的语言功能在启用NVCC(因此使用CUDA 8.0):

~/SO$ nvcc -std=c++11 -arch=sm_52 thrust_insert.cu 
~/SO$ ./a.out 
0 0 
1 0 
2 0 
3 0xdeadbeef 
4 0 
5 0 
6 0 
7 0 
8 0 
9 0 
10 0