Repository navigation
Expand file tree
/
Copy pathgpu_sup.go
More file actions
104 lines (92 loc) · 2.36 KB
/
Copy pathgpu_sup.go
File metadata and controls
104 lines (92 loc) · 2.36 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2026-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
//go:build gpu
// +build gpu
package cbft
/*
#cgo CFLAGS: -DGPU=1
#cgo LDFLAGS: -lcudart
#include "gpu.h"
*/
import "C"
import (
"errors"
"unsafe"
log "github.com/couchbase/clog"
)
const (
// stringBufferSize is the size of the buffer used to hold C strings returned by the GPU APIs.
// if the C string is larger than this size, it will be truncated.
stringBufferSize = 256
)
func LogGPUState() {
count, err := getGPUCount()
if err != nil {
log.Printf("GPU: error getting GPU device count: %v", err)
return
}
log.Printf("GPU: %d GPU device(s) detected", count)
for i := 0; i < count; i++ {
name, err := getGPUName(i)
if err != nil {
log.Printf("GPU: error getting name for GPU device %d: %v", i, err)
continue
}
freeMem, totalMem, err := getGPUMemoryInfo(i)
if err != nil {
log.Printf("GPU: error getting memory info for GPU device %d: %v", i, err)
continue
}
log.Printf("GPU: device %d: name=%s, totalMem=%d bytes, freeMem=%d bytes",
i, name, totalMem, freeMem)
}
}
func getGPUCount() (int, error) {
var count C.int
if c := C.gpu_device_count(&count); c != 0 {
return 0, getLastError()
}
return int(count), nil
}
func getGPUName(deviceID int) (string, error) {
buf := make([]byte, stringBufferSize)
if c := C.gpu_get_device_name(
C.int(deviceID),
(*C.char)(unsafe.Pointer(&buf[0])),
C.size_t(len(buf)),
); c != 0 {
return "", getLastError()
}
return bufferToString(buf), nil
}
func getLastError() error {
buf := make([]byte, stringBufferSize)
C.gpu_last_error(
(*C.char)(unsafe.Pointer(&buf[0])),
C.size_t(len(buf)),
)
return errors.New(bufferToString(buf))
}
func getGPUMemoryInfo(deviceID int) (uint64, uint64, error) {
var freeMem, totalMem C.size_t
if c := C.gpu_get_device_memory(
C.int(deviceID),
&totalMem,
&freeMem,
); c != 0 {
return 0, 0, getLastError()
}
return uint64(freeMem), uint64(totalMem), nil
}
func bufferToString(buf []byte) string {
n := 0
for n < len(buf) && buf[n] != 0 {
n++
}
return string(buf[:n])
}