如何使用CUDA / Thrust对一个数组中的值对两个数组/向量进行排序 [英] How to sort two arrays/vectors in respect to values in one of the arrays, using CUDA/Thrust

查看:747
本文介绍了如何使用CUDA / Thrust对一个数组中的值对两个数组/向量进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个在编程方面的概念问题。

This is a conceptual question in regards programming.

总而言之,我有两个数组/向量,我需要对其中的变化进行排序,所以如果我对arrayOne排序,在排序 - 同样的事情发生在arrayTwo。现在,我知道std :: sort允许你定义一个比较函数(对于自定义对象我假设),我正在想定义一个交换arrayTwo同时。

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.

所以我想要的是使用CUDA根据其中一个向量的值对两个向量进行排序。

在那里我的不确定性上升,基本上我想使用Thrust库做排序。它是否支持自定义比较功能的定义?如果是这样,我仍然没有想出如何传播在arrayTwo的变化,因为它将是基于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).

我真的没有

原因

本质上,我需要对一组变量数组和单个数组(认为回归树)执行排序和计算。自然,我需要尽快这样做,基于CPU的排序只是不够快。

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

我应该强调,我没有问题在主机上排序这两个,我正在寻找使用 CUDA 的解决方案。非常感谢。

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

我想我真的很幸运,解决方案,因为我发布的问题,
,结果是Thrust实际上提供了我正在寻找的默认值:

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'}

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

所以,现在我要做的是从两个数组中获取两个推力:: device_vectors(我必须从二维数组中取出)。快乐。

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.

推荐答案

创建一个整数索引向量,初始化为{0,1,2,3,etc}。每个整数表示向量中的一个位置。使用自定义比较函数(使用索引引用vector1)对索引向量进行排序。完成后,您可以使用排序的索引重新排序vector1 vector2。

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.

但我不认为你可以做这个重新排序,所以你必须从向量到向量复制,所以我认为Kerrek的建议也很好。

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.

这篇关于如何使用CUDA / Thrust对一个数组中的值对两个数组/向量进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆