Skip to content
forked from Dadido3/blackcl

highly opinionated OpenCL bindings for Go with some sugar

License

Notifications You must be signed in to change notification settings

opencl-pure/highCL

 
 

Repository files navigation

highCL

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!

goal

  • 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

not goal

  • be faster as cgo version, purego is using same mechanism as cgo

examples

0

examples dictionary:

Sierpinski Triangle Mandelbrot Julia Mandelbrot Basic
Mandelbrot Pseudo Random Colors Sierpinski Triangle 2 Julia Set Julia Basic

1

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))
}

About

highly opinionated OpenCL bindings for Go with some sugar

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%