|
Expand Up
|
@@ -4509,6 +4509,111 @@ static int |
|
|
} |
|
|
/**end repeat**/ |
|
|
|
|
|
/* |
|
|
***************************************************************************** |
|
|
** SORTING ARRAYMETHODS ** |
|
|
***************************************************************************** |
|
|
*/ |
|
|
|
|
|
static NPY_CASTING |
|
|
sort_resolve_descriptors( |
|
|
PyArrayMethodObject *method, |
|
|
PyArray_DTypeMeta *const *dtypes, |
|
|
PyArray_Descr *const *input_descrs, |
|
|
PyArray_Descr **output_descrs, |
|
|
npy_intp *view_offset) |
|
|
{ |
|
|
output_descrs[0] = NPY_DT_CALL_ensure_canonical(input_descrs[0]); |
|
|
if (NPY_UNLIKELY(output_descrs[0] == NULL)) { |
|
|
return -1; |
|
|
} |
|
|
output_descrs[1] = NPY_DT_CALL_ensure_canonical(input_descrs[1]); |
|
|
if (NPY_UNLIKELY(output_descrs[1] == NULL)) { |
|
|
Py_XDECREF(output_descrs[0]); |
|
|
return -1; |
|
|
} |
Comment thread
seberg marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
|
|
|
|
|
|
return method->casting; |
|
|
} |
|
|
|
|
|
static NPY_CASTING |
|
|
argsort_resolve_descriptors( |
|
|
PyArrayMethodObject *method, |
|
|
PyArray_DTypeMeta *const *dtypes, |
|
|
PyArray_Descr *const *input_descrs, |
|
|
PyArray_Descr **output_descrs, |
|
|
npy_intp *view_offset) |
|
|
{ |
|
|
output_descrs[0] = NPY_DT_CALL_ensure_canonical(input_descrs[0]); |
|
|
if (NPY_UNLIKELY(output_descrs[0] == NULL)) { |
|
|
return -1; |
|
|
} |
|
|
output_descrs[1] = PyArray_DescrFromType(NPY_INTP); |
|
|
if (NPY_UNLIKELY(output_descrs[1] == NULL)) { |
|
|
Py_XDECREF(output_descrs[0]); |
|
|
return -1; |
|
|
} |
|
|
|
|
|
return method->casting; |
|
|
} |
Comment thread
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, I guess we these them for timedelta64/datetime64 unfortunately, because it is parametric? (Just too bad, since this is all very simple in principle...)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunate indeed, yes, it's because they're parametric. Given how often we're running into this, probably good to fix the issue somehow...
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
|
|
|
|
/**begin repeat |
|
|
* |
|
|
* #suff = bool, |
|
|
* byte, ubyte, short, ushort, int, uint, |
|
|
* long, ulong, longlong, ulonglong, |
|
|
* half, float, double, longdouble, |
|
|
* cfloat, cdouble, clongdouble, |
|
|
* datetime, timedelta# |
|
|
* #rsort = 1*5, 0*15# |
|
|
*/ |
|
|
|
|
|
static int |
|
|
@suff@_sort_loop(PyArrayMethod_Context *context, |
|
|
char *const data[], npy_intp const dimensions[], |
|
|
npy_intp const strides[], NpyAuxData *NPY_UNUSED(auxdata)) |
|
|
{ |
|
|
PyArrayMethod_SortParameters *params = (PyArrayMethod_SortParameters *)context->parameters; |
|
|
switch (params->flags) { |
|
|
case NPY_SORT_DEFAULT: |
|
|
return quicksort_@suff@(data[0], dimensions[0], NULL); |
|
|
case _NPY_SORT_HEAPSORT: |
|
|
case NPY_SORT_STABLE: |
|
|
#if @rsort@ |
|
|
return radixsort_@suff@(data[0], dimensions[0], NULL); |
|
|
#else |
|
|
return timsort_@suff@(data[0], dimensions[0], NULL); |
|
|
#endif |
|
|
default: |
|
|
PyErr_Format(PyExc_RuntimeError, |
Comment thread
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering if it was possible to hit this RuntimeError -- but ideally from python. But that does seem tricky; having checked with print statements really is good enough!
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, not sure we can reach it easily. Although, once we follow up with reverse sorting, I think we'll have our proof at least?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can reach it by setting descending=True right? That should hit this?
Edit: sorry, I didn't recall we don't have it in Python yet for some reason! Yeah, that could be easier to test... no kwarg seems to hit this path right now in Python.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
|
"unknown sort kind %d for @suff@", (int)params->flags); |
|
|
return -1; |
|
|
} |
|
|
} |
|
|
static int |
|
|
@suff@_argsort_loop(PyArrayMethod_Context *context, |
|
|
char *const data[], npy_intp const dimensions[], |
|
|
npy_intp const strides[], NpyAuxData *NPY_UNUSED(auxdata)) |
|
|
{ |
|
|
PyArrayMethod_SortParameters *params = (PyArrayMethod_SortParameters *)context->parameters; |
|
|
switch (params->flags) { |
|
|
case NPY_SORT_DEFAULT: |
|
|
return aquicksort_@suff@(data[0], (npy_intp *)data[1], dimensions[0], NULL); |
|
|
case _NPY_SORT_HEAPSORT: |
|
|
case NPY_SORT_STABLE: |
|
|
#if @rsort@ |
|
|
return aradixsort_@suff@(data[0], (npy_intp *)data[1], dimensions[0], NULL); |
Comment thread
seberg marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
|
|
|
#else |
|
|
return atimsort_@suff@(data[0], (npy_intp *)data[1], dimensions[0], NULL); |
|
|
#endif |
|
|
default: |
|
|
PyErr_Format(PyExc_RuntimeError, |
|
|
"unknown sort kind %d for @suff@", (int)params->flags); |
|
|
return -1; |
|
|
} |
|
|
} |
|
|
|
|
|
/**end repeat**/ |
|
|
|
|
|
/* |
|
|
***************************************************************************** |
|
Expand Down
Expand Up
|
@@ -4600,6 +4705,72 @@ set_typeinfo(PyObject *dict) |
|
|
|
|
|
initialize_legacy_dtypemeta_aliases(_builtin_descrs); |
|
|
|
|
|
/* |
|
|
* Add sorting array methods for the new types. |
|
|
*/ |
|
|
|
|
|
/**begin repeat |
|
|
* #name = bool, |
|
|
* byte, ubyte, short, ushort, int, uint, |
|
|
* long, ulong, longlong, ulonglong, |
|
|
* half, float, double, longdouble, |
|
|
* cfloat, cdouble, clongdouble, |
|
|
* datetime, timedelta# |
|
|
* #NAME = BOOL, |
|
|
* BYTE, UBYTE, SHORT, USHORT, INT, UINT, |
|
|
* LONG, ULONG, LONGLONG, ULONGLONG, |
|
|
* HALF, FLOAT, DOUBLE, LONGDOUBLE, |
|
|
* CFLOAT, CDOUBLE, CLONGDOUBLE, |
|
|
* DATETIME, TIMEDELTA# |
|
|
*/ |
|
|
dtypemeta = NPY_DTYPE(_builtin_descrs[NPY_@NAME@]); |
|
|
|
|
|
PyArray_DTypeMeta *sort_dtypes_@name@[2] = {dtypemeta, dtypemeta}; |
|
|
PyType_Slot sort_slots_@name@[3] = { |
|
|
{NPY_METH_resolve_descriptors, sort_resolve_descriptors}, |
|
|
{NPY_METH_strided_loop, @name@_sort_loop}, |
|
|
{0, NULL} |
|
|
}; |
|
|
PyArrayMethod_Spec sort_spec_@name@ = { |
|
|
.name = "@name@_sort", |
|
|
.nin = 1, |
|
|
.nout = 1, |
|
|
.dtypes = sort_dtypes_@name@, |
|
|
.slots = sort_slots_@name@, |
|
|
.flags = NPY_METH_NO_FLOATINGPOINT_ERRORS, |
|
|
}; |
|
|
PyBoundArrayMethodObject *sort_method_@name@ = PyArrayMethod_FromSpec_int( |
|
|
&sort_spec_@name@, 1); |
|
|
if (sort_method_@name@ == NULL) { |
|
|
return -1; |
|
|
} |
|
|
NPY_DT_SLOTS(dtypemeta)->sort_meth = sort_method_@name@->method; |
|
|
Py_INCREF(sort_method_@name@->method); |
Comment thread
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason, decrefing sort_method_@name@ causes a segfault, but this is leaking otherwise...
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather cryptic, I agree. The problem is that:
if (PyType_Ready(&PyArrayMethod_Type) < 0) {
return -1;
}
if (PyType_Ready(&PyBoundArrayMethod_Type) < 0) {
return -1;
}
needs to be called first. Remind me: object and void already use this due to being the default fallback path, I guess?
(But I agree that they are different enough, either way.)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No I don't think so actually! object and void have a comparison function that is set here:
(PyArray_CompareFunc*)@from@_compare,
So, they are using the old path. That said it shouldn't at all be hard to use npy_default_sort_loop to take in a new comparison function for these.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, if those use @from@_compare, can we then not do the same in #30415 (we discussed there somewhat at #30415 (review))
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, we could add that slot, but in the end, I suppose either way we'll be using a hard-coded path for those. (We'll probably just want to change the signature of the internal sort function to re-align, but it is hard-coded either way.)
One could add a "uses compare slot" auto-fill, but I am not sure it is worth it.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, probably will hard-code those, so unclear whether filling the new slot with legacy sort is worth the tradeoff.
So for the DECREF... I guess this entire registration needed to be moved, so I added a function and called that after that block instead of within set_typeinfo, thanks!
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
seberg reacted with thumbs up emoji
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That should work yeah. If it is nicer, you can also just move the finalization of ArrayMethod_Type and BoundArrayMethod_Type to happen first, they don't have any dependencies and can happen much earlier.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah okay, thanks! That does look nicer (less verbose), pushed!
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
|
|
|
|
PyArray_DTypeMeta *argsort_dtypes_@name@[2] = {dtypemeta, &PyArray_IntpDType}; |
|
|
PyType_Slot argsort_slots_@name@[3] = { |
|
|
{NPY_METH_resolve_descriptors, argsort_resolve_descriptors}, |
|
|
{NPY_METH_strided_loop, @name@_argsort_loop}, |
|
|
{0, NULL} |
|
|
}; |
|
|
PyArrayMethod_Spec argsort_spec_@name@ = { |
|
|
.name = "@name@_argsort", |
|
|
.nin = 1, |
|
|
.nout = 1, |
|
|
.dtypes = argsort_dtypes_@name@, |
|
|
.slots = argsort_slots_@name@, |
|
|
.flags = NPY_METH_NO_FLOATINGPOINT_ERRORS, |
|
|
}; |
|
|
PyBoundArrayMethodObject *argsort_method_@name@ = PyArrayMethod_FromSpec_int( |
|
|
&argsort_spec_@name@, 1); |
|
|
if (argsort_method_@name@ == NULL) { |
|
|
return -1; |
|
|
} |
|
|
NPY_DT_SLOTS(dtypemeta)->argsort_meth = argsort_method_@name@->method; |
|
|
Py_INCREF(argsort_method_@name@->method); |
|
|
|
|
|
/**end repeat**/ |
|
|
|
|
|
/* |
|
|
* Add cast functions for the new types |
|
|
*/ |
|
Expand Down
|
|