Question

I am trying to write a custom function to carry out sum. I followed this question Cuda Thrust Custom function to take reference.Here is how I have defined my functor

struct hashElem
{
    int freq;
    int error;
};
//basically this function adds some value to to the error field of each element
struct hashErrorAdd{
    const int error;

    hashErrorAdd(int _error): error(_error){}

    __host__ __device__
    struct hashElem operator()(const hashElem& o1,const int& o2)
    {
            struct hashElem o3;
            o3.freq = o1.freq;
            o3.error = o1.error + (NUM_OF_HASH_TABLE-o2)*error;   //NUM_OF_HASH_TABLE is a constant
            return o3;
    }
};

struct hashElem freqError[SIZE_OF_HASH_TABLE*NUM_OF_HASH_TABLE];
int count[SIZE_OF_HASH_TABLE*NUM_OF_HASH_TABLE];

thrust::device_ptr<struct hashElem> d_freqError(freqError); 
thrust::device_ptr<int> d_count(count);

thrust::transform(thrust::device,d_freqError,d_freqError+new_length,d_count,hashErrorAdd(perThreadLoad)); //new_length is a constant

This code on compilation gives the following error:

error: function "hashErrorAdd::operator()" cannot be called with the given argument list

argument types are: (hashElem)

object type is: hashErrorAdd

Please can anybody explain to me why I am getting this error? and how I can resolve it. Please comment in case I am not able to explain the problem clearly. Thankyou.

Was it helpful?

Solution

It appears that you want to pass two input vectors to thrust::transform and then do an in-place transform (i.e. no output vector is specified).

There is no such incarnation of thrust::transform

Since you have passed:

thrust::transform(vector_first, vector_last, vector_first, operator);

The closest matching prototype is a version of transform that takes one input vector and creates one output vector. In that case, you would need to pass a unary op that takes the input vector type (hashElem) only as an argument, and returns a type appropriate for the output vector, which is int in this case, i.e. as you have written it (not as your intent). Your operator() does not do that, and it cannot be called with the arguments that thrust is expecting to pass to it.

As I see it, you have a couple options:

  1. You could switch to the version of transform that takes two input vectors and produces one output vector, and create a binary op as functor.

  2. You could zip together your two input vectors, and do an in-place transform if that is what you want. Your functor would then be a unary op, but it would take as argument whatever tuple was created from dereferencing the input vector, and it would have to return or modify the same kind of tuple.

As an aside, your method of creating device pointers directly from host arrays looks broken to me. You may wish to review the thrust quick start guide.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top