1

What's wrong

I'm trying to include some C code in my .cl code to call from the kernel. As far as I can tell, the compiler can find the .h files fine, but fails to include the implementation .c file.

I'm using C# with OpenCL.NetCore source code is located in a folder named cl

Program(s) are built with this: Cl.BuildProgram(program, 1, new[] { device }, "-I cl", null, IntPtr.Zero);

Error reads as: ptxas fatal: Unresolved extern function 'iTest'

Code

Kernel code

#include "test.h"
    
__kernel void sampleKernel(
        __global const int *a,
        __global const int *b,
        __global       int *c
) {
    int gid = get_global_id(0);
    c[gid] = iTest(a[gid] * b[gid]);
}

test.h

#pragma once

int iTest(int x);

test.c

#include "test.h"

int iTest(int x) {
     return x + 1000000;
}

If I add #include "test.c"to the end of test.h the code runs as expected so it can reach the file at least, but I'd like to understand why the compiler isn't including the implementation on it's own, or at least whatever is the correct/best way to do this. Did I forget something really basic?

TheINCGI
  • 35
  • 5

1 Answers1

0

OpenCL C is compiled at runtime. You can either embed the OpenCL C code as a string in the executable, or load it from one or multiple .cl files at runtime. You have to explicitly specify what these files are to the program object handed over to BuildProgram.

You can include headers in the OpenCL C code (#include just inserts the contents of the header file), but other source files will not magically appear in the OpenCL C code then. So placing another #include test.c at the end will work, or handing test.c over to the program object directly.

Note that OpenCL C is based on C99, but has additional restrictions, so not all C code, especially C libraries, will be compatible.

ProjectPhysX
  • 4,535
  • 2
  • 14
  • 34
  • 1
    yup, got that. `-I ` specifies where to find source files. I've loaded in the main cl code with a string simply reading `#include "main.cl"` which works fine. When doing C code on something like replit.com no extra `#include` is needed for the `.c` file to be used, so there is some type of 'magic', (maybe the CL compiler doesn't do?) C99 is good to know. If there's not a better solution I might settle for doing `#include` for all of the c files in the kernel's file (not elegant, but also works) Also, thanks for taking the time to respond to my question :) – TheINCGI Dec 10 '22 at 16:29