使用 Thrust CUDA 对对象进行排序

发布于 2024-10-21 14:05:12 字数 210 浏览 6 评论 0原文

是否可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浅听莫相离 2024-10-28 14:05:12

thrust::sort 的文档显示它接受比较运算符。请参阅其示例如何定义和使用这些内容。我还没有对此进行测试,但根据示例,您需要的只是一个如下所示的结构:

struct OBCmp {
  __host__ __device__
  bool operator()(const OB& o1, const OB& o2) {
      return o1.N < o2.N;
  }
};

然后只需调用 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:

struct OBCmp {
  __host__ __device__
  bool operator()(const OB& o1, const OB& o2) {
      return o1.N < o2.N;
  }
};

and then just invoke thrust::sort(obs.begin(), obs.end(), OBCmp()).

殤城〤 2024-10-28 14:05:12

即使您可以使用特殊的结构定义对对象进行排序,使用结构作为函子,也会促使将排序算法从基数排序更改为合并排序。基数排序的速度明显快于合并排序。所以在使用thrust的时候,尽量使用整数类型作为键值。

我可能建议您使用“thrust::sory_by_key(..)”函数。

您应该将结构从 AOS 更改为 SOA 结构。

struct OB{
  int N;
  Cls *C; //CLS is another struct.
}

struct OBs{
   int []Ns; -> thrust::device_vector<int> indices;
   Cls *C[]; -> thrust::device_vector<Cls> values;
}

您使用 sort_by_key 对索引进行排序时,值已经被排序。

thrust::sort_by_key(indices.begin(), indices.end(), values.begin());

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.

struct OB{
  int N;
  Cls *C; //CLS is another struct.
}

to

struct OBs{
   int []Ns; -> thrust::device_vector<int> indices;
   Cls *C[]; -> thrust::device_vector<Cls> values;
}

When you sort the indices with sort_by_key, the values will already be sorted.

thrust::sort_by_key(indices.begin(), indices.end(), values.begin());
静谧幽蓝 2024-10-28 14:05:12

您可以通过重载运算符<来对对象进行排序。例如:

__host__ __device__ struct Color{
  double blue, green, red;
  double distance;
  void dist()
  {
    distance = sqrt(blue*blue + green*green + red*red);
  }
};

__host__ __device__ bool operator<(const Color &lhs, const Color &rhs) 
{
   return lhs.distance < rhs.distance;
}

int main(void)
{
   thrust::device_vector<Color> cd;
   thrust::host_vector<Color> ch;
   for (int i = 0; i<6; i++)
   {
      Color c;
      c.blue = rand()*255;
      c.green = rand()*255;
      c.red = rand()*255;
      c.dist();
      ch.push_back(c);
   }
   cd = ch;
   thrust::sort(cd.begin(), cd.end());
   ch = cd;
   return 0;
}

对象将按照距离排序。

you can sort objects by overloading operator< . For example:

__host__ __device__ struct Color{
  double blue, green, red;
  double distance;
  void dist()
  {
    distance = sqrt(blue*blue + green*green + red*red);
  }
};

__host__ __device__ bool operator<(const Color &lhs, const Color &rhs) 
{
   return lhs.distance < rhs.distance;
}

int main(void)
{
   thrust::device_vector<Color> cd;
   thrust::host_vector<Color> ch;
   for (int i = 0; i<6; i++)
   {
      Color c;
      c.blue = rand()*255;
      c.green = rand()*255;
      c.red = rand()*255;
      c.dist();
      ch.push_back(c);
   }
   cd = ch;
   thrust::sort(cd.begin(), cd.end());
   ch = cd;
   return 0;
}

the objects will be sorted after the distance.

掩于岁月 2024-10-28 14:05:12

到目前为止,您无法对自定义对象进行排序。您可以进行基于键的排序,但不能对自定义对象进行排序,例如您提到的结构。还有一些其他基于 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.

握住你手 2024-10-28 14:05:12

我还没有尝试过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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文