# CUB Repost - I apologize

**URL:** <https://forums.developer.nvidia.com/t/cub-repost-i-apologize/124901>\
**Category:** GPU-Accelerated Libraries\
**Created:** [May 26, 2020, 1:02pm UTC](https://forums.developer.nvidia.com/t/cub-repost-i-apologize/124901 "2020-05-26T13:02:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhaney](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@rhaney](https://forums.developer.nvidia.com/u/rhaney)\
**Post date:** [May 26, 2020, 1:02pm UTC](https://forums.developer.nvidia.com/t/cub-repost-i-apologize/124901/1 "2020-05-26T13:02:03Z")

</div>

I apologize for reposting this question, but I think I might have placed this question in the incorrect forum division previously. However this is a question that I have been unable to find any sort of answer to online anywhere else - hopefully someone here will have some idea if this can even be done.

I am working with CUDA UnBound (CUB) and cuFloatComplex numbers such that I would like to reduce an array of cuFloatComplex numbers to a single float value. The pseudo code I would like to implement using cub::DeviceReduce::Reduce operation follows where N is total length of an array of cuFloatComplex numbers:

```
      cuFloatComplex *array = NULL;
      array = (cuComplexFloat *)malloc(N*sizeof(cuFloatComplex));
      // initialize array to some set of random numbers:
      initArray(array, N);

      // reduce to single float
      float sum = 0.0f;
      for (int i = 0; i < N; ++i)
      {
        sum += (array[i].x * array[i].x + array[i].y * array[i].y);
      }

```

I have tried creating a custom operation function that will incorporate the above and pass to DeviceReduce in CUB it doesn’t give same results as in following where d\_in and d\_out are cuFloatComplex values (d\_in is array of length N, d\_out is scalar of length 1) - result would be stored in real portion of d\_out:

```
struct CplxReduce{
  cuFloatComplex operator()(const cuFloatComplex &a, const cuFloatComplex &b) const {
    cuFloatComplex c;
    c.x = (a.x *a.x + a.y*a.y);
    c.x += (b.x*b.x + b.y*.b.y);
    return c;
}
};
...
FltCplxReduce reduceOp;
  void *tmp = NULL;
  size_t tmp_bytes = 0;
  cuFloatComplex init;
  //init.x = init.y = FLT_MIN;
  init.x = init.y = 0.0f;

  cub::DeviceReduce::Reduce(tmp, tmp_bytes, d_in, d_out, N, reduceOp, init);

  checkCudaErrors(cudaMalloc(&tmp, tmp_bytes));

  cub::DeviceReduce::Reduce(tmp, tmp_bytes, d_in, d_out, N, reduceOp, init);

  if (tmp)
    checkCudaErrors(cudaFree(tmp));

```

Is what I am trying to do even possible? Any help/hints would be great.

Thanks
