Cuda 推力镜重排序

Cuda 推力镜重排序,cuda,gpu,thrust,Cuda,Gpu,Thrust,我用的是推力矢量 我正在寻找一种优雅的方法,使用“镜像”排序对推力装置矢量进行重新排序(举个例子,在推力中找不到任何函数) 例如,假设我的向量包含一个结构,每个结构包含几个数字。 我的向量如下所示 [1,2] [5,4] [-2,5] [6,1] [2,6] 在镜像重新排序操作之后,我希望接收以下向量 (第一个元件与第n个元件切换) (i元件与n-i元件切换,等等) 有什么优雅的方法可以做到这一点吗 顺便说一句,我想给每个结构一个唯一的ID号,并根据该编号进行排序,

我用的是推力矢量

我正在寻找一种优雅的方法,使用“镜像”排序对推力装置矢量进行重新排序(举个例子,在推力中找不到任何函数)

例如,假设我的向量包含一个结构,每个结构包含几个数字。 我的向量如下所示

[1,2]   [5,4]    [-2,5]     [6,1]     [2,6] 
在镜像重新排序操作之后,我希望接收以下向量 (第一个元件与第n个元件切换) (i元件与n-i元件切换,等等)

有什么优雅的方法可以做到这一点吗

顺便说一句,我想给每个结构一个唯一的ID号,并根据该编号进行排序,这样我就可以“镜像”使用排序对向量进行重新排序

允许根据映射(矢量)将源矢量元素任意复制到目标矢量元素

下面是一个成功的例子:

#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;
}
#包括
#包括
#包括
#包括
#包括
#定义DSIZE 7
结构myStruct{
int x;
int-y;
};
int main(){
//创建输入数据
推力:主机向量h(DSIZE);
对于(int i=0;i
使用
推力::反向

#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;
}

请不要在问题的标题中加标签。没有必要。有标记,在标题中重复它们不仅是不必要的,而且是不必要的。这不是仅仅是
推力::反向
?@Jared Hoberock,你是对的,它简单到反向,写一个答案,我会将你的答案设置为正确答案。我认为Jared Hoberock的建议更简单。
#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]