2

I have a 2D vector, and I want to implement transform functor to each element of the 2D vector(every element is a postfix expression which is to be evaluated via different threads)

how is it possible using Cuda thrust? I have written a test code which evaluates sum of each vector but its giving error. Is this the correct approach ?

struct sum_v
{


     __host__ __device__ float operator()(thrust::device_vector<float> v)
     {
        float sum=reduce(v.begin(),v.end());

        return sum;
     }
};

int main(void)
{

    thrust::host_vector<float> h1_vec(3);
    thrust::device_vector<float> vectors[3];

    vectors[0] = thrust::device_vector<float>(3);
    vectors[1] = thrust::device_vector<float>(3);
    vectors[2] = thrust::device_vector<float>(3);
    thrust::sequence(vectors[0].begin(), vectors[0].end());
    thrust::sequence(vectors[1].begin(), vectors[1].end());
    thrust::sequence(vectors[2].begin(), vectors[2].end());
    thrust::device_vector<float> d1_vec(3);
    thrust::transform(vectors.begin(), vectors.end(),d1_vec.begin(),sum_v());
    thrust::copy(d1_vec.begin(), d1_vec.end(), h1_vec.begin());

    return 0;

}
talonmies
  • 70,661
  • 34
  • 192
  • 269
doll
  • 31
  • 5
  • 5
    Thrust doesn't really comprehend arrays of vectors, or vectors of vectors. A typical suggestion is to flatten your array of vectors into a single vector. You will then need to come up with clever strategies to handle the vectors individually when they are in a single vector. For this particular case (sum of vectors) you may want to try adapting the thrust [sum-rows example](https://github.com/thrust/thrust/blob/master/examples/sum_rows.cu), which uses [reduce_by_key](http://thrust.github.io/doc/group__reductions.html#gac1fadcdd29791f655a3144c2e741838a), to see if it will meet your needs. – Robert Crovella Jun 10 '13 at 15:22
  • You may want to try arrayfire(link at the end) if you are looking for libraries with multi dimensional support. (http://www.accelereyes.com/arrayfire/c/group__summul__mat.htm#ga808f0e19bbc711642067f9507c392da9) – Pavan Yalamanchili Jun 10 '13 at 18:20
  • 1
    I guess my answer [here](http://stackoverflow.com/questions/15989178/cuda-thrust-reduce-by-key-on-only-some-values-in-an-array-based-off-values-in/15995193#15995193) may possibly be of some interest as well. – Robert Crovella Jun 10 '13 at 18:45

0 Answers0