File: utilities.pyx

package info (click to toggle)
mpi4py-fft 2.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 720 kB
  • sloc: python: 3,053; ansic: 87; makefile: 42; sh: 33
file content (104 lines) | stat: -rw-r--r-- 2,461 bytes parent folder | download
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
#cython: language_level=3str

cimport numpy as np
import numpy as np
from libc.stdint cimport intptr_t

cpdef enum:
    FFTW_FORWARD  = -1
    FFTW_R2HC     = 0
    FFTW_BACKWARD = 1
    FFTW_HC2R     = 1
    FFTW_DHT      = 2
    FFTW_REDFT00  = 3
    FFTW_REDFT01  = 4
    FFTW_REDFT10  = 5
    FFTW_REDFT11  = 6
    FFTW_RODFT00  = 7
    FFTW_RODFT01  = 8
    FFTW_RODFT10  = 9
    FFTW_RODFT11  = 10

cpdef enum:
    C2C_FORWARD = -1
    C2C_BACKWARD = 1
    R2C = -2
    C2R = 2

cpdef enum:
    FFTW_MEASURE = 0
    FFTW_DESTROY_INPUT = 1
    FFTW_UNALIGNED = 2
    FFTW_CONSERVE_MEMORY = 4
    FFTW_EXHAUSTIVE = 8
    FFTW_PRESERVE_INPUT = 16
    FFTW_PATIENT = 32
    FFTW_ESTIMATE = 64
    FFTW_WISDOM_ONLY = 2097152

cpdef int get_alignment(array):
   """Return alignment assuming highest allowed is 32

    Parameters
    ----------
    array : array
    """
   cdef int i, n
   cdef intptr_t addr = <intptr_t>np.PyArray_DATA(array)
   for i in range(5, -1, -1):
       n = 1 << i
       if addr % n == 0:
           break
   return n

cpdef aligned(shape, n=32, dtype=np.dtype('d'), fill=None):
    """Returned array with byte-alignment according to n

    Parameters
    ----------
    shape : sequence of ints
        The shape of the array to be created
    n : int, optional
        The chosen byte-alignment
    dtype : np.dtype, optional
        The type of the returned array
    fill : None or number, optional
        If number then fill returned array with this number, otherwise return
        empty array

    Returns
    -------
    array
        byte-aligned array

    """
    dtype = np.dtype(dtype)
    M = np.prod(shape)*dtype.itemsize
    a = np.empty(M+n, dtype=np.dtype('uint8'))
    offset = a.ctypes.data % n
    offset = 0 if offset == 0 else (n - offset)
    b = np.frombuffer(a[offset:(offset+M)].data, dtype=dtype).reshape(shape)
    if fill is not None:
        assert isinstance(fill, int)
        b[...] = fill
    return b

cpdef aligned_like(z, fill=None):
    """Return array with byte-alignment, shape and type like array z

    Parameters
    ----------
    z : array
        An array with shape and type we want to recreate
    fill : None or number, optional
        If number then fill returned array with this number, otherwise return
        empty array

    Returns
    -------
    array
        byte-aligned array

    """
    n = get_alignment(z)
    return aligned(z.shape, n=n, dtype=z.dtype, fill=fill)