Home > front end >  OpenCL including header causes ptxas fatal: Unresolved extern function
OpenCL including header causes ptxas fatal: Unresolved extern function

Time:12-11

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?

CodePudding user response:

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.

  • Related