arrayfire / arrayfire

ArrayFire: a general purpose GPU library.

Home Page:https://arrayfire.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] Sorting can fail when a NAN is present

dmittiga opened this issue · comments

For some arrays, sorting can fail when a single nan is present.

Description

When sorting an array, the results are not always ordered when a single nan is present. An example is provided below.

I would expect that all non-nan values would be sorted at the front of the result and that any nans are at the end. Instead, I find that the non-nan values are not ordered.
image

Reproducible Code and/or Steps

float data[] = {0,  1,  2,  -3,
                8,  9, -10, 11,
                4,  5,  6,  7,
                12, -13, 14, 15};

af::array A(16, 1, data);
A=af::join(0,A,A+50,A+100,A-50);
A=af::join(0,A-200,A+250,A,A+550);
A(123)=af::NaN;
af::array inds, sorted;
af::sort(sorted, inds, A);
af::print("problem region",sorted(af::seq(185,195)));
af::print("full array sorted",sorted);

System Information

ArrayFire v3.8.2 (CPU, 64-bit Linux, build 5752f2d)
[0] Intel: 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz

LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: AlmaLinux
Description: AlmaLinux release 8.7 (Stone Smilodon)
Release: 8.7
Codename: StoneSmilodon

There is no logically consistent way of sorting NaN and we'd need to think of some custom operator if we wanted to emulate that nonstandard behavior.
I'd recommend replacing NaNs with inf or -inf for the first/last behavior you want, or masking the array to only sort non-NaN values.