NumPy C code explanations
char*
rank-0
NULL
Besides the structural information contained in the strides and
dimensions members of the PyArrayObject, the flags contain
important information about how the data may be accessed. In particular,
the NPY_ARRAY_ALIGNED flag is set when the memory is on a
suitable boundary according to the datatype array. Even if you have
a contiguous chunk of memory, you cannot just assume it is safe to
dereference a datatype-specific pointer to an element. Only if the
NPY_ARRAY_ALIGNED flag is set, this is a safe operation. On
some platforms it will work but on others, like Solaris, it will cause
a bus error. The NPY_ARRAY_WRITEABLE should also be ensured
if you plan on writing to the memory area of the array. It is also
possible to obtain a pointer to an unwritable memory area. Sometimes,
writing to the memory area when the NPY_ARRAY_WRITEABLE flag is not
set will just be rude. Other times it can cause program crashes (e.g.
a data-area that is a read-only memory-mapped file).
:]
There is a subarray but it has exactly one element. This case can be handled
as if there is no subarray but needs some care during setup.
Deciding what case applies, checking broadcasting, and determining the kind
of transposition needed are all done in PyArray_MapIterNew. After
setting up, there are two cases. If there is no subarray or it only has one
element, no subarray iteration is necessary and an iterator is prepared
which iterates all indexing arrays as well as the result or value array.
If there is a subarray, there are three iterators prepared. One for the
indexing arrays, one for the result or value array (minus its subarray),
and one for the subarrays of the original and the result/assignment array.
The first two iterators give (or allow calculation) of the pointers into
the start of the subarray, which then allows restarting the subarray
iteration.
When advanced indices are next to each other transposing may be necessary.
All necessary transposing is handled by PyArray_MapIterSwapAxes and
has to be handled by the caller unless PyArray_MapIterNew is asked to
allocate the result.
After preparation, getting and setting are relatively straightforward,
although the different modes of iteration need to be considered. Unless
there is only a single indexing array during item getting, the validity of
the indices is checked beforehand. Otherwise, it is handled in the inner
loop itself for optimization.
After checking, the thread-specific global variables, the inputs are
evaluated to determine how the ufunc should proceed and the input and
output arrays are constructed if necessary. Any inputs which are not
arrays are converted to arrays (using context if necessary). Which of
the inputs are scalars (and therefore converted to 0-D arrays) is
noted.
Next, an appropriate 1-D loop is selected from the 1-D loops available
to the ufunc based on the input array types. This 1-D loop is selected
by trying to match the signature of the datatypes of the inputs
against the available signatures. The signatures corresponding to
built-in types are stored in the ufunc.types member of the ufunc
structure. The signatures corresponding to user-defined types are stored in a
linked list of function information with the head element stored as a
CObject in the userloops dictionary keyed by the datatype number
(the first user-defined type in the argument list is used as the key).
The signatures are searched until a signature is found to which the
input arrays can all be cast safely (ignoring any scalar arguments
which are not allowed to determine the type of the result). The
implication of this search procedure is that “lesser types” should be
placed below “larger types” when the signatures are stored. If no 1-D
loop is found, then an error is reported. Otherwise, the argument_list
is updated with the stored signature — in case casting is necessary
and to fix the output types assumed by the 1-D loop.
If the ufunc has 2 inputs and 1 output and the second input is an
Object array then a special-case check is performed so that
NotImplemented is returned if the second input is not an ndarray, has
the __array_priority__ attribute, and has an __r{op}__
special method. In this way, Python is signaled to give the other object a
chance to complete the operation instead of using generic object-array
calculations. This allows (for example) sparse matrices to override
the multiplication operator 1-D loop.
For input arrays that are smaller than the specified buffer size,
copies are made of all non-contiguous, misaligned, or out-of-byteorder
arrays to ensure that for small arrays, a single loop is
used. Then, array iterators are created for all the input arrays and
the resulting collection of iterators is broadcast to a single shape.
The output arguments (if any) are then processed and any missing
return arrays are constructed. If any provided output array doesn’t
have the correct type (or is misaligned) and is smaller than the
buffer size, then a new output array is constructed with the special
NPY_ARRAY_WRITEBACKIFCOPY flag set. At the end of the function,
PyArray_ResolveWritebackIfCopy is called so that
its contents will be copied back into the output array.
Iterators for the output arguments are then processed.
Finally, the decision is made about how to execute the looping
mechanism to ensure that all elements of the input arrays are combined
to produce the output arrays of the correct type. The options for loop
execution are one-loop (for contiguous, aligned, and correct data
type), strided-loop (for non-contiguous but still aligned and correct
data type), and a buffered loop (for misaligned or incorrect data
type situations). Depending on which execution method is called for,
the loop is then set up and computed.
This is the code that handles the situation whenever the input and/or
output arrays are either misaligned or of the wrong datatype
(including being byteswapped) from what the underlying 1-D loop
expects. The arrays are also assumed to be non-contiguous. The code
works very much like the strided-loop except for the inner 1-D loop is
modified so that pre-processing is performed on the inputs and post-processing
is performed on the outputs in bufsize chunks (where
bufsize is a user-settable parameter). The underlying 1-D
computational loop is called on data that is copied over (if it needs
to be). The setup code and the loop code is considerably more
complicated in this case because it has to handle:
memory allocation of the temporary buffers
deciding whether or not to use buffers on the input and output data
(misaligned and/or wrong datatype)
copying and possibly casting data for any inputs or outputs for which
buffers are necessary.
special-casing Object arrays so that reference counts are properly
handled when copies and/or casts are necessary.
breaking up the inner 1-D loop into bufsize chunks (with a possible
remainder).
Again, the hardware error flags are checked at the end of each 1-D
loop.
TypeError
[otype,
otype,
otype]
otype