This blog post offers an interesting solution for replacing function pointers with function objects and lambdas.
Alternatives to C++ Function Pointers in SYCL using Function Objects
by Georgi Mirazchiyski
From the article:
Function Pointers are a feature of the C language and so form part of the C++ standard. As such, a function pointer allows the following behavior:
"A pointer to a function can be passed as a parameter to another function"
In C++, especially in modern C++, function pointers are a legacy feature from the C language but they still exist in some code bases.
SYCL enables single source development where template functions can contain both host and device code to construct complex algorithms that use acceleration. However, SYCL does not provide support for function pointers since this is a limitation posed by the design of OpenCL v1.2 which is the basis of the current SYCL v1.2.1 definition.
But there is good news, we can use modern C++ to implement a solution that can be used with SYCL. SYCL is built with C++11 (and onward depending on the implementation), meaning features like anonymous functions known as "lambdas" can be used with little to zero overhead. Even going back to C++98/03 it is possible to use function objects defined as either structs or classes, and additionally, you can template your operation (the computation logic) to provide a generic way to consume the function objects or lambdas.
Add a Comment