-
Notifications
You must be signed in to change notification settings - Fork 8
/
capi.pyx
310 lines (232 loc) · 8.68 KB
/
capi.pyx
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# distutils: language = c++
"""
Python wrapper for PICT C/API.
"""
from libc.stdlib cimport malloc, free
from libc.stddef cimport wchar_t
from libc.stdint cimport uint32_t
from cpython.mem cimport PyMem_Free
cdef extern from "<string>" namespace "std":
cdef cppclass wstring:
wstring()
wchar_t* data()
cdef extern from "Python.h":
wchar_t* PyUnicode_AsWideCharString(object, Py_ssize_t*)
object PyUnicode_FromWideChar(wchar_t*, Py_ssize_t)
cdef extern from "pictapi.h":
##
## Types
##
ctypedef void * PICT_HANDLE;
ctypedef size_t PICT_VALUE;
ctypedef size_t * PICT_RESULT_ROW;
ctypedef unsigned int PICT_RET_CODE;
ctypedef struct PICT_EXCLUSION_ITEM:
PICT_HANDLE Parameter
PICT_VALUE ValueIndex
ctypedef struct PICT_SEED_ITEM:
PICT_HANDLE Parameter
PICT_VALUE ValueIndex
##
## Return codes
##
int PICT_SUCCESS
int PICT_OUT_OF_MEMORY
int PICT_GENERATION_ERROR
##
## Defaults
##
int PICT_PAIRWISE_GENERATION
int PICT_DEFAULT_RANDOM_SEED
##
## Interface
##
PICT_HANDLE PictCreateTask()
void PictSetRootModel(
const PICT_HANDLE task,
const PICT_HANDLE model)
PICT_RET_CODE PictAddExclusion(
const PICT_HANDLE task,
const PICT_EXCLUSION_ITEM exlusionItems[],
size_t exclusionItemCount)
PICT_RET_CODE PictAddSeed(
const PICT_HANDLE task,
const PICT_SEED_ITEM seedItems[],
size_t seedItemCount)
PICT_RET_CODE PictGenerate(
const PICT_HANDLE task)
PICT_RESULT_ROW PictAllocateResultBuffer(
const PICT_HANDLE task)
void PictFreeResultBuffer(
const PICT_RESULT_ROW resultRow)
void PictResetResultFetching(
const PICT_HANDLE task)
size_t PictGetNextResultRow(
const PICT_HANDLE task,
PICT_RESULT_ROW resultRow)
void PictDeleteTask(
const PICT_HANDLE task)
PICT_HANDLE PictCreateModel(
unsigned int randomSeed)
PICT_HANDLE PictAddParameter(
const PICT_HANDLE model,
size_t valueCount,
unsigned int order,
unsigned int valueWeights[])
size_t PictGetTotalParameterCount(
const PICT_HANDLE task)
PICT_RET_CODE PictAttachChildModel(
const PICT_HANDLE modelParent,
const PICT_HANDLE modelChild,
unsigned int order)
void PictDeleteModel(
const PICT_HANDLE model)
cdef extern from "pypict_pict_clidll.h":
int PictExecuteCLI "execute" (int argc, wchar_t* args[], wstring& output)
##################################################
# Constants
##################################################
PAIRWISE_GENERATION = PICT_PAIRWISE_GENERATION
DEFAULT_RANDOM_SEED = PICT_DEFAULT_RANDOM_SEED
##################################################
# Error Handling
##################################################
cdef void check_retcode(PICT_RET_CODE code) except *:
if code == PICT_SUCCESS:
return
elif code == PICT_OUT_OF_MEMORY:
raise MemoryError()
elif code == PICT_GENERATION_ERROR:
raise RuntimeError('internal engine error')
raise RuntimeError('unexpected error ({})'.format(code))
##################################################
# Interface (API)
##################################################
cpdef size_t createTask() except *:
"""createTask() -> int"""
handle = PictCreateTask()
if handle == NULL:
raise MemoryError()
return <size_t>handle
cpdef void setRootModel(task: size_t, model: size_t) except *:
"""setRootModel(task: int, model: int) -> None"""
PictSetRootModel(<PICT_HANDLE>task, <PICT_HANDLE>model)
cpdef void addExclusion(task: size_t, items: tuple) except *:
"""addExclusion(task: int, items: Iterable[tuple[int, int]]) -> None"""
cdef size_t count = len(items)
cdef PICT_EXCLUSION_ITEM* packed = NULL
if count == 0:
return
packed = <PICT_EXCLUSION_ITEM*>malloc(sizeof(PICT_EXCLUSION_ITEM) * count)
if packed == NULL:
raise MemoryError()
try:
for i, (param, idx) in enumerate(items):
packed[i].Parameter = <PICT_HANDLE><size_t>param
packed[i].ValueIndex = <PICT_VALUE>idx
check_retcode(PictAddExclusion(<PICT_HANDLE>task, packed, count))
finally:
free(packed)
cpdef void addSeed(task: size_t, items: tuple) except *:
"""addSeed(task: int, items: Iterable[tuple[int, int]]) -> None"""
cdef size_t count = len(items)
cdef PICT_SEED_ITEM* packed = NULL
if count == 0:
return
packed = <PICT_SEED_ITEM*>malloc(sizeof(PICT_SEED_ITEM) * count)
if packed == NULL:
raise MemoryError()
try:
for i, (param, idx) in enumerate(items):
packed[i].Parameter = <PICT_HANDLE><size_t>param
packed[i].ValueIndex = <PICT_VALUE>idx
check_retcode(PictAddSeed(<PICT_HANDLE>task, packed, count))
finally:
free(packed)
cpdef void generate(task: size_t) except *:
"""generate(task: int) -> None"""
check_retcode(PictGenerate(<PICT_HANDLE>task))
cpdef size_t[:] allocateResultBuffer(task: size_t) except *:
"""allocateResultBuffer(task: int) -> memoryview"""
cdef size_t paramCount = PictGetTotalParameterCount(<PICT_HANDLE>task)
buf = PictAllocateResultBuffer(<PICT_HANDLE>task)
if buf == NULL:
raise MemoryError()
return <size_t[:paramCount]>buf
cpdef void freeResultBuffer(resultRow: size_t[:]) except *:
"""freeResultBuffer(resultRow: memoryview) -> None"""
PictFreeResultBuffer(<PICT_RESULT_ROW>&resultRow[0])
cpdef void resetResultFetching(task: size_t) except *:
"""resetResultFetching(task: int) -> None"""
PictResetResultFetching(<PICT_HANDLE>task)
cpdef size_t getNextResultRow(task: size_t, resultRow: size_t[:]) except *:
"""getNextResultRow(task: int, resultRow: memoryview) -> int"""
return PictGetNextResultRow(
<PICT_HANDLE>task, <PICT_RESULT_ROW>&resultRow[0])
cpdef void deleteTask(task: size_t) except *:
"""deleteTask(task: int) -> None"""
PictDeleteTask(<PICT_HANDLE>task)
cpdef size_t createModel(
randomSeed: uint32_t = PICT_DEFAULT_RANDOM_SEED) except *:
"""createModel(randomSeed: int = PICT_DEFAULT_RANDOM_SEED) -> int"""
handle = PictCreateModel(randomSeed)
if handle == NULL:
raise MemoryError()
return <size_t>handle
cpdef size_t addParameter(
model: size_t,
valueCount: size_t,
order: uint32_t = PICT_PAIRWISE_GENERATION,
valueWeights: tuple = None) except *:
"""addParameter(model: int, valueCount: int, order: int = PICT_PAIRWISE_GENERATION, valueWeights: Optional[Iterable[int]] = None) -> int"""
cdef unsigned int* packed = NULL
try:
if valueWeights is not None:
assert valueCount == <size_t>len(valueWeights)
packed = <unsigned int*>malloc(sizeof(unsigned int) * valueCount)
if packed == NULL:
raise MemoryError()
for i, weight in enumerate(valueWeights):
packed[i] = <unsigned int>weight
handle = PictAddParameter(
<PICT_HANDLE>model, valueCount, order, packed)
if handle == NULL:
raise MemoryError()
return <size_t>handle
finally:
free(packed)
cpdef size_t getTotalParameterCount(task: size_t) except *:
"""getTotalParameterCount(task: int) -> int"""
return <size_t>PictGetTotalParameterCount(<PICT_HANDLE>task)
cpdef void attachChildModel(
modelParent: size_t,
modelChild: size_t,
order: uint32_t = PICT_PAIRWISE_GENERATION) except *:
"""attachChildModel(modelParent: int, modelChild: int, order: int = PICT_PAIRWISE_GENERATION) -> None"""
check_retcode(PictAttachChildModel(
<PICT_HANDLE>modelParent, <PICT_HANDLE>modelChild, order))
cpdef void deleteModel(model: size_t) except *:
"""deleteModel(model: int) -> None"""
PictDeleteModel(<PICT_HANDLE>model)
##################################################
# Interface (CLI)
##################################################
cpdef str execute(args: list):
"""execute(args: list[str]) -> str"""
cdef int c_argc = -1
cdef wchar_t** c_args = NULL
cdef wstring c_output
c_argc = len(args) + 1
c_args = <wchar_t**>malloc(sizeof(wchar_t*) * c_argc)
if c_args == NULL:
raise MemoryError()
try:
c_args[0] = PyUnicode_AsWideCharString("", NULL)
for i in range(1, c_argc):
c_args[i] = PyUnicode_AsWideCharString(args[i-1], NULL)
PictExecuteCLI(c_argc, c_args, c_output)
return PyUnicode_FromWideChar(c_output.data(), -1)
finally:
for i in range(c_argc):
PyMem_Free(c_args[i])
free(c_args)