MCPcopy Create free account
hub / github.com/numpy/numpy / _aligned_zeros

Function _aligned_zeros

numpy/core/tests/test_multiarray.py:50–83  ·  view source on GitHub ↗

Allocate a new ndarray with aligned memory. The ndarray is guaranteed *not* aligned to twice the requested alignment. Eg, if align=4, guarantees it is not aligned to 8. If align=None uses dtype.alignment.

(shape, dtype=float, order="C", align=None)

Source from the content-addressed store, hash-verified

48
49
50def _aligned_zeros(shape, dtype=float, order="C", align=None):
51 """
52 Allocate a new ndarray with aligned memory.
53
54 The ndarray is guaranteed *not* aligned to twice the requested alignment.
55 Eg, if align=4, guarantees it is not aligned to 8. If align=None uses
56 dtype.alignment."""
57 dtype = np.dtype(dtype)
58 if dtype == np.dtype(object):
59 # Can't do this, fall back to standard allocation (which
60 # should always be sufficiently aligned)
61 if align is not None:
62 raise ValueError("object array alignment not supported")
63 return np.zeros(shape, dtype=dtype, order=order)
64 if align is None:
65 align = dtype.alignment
66 if not hasattr(shape, '__len__'):
67 shape = (shape,)
68 size = functools.reduce(operator.mul, shape) * dtype.itemsize
69 buf = np.empty(size + 2*align + 1, np.uint8)
70
71 ptr = buf.__array_interface__['data'][0]
72 offset = ptr % align
73 if offset != 0:
74 offset = align - offset
75 if (ptr % (2*align)) == 0:
76 offset += align
77
78 # Note: slices producing 0-size arrays do not necessarily change
79 # data pointer --- so we use and allocate size+1
80 buf = buf[offset:offset+size+1][:-1]
81 buf.fill(0)
82 data = np.ndarray(shape, dtype, buf, order=order)
83 return data
84
85
86class TestFlags:

Callers 3

checkMethod · 0.85

Calls 2

dtypeMethod · 0.45
reduceMethod · 0.45

Tested by

no test coverage detected