-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnvml_dl.go
63 lines (52 loc) · 1.51 KB
/
nvml_dl.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
// Copyright (c) 2015-2018, NVIDIA CORPORATION. All rights reserved.
package nvml
import (
"unsafe"
)
/*
#include <dlfcn.h>
#include "nvml.h"
// We wrap the call to nvmlInit() here to ensure that we pick up the correct
// version of this call. The macro magic in nvml.h that #defines the symbol
// 'nvmlInit' to 'nvmlInit_v2' is unfortunately lost on cgo.
static nvmlReturn_t nvmlInit_dl(void) {
return nvmlInit();
}
*/
import "C"
type dlhandles struct{ handles []unsafe.Pointer }
var dl dlhandles
// Initialize NVML, opening a dynamic reference to the NVML library in the process.
func (dl *dlhandles) nvmlInit() C.nvmlReturn_t {
handle := C.dlopen(C.CString("libnvidia-ml.so.1"), C.RTLD_LAZY|C.RTLD_GLOBAL)
if handle == C.NULL {
return C.NVML_ERROR_LIBRARY_NOT_FOUND
}
dl.handles = append(dl.handles, handle)
return C.nvmlInit_dl()
}
// Shutdown NVML, closing our dynamic reference to the NVML library in the process.
func (dl *dlhandles) nvmlShutdown() C.nvmlReturn_t {
ret := C.nvmlShutdown()
if ret != C.NVML_SUCCESS {
return ret
}
for _, handle := range dl.handles {
err := C.dlclose(handle)
if err != 0 {
return C.NVML_ERROR_UNKNOWN
}
}
return C.NVML_SUCCESS
}
// Check to see if a specific symbol is present in the NVMl library.
func (dl *dlhandles) lookupSymbol(symbol string) C.nvmlReturn_t {
for _, handle := range dl.handles {
C.dlerror()
C.dlsym(handle, C.CString(symbol))
if unsafe.Pointer(C.dlerror()) == C.NULL {
return C.NVML_SUCCESS
}
}
return C.NVML_ERROR_FUNCTION_NOT_FOUND
}