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

Source Code for Module qubx.fast.types

 1  """Common imports and data marshalling for qubfast/ctypes bridge. 
 2   
 3  Copyright 2008-2011 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 scipy 
24  import traceback 
25   
26  import ctypes 
27  from ctypes import cdll, 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 
28  c_short_p = POINTER(c_short) 
29  c_int_p = POINTER(c_int) 
30  c_int_pp = POINTER(c_int_p) 
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_double_p = POINTER(c_double) 
35  c_float_pp = POINTER(c_float_p) 
36  c_double_pp = POINTER(c_double_p) 
37  c_double_ppp = POINTER(c_double_pp) 
38   
39  StatusCallback = CFUNCTYPE(c_int, c_int_p, c_double) 
40  ReportFunc = CFUNCTYPE(c_int, c_char_p, c_int_p) 
41   
42  pybuf = ctypes.pythonapi.PyBuffer_FromReadWriteMemory 
43  pybuf.restype = py_object 
44  pyobj_asreadbuf = ctypes.pythonapi.PyObject_AsReadBuffer 
45  pyobj_asreadbuf.argtypes = (py_object, c_long_p, c_size_t_p) 
46 -def buffer_to_pointer(buf):
47 ptr = c_long() 48 plen = c_size_t() 49 pyobj_asreadbuf(buf, byref(ptr), byref(plen)) 50 return cast(ptr.value, c_int_p)
51 52
53 -def cdata(arr, typ):
54 if arr == None: return arr 55 return arr.ctypes.data_as(typ)
56 57 try: 58 qubfast = cdll.LoadLibrary('qubfast.dll') 59 except: 60 # RTLD_GLOBAL makes symbols available to subsequent ctypes LoadLibrary 61 qubfast = ctypes.CDLL('libqubfast.so', mode=ctypes.RTLD_GLOBAL) 62 63
64 -def ptr_to_array(ptr, count, ctype, dtype):
65 ptr_as_array = numpy.frombuffer(pybuf(ptr, count*sizeof(ctype)), dtype=dtype, count=count) 66 return numpy.array(ptr_as_array, copy=True)
67 72