-
Notifications
You must be signed in to change notification settings - Fork 2
/
memory.go
57 lines (50 loc) · 1.43 KB
/
memory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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 memory Memory
memory.memory = (*C.cl_mem)(C.malloc(C.sizeof_cl_mem))
var err C.cl_int
*memory.memory = C.clCreateBuffer(*context.context, flags.asClFlags(), C.size_t(size), hostPtr, &err)
if err != C.CL_SUCCESS {
C.free(unsafe.Pointer(memory.memory))
return nil, fmt.Errorf("failed to create buffer: %d", err)
}
return &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)
}
C.free(unsafe.Pointer(m.memory))
return nil
}