numo-narray icon indicating copy to clipboard operation
numo-narray copied to clipboard

`sort_index` returns incorrect results with GCC 13

Open ankane opened this issue 4 months ago • 2 comments

Hi, the sort_index method returns incorrect results with GCC 13 (the default for Ubuntu 24.04).

Repro script

require "numo/narray"

3.times do
  arr = Numo::SFloat.new(1000).rand
  indexes = arr.sort_index
  result = arr[indexes[0..4]]
  p result if result.to_a != result.to_a.sort
end

Output

Numo::SFloat(view)#shape=[5]
[0.000451068, 0.00365162, 0.00525034, 0.00696475, 0.00320271]
Numo::SFloat(view)#shape=[5]
[3.87633e-05, 0.00209275, 0.0235566, 0.00297531, 0.004504]

The issue doesn't occur with GCC 11 on Ubuntu 22.04, or Clang 17 on Mac.

ankane avatar Sep 06 '25 19:09 ankane

I am in the same environment as you:

$  lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04.3 LTS
Release:        24.04
Codename:       noble
$ gcc -v
...
gcc version 13.3.0 (Ubuntu 13.3.0-6ubuntu2~24.04)

I checked compiler options of Ruby and found that the -O3 option was enabled.

$ ruby -r rbconfig -e 'p RbConfig::CONFIG["CFLAGS"]'
...

The issue no longer occurs when I specify the -O2 option to lower the optimization level. For the current release, I think specifying the -O2 option is a good temporary solution.

$ gem install numo-narray -- --with-cflags='-O2'

yoshoku avatar Sep 10 '25 06:09 yoshoku

Thanks @yoshoku. My main concern is end users getting silently incorrect results.

It looks like @kojix2 found the issue and submitted a PR in #245 (huge thanks!!). Can confirm that fixes it.

ankane avatar Sep 10 '25 20:09 ankane