如何使用 CUDA/Thrust 对两个数组/向量根据其中一个数组中的值进行排序
这是一个关于编程的概念问题。
总而言之,我有两个数组/向量,我需要对一个数组/向量进行排序,并将更改传播到另一个数组/向量中,这样,如果我对 arrayOne 进行排序,则对于排序中的每个交换 - arrayTwo 都会发生同样的情况。现在,我知道 std::sort 允许您定义一个比较函数(对于我假设的自定义对象),并且我正在考虑定义一个比较函数来同时交换 arrayTwo。
所以我想要的是 - 使用 CUDA 根据其中一个向量中的值对两个向量进行排序。
这就是我的不确定性上升的地方,本质上我想使用 Thrust 库来进行排序。它是否支持自定义比较函数的定义?如果是这样,我仍然没有弄清楚如何传播 arrayTwo 中的更改(因为它将基于 CUDA)。
我确实没有时间在 CUDA 上实现自定义并行快速排序,尽管我应该/想要这样做。
原因
本质上,我需要对一堆变量数组与单个数组执行排序和计算(想想回归树)。当然,我需要尽快完成,基于 CPU 的排序不够快。
#UPDATE
我应该强调,我在主机上对两者进行排序没有问题,我正在寻找使用CUDA的解决方案。谢谢。
#UPDATE 2
我想我真的很幸运,自从我发布问题后找到了解决方案, 事实证明,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'}
*取自 http ://code.google.com/p/thrust/wiki/QuickStartGuide#Fancy_Iterators*
所以,现在我所要做的就是从两个数组中获取两个 Thrust::device_vectors (我必须摆脱二维数组)。快乐的。
This is a conceptual question in regards programming.
To summarize, I have two arrays/vectors and I need to sort one with the changes propagating in the other as well, so that if I sort arrayOne, for each swap in the sort - the same thing happens to arrayTwo. Now, I know that std::sort allows you to define a comparison function (for custom objects I assume) and I was thinking of defining one to swap arrayTwo at the same time.
So what I want is - to sort the two vectors based on values in one of the vectors, using CUDA.
This is where my uncertainty rises, essentially I want to use the Thrust library to do the sort. Does it support the definition of a custom comparison function? If so, I still haven't figured out how to propagate the change in the arrayTwo though (since it will be CUDA based).
I really don't have the time to implement a custom parallel quicksort on CUDA, as much as I should/want to.
The reason
Essentially I need to perform sorting and calculation on a bunch of arrays of variables versus a single array (think regression trees). Naturally, I need to do so as quickly as possible, CPU based sort is just not quick enough.
#UPDATE
I should emphasize, I have no problems sorting the two on the host, I'm looking for a solution that uses CUDA. Thanks.
#UPDATE 2
I think I actually got lucky and found the solution since I posted the question,
turns out Thrust actually provides exactly what I'm looking for by default:
#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*
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). Happy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个整数索引向量,初始化为 { 0, 1, 2, 3, etc }。每个整数代表向量中的一个位置。使用自定义比较函数对索引向量进行排序,该函数使用索引来引用向量 1。完成后,您可以使用排序索引对向量 1 和 向量 2 进行重新排序。
但我认为你不能就地进行重新排序,所以无论如何你都必须从一个向量复制到另一个向量,所以我认为 Kerrek 的建议也很好。
Create a vector of integer indexes, initialised to { 0, 1, 2, 3, etc }. Each integer represents one position in your vector. Sort your vector of indexes using a custom comparision function that uses the indexes to refer to vector1. When finished you can use the sorted indexes to reorder vector1 and vector2.
But I don't think you can do this reordering in place, so you going to have to copy from vector to vector anyway, so I think Kerrek's suggestion is good too.