Как изменить порядок вектора при преобразовании тяги? - PullRequest
1 голос
/ 30 сентября 2011

Как я могу преобразовать этот простой код в код подтверждения?

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

Дополнительная информация: cA и rA являются целыми числами const, поэтому мы можем считать их как 'n' = cA-rA sn: массив с плавающей точкой(n) n_index: массив int (n) c: массив с плавающей точкой (cA)

Моя проблема связана с n_index [i], который указывает на элемент массива C.спасибо!

Ответы [ 2 ]

3 голосов
/ 30 сентября 2011

Вы можете реализовать это, смешав 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;
}
2 голосов
/ 01 августа 2012

Я попробовал первый и не получил правильных результатов. второй 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>());
...