April
April

Reputation: 39

Insert into host_vector using thrust

I'm trying to insert one value into the third location in a host_vector using thrust.

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

But am getting errors:

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 *>>

Has anyone seen this before, and how can I solve this? I want to use a vector to pass information into the GPU. I was originally trying to use a vector of vectors to represent spatial cells that hold different numbers of data, but learned that wasn't possible with thrust. So instead, I'm using a vector bins that holds my data, sorted by the spatial cell (first 3 values might correspond to the first cell, the next 2 to the second cell, the next 0 to the third cell, etc.). The values held are pointers to particles, and represent the numbers of particles in the spatial cell (which is not known before runtime).

Upvotes: 0

Views: 1049

Answers (1)

talonmies
talonmies

Reputation: 72372

As noted in comments, thrust::host_vector is modelled directly on std::vector and the operation you are trying to use requires an iterator for the position argument, which is why you get a compilation error. You can see this if you consult the relevant documentation:

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

A complete working example of the code snippet you showed would look like this:

#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;
}

Note that this requires that C++11 language features are enabled in nvcc (so use 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

Upvotes: 2

Related Questions