简体   繁体   English

如何从两个 arrays 中创建一个对向量,然后使用 CUDA/Thrust 按对的第一个元素进行排序?

[英]How do you make a pair vector out of two arrays and then sort by the first element of the pair using CUDA/Thrust?

Okay, this is going to be a mouthful.好吧,这将是一口。

I have a pointer to serialized 2D array, pointer arithmetic aside, I want to make a vector of pairs out of (essentially) two arrays(that I plan to get out of the 2D array).我有一个指向序列化二维数组的指针,除了指针算术之外,我想从(基本上)两个数组(我打算从二维数组中取出)制作一个对向量。 However, I need to use the Thrust library.但是,我需要使用Thrust库。

This is so that I can use thrust::sort() on the vector, based on the values of the first element of the pair.这样我就可以根据对的第一个元素的值在向量上使用推力::sort()。 On the Device.在设备上。

So, I need a vector(preferably thrust::device_vector) of pairs (of size to match the size of arrayOne and arrayTwo obviously), where the first element of the pair is from arrayOne, the second from arrayTwo.所以,我需要一个成对的向量(最好是推力::device_vector)(大小显然与arrayOne和arrayTwo的大小相匹配),其中对的第一个元素来自arrayOne,第二个来自arrayTwo。 So that I can then use thrust::sort() in order to sort by the first element of the pair.这样我就可以使用推力::sort() 来按对的第一个元素进行排序。

Sorry, for the lack of code, but I'm still trying to figure out the finer details of how to implement this, hence the question.抱歉,由于缺少代码,但我仍在试图弄清楚如何实现这一点的更详细的细节,因此是这个问题。 I'll post my attempts as I go along.我将在 go 中发布我的尝试。 Thank you in advance!先感谢您!

#UPDATE #更新

I think I actually got lucky and found the solution since I posted the question(sort of, it a better solution than using pair - which in turn was suggested to me from a previous question I asked), turns out Thrust actually provides exactly what I'm looking for by default:我想我实际上很幸运并找到了解决方案,因为我发布了问题(有点,它比使用 pair 更好的解决方案 - 这反过来是从我之前提出的问题中向我建议的),事实证明 Thrust 实际上提供了我默认情况下正在寻找:

#include <thrust/sort.h>
  ...
  const int N = 6;
  int    keys[N] = {  1,   4,   2,   8,   5,   7};
  char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
  thrust::sort_by_key(keys, keys + N, values);
  // keys is now   {  1,   2,   4,   5,   7,   8}
  // values is now {'a', 'c', 'b', 'e', 'f', 'd'}

*taken from http://code.google.com/p/thrust/wiki/QuickStartGuide#Fancy_Iterators * *取自http://code.google.com/p/thrust/wiki/QuickStartGuide#Fancy_Iterators *

So, now all I have to do is get two thrust::device_vectors out of the two arrays (that I have to get out of the 2D array).所以,现在我要做的就是从两个 arrays (我必须从二维数组中取出)中取出两个推力::device_vectors。 Happy.快乐的。

The original poster found a solution using thrust::sort_by_key as follows:原发帖人找到了使用thrust::sort_by_key的解决方案,如下:

#include <thrust/sort.h>
  ...
  const int N = 6;
  int    keys[N] = {  1,   4,   2,   8,   5,   7};
  char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
  thrust::sort_by_key(keys, keys + N, values);
  // keys is now   {  1,   2,   4,   5,   7,   8}
  // values is now {'a', 'c', 'b', 'e', 'f', 'd'}

which was taken from http://code.google.com/p/thrust/wiki/QuickStartGuide#Fancy_Iterators取自http://code.google.com/p/thrust/wiki/QuickStartGuide#Fancy_Iterators

The application in the question will require creating a pair of input iterators providing the key and value vectors from the original pitched array mentioned in the question.问题中的应用程序将需要创建一对输入迭代器,从问题中提到的原始音高数组中提供键和值向量。

This answers was added as a community wiki answer to get this question off the unanswered list此答案已作为社区 wiki 答案添加,以将此问题从未回答列表中删除

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据成对中的第一个和第二个元素对成对向量进行排序? - How do I sort a vector of pairs based on both first and second element in a pair? 如何根据对的第二个元素对对的向量进行排序? - How do I sort a vector of pairs based on the second element of the pair? 如何使用 CUDA/Thrust 根据 arrays 之一中的值对两个数组/向量进行排序 - How to sort two arrays/vectors in respect to values in one of the arrays, using CUDA/Thrust 如何将对向量中的对的第一个元素(字符串)与另一个字符串进行比较? - How do I compare the first element (string) of a pair in a pair vector with another string? 您如何构建示例CUDA Thrust设备排序? - How do you build the example CUDA Thrust device sort? 向量对的第一个元素的范围 - Range of the first element of a vector pair 如何仅使用对的第一个元素在集合中找到对? - How to find the pair in the set using first element of the pair only? 从一维向量复制<int>从二维向量对向量的第一个元素开始<pair<int,int> &gt;矩阵 - Copying from one dimensional vector vector<int> starts to first element of two dimensional vector pair vector<pair<int,int>>matrix 在C ++中,如何在对向量的第二个元素上获得迭代器 - In C++, how do you obtain an iterator on the second element of a vector of pair 如何访问向量中的元素 <map<first, pair<K,V> &gt;&gt; - How to access element in vector<map<first, pair<K,V>>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM