ruby-numo / numo-narray

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

[Propose] Return 0-dimensional array instead of Ruby numeric object #110

Open sonots opened 6 years ago

sonots commented 6 years ago

This is a feedback from Cumo implementation. https://speakerdeck.com/sonots/fast-numerical-computing-and-deep-learning-in-ruby-with-cumo?slide=42

Numo:

irb(main):015: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):016:0> a.sum
=> 66

Cumo:

irb(main):002:0> a = Cumo::Int32.new(3,4).seq
=> Cumo::Int32#shape=[3,4]
[[0, 1, 2, 3],
 [4, 5, 6, 7],
 [8, 9, 10, 11]]
irb(main):003:0> a.sum
=> Cumo::Int64#shape=[]
66

The reason why Cumo returns 0-dimensional array instead of Ruby numeric object is, to avoid cudaMemcpy from GPU to CPU for performance.

For compatibility between Numo and Cumo, I propose to change behavior of Numo to return 0-dimensional NArray directly without converting it into Ruby numeric object.

giuse commented 6 years ago

I second this proposition. Had a long discussion on the mathematical reasons behind it in issue #91 (which incidentally would be solved by this).