1

この単純なコードを推力コードに変換するにはどうすればよいですか?

for (i=0;i<cA-rA;i++)
    sn[i]=c[n_index[i]]-sn[i];

詳細情報: cA と rA は const 整数であるため、「n」= cA-rA と考えることができます。 sn : float(n) の配列 n_index : int(n) の配列 c : float(cA) の配列

私の問題は、C 配列の要素を指す n_index[i] にあります。ありがとう!

4

2 に答える 2

3

thrust::transformを使用して「収集」操作と融合することにより、これを実装できますpermutation_iterator

#include <thrust/device_vector.h>
#include <thrust/iterator/permutation_iterator.h>
#include <thrust/transform.h>
#include <thrust/sequence.h>
#include <thrust/functional.h>

int main()
{
  size_t n = 100;

  // declare storage
  thrust::device_vector<int> sn(n);
  thrust::device_vector<int> n_index(n);
  thrust::device_vector<int> c(n);

  // initialize vectors with some sequential values for demonstrative purposes
  thrust::sequence(sn.begin(), sn.end());
  thrust::sequence(n_index.begin(), n_index.end());
  thrust::sequence(c.begin(), c.end());

  // sn[i] = c[n_index[i]] - sn[i]
  thrust::transform(thrust::make_permutation_iterator(c.begin(), n_index.begin()),
                    thrust::make_permutation_iterator(c.end(), n_index.end()),
                    sn.begin(),
                    sn.begin(),
                    thrust::minus<int>());

  return 0;
}
于 2011-09-29T21:15:07.907 に答える
2

最初のものを試しましたが、正しい結果が得られませんでした。2番目のpermutation_iteratorは、両方のベクトルの最後にある必要があります。

次の修正を試してください。

// sn[i] = c[n_index[i]] - sn[i]
thrust::transform(thrust::make_permutation_iterator(c.begin(), n_index.begin()),
            thrust::make_permutation_iterator(c.end(), n_index.end()),
            sn.begin(),
            sn.begin(),
            thrust::minus<int>());
于 2012-07-31T20:48:08.107 に答える