1

I'm working on the implementation of a few related algorithms in CUDA, all of which require a primitive that we'll call f(). The related algorithms can't simply call f though, as they require f to have slightly different behavior for each algorithm.

f is highly optimized feature though, so I don't want to overload f for each case because if I end up making changes to f then I need to change each overload.

f looks something like the following:

__device__ void f(const int *R, const int *C, const int n, int *d, int *Q, int *Q2)
{
    for(int i=blockIdx.x; i<n; i+=gridDim.x)
    {
         //Cooperatively inspect R and C and place results into d
    }
}

It's actually way more complicated as it uses lots of __shfl instructions and whatnot, but that shouldn't be relevant here. The problem is that the related algorithms all need slightly different variations of f but since f is complicated I only want it's code in one location.

Here are the requirements of the related algorithms:

A: An additional global device variable to keep track of the maximum of a set of data

B: An additional O(n) or O(n^2) array for recording integer data at various stages of f

C: Similar to B but recording binary data

D: Requires a stack and an array for recording data at various stages of f (the array is the same as needed in B). Has other requirements too but those are better handled separately.

How can I avoid duplicating f and make it flexible for these use cases?

  • Since all the requirements involve tracking some sort of data, how about accepting function pointers that get called at the various points? E.g. Algorithm A can supply a function pointer that gets called once on each piece of data and takes care of tracking the maximum. If the requirements would involve multiple function pointers that accept different types of input and get called in different places, algorithm A can provide no-op functions for the things it doesn't need. – Doval Jan 22 '15 at 20:31
  • @Doval Perhaps a functor, even? I also have C++11 features available, if that's relevant. I guess this implies that the argument list will grow for everyone fp/functor needed? – Adam27X Jan 22 '15 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.