-
Notifications
You must be signed in to change notification settings - Fork 64
/
device.go
62 lines (54 loc) · 1.42 KB
/
device.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
package cu
//#include <cuda.h>
import "C"
import (
"fmt"
"unsafe"
"github.com/google/uuid"
)
// Device is the representation of a CUDA device
type Device int
const (
CPU Device = -1
BadDevice Device = -2
)
// Name returns the name of the device.
//
// Wrapper over cuDeviceGetName: http://docs.nvidia.com/cuda/cuda-driver-api/group__CUDA__DEVICE.html#group__CUDA__DEVICE_1gef75aa30df95446a845f2a7b9fffbb7f
func (d Device) Name() (string, error) {
size := 256
buf := make([]byte, 256)
cstr := C.CString(string(buf))
defer C.free(unsafe.Pointer(cstr))
if err := result(C.cuDeviceGetName(cstr, C.int(size), C.CUdevice(d))); err != nil {
return "", err
}
return C.GoString(cstr), nil
}
// UUID returns the UUID of the device
//
// Wrapper over cuDeviceGetUuid: https://docs.nvidia.com/cuda/cuda-driver-api/group__CUDA__DEVICE.html#group__CUDA__DEVICE_1g987b46b884c101ed5be414ab4d9e60e4
func (d Device) UUID() (retVal uuid.UUID, err error) {
ptr := &retVal
if err = result(C.cuDeviceGetUuid((*C.CUuuid)(unsafe.Pointer(ptr)), C.CUdevice(d))); err != nil {
return retVal, err
}
return retVal, nil
}
// String implementes fmt.Stringer (and runtime.stringer)
func (d Device) String() string {
if d == CPU {
return "CPU"
}
if d < 0 {
return "Invalid Device"
}
return fmt.Sprintf("GPU(%d)", int(d))
}
// IsGPU returns true if the device is a GPU.
func (d Device) IsGPU() bool {
if d < 0 {
return false
}
return true
}