-
Notifications
You must be signed in to change notification settings - Fork 10
/
driver.go
146 lines (120 loc) · 3.64 KB
/
driver.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
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
package cura
// #include "c/arrow.h"
// #include <cura/c/api.h>
// #include <stdlib.h>
import (
"C"
)
import (
"github.com/apache/arrow/go/arrow/array"
"unsafe"
)
type MemoryResource int8
const (
Arena MemoryResource = iota
ArenaPerThread
Pool
PoolPerThread
Managed
Cuda
)
type Driver struct {
handle C.DriverHandle
}
func CreateDriver() *Driver {
driver := &Driver{
handle: C.create_driver(),
}
return driver
}
func ReleaseDriver(d *Driver) {
C.release_driver(d.handle)
}
func (d *Driver) Explain(json string, extended bool) (int, []string) {
cs := C.CString(json)
defer C.free(unsafe.Pointer(cs))
var ci = 0
if extended {
ci = 1
}
var out **C.char
n := int(C.explain(d.handle, cs, C.int(ci), &out))
if n <= 0 {
return n, make([]string, 0)
}
outSlice := (*[1 << 28]*C.char)(unsafe.Pointer(out))[:n:n]
result := make([]string, n)
for i := 0; i < n; i++ {
result[i] = C.GoString(outSlice[i])
C.free(unsafe.Pointer(outSlice[i]))
}
C.free(unsafe.Pointer(out))
return n, result
}
func (d *Driver) Compile(json string) int {
cs := C.CString(json)
defer C.free(unsafe.Pointer(cs))
return int(C.compile(d.handle, cs))
}
func (d *Driver) SetMemoryResource(memoryResource MemoryResource) int {
return int(C.set_memory_resource(d.handle, C.int8_t(memoryResource)))
}
func (d *Driver) SetExclusiveDefaultMemoryResource(exclusive bool) int {
if exclusive {
return int(C.set_exclusive_default_memory_resource(d.handle, C.int8_t(1)))
}
return int(C.set_exclusive_default_memory_resource(d.handle, C.int8_t(0)))
}
func (d *Driver) SetMemoryResourceSize(size uint64) int {
return int(C.set_memory_resource_size(d.handle, C.size_t(size)))
}
func (d *Driver) SetThreadsPerPipeline(threadsPerPipeline uint64) int {
return int(C.set_threads_per_pipeline(d.handle, C.size_t(threadsPerPipeline)))
}
func (d *Driver) SetMemoryResourceSizePerThread(size uint64) int {
return int(C.set_memory_resource_size_per_thread(d.handle, C.size_t(size)))
}
func (d *Driver) SetBucketAggregate(enable bool) int {
if enable {
return int(C.set_bucket_aggregate(d.handle, C.int8_t(1)))
}
return int(C.set_bucket_aggregate(d.handle, C.int8_t(0)))
}
func (d *Driver) SetBucketAggregateBuckets(buckets uint64) int {
return int(C.set_bucket_aggregate_buckets(d.handle, C.size_t(buckets)))
}
func (d *Driver) HasNextPipeline() bool {
return C.has_next_pipeline(d.handle) != 0
}
func (d *Driver) PreparePipeline() int {
return int(C.prepare_pipeline(d.handle))
}
func (d *Driver) IsPipelineFinal() bool {
return C.is_pipeline_final(d.handle) != 0
}
func (d *Driver) FinishPipeline() int {
return int(C.finish_pipeline(d.handle))
}
func (d *Driver) PipelineHasNextSource() bool {
return C.pipeline_has_next_source(d.handle) != 0
}
func (d *Driver) PipelineNextSource() int64 {
return int64(C.pipeline_next_source(d.handle))
}
func (d *Driver) IsHeapSource(sourceId int64) bool {
return C.is_heap_source(C.int64_t(sourceId)) != 0
}
func (d *Driver) PipelinePush(threadId int64, sourceId int64, record *array.Record) (int, *InputRecord) {
inputRecord := fromGoRecord(record)
return int(C.pipeline_push(d.handle, C.int64_t(threadId), C.int64_t(sourceId), inputRecord.cArray, inputRecord.cSchema)), inputRecord
}
func (d *Driver) PipelineStream(threadId int64, sourceId int64, record *array.Record, rows uint64) (int, *OutputRecord) {
inputRecord := fromGoRecord(record)
var outCSchema C.ArrowSchema
var outCArray C.ArrowArray
res := int(C.pipeline_stream(d.handle, C.int64_t(threadId), C.int64_t(sourceId), inputRecord.cArray, inputRecord.cSchema, &outCArray, &outCSchema, C.size_t(rows)))
if res <= 0 {
return res, nil
}
return res, toGoRecord(&outCSchema, &outCArray)
}