Cuda 仅对一个类成员进行推力扫描

Cuda 仅对一个类成员进行推力扫描,cuda,thrust,Cuda,Thrust,我有一个自定义类myClass,其中包含成员weight和config。我想在一堆myClasses上运行包含扫描,但只在weights上运行。基本上我想要的是: [{configA,weightA},{configB,weightB},{configC,weightC},…] 致: [{configA,weightA},{configB,weightA+weightB},{configC,weightA+weightB+weightC},…] 有没有一种简单的方法可以使用推力的奇特迭代器来实现

我有一个自定义类
myClass
,其中包含成员
weight
config
。我想在一堆
myClass
es上运行包含扫描,但只在
weight
s上运行。基本上我想要的是:

[{configA,weightA},{configB,weightB},{configC,weightC},…]

致:

[{configA,weightA},{configB,weightA+weightB},{configC,weightA+weightB+weightC},…]


有没有一种简单的方法可以使用推力的奇特迭代器来实现这一点?由于
binaryOp
必须是关联的,我不知道如何通过重载
操作符+

inclusive\u scan
需要关联操作符,但它不需要是可交换的。如果您创建一个二进制函数,将其第二个参数的config成员复制到结果中,则该函数应为:

#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/scan.h>

struct my_struct
{
  __host__ __device__
  my_struct() {}

  __host__ __device__
  my_struct(const my_struct &other)
    : config(other.config), weight(other.weight)
  {}

  __host__ __device__
  my_struct(char c, double w)
    : config(c), weight(w)
  {}

  char config;
  double weight;
};


struct functor
{
  __host__ __device__
  my_struct operator()(my_struct a, my_struct b)
  {
    my_struct result;
    result.config = b.config;
    result.weight = a.weight + b.weight;
    return result;
  }
};

int main()
{
  thrust::device_vector<my_struct> vec(3);

  vec[0] = my_struct('a', 1);
  vec[1] = my_struct('b', 2);
  vec[2] = my_struct('c', 3);

  std::cout << "input: ";
  for(int i = 0; i < vec.size(); ++i)
  {
    my_struct x = vec[i];
    std::cout << "{" << x.config << ", " << x.weight << "} ";
  }
  std::cout << std::endl;

  thrust::inclusive_scan(vec.begin(), vec.end(), vec.begin(), functor());

  std::cout << "result: ";
  for(int i = 0; i < vec.size(); ++i)
  {
    my_struct x = vec[i];
    std::cout << "{" << x.config << ", " << x.weight << "} ";
  }
  std::cout << std::endl;

  return 0;
}

inclusive\u scan
需要关联运算符,但它不需要是可交换的。如果您创建一个二进制函数,将其第二个参数的config成员复制到结果中,则该函数应为:

#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/scan.h>

struct my_struct
{
  __host__ __device__
  my_struct() {}

  __host__ __device__
  my_struct(const my_struct &other)
    : config(other.config), weight(other.weight)
  {}

  __host__ __device__
  my_struct(char c, double w)
    : config(c), weight(w)
  {}

  char config;
  double weight;
};


struct functor
{
  __host__ __device__
  my_struct operator()(my_struct a, my_struct b)
  {
    my_struct result;
    result.config = b.config;
    result.weight = a.weight + b.weight;
    return result;
  }
};

int main()
{
  thrust::device_vector<my_struct> vec(3);

  vec[0] = my_struct('a', 1);
  vec[1] = my_struct('b', 2);
  vec[2] = my_struct('c', 3);

  std::cout << "input: ";
  for(int i = 0; i < vec.size(); ++i)
  {
    my_struct x = vec[i];
    std::cout << "{" << x.config << ", " << x.weight << "} ";
  }
  std::cout << std::endl;

  thrust::inclusive_scan(vec.begin(), vec.end(), vec.begin(), functor());

  std::cout << "result: ";
  for(int i = 0; i < vec.size(); ++i)
  {
    my_struct x = vec[i];
    std::cout << "{" << x.config << ", " << x.weight << "} ";
  }
  std::cout << std::endl;

  return 0;
}

您不需要使用花哨的迭代器。只需创建一个接受两个
myClass
参数的函子,并返回它们的
weight
s之和,并将其传递给
inclusive\u scan
inclusive\u scan
是否知道将向量中
myClass
权重设置为我的函子将返回的
双精度值?您不需要这样做使用一个奇特的迭代器。只需创建一个接受两个
myClass
参数的函子,返回它们的
weight
s之和,并将其传递给
inclusive\u scan
inclusive\u scan
是否知道将向量中
myClass
es的
weight
设置为我的函子将返回的
double