ruby-numo / numo-narray

Ruby/Numo::NArray - New NArray class library
http://ruby-numo.github.io/narray/
BSD 3-Clause "New" or "Revised" License
418 stars 42 forks source link

[Propose] Stop making a view of NArray at #aref (or Stop using idx) #108

Open sonots opened 6 years ago

sonots commented 6 years ago

In Numo, advanced indexing returns a view of a Numo NArray.

irb(main):007:0> a = Numo::Int32.new(3,4).seq
=> Numo::Int32#shape=[3,4]
[[0, 1, 2, 3],
 [4, 5, 6, 7],
 [8, 9, 10, 11]]
irb(main):012:0> a[a > 5]
=> Numo::Int32(view)#shape=[6]
[6, 7, 8, 9, 10, 11]

In Numpy, advanced indexing returns a new ndarray.

>>> a = numpy.arange(12).reshape(3,4)
>>> b = a[a > 5]
>>> b
array([ 6,  7,  8,  9, 10, 11])
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> b[0] = 1
>>> b
array([ 1,  7,  8,  9, 10, 11])
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

I propose Numo to work similarly with NumPy.

By stopping to make a view of NArray at #[], we can remove NDF_INDEX_LOOP, or all of codes using idx internally, and results in making very simple implementation.


This is a kind of feedback from Cumo. In Cumo, idx C array must be transferred from CPU to GPU when we want to use it in CUDA Kernel. Also, we have to copy back from GPU to CPU when we want to use it in CPU side.

It made implementation of Cumo very complex. I want to stop using idx.

sonots commented 6 years ago

I talked with @masa16, and we agreed that making the behavior same with NumPy ndarray.