Skip to content

Commit

Permalink
Remove #include "../opencl.h" from source files during runtime inst…
Browse files Browse the repository at this point in the history
…ead of having a dummy header file.
  • Loading branch information
ThatRedox committed Aug 17, 2024
1 parent 50b1085 commit f9979a4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ public cl_program loadProgram(Function<String, String> sourceReader, String kern
// Search for include headers
HashMap<String, cl_program> headerFiles = new HashMap<>();

// Fake `opencl.h` header
headerFiles.put("../opencl.h", clCreateProgramWithSource(context, 1, new String[] { "" },
null, null));

// Load headers
readHeaders(kernel, headerFiles);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.function.BiFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class KernelLoader {
private static final KernelLoader instance = new KernelLoader();
private static final Pattern openclIncludeMatcher = Pattern.compile(
"^\\h*#include\\h*\"\\.\\./opencl\\.h\"\\h*$", Pattern.MULTILINE);

private final BiFunction<String, String, String> rawSourceReader;
private final boolean hotReload;
Expand Down Expand Up @@ -54,7 +58,12 @@ private KernelLoader() {
* @return OpenCL program.
*/
public static cl_program loadProgram(ClContext context, String base, String kernelName) {
return context.loadProgram(file -> instance.rawSourceReader.apply(base, file), kernelName);
return context.loadProgram(file -> {
String program = instance.rawSourceReader.apply(base, file);
Matcher matcher = openclIncludeMatcher.matcher(program);
program = matcher.replaceFirst("// #include \"../opencl.h\"");
return program;
}, kernelName);
}

public static boolean canHotReload() {
Expand Down

0 comments on commit f9979a4

Please sign in to comment.