Package qubx :: Package fast :: Module fast_utils
[hide private]
[frames] | no frames]

Source Code for Module qubx.fast.fast_utils

 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) 
47 -def buffer_to_pointer(buf):
48 ptr = c_long() 49 plen = c_size_t() 50 pyobj_asreadbuf(buf, byref(ptr), byref(plen)) 51 return cast(ptr.value, c_int_p)
52 53
54 -def cdata(arr, typ):
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 # RTLD_GLOBAL makes symbols available to subsequent ctypes LoadLibrary 62 try: 63 qubfast = ctypes.CDLL('libqubfast.so', mode=ctypes.RTLD_GLOBAL) 64 except OSError: 65 #traceback.print_exc() 66 qubfast = ctypes.CDLL('@executable_path/../Frameworks/libqubfast.so', mode=ctypes.RTLD_GLOBAL) 67 68
69 -def ptr_to_array(ptr, count, ctype, dtype):
70 ptr_as_array = numpy.frombuffer(pybuf(ptr, count*sizeof(ctype)), dtype=dtype, count=count) 71 return numpy.array(ptr_as_array, copy=True)
72 77
78 -def buffer_as_array(buf, ctype, dtype, count):
79 return numpy.frombuffer(pybuf(buf, count*sizeof(ctype)), dtype=dtype, count=count)
80
81 -def buffer_as_array_of_int(buf, N):
82 return buffer_as_array(buf, c_int, 'int32', N)
83 -def buffer_as_array_of_double(buf, N):
84 return buffer_as_array(buf, c_double, 'float64', N)
85 -def buffer_as_matrix_of_int(buf, N, M):
86 return buffer_as_array(buf, c_int, 'int32', N*M).reshape((N, M))
87 -def buffer_as_matrix_of_double(buf, N, M):
88 return buffer_as_array(buf, c_double, 'float64', N*M).reshape((N, M))
89