This repository has been archived by the owner on Jan 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
bindings.go
82 lines (67 loc) · 2.08 KB
/
bindings.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
package cuda
// #cgo LDFLAGS: -lcudart_static -ldl -lrt
// #include <stdlib.h>
// #include <cuda_runtime_api.h>
import "C"
import (
"fmt"
"unsafe"
)
type handle struct{ dev C.int }
type deviceProp struct {
major int
minor int
multiProcessorCount uint
ECCEnabled bool
totalGlobalMem uint
sharedMemPerMultiprocessor uint
totalConstMem uint
l2CacheSize uint
memoryClockRate uint
memoryBusWidth uint
}
func errorString(ret C.cudaError_t) error {
if ret == C.cudaSuccess {
return nil
}
err := C.GoString(C.cudaGetErrorString(ret))
return fmt.Errorf("cuda: %v", err)
}
func driverGetVersion() (int, error) {
var driver C.int
r := C.cudaDriverGetVersion(&driver)
return int(driver), errorString(r)
}
func deviceGetByPCIBusId(busid string) (handle, error) {
var dev C.int
id := C.CString(busid)
r := C.cudaDeviceGetByPCIBusId(&dev, id)
C.free(unsafe.Pointer(id))
return handle{dev}, errorString(r)
}
func deviceCanAccessPeer(h1, h2 handle) (bool, error) {
var ok C.int
r := C.cudaDeviceCanAccessPeer(&ok, h1.dev, h2.dev)
return (ok != 0), errorString(r)
}
func deviceReset() error {
return errorString(C.cudaDeviceReset())
}
func (h handle) getDeviceProperties() (*deviceProp, error) {
var props C.struct_cudaDeviceProp
r := C.cudaGetDeviceProperties(&props, h.dev)
p := &deviceProp{
major: int(props.major),
minor: int(props.minor),
multiProcessorCount: uint(props.multiProcessorCount),
ECCEnabled: bool(props.ECCEnabled != 0),
totalGlobalMem: uint(props.totalGlobalMem),
sharedMemPerMultiprocessor: uint(props.sharedMemPerMultiprocessor),
totalConstMem: uint(props.totalConstMem),
l2CacheSize: uint(props.l2CacheSize),
memoryClockRate: uint(props.memoryClockRate),
memoryBusWidth: uint(props.memoryBusWidth),
}
return p, errorString(r)
}