2011/7/7 Jens Jørgen Mortensen <[email protected]> > Hi! > > With numpy 1.5, I get this: > > >>> a = np.ones((2, 2)) > >>> (2 * a.T).strides > (16, 8) > > With 1.6, I get this: > > >>> (2 * a.T).strides > (8, 16) > > So, this means I can't count on new arrays being C-contiguous any more. > I guess there is a good reason for this. >
Yes, this was debated together with several other issues during the 1.6 beta period. The primary reason the default 'order=' setting for ufuncs changed from 'C' to 'K' was performance. For those NumPy users dealing with Fortran memory layouts or memory layouts which are neither C nor Fortran, having everything regress to a C memory layout caused many unnecessary copies and was more than an order of magnitude slower during certain computations. > Anyway, I just thought I would mention it here - maybe I'm not the only > one making this assumption when passing ndarrays to C code. > One way to deal with this is to use PyArray_FromAny with the NPY_C_CONTIGUOUS flag to ensure you have a C-aligned array. If you need to write to the array, you should also use the NPY_UPDATEIFCOPY flag. Here's how this may look (This is off the top of my head based on the documentation, I haven't tested it): http://docs.scipy.org/doc/numpy/reference/c-api.array.html#PyArray_FromAny int modify_array(PyArrayObject *arr) { PyArrayObject *c_arr = NULL; c_arr = PyArray_FromAny(arr, NULL, 0, 0, NPY_C_CONTIGUOUS | NPY_UPDATEIFCOPY, NULL); if (c_arr == NULL) { /* Return -1 indicating an error */ return -1; } /* Can now assume c_arr is C contiguous, and both read from it and write to it */ /* This triggers the UPDATE back to the original 'arr' object if a copy was made */ Py_DECREF(c_arr); /* Return 0 indicating success */ return 0; } Hope that helps! -Mark > > Jens Jørgen > > > _______________________________________________ > NumPy-Discussion mailing list > [email protected] > http://mail.scipy.org/mailman/listinfo/numpy-discussion >
_______________________________________________ NumPy-Discussion mailing list [email protected] http://mail.scipy.org/mailman/listinfo/numpy-discussion
