This is fork of blackcl, I believe that is not about black magic,
This package provide high level wrapper to OpenCL,
that means it is completly hiding call of OpenCL. This package use pureCL to call OpenCL WITHOUT CGO!!! And it is continue of middleCL
Absence cgo means you do not need c compiler, it is powered by. purego and inspired by libopencl-stub and Zyko0's opencl.
Thank to all of them!
- easy to multiplatform (thank purego)
- easy find path (custumize path to openclLib shared library)
- easy to compile, we do not need cgo and not need knowing link to shared library
- try purego and bring opencl on android without complicate link
- be high level and allow more than
[]float32
vectors
- be faster as cgo version, purego is using same mechanism as cgo
package main
import (
"fmt"
opencl "github.com/opencl-pure/highCL"
pure "github.com/opencl-pure/pureCL"
"log"
)
func main() {
//init with version of OpenCL and variadic special paths (if you know)
err := opencl.Init(pure.Version2_0/*, "some/special/path/opencl.dll", "some/special/path/opencl.so"*/)
if err != nil {
log.Fatal(err)
}
//Do not create platforms/devices/contexts/queues/...
//Just get the GPU
d, err := opencl.GetDefaultDevice()
if err != nil {
log.Fatal(err)
}
defer d.Release()
data := []float32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} // []int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
// has several kinds of device memory object: Bytes, Vector, Image
//allocate buffer on the device (16 elems of float32)
v, err := d.NewVector(data)
if err != nil {
log.Fatal(err)
}
defer v.Release()
//an complicated kernel
const kernelSource = `
__kernel void addOne(__global float* data) {
const int i = get_global_id (0);
data[i] += 1;
}
`
//Add program source to device, get kernel
_, err = d.AddProgram(fmt.Sprint(kernelSource))
if err != nil {
log.Fatal(err)
}
k, err := d.Kernel("addOne")
if err != nil {
log.Fatal(err)
}
//run kernel (global work size 16 and local work size 1)
event, err := k.Global(16).Local(1).Run(nil, v)
if err != nil {
log.Fatal(err)
}
defer event.Release()
//Get data from vector
newData, err := v.Data()
if err != nil {
log.Fatal(err)
}
//prints out [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
fmt.Println(newData.Interface().([]float32))
}