Commit bf5ebeab by Philipp Adolf

Enable creating and releasing buffers

parent 0d8be358
......@@ -20,7 +20,7 @@ It is mainly developed and tested on Linux and with Intel and Nvidia GPUs. It sh
- [x] creating and releasing command queues
- [ ] querying info
- [ ] buffers
- [ ] creating and releasing buffers
- [*] creating and releasing buffers
- [ ] writing buffers
- [ ] reading buffers
- [ ] querying info
......
......@@ -69,3 +69,7 @@ func (c Context) Devices() []*Device {
func (c Context) CreateCommandQueue(device Device, properties *CommandQueueProperties) (*CommandQueue, error) {
return createCommandQueue(c, device, properties)
}
func (c Context) CreateBuffer(flags MemoryFlags, size uintptr, hostPtr unsafe.Pointer) (*Memory, error) {
return createBuffer(c, flags, size, hostPtr)
}
package opencl
/*
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif
*/
import "C"
import (
"fmt"
"unsafe"
)
type MemoryFlags int
const (
MemReadWrite = MemoryFlags(C.CL_MEM_READ_WRITE)
MemWriteOnly = MemoryFlags(C.CL_MEM_WRITE_ONLY)
MemReadOnly = MemoryFlags(C.CL_MEM_READ_ONLY)
MemUseHostPtr = MemoryFlags(C.CL_MEM_USE_HOST_PTR)
MemAllocHostPtr = MemoryFlags(C.CL_MEM_ALLOC_HOST_PTR)
MemCopyHostPtr = MemoryFlags(C.CL_MEM_COPY_HOST_PTR)
MemHostWriteOnly = MemoryFlags(C.CL_MEM_HOST_WRITE_ONLY)
MemHostReadOnly = MemoryFlags(C.CL_MEM_READ_ONLY)
MemHostNoAccess = MemoryFlags(C.CL_MEM_HOST_NO_ACCESS)
)
func (f MemoryFlags) asClFlags() C.cl_mem_flags {
return C.cl_mem_flags(f)
}
type Memory struct {
memory C.cl_mem
}
func createBuffer(context Context, flags MemoryFlags, size uintptr, hostPtr unsafe.Pointer) (*Memory, error) {
var err C.cl_int
memory := C.clCreateBuffer(context.context, flags.asClFlags(), C.size_t(size), hostPtr, &err)
if err != C.CL_SUCCESS {
return nil, fmt.Errorf("failed to create buffer: %d", err)
}
return &Memory{memory}, nil
}
func (m Memory) Release() error {
err := C.clReleaseMemObject(m.memory)
if err != C.CL_SUCCESS {
return fmt.Errorf("failed to release memory object: %d", err)
}
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment