[英]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.