middleCL

package module
v0.0.0-...-0feb991 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 17, 2024 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

pure

This is real fork of Zyko0's opencl, big thank. My modification call pureCL(simplyfy Zyko0's opencl) as button layer.
This package provide higher level wrapper to OpenCL, that means it provide GO error handling, handle OpenCL errors to GO errors, but still it is near 1:1 wrapper

goal

  • low level wrap of OpenCL
  • GO error handling
  • try to have all functions of OpenCL (so if you have some, give PR)
  • 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

not goal

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

examples

example

package main

import (
	"fmt"
	constants "github.com/opencl-pure/constantsCL"
	middle "github.com/opencl-pure/middleCL"
	pure "github.com/opencl-pure/pureCL"
	"log"
)

const (
	dataSize = 32
)

var (
	code = `
        __kernel void set_i(__global float* out){
		size_t i = get_global_id(0);
		out[i] = i;
	}`
)

func main() {
	//init with version of OpenCL and variadic special paths (if you know)
	err := middle.Init(pure.Version2_0/*, "special_path1/openclLib.dll", "special_path1/openclLib.so" */)
	if err != nil {
		log.Fatal("err:", err)
	}

	platforms, err := middle.GetPlatforms()
	if err != nil {
		log.Fatal("err:", err)
	}
	fmt.Println("platforms", len(platforms))

	name, err := platforms[0].GetName()
	if err != nil {
		log.Fatal("err:", err)
	}
	fmt.Println("name:", name)

	version, err := platforms[0].GetVersion()
	if err != nil {
		log.Fatal("err:", err)
	}
	fmt.Println("version:", version)

	platformExtensions, err := platforms[0].GetExtensions()
	if err != nil {
		log.Fatal("err:", err)
	}
	fmt.Println("platform extensions:", platformExtensions)

	devices, err := platforms[0].GetDevices(constants.CL_DEVICE_TYPE_ALL)
	if err != nil {
		log.Fatal("err:", err)
	}
	fmt.Println("devices:", len(devices))

	deviceExtensions, err := devices[0].GetExtensions()
	if err != nil {
		log.Fatal("err:", err)
	}
	fmt.Println("device extensions:", deviceExtensions)

	ctx, err := devices[0].CreateContext(nil)
	if err != nil {
		log.Fatal("err:", err)
	}
	defer ctx.Release()
	fmt.Println("context:", ctx)

	queue, err := ctx.CreateCommandQueue(&devices[0])
	if err != nil {
		log.Fatal("err:", err)
	}
	defer queue.Release()
	fmt.Println("queue:", queue)

	program, err := ctx.CreateProgram(fmt.Sprint(code))
	if err != nil {
		log.Fatal("err:", err)
	}
	defer program.Release()
	fmt.Println("program:", program)

	logs, err := program.Build(&devices[0], nil)
	if err != nil {
		log.Fatal("err:", err)
	}
	fmt.Println("logs:", logs)

	kernel, err := program.CreateKernel("set_i")
	if err != nil {
		log.Fatal("err:", err)
	}
	defer kernel.Release()
	fmt.Println("kernel:", kernel)

	data := make([]float32, dataSize)
	bufferData := middle.GetBufferData(data)
	fmt.Println("buffer data:", bufferData.DataSize, bufferData.Pointer)
	buffer, err := ctx.CreateBuffer(
		[]pure.MemFlag{
			constants.CL_MEM_WRITE_ONLY,
			constants.CL_MEM_ALLOC_HOST_PTR,
		},
		uint(bufferData.DataSize),
	)
	if err != nil {
		log.Fatal("err:", err)
	}
	defer buffer.Release()
	fmt.Println("buffer:", buffer)

	size, err := buffer.Size()
	if err != nil {
		log.Fatal("err:", err)
	}
	fmt.Println("buffer size:", size)
	arg, _ := middle.NewKernelArg(buffer)
	err = kernel.SetArg(0, arg)
	if err != nil {
		log.Fatal("err:", err)
	}

	err = queue.EnqueueNDRangeKernel(kernel, 1, nil, []uint64{dataSize}, nil)
	if err != nil {
		log.Fatal("err:", err)
	}

	_ = queue.Flush()
	_ = queue.Finish()

	err = queue.EnqueueReadBuffer(buffer, true, middle.GetBufferData(data))
	if err != nil {
		log.Fatal("err:", err)
	}
	fmt.Println("data:", data)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBufferData

func GetBufferData[T pure.BufferType](data []T) *pure.BufferData

func GetImageBufferData

func GetImageBufferData(img image.RGBA) *pure.ImageData

func Init

func Init(v pure.Version, paths ...string) error

Types

type Buffer

type Buffer struct {
	B pure.Buffer
}

func (*Buffer) GetGLObjectInfo

func (b *Buffer) GetGLObjectInfo() (pure.CLGLObjectType, error)

func (*Buffer) GetGLTextureInfo

func (b *Buffer) GetGLTextureInfo(info pure.CLGLTextureInfo) (uint32, error)

func (*Buffer) Release

func (b *Buffer) Release() error

func (*Buffer) Size

func (b *Buffer) Size() (uint, error)

type CommandQueue

type CommandQueue struct {
	C pure.CommandQueue
}

func (*CommandQueue) EnqueueAcquireGLObjects

func (cq *CommandQueue) EnqueueAcquireGLObjects(objects []Buffer) error

func (*CommandQueue) EnqueueBarrier

func (cq *CommandQueue) EnqueueBarrier() error

func (*CommandQueue) EnqueueMapBuffer

func (cq *CommandQueue) EnqueueMapBuffer(buffer Buffer, blockingMap bool, flags []pure.MapFlag, data *pure.BufferData) error

func (*CommandQueue) EnqueueMapImage

func (cq *CommandQueue) EnqueueMapImage(image Buffer, blockingMap bool, flags []pure.MapFlag, data *pure.ImageData) error

func (*CommandQueue) EnqueueNDRangeKernel

func (cq *CommandQueue) EnqueueNDRangeKernel(kernel *Kernel, workDim uint, globalOffsets, globalWorkSizes, localWorkSizes []uint64) error

func (*CommandQueue) EnqueueReadBuffer

func (cq *CommandQueue) EnqueueReadBuffer(buffer *Buffer, blockingRead bool, data *pure.BufferData) error

func (*CommandQueue) EnqueueReadImage

func (cq *CommandQueue) EnqueueReadImage(image *Image, blockingRead bool, data *pure.ImageData) error

func (*CommandQueue) EnqueueReleaseGLObjects

func (cq *CommandQueue) EnqueueReleaseGLObjects(objects []Buffer) error

func (*CommandQueue) EnqueueUnmapBuffer

func (cq *CommandQueue) EnqueueUnmapBuffer(buffer Buffer, data *pure.BufferData) error

func (*CommandQueue) EnqueueWriteBuffer

func (cq *CommandQueue) EnqueueWriteBuffer(buffer *Buffer, blockingWrite bool, data *pure.BufferData) error

func (*CommandQueue) EnqueueWriteImage

func (cq *CommandQueue) EnqueueWriteImage(image *Image, blockingRead bool, data *pure.ImageData) error

func (*CommandQueue) Finish

func (cq *CommandQueue) Finish() error

func (*CommandQueue) Flush

func (cq *CommandQueue) Flush() error

func (*CommandQueue) Release

func (cq *CommandQueue) Release() error

type CommandQueueProperty

type CommandQueueProperty pure.CommandQueueProperty

type Context

type Context struct {
	C pure.Context
}

func (Context) CreateBuffer

func (c Context) CreateBuffer(flags []pure.MemFlag, size uint) (*Buffer, error)

func (*Context) CreateCommandQueue

func (c *Context) CreateCommandQueue(device *Device) (*CommandQueue, error)

func (*Context) CreateCommandQueueWithProperties

func (c *Context) CreateCommandQueueWithProperties(device Device, properties []pure.CommandQueueProperty) (*CommandQueue, error)

func (*Context) CreateFromGLTexture

func (c *Context) CreateFromGLTexture(flags []pure.MemFlag, target pure.GLEnum, texture pure.GLUint) (*Buffer, error)

func (Context) CreateImage2D

func (c Context) CreateImage2D(flags []pure.MemFlag, format pure.ImageFormat, width, height, rowPitch int, data unsafe.Pointer) (*Image, error)

func (*Context) CreateProgram

func (c *Context) CreateProgram(source string) (*Program, error)

func (*Context) Release

func (c *Context) Release() error

type ContextProperties

type ContextProperties pure.ContextProperties

type Device

type Device struct {
	D pure.Device
}

func (*Device) CreateContext

func (d *Device) CreateContext(properties *ContextProperties) (*Context, error)

func (*Device) GetExtensions

func (d *Device) GetExtensions() ([]pure.Extension, error)

type Event

type Event pure.Event

type Image

type Image struct {
	B pure.Buffer
}

type Kernel

type Kernel struct {
	K pure.Kernel
}

func (*Kernel) Release

func (k *Kernel) Release() error

func (*Kernel) SetArg

func (k *Kernel) SetArg(index uint, arg *KernelArg) error

type KernelArg

type KernelArg struct {
	KA pure.KernelArg
}

func NewKernelArg

func NewKernelArg(arg interface{}) (*KernelArg, error)

type Platform

type Platform struct {
	P pure.Platform
}

func GetPlatforms

func GetPlatforms() ([]Platform, error)

func (*Platform) GetDevices

func (p *Platform) GetDevices(deviceType pure.DeviceType) ([]Device, error)

func (*Platform) GetExtensions

func (p *Platform) GetExtensions() ([]pure.Extension, error)

func (*Platform) GetName

func (p *Platform) GetName() (string, error)

func (*Platform) GetProfile

func (p *Platform) GetProfile() (string, error)

func (*Platform) GetVendor

func (p *Platform) GetVendor() (string, error)

func (*Platform) GetVersion

func (p *Platform) GetVersion() (string, error)

type Program

type Program struct {
	P pure.Program
}

func (*Program) Build

func (p *Program) Build(device *Device, opts *ProgramBuildOptions) (string, error)

func (*Program) CreateKernel

func (p *Program) CreateKernel(name string) (*Kernel, error)

func (*Program) Release

func (p *Program) Release() error

type ProgramBuildOptions

type ProgramBuildOptions struct {
	// Preprocessor options
	Warnings          bool
	Macros            map[string]string
	DirectoryIncludes []string
	Version           pure.Version
	// Math intrinsics options
	SinglePrecisionConstant bool
	MadEnable               bool
	NoSignedZeros           bool
	FastRelaxedMaths        bool
	// Extensions
	NvidiaVerbose bool
}

func (*ProgramBuildOptions) String

func (po *ProgramBuildOptions) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址