Petr Schreiber
03-10-2012, 11:55
As mentioned in previous example (http://www.thinbasic.com/community/showthread.php?11874-Chapter-2-example-4-Creating-a-program-from-file), the calculation in OpenCL is performed by executing functions named kernels in OpenCL.
We also learned that kernels are collected in a program (OpenCL name for library of functions), typically stored as separate CL file.
Before we jump on the execution of kernels, we will have a look at one useful feature - detecting kernel names in a program.
In my older OpenCL examples, I did this by parsing CL file using ThinBASIC string functions, but there is more automatic way directly in OpenCL.
After building the program, you can use clGetKernelInfo to retrieve various kinds of information about each kernel, including the name.
The attached example examines this simple file:
__kernel void add(__global float *a,
__global float *b,
__global float *c) {
*c = *a + *b;
}
__kernel void sub(__global float *a,
__global float *b,
__global float *c) {
*c = *a - *b;
}
__kernel void mult(__global float *a,
__global float *b,
__global float *c) {
*c = *a * *b;
}
__kernel void div(__global float *a,
__global float *b,
__global float *c) {
*c = *a / *b;
}
You will need the latest ThinBASIC and OpenCL headers (http://www.thinbasic.com/community/showthread.php?10159-OpenCL-Headers-Updated-Sep-15-2011) to run it + of course modern GPU or CPU (http://www.thinbasic.com/community/showthread.php?10161-OpenCL-Supported-hardware).
We also learned that kernels are collected in a program (OpenCL name for library of functions), typically stored as separate CL file.
Before we jump on the execution of kernels, we will have a look at one useful feature - detecting kernel names in a program.
In my older OpenCL examples, I did this by parsing CL file using ThinBASIC string functions, but there is more automatic way directly in OpenCL.
After building the program, you can use clGetKernelInfo to retrieve various kinds of information about each kernel, including the name.
The attached example examines this simple file:
__kernel void add(__global float *a,
__global float *b,
__global float *c) {
*c = *a + *b;
}
__kernel void sub(__global float *a,
__global float *b,
__global float *c) {
*c = *a - *b;
}
__kernel void mult(__global float *a,
__global float *b,
__global float *c) {
*c = *a * *b;
}
__kernel void div(__global float *a,
__global float *b,
__global float *c) {
*c = *a / *b;
}
You will need the latest ThinBASIC and OpenCL headers (http://www.thinbasic.com/community/showthread.php?10159-OpenCL-Headers-Updated-Sep-15-2011) to run it + of course modern GPU or CPU (http://www.thinbasic.com/community/showthread.php?10161-OpenCL-Supported-hardware).