forked from arrayfire/arrayfire-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.py
More file actions
352 lines (296 loc) · 7.44 KB
/
library.py
File metadata and controls
352 lines (296 loc) · 7.44 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################
"""
module containing enums and other constants from arrayfire library
"""
import platform
import ctypes as ct
try:
from enum import Enum as _Enum
def _Enum_Type(v):
return v
except:
class _Enum(object):
pass
class _Enum_Type(object):
def __init__(self, v):
self.value = v
class _clibrary(object):
def __libname(self, name):
platform_name = platform.system()
assert(len(platform_name) >= 3)
libname = 'libaf' + name
if platform_name == 'Linux':
libname += '.so'
elif platform_name == 'Darwin':
libname += '.dylib'
elif platform_name == "Windows" or platform_name[:3] == "CYG":
libname += '.dll'
libname = libname[3:] # remove 'lib'
if platform_name == "Windows":
'''
Supressing crashes caused by missing dlls
http://stackoverflow.com/questions/8347266/missing-dll-print-message-instead-of-launching-a-popup
https://msdn.microsoft.com/en-us/library/windows/desktop/ms680621.aspx
'''
ct.windll.kernel32.SetErrorMode(0x0001 | 0x0002);
else:
raise OSError(platform_name + ' not supported')
return libname
def set(self, name, unsafe=False):
if (not unsafe and self.__lock):
raise RuntimeError("Can not change backend after creating an Array")
if (self.clibs[name] is None):
raise RuntimeError("Could not load any ArrayFire %s backend" % name)
self.name = name
return
def __init__(self):
self.clibs = {}
self.name = None
self.__lock = False
# Iterate in reverse order of preference
for name in ('cpu', 'opencl', 'cuda'):
try:
libname = self.__libname(name)
ct.cdll.LoadLibrary(libname)
self.clibs[name] = ct.CDLL(libname)
self.name = name
except:
self.clibs[name] = None
if (self.name is None):
raise RuntimeError("Could not load any ArrayFire libraries")
def get(self):
return self.clibs[self.name]
def lock(self):
self.__lock = True
backend = _clibrary()
class ERR(_Enum):
"""
Error values. For internal use only.
"""
NONE = _Enum_Type(0)
#100-199 Errors in environment
NO_MEM = _Enum_Type(101)
DRIVER = _Enum_Type(102)
RUNTIME = _Enum_Type(103)
# 200-299 Errors in input parameters
INVALID_ARRAY = _Enum_Type(201)
ARG = _Enum_Type(202)
SIZE = _Enum_Type(203)
TYPE = _Enum_Type(204)
DIFF_TYPE = _Enum_Type(205)
BATCH = _Enum_Type(207)
# 300-399 Errors for missing software features
NOT_SUPPORTED = _Enum_Type(301)
NOT_CONFIGURED = _Enum_Type(302)
NONFREE = _Enum_Type(303)
# 400-499 Errors for missing hardware features
NO_DBL = _Enum_Type(401)
NO_GFX = _Enum_Type(402)
# 900-999 Errors from upstream libraries and runtimes
INTERNAL = _Enum_Type(998)
UNKNOWN = _Enum_Type(999)
class Dtype(_Enum):
"""
Error values. For internal use only.
"""
f32 = _Enum_Type(0)
c32 = _Enum_Type(1)
f64 = _Enum_Type(2)
c64 = _Enum_Type(3)
b8 = _Enum_Type(4)
s32 = _Enum_Type(5)
u32 = _Enum_Type(6)
u8 = _Enum_Type(7)
s64 = _Enum_Type(8)
u64 = _Enum_Type(9)
class Source(_Enum):
"""
Source of the pointer
"""
device = _Enum_Type(0)
host = _Enum_Type(1)
class INTERP(_Enum):
"""
Interpolation method
"""
NEAREST = _Enum_Type(0)
LINEAR = _Enum_Type(1)
BILINEAR = _Enum_Type(2)
CUBIC = _Enum_Type(3)
LOWER = _Enum_Type(4)
class PAD(_Enum):
"""
Edge padding types
"""
ZERO = _Enum_Type(0)
SYM = _Enum_Type(1)
class CONNECTIVITY(_Enum):
"""
Neighborhood connectivity
"""
FOUR = _Enum_Type(4)
EIGHT = _Enum_Type(8)
class CONV_MODE(_Enum):
"""
Convolution mode
"""
DEFAULT = _Enum_Type(0)
EXPAND = _Enum_Type(1)
class CONV_DOMAIN(_Enum):
"""
Convolution domain
"""
AUTO = _Enum_Type(0)
SPATIAL = _Enum_Type(1)
FREQ = _Enum_Type(2)
class MATCH(_Enum):
"""
Match type
"""
"""
Sum of absolute differences
"""
SAD = _Enum_Type(0)
"""
Zero mean SAD
"""
ZSAD = _Enum_Type(1)
"""
Locally scaled SAD
"""
LSAD = _Enum_Type(2)
"""
Sum of squared differences
"""
SSD = _Enum_Type(3)
"""
Zero mean SSD
"""
ZSSD = _Enum_Type(4)
"""
Locally scaled SSD
"""
LSSD = _Enum_Type(5)
"""
Normalized cross correlation
"""
NCC = _Enum_Type(6)
"""
Zero mean NCC
"""
ZNCC = _Enum_Type(7)
"""
Sum of hamming distances
"""
SHD = _Enum_Type(8)
class YCC_STD(_Enum):
"""
YCC Standard formats
"""
BT_601 = _Enum_Type(601)
BT_709 = _Enum_Type(709)
BT_2020 = _Enum_Type(2020)
class CSPACE(_Enum):
"""
Colorspace formats
"""
GRAY = _Enum_Type(0)
RGB = _Enum_Type(1)
HSV = _Enum_Type(2)
YCbCr= _Enum_Type(3)
class MATPROP(_Enum):
"""
Matrix properties
"""
"""
None, general.
"""
NONE = _Enum_Type(0)
"""
Transposed.
"""
TRANS = _Enum_Type(1)
"""
Conjugate transposed.
"""
CTRANS = _Enum_Type(2)
"""
Upper triangular matrix.
"""
UPPER = _Enum_Type(32)
"""
Lower triangular matrix.
"""
LOWER = _Enum_Type(64)
"""
Treat diagonal as units.
"""
DIAG_UNIT = _Enum_Type(128)
"""
Symmetric matrix.
"""
SYM = _Enum_Type(512)
"""
Positive definite matrix.
"""
POSDEF = _Enum_Type(1024)
"""
Orthogonal matrix.
"""
ORTHOG = _Enum_Type(2048)
"""
Tri diagonal matrix.
"""
TRI_DIAG = _Enum_Type(4096)
"""
Block diagonal matrix.
"""
BLOCK_DIAG = _Enum_Type(8192)
class NORM(_Enum):
"""
Norm types
"""
VECTOR_1 = _Enum_Type(0)
VECTOR_INF = _Enum_Type(1)
VECTOR_2 = _Enum_Type(2)
VECTOR_P = _Enum_Type(3)
MATRIX_1 = _Enum_Type(4)
MATRIX_INF = _Enum_Type(5)
MATRIX_2 = _Enum_Type(6)
MATRIX_L_PQ = _Enum_Type(7)
EUCLID = VECTOR_2
class COLORMAP(_Enum):
"""
Colormaps
"""
DEFAULT = _Enum_Type(0)
SPECTRUM = _Enum_Type(1)
COLORS = _Enum_Type(2)
RED = _Enum_Type(3)
MOOD = _Enum_Type(4)
HEAT = _Enum_Type(5)
BLUE = _Enum_Type(6)
class IMAGE_FORMAT(_Enum):
"""
Image Formats
"""
BMP = _Enum_Type(0)
ICO = _Enum_Type(1)
JPEG = _Enum_Type(2)
JNG = _Enum_Type(3)
PNG = _Enum_Type(13)
PPM = _Enum_Type(14)
PPMRAW = _Enum_Type(15)
TIFF = _Enum_Type(18)
PSD = _Enum_Type(20)
HDR = _Enum_Type(26)
EXR = _Enum_Type(29)
JP2 = _Enum_Type(31)
RAW = _Enum_Type(34)