[Numpy-discussion] numpy ndarray questions

Jochen cycomanic@gmail....
Tue Jan 27 13:23:44 CST 2009


On Tue, 2009-01-27 at 14:16 +0100, Sturla Molden wrote:
> On 1/27/2009 12:37 PM, Sturla Molden wrote:
> 
> > import ctypes
> > import numpy
> > 
> > fftw_malloc = ctypes.cdll.fftw.fftw_malloc
> > fftw_malloc.argtypes = [ctypes.c_ulong,]
> > fftw_malloc.restype = ctypes.c_ulong
> > 
> > def aligned_array(N, dtype):
> >      d = dtype()
> >      address = fftw_malloc(N * d.nbytes) # restype = ctypes.c_ulong
> >      if (address = 0): raise MemoryError, 'fftw_malloc returned NULL'
> >      class Dummy(object): pass
> >      d = Dummy()
> >      d.__array_interface__ = {
> >          'data' = (address, False),
> >          'typestr' : dtype.str,
> >          'descr' : dtype.descr,
> >          'shape' : shape,
> >          'strides' : strides,
> >          'version' : 3
> >      }
> >      return numpy.asarray(d)
> 
> 
> Or if you just want to use numpy, aligning to a 16 byte boundary
> can be done like this:
> 
> 
> def aligned_array(N, dtype):
>      d = dtype()
>      tmp = numpy.array(N * d.nbytes + 16, dtype=numpy.uint8)
>      address = tmp.__array_interface__['data'][0]
>      offset = (16 - address % 16) % 16
>      return tmp[offset:offset+N].view(dtype=dtype)
> 
> 
> S.M.
> 
Ah, I didn't think about doing it in python, cool thanks.


> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion



More information about the Numpy-discussion mailing list