0

この機能をThrustでどのように実装できますか?

for (i=0;i<n;i++)
    if (i==pos)
        h1[i]=1/h1[i];
    else
        h1[i]=-h1[i]/value;

CUDAでは次のようにしました:

__global__ void inverse_1(double* h1, double value, int pos, int N)
{
    int i = blockDim.x * blockIdx.x + threadIdx.x;
    if (i < N){
        if (i == pos)
            h1[i] = 1 / h1[i];
        else
            h1[i] = -h1[i] / value;
    }
}

ありがとう!

4

1 に答える 1

4

操作を適用するバイナリファンクターを作成してから、2番目の入力としてカウントイテレーターを使用する必要があります。ファンクターのコンストラクターにpos渡すことができます。value次のようになります。

struct inv1_functor
{
  const int pos;
  const double value;

  inv1_functor(double _value, int _pos) : value(_value), pos(_pos) {}

  __host__ __device__
  double operator()(const double &x, const int &i) const {
    if (i == pos)
      return 1.0/x;
    else
      return -x/value;
  }
};

//...

thrust::transform(d_vec.begin(), d_vec.end(), thrust::counting_iterator<int>(),  d_vec.begin(), inv1_functor(value, pos));
于 2011-09-20T16:16:32.827 に答える