0

推力ベクトルを使用しています。

「ミラー」順序付けを使用して推力デバイス ベクトルを並べ替えるためのエレガントな方法を探しています (例として、 Thrust でその関数が見つかりませんでした)。

たとえば、私のベクトルに構造体が含まれているとしましょう。各構造体にはいくつかの数値が含まれています。私のベクトルは次のようになります

[1,2]   [5,4]    [-2,5]     [6,1]     [2,6] 

ミラーの並べ替え操作の後、次のベクトルを受け取りたいです (1 番目の要素が n 番目の要素に切り替えられます) (i 要素が ni 要素に切り替えられるなど)

[2,6]   [6,1]    [-2,5]    [5,4]    [1,2]  

Thrust でエレガントな方法はありますか?

ところで、私は各構造体に一意の ID 番号を付け、その番号に従ってソートすることを考えていました。そのようにして、ソートを使用してベクトルを「ミラーリング」し直すことができました。

4

2 に答える 2

2

使用thrust::reverse:

#include <thrust/device_vector.h>
#include <thrust/reverse.h>
#include <thrust/pair.h>
#include <iostream>

int main()
{
  thrust::device_vector<thrust::pair<int,int> > vec;

  vec.push_back(thrust::make_pair( 1,2));
  vec.push_back(thrust::make_pair( 5,4));
  vec.push_back(thrust::make_pair(-2,5));
  vec.push_back(thrust::make_pair( 6,1));
  vec.push_back(thrust::make_pair( 2,6));

  std::cout << "input: " << std::endl;
  for(int i = 0; i < vec.size(); ++i)
  {
    thrust::pair<int,int> x = vec[i];
    std::cout << " [" << x.first << ", " << x.second << "]";
  }
  std::cout << std::endl;

  thrust::reverse(vec.begin(), vec.end());

  std::cout << "output: " << std::endl;
  for(int i = 0; i < vec.size(); ++i)
  {
    thrust::pair<int,int> x = vec[i];
    std::cout << " [" << x.first << ", " << x.second << "]";
  }
  std::cout << std::endl;

  return 0;
}

出力:

$ nvcc reverse.cu -run
input: 
 [1, 2] [5, 4] [-2, 5] [6, 1] [2, 6]
output: 
 [2, 6] [6, 1] [-2, 5] [5, 4] [1, 2]
于 2013-06-13T06:14:45.327 に答える
0

Thrust::gatherを使用すると、マップ (ベクトル) に従ってソース ベクター要素を宛先ベクター要素に任意にコピーできます。

これが実際の例です:

#include <stdio.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/gather.h>
#include <thrust/sequence.h>

#define DSIZE 7

struct myStruct {
  int x;
  int y;
};

int main(){

  // create input data
  thrust::host_vector<myStruct> h(DSIZE);
  for (int i=0; i< DSIZE; i++){
    h[i].x = 2*i;
    h[i].y = (2*i)+1;
    }

  // create map
  thrust::device_vector<int> map(DSIZE);
  thrust::sequence(map.begin(), map.end(), DSIZE-1, -1);

  //move to device
  thrust::device_vector<myStruct> d = h;
  thrust::device_vector<myStruct> d_result(DSIZE);

  thrust::gather(map.begin(), map.end(), d.begin(), d_result.begin());

  //move to host
  thrust::host_vector<myStruct> h_result = d_result;

  for (int i = 0; i < DSIZE; i++){
    printf("result[%d].x = %d, result[%d].y = %d\n", i, h_result[i].x, i, h_result[i].y);
    }
  return 0;
}
于 2013-06-12T13:22:05.137 に答える