c++gpgpuarrayfiresycl

Optimize member function selection at runtime on CPU/GPU


I have the following piece of code that needs to optimized (and be later ported to the GPU through SYCL or ArrayFire):

struct Item {
    float value;
    int f;
    float Func(float);
    float Func1(float);
    float Func2(float);
    float Func3(float);
};

float Item::Func(float v) {
    value = v;
    switch(f) {
        case 1: return Func1(v);
        case 2: return Func2(v);
        case 3: return Func3(v);
    }
    return Func1(v);
}

std::vector<Item> items;

AFAIK, on GPUs the function pointer approach is not suitable.

Is there a more performant approach on CPUs and/or GPUs than this one?


Solution

  • There is a blog post about how to implement an alternative to function pointers using SYCL on this website. The solution uses the template feature and function objects instead. I believe the history of this is that most hardware doesn't support jumping to computed addresses.