Skip to content

Commit 6e556bf

Browse files
ilagomatisananos
authored andcommitted
feat: Add resource methods
- Resource from buffer - Resource from blobs - Resource init multi - Resource refcount accessor Also, improve resource handling. Use a pointer to the C structure instead of storing it in Golang stack memory. Signed-off-by: Ilias Lagomatis <ilago@nubificus.co.uk>
1 parent 9dc7d20 commit 6e556bf

7 files changed

Lines changed: 129 additions & 14 deletions

File tree

vaccel/blob.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package vaccel
4+
5+
/*
6+
#cgo pkg-config: vaccel
7+
#cgo LDFLAGS: -lvaccel -ldl
8+
#include <vaccel.h>
9+
10+
*/
11+
import "C"
12+
import "unsafe"
13+
14+
type BlobType int32
15+
16+
const (
17+
BlobFile BlobType = C.VACCEL_BLOB_FILE
18+
BlobBuffer BlobType = C.VACCEL_BLOB_BUFFER
19+
BlobMapped BlobType = C.VACCEL_BLOB_MAPPED
20+
)
21+
22+
type Blob struct {
23+
cBlob *C.struct_vaccel_blob
24+
}
25+
26+
func (b *Blob) Init(path string) int {
27+
return int(C.vaccel_blob_new(&b.cBlob, C.CString(path))) //nolint:gocritic
28+
}
29+
30+
func (b *Blob) InitFromBuf(bytes []byte, own bool, filename string, dir string, randomize bool) int {
31+
var cdname *C.char
32+
33+
cBlobBytes := (*C.uchar)(&bytes[0])
34+
cBlobLen := C.size_t(len(bytes))
35+
36+
if dir == "" {
37+
cdname = nil
38+
} else {
39+
cdname = C.CString(dir)
40+
defer C.free(unsafe.Pointer(cdname))
41+
}
42+
43+
return int(C.vaccel_blob_from_buf(&b.cBlob, cBlobBytes, cBlobLen, C.bool(own), C.CString(filename), cdname, C.bool(randomize))) //nolint:gocritic
44+
}
45+
46+
func (b *Blob) Release() int {
47+
return int(C.vaccel_blob_delete(b.cBlob)) //nolint:gocritic
48+
}

vaccel/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func ExecWithResource(sess *Session, res *Resource, funcname string,
2020
cNrRead := C.size_t(read.cList.size)
2121
cNrWrite := C.size_t(write.cList.size)
2222

23-
cRet := C.vaccel_exec_with_resource(&sess.cSess, &res.cRes, cfunc, cread, cNrRead, cwrite, cNrWrite) //nolint:gocritic
23+
cRet := C.vaccel_exec_with_resource(&sess.cSess, res.cRes, cfunc, cread, cNrRead, cwrite, cNrWrite) //nolint:gocritic
2424

2525
return int(cRet)
2626

vaccel/resource.go

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ package vaccel
66
#cgo pkg-config: vaccel
77
#cgo LDFLAGS: -lvaccel -ldl
88
#include <vaccel.h>
9+
#include <stdatomic.h>
10+
#include <stdint.h>
11+
12+
static uint32_t get_refcount(atomic_uint *ref) {
13+
return atomic_load((_Atomic unsigned int *)ref);
14+
}
915
1016
*/
1117
import "C"
18+
import "unsafe"
1219

1320
type ResourceType int
1421

@@ -19,21 +26,79 @@ const (
1926
)
2027

2128
type Resource struct {
22-
cRes C.struct_vaccel_resource
29+
cRes *C.struct_vaccel_resource
2330
}
2431

2532
func (t ResourceType) ToCEnum() C.vaccel_resource_type_t {
2633
return C.vaccel_resource_type_t(t)
2734
}
2835

2936
func (r *Resource) Init(path string, resType ResourceType) int {
30-
return int(C.vaccel_resource_init(&r.cRes, C.CString(path), resType.ToCEnum())) //nolint:gocritic
37+
return int(C.vaccel_resource_new(&r.cRes, C.CString(path), resType.ToCEnum())) //nolint:gocritic
38+
}
39+
40+
func (r *Resource) InitMulti(paths []string, resType ResourceType) int {
41+
nrPaths := len(paths)
42+
cNrPaths := C.size_t(nrPaths)
43+
44+
spaceSize := cNrPaths * C.size_t(unsafe.Sizeof(uintptr(0)))
45+
cSpace := C.malloc(spaceSize)
46+
defer C.free(cSpace)
47+
48+
cPathsPtr := (**C.char)(cSpace)
49+
pathSlice := unsafe.Slice((**C.char)(cPathsPtr), nrPaths)
50+
51+
for i := 0; i < nrPaths; i++ {
52+
str := C.CString(paths[i])
53+
defer C.free(unsafe.Pointer(str))
54+
55+
pathSlice[i] = str
56+
}
57+
return int(C.vaccel_resource_multi_new(&r.cRes, cPathsPtr, cNrPaths, resType.ToCEnum())) //nolint:gocritic
58+
}
59+
60+
func (r *Resource) InitFromBuf(bytes []byte, resType ResourceType, filename string, memOnly bool) int {
61+
cResBytes := (*C.uchar)(&bytes[0])
62+
cResBuf := unsafe.Pointer(cResBytes)
63+
cResLen := C.size_t(len(bytes))
64+
65+
var cfname *C.char
66+
if filename == "" {
67+
cfname = nil
68+
} else {
69+
cfname = C.CString(filename)
70+
defer C.free(unsafe.Pointer(cfname))
71+
}
72+
73+
return int(C.vaccel_resource_from_buf(&r.cRes, cResBuf, cResLen, resType.ToCEnum(), cfname, C.bool(memOnly))) //nolint:gocritic
74+
}
75+
76+
func (r *Resource) InitFromBlobs(blobs []Blob, resType ResourceType) int {
77+
nrBlobs := len(blobs)
78+
cNrBlobs := C.size_t(nrBlobs)
79+
80+
bufSize := cNrBlobs * C.size_t(unsafe.Sizeof(uintptr(0)))
81+
cSpace := C.malloc(bufSize)
82+
defer C.free(cSpace)
83+
84+
cBlobsPtr := (**C.struct_vaccel_blob)(cSpace)
85+
blobSlice := unsafe.Slice((**C.struct_vaccel_blob)(cBlobsPtr), nrBlobs)
86+
87+
for i := 0; i < nrBlobs; i++ {
88+
blobSlice[i] = blobs[i].cBlob
89+
}
90+
91+
return int(C.vaccel_resource_from_blobs(&r.cRes, cBlobsPtr, cNrBlobs, resType.ToCEnum())) //nolint:gocritic
3192
}
3293

3394
func (r *Resource) Release() int {
34-
return int(C.vaccel_resource_release(&r.cRes)) //nolint:gocritic
95+
return int(C.vaccel_resource_delete(r.cRes)) //nolint:gocritic
3596
}
3697

3798
func (r *Resource) GetID() int64 {
3899
return int64(r.cRes.id)
39100
}
101+
102+
func (r *Resource) GetRefcount() uint32 {
103+
return uint32(C.get_refcount(&r.cRes.refcount))
104+
}

vaccel/session.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ func (s *Session) Release() int {
2323
}
2424

2525
func (s *Session) Register(r *Resource) int {
26-
return int(C.vaccel_resource_register(&r.cRes, &s.cSess)) //nolint:gocritic
26+
return int(C.vaccel_resource_register(r.cRes, &s.cSess)) //nolint:gocritic
2727
}
2828

2929
func (s *Session) Unregister(r *Resource) int {
30-
return int(C.vaccel_resource_unregister(&r.cRes, &s.cSess)) //nolint:gocritic
30+
return int(C.vaccel_resource_unregister(r.cRes, &s.cSess)) //nolint:gocritic
3131
}
3232

3333
func (s *Session) GetID() int64 {

vaccel/tf.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ func TFModelLoad(sess *Session, model *Resource, status *TFStatus) int {
299299
if sess == nil || model == nil {
300300
return EINVAL
301301
}
302-
return int(C.vaccel_tf_model_load(&sess.cSess, &model.cRes, &status.cTFStatus)) //nolint:gocritic
302+
303+
return int(C.vaccel_tf_model_load(&sess.cSess, model.cRes, &status.cTFStatus)) //nolint:gocritic
303304
}
304305

305306
func TFModelRun(
@@ -337,7 +338,7 @@ func TFModelRun(
337338

338339
ret := int(C.vaccel_tf_model_run(
339340
&sess.cSess,
340-
&model.cRes,
341+
model.cRes,
341342
func() *C.struct_vaccel_tf_buffer {
342343
if runOptions != nil {
343344
return &runOptions.cTFBuf
@@ -401,6 +402,6 @@ func TFModelRun(
401402
}
402403

403404
func TFModelUnload(sess *Session, model *Resource, status *TFStatus) int {
404-
err := int(C.vaccel_tf_model_unload(&sess.cSess, &model.cRes, &status.cTFStatus)) //nolint:gocritic
405+
err := int(C.vaccel_tf_model_unload(&sess.cSess, model.cRes, &status.cTFStatus)) //nolint:gocritic
405406
return err
406407
}

vaccel/tflite.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ func TFLiteModelLoad(sess *Session, model *Resource) int {
229229
if sess == nil || model == nil {
230230
return EINVAL
231231
}
232-
return int(C.vaccel_tflite_model_load(&sess.cSess, &model.cRes)) //nolint:gocritic
232+
233+
return int(C.vaccel_tflite_model_load(&sess.cSess, model.cRes)) //nolint:gocritic
233234
}
234235

235236
func TFLiteModelRun(
@@ -264,7 +265,7 @@ func TFLiteModelRun(
264265
var cStatus C.uint8_t
265266
ret := int(C.vaccel_tflite_model_run(
266267
&sess.cSess,
267-
&model.cRes,
268+
model.cRes,
268269
(**C.struct_vaccel_tflite_tensor)(cInPtr),
269270
C.int(nrInputs),
270271
(**C.struct_vaccel_tflite_tensor)(cOutPtr),
@@ -320,6 +321,6 @@ func TFLiteModelRun(
320321
}
321322

322323
func TFLiteModelUnload(sess *Session, model *Resource) int {
323-
err := int(C.vaccel_tflite_model_unload(&sess.cSess, &model.cRes)) //nolint:gocritic
324+
err := int(C.vaccel_tflite_model_unload(&sess.cSess, model.cRes)) //nolint:gocritic
324325
return err
325326
}

vaccel/torch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func TorchModelLoad(sess *Session, model *Resource) int {
190190
return EINVAL
191191
}
192192

193-
return int(C.vaccel_torch_model_load(&sess.cSess, &model.cRes)) //nolint:gocritic
193+
return int(C.vaccel_torch_model_load(&sess.cSess, model.cRes)) //nolint:gocritic
194194
}
195195

196196
func TorchModelRun(
@@ -232,7 +232,7 @@ func TorchModelRun(
232232

233233
ret := int(C.vaccel_torch_model_run(
234234
&sess.cSess,
235-
&model.cRes,
235+
model.cRes,
236236
bufPtr,
237237
(**C.struct_vaccel_torch_tensor)(cInPtr),
238238
C.int(nrInputs),

0 commit comments

Comments
 (0)