使用 Thrust CUDA 对对象进行排序
是否可以使用 Thrust 库对对象进行排序? 我有以下结构:
struct OB{
int N;
Cls *C; //CLS is another struct.
}
是否可以使用推力根据 N 对 OB 数组进行排序?您能提供一个使用推力对对象进行排序的简单示例吗?如果推力无法做到这一点,是否有其他 CUDA 库允许我这样做?
Is it possible to sort objects using the Thrust library?
I have the following struct:
struct OB{
int N;
Cls *C; //CLS is another struct.
}
Is it possible to use thrust in order to sort an array of OB according to N? Can you provide a simple example on using thrust to sort objects? If thrust is not able to do so, is there any other CUDA libraries that allows me to do so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
thrust::sort 的文档显示它接受比较运算符。请参阅其示例如何定义和使用这些内容。我还没有对此进行测试,但根据示例,您需要的只是一个如下所示的结构:
然后只需调用
thrust::sort(obs.begin(), obs.end(), OBCmp())
。The docs for thrust::sort show it accepts a comparison operator. See in their example how those are defined and used. I haven't tested this, but based on the example, all you would need is a struct that looks something like this:
and then just invoke
thrust::sort(obs.begin(), obs.end(), OBCmp())
.即使您可以使用特殊的结构定义对对象进行排序,使用结构作为函子,也会促使将排序算法从基数排序更改为合并排序。基数排序的速度明显快于合并排序。所以在使用thrust的时候,尽量使用整数类型作为键值。
我可能建议您使用“thrust::sory_by_key(..)”函数。
您应该将结构从 AOS 更改为 SOA 结构。
当
您使用 sort_by_key 对索引进行排序时,值已经被排序。
Even though you may sort the objects by using special struct definitions, using a struct as functor, it will make thrust to change the sort algorithm from radix-sort to merge-sort. Speed of radix-sort is noticeably faster than merge-sort. So when using thrust, try to use integer types as key values as possible.
I may suggest you using "thrust::sory_by_key(..)" function.
You should change your struct from AOS to SOA structure.
to
When you sort the indices with sort_by_key, the values will already be sorted.
您可以通过重载运算符<来对对象进行排序。例如:
对象将按照距离排序。
you can sort objects by overloading operator< . For example:
the objects will be sorted after the distance.
到目前为止,您无法对自定义对象进行排序。您可以进行基于键的排序,但不能对自定义对象进行排序,例如您提到的结构。还有一些其他基于 CUDA 的开放算法可用于执行此操作,但这也需要进行一些修改等才能使它们为您工作。
Up until now you cannot sort custom objects. You can do key based sorting but not of custom objects like the struct you mentioned. There are a couple of other open CUDA based algorithms available for doing this but that too requires some modification etc to make them work for you.
我还没有尝试过Thrust,但是CUDPP<中有类似的排序功能/a> 称为cudppSort。您不能使用 cudppSort 直接对结构进行排序,它只能处理整数或浮点数。
因此,对结构数组进行排序的一种方法是对(结构的)键和值的索引数组进行排序。稍后,使用排序后的索引数组将结构移动到最终排序的位置。我在博客文章 此处。 cudppSort 的技术也应该类似。
I have not tried Thrust yet, but there is a similar sort function in CUDPP called cudppSort. You cannot directly sort structures using cudppSort, it can only handle integers or floats.
So, one way to sort array of structures is to sort the keys (of your structure) and an index array of values along with it. Later, use the sorted index array to move the structures to their final sorted locations. I have described how to do this for the cudppCompact compaction algorithm in a blog post here. The technique should be similar for cudppSort too.