1 """Common imports and data marshalling for qubfast/ctypes bridge.
2
3 Copyright 2008-2014 Research Foundation State University of New York
4 This file is part of QUB Express.
5
6 QUB Express is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 QUB Express is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License,
17 named LICENSE.txt, in the QUB Express program directory. If not, see
18 <http://www.gnu.org/licenses/>.
19
20 """
21
22 import numpy
23 import traceback
24
25 import ctypes
26 from ctypes import cdll, c_char, c_short, c_int, c_long, c_size_t, c_float, c_double, c_char_p, c_void_p, Structure, POINTER, byref, CFUNCTYPE, sizeof, py_object, cast
27 c_short_p = POINTER(c_short)
28 c_int_p = POINTER(c_int)
29 c_int_pp = POINTER(c_int_p)
30 c_int_ppp = POINTER(c_int_pp)
31 c_long_p = POINTER(c_long)
32 c_size_t_p = POINTER(c_size_t)
33 c_float_p = POINTER(c_float)
34 c_float_pp = POINTER(c_float_p)
35 c_float_ppp = POINTER(c_float_pp)
36 c_double_p = POINTER(c_double)
37 c_double_pp = POINTER(c_double_p)
38 c_double_ppp = POINTER(c_double_pp)
39
40 StatusCallback = CFUNCTYPE(c_int, c_int_p, c_double)
41 ReportFunc = CFUNCTYPE(c_int, c_char_p, c_int_p)
42
43 pybuf = ctypes.pythonapi.PyBuffer_FromReadWriteMemory
44 pybuf.restype = py_object
45 pyobj_asreadbuf = ctypes.pythonapi.PyObject_AsReadBuffer
46 pyobj_asreadbuf.argtypes = (py_object, c_long_p, c_size_t_p)
52
53
55 if arr is None: return arr
56 return arr.ctypes.data_as(typ)
57
58 try:
59 qubfast = cdll.LoadLibrary('qubfast.dll')
60 except:
61
62 try:
63 qubfast = ctypes.CDLL('libqubfast.so', mode=ctypes.RTLD_GLOBAL)
64 except OSError:
65
66 qubfast = ctypes.CDLL('@executable_path/../Frameworks/libqubfast.so', mode=ctypes.RTLD_GLOBAL)
67
68
72
74 print "%s%s:" % (indent, s.__class__)
75 for field, typ in s._fields_:
76 print "%s%20s:\t%s" % (indent, field, s.__getattribute__(field))
77
80
89