Thrust vector transformation involving neighboring elements

I have a vector, and I would like to do the following using the CUDA and Thrust transforms:

// thrust::device_vector v;
// for k times:
//     calculate constants a and b as functions of k;
//     for (i=0; i < v.size(); i++)
//         v[i] = a*v[i] + b*v[i+1];

How to implement this correctly? One way to do this is to have the vector w and apply thrust :: transform to v and save the results to w. But k is unknown ahead of time, and I do not want to create w1, w2, ... and spend a lot of memory on the GPU. Preferably, I want to minimize the amount of data copying. But I'm not sure how to implement this using a single vector with no values ​​coming on top of each other. Is there something thrust that can do this?

+1
source share
2 answers

v.size() , , k , mem .

thrust::device_vector u(v.size());
for(k=0;;)
{
    // calculate a & b
    thrust::transform(v.begin(), v.end()-1, v.begin()+1, u.begin(), a*_1 + b*_2);
    k++;
    if(k>=K)
        break;

    // calculate a & b
    thrust::transform(u.begin(), u.end()-1, u.begin()+1, v.begin(), a*_1 + b*_2);
    k++;
    if(k>=K)
        break;      
}
+1

"k ", .

struct OP {
    const int a, b;
    OP(const int p, const int q): a(p), b(q){};
    int operator()(const int v1, const int v2) {
      return a*v1+b*v2;
    }
}
thrust::device_vector<int> w(v.size());
thrust::transform(v.begin(), v.end()-1, //input_1
                  v.begin()+1,          //input_2
                  w.begin(),            //output
                  OP(a, b));            //functor
v = w;

, "", .

, .:)

0

All Articles