Skip to content

Commit b002893

Browse files
committed
Add a few functions + extensions helpers
1 parent 71f9eff commit b002893

File tree

7 files changed

+157
-52
lines changed

7 files changed

+157
-52
lines changed

context.go

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,61 @@ import (
66
"unsafe"
77
)
88

9-
type ContextProperty uint
9+
type contextProperty uint
10+
11+
type ContextProperties struct {
12+
Platform *Platform
13+
// Interop
14+
InteropUserSync *bool
15+
// OpenGL
16+
GLContextKHR *uint
17+
// Windows
18+
WGL_HDC_KHR *uint
19+
}
1020

11-
const (
12-
ContextPropertyPlatform ContextProperty = 0x1084
13-
ContextPropertyInteropUserSync ContextProperty = 0x1085 // >= 1.2
14-
// GL
15-
ContextPropertyGLContextKHR ContextProperty = 0x2008
16-
ContextPropertyWGL_HDC_KHR ContextProperty = 0x200B
17-
)
21+
func (cp *ContextProperties) compile() []contextProperty {
22+
if cp == nil {
23+
return []contextProperty{0}
24+
}
25+
26+
const (
27+
ContextPropertyPlatform contextProperty = 0x1084
28+
ContextPropertyInteropUserSync contextProperty = 0x1085 // >= 1.2
29+
// GL
30+
ContextPropertyGLContextKHR contextProperty = 0x2008
31+
ContextPropertyWGL_HDC_KHR contextProperty = 0x200B
32+
)
33+
34+
var properties []contextProperty
35+
if cp.Platform != nil {
36+
properties = append(properties, ContextPropertyPlatform, contextProperty(*cp.Platform))
37+
}
38+
if cp.InteropUserSync != nil {
39+
b := contextProperty(0)
40+
if *cp.InteropUserSync {
41+
b = 1
42+
}
43+
properties = append(properties, ContextPropertyInteropUserSync, b)
44+
}
45+
if cp.GLContextKHR != nil {
46+
properties = append(properties, ContextPropertyGLContextKHR, contextProperty(*cp.GLContextKHR))
47+
}
48+
if cp.WGL_HDC_KHR != nil {
49+
properties = append(properties, ContextPropertyWGL_HDC_KHR, contextProperty(*cp.WGL_HDC_KHR))
50+
}
51+
// End of list should be marked as an extra zero
52+
return append(properties, 0)
53+
}
1854

1955
type Context uint
2056

2157
// TODO: make properties into a struct instead of weird map<uint32>
2258

23-
func (d Device) CreateContext(properties map[ContextProperty]ContextProperty) (Context, error) {
59+
func (d Device) CreateContext(properties *ContextProperties) (Context, error) {
2460
var st clStatus
2561

26-
flatten := make([]ContextProperty, 0, len(properties)*2)
27-
for k, v := range properties {
28-
flatten = append(flatten, k, ContextProperty(v))
29-
}
30-
flatten = append(flatten, 0) // End of list
31-
ctx := createContext(unsafe.Pointer(&flatten[0]), 1, []Device{d}, nil, nil, &st)
62+
flattened := properties.compile()
63+
ctx := createContext(unsafe.Pointer(&flattened[0]), 1, []Device{d}, nil, nil, &st)
3264
if st != CL_SUCCESS {
3365
return 0, errors.New("oops at create context: " + strconv.FormatInt(int64(st), 10))
3466
}

device.go

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package opencl
22

3+
import (
4+
"errors"
5+
"strings"
6+
)
7+
38
type DeviceType uint32
49

510
const (
@@ -11,16 +16,42 @@ const (
1116
DeviceTypeAll DeviceType = 0xFFFFFFFF
1217
)
1318

14-
type DeviceInfo uint32
19+
type Device uint
20+
21+
type deviceInfo uint32
1522

1623
const (
17-
DeviceInfoType DeviceInfo = 0x1000
18-
DeviceInfoAddressBits DeviceInfo = 0x100D
19-
DeviceInfoAvailable DeviceInfo = 0x1027
20-
DeviceInfoCompilerAvailable DeviceInfo = 0x1028
21-
DeviceInfoBuiltInKernels DeviceInfo = 0x103F
22-
DeviceInfoVendor DeviceInfo = 0x102C
23-
DeviceInfoDriverVersion DeviceInfo = 0x102D
24+
deviceInfoType deviceInfo = 0x1000
25+
deviceInfoAddressBits deviceInfo = 0x100D
26+
deviceInfoAvailable deviceInfo = 0x1027
27+
deviceInfoCompilerAvailable deviceInfo = 0x1028
28+
deviceInfoBuiltInKernels deviceInfo = 0x103F
29+
deviceInfoVendor deviceInfo = 0x102C
30+
deviceInfoDriverVersion deviceInfo = 0x102D
31+
32+
deviceInfoExtensions deviceInfo = 0x1030
2433
)
2534

26-
type Device uint
35+
func (d Device) getInfo(name deviceInfo) (string, error) {
36+
size := clSize(0)
37+
st := getDeviceInfo(d, name, clSize(0), nil, &size)
38+
if st != CL_SUCCESS {
39+
return "", errors.New("oops at 1st get device info")
40+
}
41+
42+
info := make([]byte, size)
43+
st = getDeviceInfo(d, name, size, info, nil)
44+
if st != CL_SUCCESS {
45+
return "", errors.New("oops at 2nd get device info")
46+
}
47+
48+
return string(info), nil
49+
}
50+
51+
func (d Device) GetExtensions() ([]Extension, error) {
52+
extensions, err := d.getInfo(deviceInfoExtensions)
53+
if err != nil {
54+
return nil, err
55+
}
56+
return strings.Split(extensions, " "), nil
57+
}

extensions.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package opencl
2+
3+
type Extension = string
4+
5+
// TODO: Not exhaustive
6+
const (
7+
// OpenCL
8+
Extension_khr_gl_sharing Extension = "cl_khr_gl_sharing"
9+
Extension_khr_fp64 Extension = "cl_khr_fp64"
10+
// Nvidia
11+
Extension_nv_pragma_unroll Extension = "cl_nv_pragma_unroll"
12+
Extension_nv_compiler_options Extension = "cl_nv_compiler_options"
13+
)

library.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var (
2222
getPlatformInfo func(platform Platform, platformInfo platformInfo, paramValueSize clSize, paramValue []byte, paramValueSizeRet *clSize) clStatus
2323
// Device
2424
getDeviceIDs func(platform Platform, deviceType DeviceType, numEntries uint32, devices []Device, numDevices *uint32) clStatus
25-
getDeviceInfo func(device Device, deviceInfo DeviceInfo, paramValueSize clSize, paramValue []byte, paramValueSizeRet *uint32) clStatus
25+
getDeviceInfo func(device Device, deviceInfo deviceInfo, paramValueSize clSize, paramValue []byte, paramValueSizeRet *clSize) clStatus
2626
// Context
2727
createContext func(properties unsafe.Pointer, numDevices uint32, devices []Device, pfnNotify *createContextNotifyFunc, userData []byte, errCodeRet *clStatus) Context
2828
releaseContext func(ctx Context) clStatus
@@ -40,8 +40,6 @@ var (
4040
enqueueMapBuffer func(queue CommandQueue, buffer Buffer, blockingMap bool, mapFlags MapFlag, offset, size clSize, numEventsWaitList uint, eventWaitList []Event, event *Event, errCodeRet *clStatus) uintptr
4141
enqueueUnmapMemObject func(queue CommandQueue, buffer Buffer, mappedPtr unsafe.Pointer, numEventsWaitList uint, eventWaitList []Event, event *Event) clStatus
4242
enqueueMapImage func(queue CommandQueue, image Buffer, blockingMap bool, mapFlags MapFlag, origin, region [3]clSize, imageRowPitch, imageSlicePitch *clSize, numEventsWaitList uint, eventWaitList []Event, event *Event, errCodeRet *clStatus) uintptr
43-
enqueueAcquireGLObjects func(queue CommandQueue, numObjects uint32, memObjects unsafe.Pointer, numEventsInWaitList uint32, eventWaitList []Event, event *Event) clStatus
44-
enqueueReleaseGLObjects func(queue CommandQueue, numObjects uint32, memObjects unsafe.Pointer, numEventsInWaitList uint32, eventWaitList []Event, event *Event) clStatus
4543
finishCommandQueue func(queue CommandQueue) clStatus
4644
flushCommandQueue func(queue CommandQueue) clStatus
4745
releaseCommandQueue func(queue CommandQueue) clStatus
@@ -56,10 +54,6 @@ var (
5654
// Buffer
5755
getMemObjectInfo func(buffer Buffer, memInfo memInfo, paramValueSize clSize, paramValue unsafe.Pointer, paramValueSizeRet *clSize) clStatus
5856
releaseMemObject func(buffer Buffer) clStatus
59-
// GL
60-
createFromGLTexture func(ctx Context, memFlags MemFlag, textureTarget GLEnum, mipLevel GLInt, texture GLUint, errCodeRet *clStatus) Buffer
61-
getGLObjectInfo func(memObj Buffer, objectType *CLGLObjectType, objectName *GLUint) clStatus
62-
getGLTextureInfo func(memObj Buffer, paramName CLGLTextureInfo, paramValueSize clSize, paramValue unsafe.Pointer, paramValueSizeRet *clSize) clStatus
6357
)
6458

6559
func Initialize() error {
@@ -105,6 +99,24 @@ func Initialize() error {
10599
// Buffer
106100
purego.RegisterLibFunc(&getMemObjectInfo, handle, "clGetMemObjectInfo")
107101
purego.RegisterLibFunc(&releaseMemObject, handle, "clReleaseMemObject")
102+
103+
return nil
104+
}
105+
106+
var (
107+
// GL
108+
createFromGLTexture func(ctx Context, memFlags MemFlag, textureTarget GLEnum, mipLevel GLInt, texture GLUint, errCodeRet *clStatus) Buffer
109+
getGLObjectInfo func(memObj Buffer, objectType *CLGLObjectType, objectName *GLUint) clStatus
110+
getGLTextureInfo func(memObj Buffer, paramName CLGLTextureInfo, paramValueSize clSize, paramValue unsafe.Pointer, paramValueSizeRet *clSize) clStatus
111+
enqueueAcquireGLObjects func(queue CommandQueue, numObjects uint32, memObjects unsafe.Pointer, numEventsInWaitList uint32, eventWaitList []Event, event *Event) clStatus
112+
enqueueReleaseGLObjects func(queue CommandQueue, numObjects uint32, memObjects unsafe.Pointer, numEventsInWaitList uint32, eventWaitList []Event, event *Event) clStatus
113+
)
114+
115+
func InitializeGLSharing() error {
116+
handle, err := loadLibrary()
117+
if err != nil {
118+
return err
119+
}
108120
// GL
109121
purego.RegisterLibFunc(&createFromGLTexture, handle, "clCreateFromGLTexture")
110122
purego.RegisterLibFunc(&enqueueAcquireGLObjects, handle, "clEnqueueAcquireGLObjects")

opencl_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,31 @@ func Test_Compute(m *testing.T) {
4141
log.Fatal("err:", err)
4242
}
4343
fmt.Println("name:", name)
44-
44+
4545
version, err := platforms[0].GetVersion()
4646
if err != nil {
4747
log.Fatal("err:", err)
4848
}
4949
fmt.Println("version:", version)
5050

51+
platformExtensions, err := platforms[0].GetExtensions()
52+
if err != nil {
53+
log.Fatal("err:", err)
54+
}
55+
fmt.Println("platform extensions:", platformExtensions)
56+
5157
devices, err := platforms[0].GetDevices(opencl.DeviceTypeAll)
5258
if err != nil {
5359
log.Fatal("err:", err)
5460
}
5561
fmt.Println("devices:", len(devices))
5662

63+
deviceExtensions, err := devices[0].GetExtensions()
64+
if err != nil {
65+
log.Fatal("err:", err)
66+
}
67+
fmt.Println("device extensions:", deviceExtensions)
68+
5769
ctx, err := devices[0].CreateContext(nil)
5870
if err != nil {
5971
log.Fatal("err:", err)

platform.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package opencl
22

33
import (
44
"errors"
5+
"strings"
56
)
67

78
type Platform uint
@@ -25,11 +26,11 @@ func GetPlatforms() ([]Platform, error) {
2526
type platformInfo uint
2627

2728
const (
28-
PlatformInfoProfile platformInfo = 0x0900
29-
PlatformInfoVersion platformInfo = 0x0901
30-
PlatformInfoName platformInfo = 0x0902
31-
PlatformInfoVendor platformInfo = 0x0903
32-
PlatformInfoExtensions platformInfo = 0x0904
29+
platformInfoProfile platformInfo = 0x0900
30+
platformInfoVersion platformInfo = 0x0901
31+
platformInfoName platformInfo = 0x0902
32+
platformInfoVendor platformInfo = 0x0903
33+
platformInfoExtensions platformInfo = 0x0904
3334
)
3435

3536
func (p Platform) getInfo(name platformInfo) (string, error) {
@@ -49,23 +50,27 @@ func (p Platform) getInfo(name platformInfo) (string, error) {
4950
}
5051

5152
func (p Platform) GetProfile() (string, error) {
52-
return p.getInfo(PlatformInfoProfile)
53+
return p.getInfo(platformInfoProfile)
5354
}
5455

5556
func (p Platform) GetVersion() (string, error) {
56-
return p.getInfo(PlatformInfoVersion)
57+
return p.getInfo(platformInfoVersion)
5758
}
5859

5960
func (p Platform) GetName() (string, error) {
60-
return p.getInfo(PlatformInfoName)
61+
return p.getInfo(platformInfoName)
6162
}
6263

6364
func (p Platform) GetVendor() (string, error) {
64-
return p.getInfo(PlatformInfoVendor)
65+
return p.getInfo(platformInfoVendor)
6566
}
6667

67-
func (p Platform) GetExtensions() (string, error) {
68-
return p.getInfo(PlatformInfoExtensions)
68+
func (p Platform) GetExtensions() ([]Extension, error) {
69+
extensions, err := p.getInfo(platformInfoExtensions)
70+
if err != nil {
71+
return nil, err
72+
}
73+
return strings.Split(extensions, " "), nil
6974
}
7075

7176
func (p Platform) GetDevices(deviceType DeviceType) ([]Device, error) {

program.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ const (
1616
programBuildLog programBuildInfo = 0x1183
1717
)
1818

19-
type CLVersion string
19+
type Version string
2020

2121
const (
22-
CLVersion1_0 CLVersion = "CL1.0"
23-
CLVersion1_1 CLVersion = "CL1.1"
24-
CLVersion1_2 CLVersion = "CL1.2"
25-
CLVersion2_0 CLVersion = "CL2.0"
26-
CLVersion3_0 CLVersion = "CL3.0"
22+
Version1_0 Version = "CL1.0"
23+
Version1_1 Version = "CL1.1"
24+
Version1_2 Version = "CL1.2"
25+
Version2_0 Version = "CL2.0"
26+
Version3_0 Version = "CL3.0"
2727
)
2828

2929
type ProgramBuildOptions struct {
3030
// Preprocessor options
3131
Warnings bool
3232
Macros map[string]string
3333
DirectoryIncludes []string
34-
CLVersion CLVersion
34+
Version Version
3535
// Math intrinsics options
3636
SinglePrecisionConstant bool
3737
MadEnable bool
@@ -53,8 +53,8 @@ func (po *ProgramBuildOptions) String() string {
5353
sb.WriteString("-w")
5454
sb.WriteRune(' ')
5555
}
56-
if po.CLVersion != "" {
57-
sb.WriteString("-cl-std=" + string(po.CLVersion))
56+
if po.Version != "" {
57+
sb.WriteString("-cl-std=" + string(po.Version))
5858
sb.WriteRune(' ')
5959
}
6060
// Math intrinsics

0 commit comments

Comments
 (0)