SciRuby / nmatrix

Dense and sparse linear algebra library for Ruby via SciRuby
Other
469 stars 133 forks source link

Segmentation fault on NMatrix#each #146

Closed dustalov closed 11 years ago

dustalov commented 11 years ago

I had encountered a very strange NMatrix behaviour that causes the whole Ruby program to crash randomly.

#!/usr/bin/env ruby

require 'rubygems'
require 'nmatrix'

# the dimensions are provided by example
matrix = NMatrix.new([228, 359], :stype => :yale)

228.times { |i| matrix.col(i).to_a; p i }

When I ran this code on Ruby 2.0.0p247, the following lines appeared on my terminal.

bundle exec ruby /home/dmchk/Work/recsys/PA1/reproduce.rb
0
1
...
146
147
/usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/nmatrix.rb:256: [BUG] Segmentation fault
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]

-- Control frame information -----------------------------------------------
c:0012 p:---- s:0039 E:000790 CFUNC  :each
c:0011 p:---- s:0037 E:000170 CFUNC  :with_index
c:0010 p:0025 s:0034 E:001080 METHOD /usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/nmatrix.rb:256
c:0009 p:0010 s:0030 e:000028 BLOCK  /usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/nmatrix.rb:313
c:0008 p:0013 s:0026 e:000025 BLOCK  /usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/enumerate.rb:123 [FINISH]
c:0007 p:---- s:0023 e:000022 CFUNC  :each
c:0006 p:0040 s:0020 e:000019 METHOD /usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/enumerate.rb:122
c:0005 p:0048 s:0016 e:000015 METHOD /usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/nmatrix.rb:312
c:0004 p:0012 s:0011 e:000010 BLOCK  /home/dmchk/Work/recsys/PA1/reproduce.rb:8 [FINISH]
c:0003 p:---- s:0008 e:000007 CFUNC  :times
c:0002 p:0043 s:0005 E:001310 EVAL   /home/dmchk/Work/recsys/PA1/reproduce.rb:8 [FINISH]
c:0001 p:0000 s:0002 E:001758 TOP    [FINISH]

-- Ruby level backtrace information ----------------------------------------
/home/dmchk/Work/recsys/PA1/reproduce.rb:8:in `<main>'
/home/dmchk/Work/recsys/PA1/reproduce.rb:8:in `times'
/home/dmchk/Work/recsys/PA1/reproduce.rb:8:in `block in <main>'
/usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/nmatrix.rb:312:in `to_a'
/usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/enumerate.rb:122:in `each_row'
/usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/enumerate.rb:122:in `each'
/usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/enumerate.rb:123:in `block in each_row'
/usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/nmatrix.rb:313:in `block in to_a'
/usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/nmatrix.rb:256:in `to_flat_array'
/usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/nmatrix.rb:256:in `with_index'
/usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/nmatrix.rb:256:in `each'

-- C level backtrace information -------------------------------------------
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x17204b) [0x7ff87cd9204b]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x6379a) [0x7ff87cc8379a]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(rb_bug+0xbb) [0x7ff87cc83e7b]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x10d2f6) [0x7ff87cd2d2f6]
/lib64/libpthread.so.0() [0x36d5e0efa0]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x7a040) [0x7ff87cc9a040]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x7a28a) [0x7ff87cc9a28a]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x7ad23) [0x7ff87cc9ad23]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x7c232) [0x7ff87cc9c232]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(rb_data_typed_object_alloc+0x18) [0x7ff87cc9c538]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x5f38f) [0x7ff87cc7f38f]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x60379) [0x7ff87cc80379]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x15c8eb) [0x7ff87cd7c8eb]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x16c40f) [0x7ff87cd8c40f]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(rb_iterate+0xdf) [0x7ff87cd8d20f]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(rb_block_call+0x2b) [0x7ff87cd8d56b]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x617af) [0x7ff87cc817af]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x15c192) [0x7ff87cd7c192]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x160ff0) [0x7ff87cd80ff0]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x165d86) [0x7ff87cd85d86]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(rb_yield+0x202) [0x7ff87cd89402]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0xdae9d) [0x7ff87ccfae9d]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x15c192) [0x7ff87cd7c192]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x160ff0) [0x7ff87cd80ff0]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x165d86) [0x7ff87cd85d86]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(rb_yield+0x202) [0x7ff87cd89402]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0xa3d7d) [0x7ff87ccc3d7d]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x15c192) [0x7ff87cd7c192]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x16da36) [0x7ff87cd8da36]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x160ff0) [0x7ff87cd80ff0]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x165d86) [0x7ff87cd85d86]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(rb_iseq_eval_main+0x81) [0x7ff87cd87c29]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(+0x66cc2) [0x7ff87cc86cc2]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(ruby_exec_node+0x1d) [0x7ff87cc88375]
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/libruby.so.2.0(ruby_run_node+0x1e) [0x7ff87cc89e8e]
ruby() [0x4008cb]
/lib64/libc.so.6(__libc_start_main+0xf5) [0x36d5621b75]
ruby() [0x4008f9]

-- Other runtime information -----------------------------------------------

* Loaded script: /home/dmchk/Work/recsys/PA1/reproduce.rb

* Loaded features:

    0 enumerator.so
    1 /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/x86_64-linux/enc/encdb.so
    2 /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/x86_64-linux/enc/trans/transdb.so
    3 /usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/x86_64-linux/rbconfig.rb
...
   86 /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/source/git/git_proxy.rb
   87 /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/dep_proxy.rb
   88 /usr/local/rvm/gems/ruby-2.0.0-p247@global/gems/bundler-1.3.5/lib/bundler/setup.rb
   89 /usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix.so
   90 /usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/lapack.rb
   91 /usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/yale_functions.rb
   92 /usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/shortcuts.rb
   93 /usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/math.rb
   94 /usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/enumerate.rb
   95 /usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/nmatrix.rb
   96 /usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/version.rb
   97 /usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/blas.rb
   98 /usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix/monkeys.rb
   99 /usr/local/rvm/gems/ruby-2.0.0-p247/bundler/gems/nmatrix-730e1ccb7006/lib/nmatrix.rb

It is also interesting that the last i value on the output varies from each invocation. I ran this code multiple times and got 147, 179, 60, etc as the last printed values.

translunar commented 11 years ago

I'll look at it in just a few minutes. As a temporary workaround, try setting a dtype. Right now you're creating a Ruby object matrix, but it may work properly with a :float64 or :byte or whatever specific type you need.

It's also worth noting that the operation you're performing (getting columns from a Yale matrix) is going to be very expensive if there are entries in the matrix. Rows is O(1). Columns are log(n).

Also consider trying it in the version currently on github. I may have fixed this bug in a recent push.

translunar commented 11 years ago

I believe this was fixed by https://github.com/SciRuby/nmatrix/commit/31fae098c1f21731221303d828d028d03b1aabcd

Can you please try getting the current version from github? There have been a number of Yale-related bug fixes.

dustalov commented 11 years ago

It seems that 31fae09 actually fixes the problem. Thank you very much for the robust response and keep up with good work!