Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package onnxruntime
 
/*
#include "cshim.h"
#include <stdlib.h>
*/
import "C"
import (
    "fmt"
    "math"
    "os"
    "path/filepath"
    "sync"
    "sync/atomic"
    "unsafe"
)
 
var (
    mu           sync.Mutex
    libPath      string
    libModule    libHandle
    initialized  atomic.Bool
    shutdown     atomic.Bool
    apiVersion   int
    env          *C.OrtEnv
    cpuMemInfo   *C.OrtMemoryInfo
    sessionCount atomic.Int64
)
 
// SetSharedLibraryPath sets the path to the ONNX Runtime shared library.
// Must be called before Init. Ignored after a successful Init.
func SetSharedLibraryPath(path string) {
    mu.Lock()
    defer mu.Unlock()
    if !initialized.Load() {
        libPath = path
    }
}
 
// Init initializes the ONNX Runtime environment. It is retryable on failure
// and idempotent after success.
func Init() error {
    mu.Lock()
    defer mu.Unlock()
 
    if shutdown.Load() {
        return errShutdown
    }
    if initialized.Load() {
        return nil
    }
 
    path := resolveLibPath()
    fn, handle, err := loadLibrary(path)
    if err != nil {
        return wrapErr("load library", err)
    }
    // Init is retryable: unload the library on every failure below, or a
    // failed attempt leaks the mapping for the lifetime of the process.
 
    var cVersion C.int
    rc := C.ort_init_api(fn, &cVersion)
    if rc == 1 {
        closeLibrary(handle)
        return wrapErr("init api", fmt.Errorf("OrtGetApiBase returned NULL"))
    }
    if rc == 2 {
        closeLibrary(handle)
        return wrapErr("init api", fmt.Errorf("GetApi failed; ORT library may be too old (need API >= %d)", C.ORT_GO_API_VERSION_MIN))
    }
    apiVersion = int(cVersion)
 
    cLogID := C.CString("onnxruntime-go")
    defer C.free(unsafe.Pointer(cLogID))
 
    var ortEnv *C.OrtEnv
    if err := checkStatus(C.ort_CreateEnv(C.ORT_LOGGING_LEVEL_WARNING, cLogID, &ortEnv)); err != nil {
        closeLibrary(handle)
        return wrapErr("create env", err)
    }
 
    var memInfo *C.OrtMemoryInfo
    if err := checkStatus(C.ort_CreateCpuMemoryInfo(C.OrtDeviceAllocator, C.OrtMemTypeDefault, &memInfo)); err != nil {
        C.ort_ReleaseEnv(ortEnv)
        closeLibrary(handle)
        return wrapErr("create cpu memory info", err)
    }
 
    env = ortEnv
    cpuMemInfo = memInfo
    libModule = handle
    initialized.Store(true)
    return nil
}
 
// GetVersion returns the ORT library version string (e.g. "1.27.0").
// Must be called after Init.
func GetVersion() (string, error) {
    mu.Lock()
    defer mu.Unlock()
    if !initialized.Load() {
        return "", errNotInitialized
    }
    return C.GoString(C.ort_GetVersionString()), nil
}
 
// APIVersion returns the ORT API version negotiated during Init.
func APIVersion() int {
    mu.Lock()
    defer mu.Unlock()
    return apiVersion
}
 
// IsInitialized reports whether Init has completed successfully.
func IsInitialized() bool {
    mu.Lock()
    defer mu.Unlock()
    return initialized.Load()
}
 
// Shutdown releases the ONNX Runtime environment. It returns an error if
// any sessions are still open. After Shutdown, Init cannot be called again.
// Shutdown must not run concurrently with other calls into this package:
// it releases the shared environment that those calls use.
func Shutdown() error {
    mu.Lock()
    defer mu.Unlock()
 
    if shutdown.Load() {
        return nil
    }
    if !initialized.Load() {
        shutdown.Store(true)
        return nil
    }
 
    if n := sessionCount.Load(); n > 0 {
        return fmt.Errorf("ort: cannot shut down: %d session(s) still open", n)
    }
 
    C.ort_ReleaseMemoryInfo(cpuMemInfo)
    cpuMemInfo = nil
    C.ort_ReleaseEnv(env)
    env = nil
    initialized.Store(false)
    shutdown.Store(true)
    return nil
}
 
// EnableTelemetry enables platform telemetry collection.
func EnableTelemetry() error {
    mu.Lock()
    defer mu.Unlock()
    if !initialized.Load() {
        return errNotInitialized
    }
    return wrapErr("enable telemetry", checkStatus(C.ort_EnableTelemetryEvents(env)))
}
 
// DisableTelemetry disables platform telemetry collection.
func DisableTelemetry() error {
    mu.Lock()
    defer mu.Unlock()
    if !initialized.Load() {
        return errNotInitialized
    }
    return wrapErr("disable telemetry", checkStatus(C.ort_DisableTelemetryEvents(env)))
}
 
// AvailableProviders returns the execution providers available in the loaded
// ORT library.
func AvailableProviders() ([]string, error) {
    mu.Lock()
    if !initialized.Load() {
        mu.Unlock()
        return nil, errNotInitialized
    }
    mu.Unlock()
 
    var cProviders **C.char
    var count C.int
    if err := checkStatus(C.ort_GetAvailableProviders(&cProviders, &count)); err != nil {
        return nil, wrapErr("get available providers", err)
    }
    defer C.ort_ReleaseAvailableProviders(cProviders, count)
 
    if count < 0 || uint64(count) > uint64(math.MaxInt)/uint64(unsafe.Sizeof("")) {
        return nil, fmt.Errorf("ort: get available providers: count %d exceeds addressable range", int(count))
    }
    n := int(count)
    if n > 0 && cProviders == nil {
        return nil, fmt.Errorf("ort: get available providers: ORT returned nil providers for count %d", n)
    }
    providers := make([]string, n)
    ptrs := unsafe.Slice((**C.char)(unsafe.Pointer(cProviders)), n)
    for i := 0; i < n; i++ {
        providers[i] = C.GoString(ptrs[i])
    }
    return providers, nil
}
 
func resolveLibPath() string {
    if libPath != "" {
        return resolveDir(libPath)
    }
    if envPath := os.Getenv("ORT_LIB_PATH"); envPath != "" {
        return resolveDir(envPath)
    }
    return platformLibraryName()
}
 
func resolveDir(path string) string {
    info, err := os.Stat(path)
    if err == nil && info.IsDir() {
        return filepath.Join(path, platformLibraryName())
    }
    return path
}
 
func checkInit() error {
    if !initialized.Load() {
        if shutdown.Load() {
            return errShutdown
        }
        return errNotInitialized
    }
    return nil
}