简体   繁体   中英

OpenCL including header causes ptxas fatal: Unresolved extern function

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM