Skip to content

CallingOpenCLKernelsFromJava

Olivier Chafik edited this page Nov 10, 2022 · 3 revisions

How to call OpenCL Kernels with JavaCL

Introduction

Let's assume you wrote the following kernel in file com/mypackage/MyProgram.cl:

__kernelvoidmyKernel(__globalconstfloat*input, __globalfloat*output, floatmultFactor) {
inti=get_global_id(0);
output[i] =input[i] *multFactor;
}

Then you have two ways to call it from JavaCL : a generic dynamic way, which is loosely typed, and a hard-typed precompiled way.

Common Host Setup code

CLContextcontext = ... ; // e.g. JavaCL.createBestContext()CLQueuequeue = ... ; // e.g. context.createDefaultQueue()intdataSize = 128;
CLBuffer<Float> input = context.createFloatBuffer(CLMem.Usage.Input, dataSize);
CLBuffer<Float> output = context.createFloatBuffer(CLMem.Usage.Output, dataSize);
floatmultFactor = 0.5f;

Generic 'Dynamic' Call

Setup the kernel :

Stringsources = ... ; // read the source file from resources / from a fileCLProgramprogram = context.createProgram(sources).build();
CLKernelkernel = program.createKernel("myKernel");

Then call it :

CLEventkernelCompletion;
// The same kernel can be safely used by different threads, as long as setArgs + enqueueNDRange are in a synchronized blocksynchronized (kernel) {
// setArgs will throw an exception at runtime if the types / sizes of the arguments are incorrectkernel.setArgs(input, output, multFactor);
// Ask for 1-dimensional execution of length dataSize, with auto choice of local workgroup size :kernelCompletion = kernel.enqueueNDRange(queue, newint[] { dataSize }, null);
}
kernelCompletion.waitFor(); // better not to wait for it but to pass it as a dependent event to some other queuable operation (CLBuffer.read, for instance)

Read [UnderstandOpenCLKernelArguments] to know which JavaCL type is appropriate for a given OpenCL C type.

Autogenerated Type-Safe Kernel Wrapper

This documentation only covers the wrapper autogeneration from within the Maven build system, but a standalone utility is also available and can be documented on demand.

Maven Project Setup

Start by adding the following required repositories to your project's pom.xml file :

<repositories>
<repository>
<id>nativelibs4java</id>
<url>https://nativelibs4java.sourceforge.net/maven</url>
</repository>
<repository>
<id>jnaerator</id>
<url>https://jnaerator.sourceforge.net/maven</url>
</repository>
</repositories>

Setup the JavaCL Generator Maven Plugin :

<plugin>
<groupId>com.nativelibs4java</groupId>
<artifactId>javacl-generator</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>

Use the autogenerated kernel wrappers

Start by setting up the kernel :

// The JavaCL Generator Maven Plugin will autogenerate a MyProgram class out of the MyProgram.cl file :importcom.mypackage.MyProgram;
...
MyProgrammyProgram = newMyProgram(context);

Then call it :

// This call is statically typed and thread-safe :CLEventkernelCompletion = myProgram.myKernel(queue, input, output, multFactor, newint[] { dataSize }, null);

Clone this wiki locally