Porting C to OpenCL C

I’m currently working on adding OpenCL functionality to an existing C library (GNUAstro) and i was wondering how compatible C and OpenCL were.

I wanted to use these C library functions (GNUAstro) within the kernel but faced the problem that just compiling and linking them through the OpenCL API was not possible, since these libraries are written in C, and the kernel expect OpenCL C.

Some of the problems were that error handling functions no longer work, which i can live without, but additionally, even malloc() free() aren’t allowed in the kernel.

So i cannot directly compile and link but i would need some kind of intermediated processing to convert the library C source code to be OpenCL C. Or I would have to maintain a separate version of the source code intended for OpenCL C only.

Whats the best way to go about this? And do you know of any existing projects that have done something like this?

My take on this is to not make an entire library run as kernels. I don’t know anything about GNUAstro (apart from looking at it for about five minutes), but I suspect there will be parts that simply make no sense whatsoever to run on an accelerator/GPU, e.g. anything that does I/O.
Instead, identify an algorithm (or part of one) that can be meaningfully parallelized in a way that is suitable for GPUs and turn that into a kernel (or multiple).
While figuring that out you can also think about how to pipe infrastructure (e.g. the OpenCL context) through the library, e.g. if there is already a “state” or “context” object that gets passed to most functions you could extend that or add variations of functions that take additional OpenCL related arguments.