# Using a Functor in CUDA Kernel

**URL:** <https://forums.developer.nvidia.com/t/using-a-functor-in-cuda-kernel/306030>\
**Category:** CUDA Programming and Performance\
**Created:** [September 6, 2024, 7:09pm UTC](https://forums.developer.nvidia.com/t/using-a-functor-in-cuda-kernel/306030 "2024-09-06T19:09:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ziqi](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Ziqi](https://forums.developer.nvidia.com/u/Ziqi)\
**Post date:** [September 6, 2024, 7:09pm UTC](https://forums.developer.nvidia.com/t/using-a-functor-in-cuda-kernel/306030/1 "2024-09-06T19:09:30Z")

</div>

I am implementing the following function:

```auto
template <typename IndexType, typename BinaryType, typename... TupleTypes>
__global__ void apply_predicate_knl(cuda::std::tuple<const TupleTypes*...> data, const IndexType num, bool (*predicate)(cuda::std::tuple<TupleTypes...>), BinaryType* binary_flag)
{
  for (IndexType idx = blockIdx.x * blockDim.x + threadIdx.x; idx < num; idx += gridDim.x * blockDim.x)
  {
    // get the total dimension of the current tuple (sequence)
    constexpr size_t num_dim = cuda::std::tuple_size<cuda::std::tuple<const TupleTypes...>>::value;

    if (num_dim == 0) // return true if input is empty set
    {
      binary_flag[idx] = static_cast<BinaryType>(1);
    }
    else
    {
      cuda::std::tuple<TupleTypes...> input_object = get_tuple_from_ptr_tuple(data, idx);
      binary_flag[idx] = static_cast<BinaryType>(predicate(input_object));
    }
  }
}

```

The idea is, to group pointers of an DoA object into a cuda::std::tuple and apply some predicate to each object in the DoA and set 0 or 1 for that object. I am thinking about how to pass in the function pointer `bool (*predicate)(cuda::std::tuple<TupleTypes...>)`. The first thing I thought of is a functor on device, e.g.,

```auto
struct predicate_on_tuple
  {
    __host__  __device__ bool operator()(cuda::std::tuple<uint32_t, uint64_t, uint16_t> input)
    {

    }

    uint32_t some_var;
  }

```

In this case, I am assuming that the functor object has to be created on device then somehow be passed in as a pointer. Is there a better way to do this?

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [September 6, 2024, 7:11pm UTC](https://forums.developer.nvidia.com/t/using-a-functor-in-cuda-kernel/306030/2 "2024-09-06T19:11:28Z")

</div>

> [@Ziqi](#):
>
> I am assuming that the functor object has to be created on device then somehow be passed in as a pointer. Is there a better way to do this?

you can (should?) declare your functor just like that, and pass it (ie. the enclosing object) by value to a kernel that needs it.

Here is an example of what I mean, perhaps that doesn’t line up with what you are asking:

```auto
# cat t278.cu
#include <iostream>
#include <cuda/std/tuple>
typedef cuda::std::tuple<int, int> my_tuple;

struct my_func
{
  int scale;
  my_func(int _s):scale(_s) {};
  __host__  __device__ int operator()(my_tuple &t) { return cuda::std::get<0>(t)*cuda::std::get<1>(t)*scale;}
};

template <class f>
__global__ void k(int *a, int *b, int *c, f op){

  int idx = blockDim.x*blockIdx.x+threadIdx.x;
  my_tuple i = cuda::std::make_tuple(a[idx], b[idx]);
  c[idx] = op(i);
}

int main(){

  const int ds = 1;
  int *a, *b, *c;
  cudaMallocManaged(&a, sizeof(a[0])*ds);
  cudaMallocManaged(&b, sizeof(b[0])*ds);
  cudaMallocManaged(&c, sizeof(c[0])*ds);
  a[0] = 1;
  b[0] = 2;
  k<<<1,1>>>(a, b, c, my_func(3));
  cudaDeviceSynchronize();
  std::cout << c[0] << std::endl;
}
# nvcc t278.cu -o t278
# compute-sanitizer ./t278
========= COMPUTE-SANITIZER
6
========= ERROR SUMMARY: 0 errors
#

```

lambdas are another option people frequently discuss in similar context to functors:

```auto
# cat t278.cu
#include <iostream>
#include <cuda/std/tuple>
typedef cuda::std::tuple<int, int> my_tuple;

template <class f>
__global__ void k(int *a, int *b, int *c, f op){

  int idx = blockDim.x*blockIdx.x+threadIdx.x;
  my_tuple i = cuda::std::make_tuple(a[idx], b[idx]);
  c[idx] = op(i);
}

int main(){

  const int ds = 1;
  int *a, *b, *c;
  cudaMallocManaged(&a, sizeof(a[0])*ds);
  cudaMallocManaged(&b, sizeof(b[0])*ds);
  cudaMallocManaged(&c, sizeof(c[0])*ds);
  a[0] = 1;
  b[0] = 2;
  const int scale = 3;
  k<<<1,1>>>(a, b, c,  
    [=] __device__ (my_tuple t) { return cuda::std::get<0>(t)*cuda::std::get<1>(t)*scale; }
    );
  cudaDeviceSynchronize();
  std::cout << c[0] << std::endl;
}
# nvcc t278.cu -o t278 -extended-lambda
# compute-sanitizer ./t278
========= COMPUTE-SANITIZER
6
========= ERROR SUMMARY: 0 errors
#

```
