Skip to content

Can cub::DeviceSegmentedReduce::Reduce support self-defined functor for struct variable instead of just integer? #1064

Answered by gevtushenko
zlwu92 asked this question in CUB
Discussion options

You must be logged in to vote

Hello @zlwu92!

We support arbitrary operations in cub::DeviceSegmentedReduce:

#include <cub/cub.cuh>
#include <thrust/device_vector.h>

struct pair_t {
  int id;
  int val;
};

struct operation_t {
  __device__ pair_t operator()(const pair_t &a, const pair_t &b) {
    if (a.val == b.val) {
      return a.id < b.id ? a : b;
    }

    return a.val < b.val ? a : b;
  }
};

int main() {
  thrust::device_vector<pair_t> in = {
    pair_t{0, 1},
    pair_t{1, 2},
    pair_t{2, 2},
    pair_t{3, 2},
  };

  const int num_segments = 2;
  thrust::device_vector<pair_t> out(num_segments);

  thrust::device_vector<int> offsets = {
    0, 2, 4
  };

  const pair_t init{42, 42};

  std::size_t temp_sto…

Replies: 0 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@zlwu92
Comment options

Answer selected by zlwu92
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
CUB
Labels
None yet
2 participants