Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation faults when using sparse matrices #35

Closed
jpmckinney opened this issue Sep 10, 2012 · 8 comments
Closed

Segmentation faults when using sparse matrices #35

jpmckinney opened this issue Sep 10, 2012 · 8 comments
Assignees
Labels

Comments

@jpmckinney
Copy link

When I try to fill a sparse matrix with floats, I get segmentation faults. I printed out the i and j variables to see if it was trying to write outside the dimensions of the matrix, but it's not the case. I print out the value that I was trying to assign to the matrix cell, but it is 0.0, so not outside the float range.

My code looks like:

matrix = NMatrix.new(:list, [row_size, column_size], :float64)
rows.each_with_index do |x,i|
  columns.each_with_index do |y,j|
    matrix[i, j] = some_method(x, y)
  end
end

With :list I get:

irb(77457,0x7fff7c400960) malloc: *** error for object 0x7fff5fe83440: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

With :yale I get:

(irb):14: [BUG] Segmentation fault
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]

-- Control frame information -----------------------------------------------
c:0037 p:---- s:0123 b:0123 l:000122 d:000122 CFUNC  :[]=
c:0036 p:0073 s:0117 b:0116 l:000618 d:000115 BLOCK  (irb):14
c:0035 p:---- s:0111 b:0111 l:000110 d:000110 FINISH
c:0034 p:---- s:0109 b:0109 l:000098 d:000108 IFUNC 
c:0033 p:---- s:0107 b:0107 l:000102 d:000106 IFUNC 
c:0032 p:---- s:0105 b:0105 l:000104 d:000104 CFUNC  :each
c:0031 p:---- s:0103 b:0103 l:000102 d:000102 CFUNC  :each_with_index
c:0030 p:---- s:0101 b:0101 l:000100 d:000100 CFUNC  :each
c:0029 p:---- s:0099 b:0099 l:000098 d:000098 CFUNC  :map
c:0028 p:0040 s:0096 b:0096 l:000618 d:000095 BLOCK  (irb):11
c:0027 p:---- s:0091 b:0091 l:000090 d:000090 FINISH
c:0026 p:---- s:0089 b:0089 l:000084 d:000088 IFUNC 
c:0025 p:---- s:0087 b:0087 l:000086 d:000086 CFUNC  :each
c:0024 p:---- s:0085 b:0085 l:000084 d:000084 CFUNC  :each_with_index
c:0023 p:0017 s:0082 b:0082 l:000618 d:000081 EVAL   (irb):9
c:0022 p:---- s:0080 b:0080 l:000079 d:000079 FINISH
c:0021 p:---- s:0078 b:0078 l:000077 d:000077 CFUNC  :eval
c:0020 p:0028 s:0071 b:0071 l:000070 d:000070 METHOD /Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/workspace.rb:80
c:0019 p:0033 s:0064 b:0063 l:000062 d:000062 METHOD /Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/context.rb:254
c:0018 p:0031 s:0058 b:0058 l:001e68 d:000057 BLOCK  /Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:159
c:0017 p:0042 s:0050 b:0050 l:000049 d:000049 METHOD /Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:273
c:0016 p:0011 s:0045 b:0045 l:001e68 d:000044 BLOCK  /Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:156
c:0015 p:0144 s:0041 b:0041 l:000024 d:000040 BLOCK  /Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:243
c:0014 p:---- s:0038 b:0038 l:000037 d:000037 FINISH
c:0013 p:---- s:0036 b:0036 l:000035 d:000035 CFUNC  :loop
c:0012 p:0009 s:0033 b:0033 l:000024 d:000032 BLOCK  /Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:229
c:0011 p:---- s:0031 b:0031 l:000030 d:000030 FINISH
c:0010 p:---- s:0029 b:0029 l:000028 d:000028 CFUNC  :catch
c:0009 p:0023 s:0025 b:0025 l:000024 d:000024 METHOD /Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:228
c:0008 p:0046 s:0022 b:0022 l:001e68 d:001e68 METHOD /Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:155
c:0007 p:0011 s:0019 b:0019 l:0011d8 d:000018 BLOCK  /Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:70
c:0006 p:---- s:0017 b:0017 l:000016 d:000016 FINISH
c:0005 p:---- s:0015 b:0015 l:000014 d:000014 CFUNC  :catch
c:0004 p:0183 s:0011 b:0011 l:0011d8 d:0011d8 METHOD /Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:69
c:0003 p:0142 s:0006 b:0006 l:0023e8 d:001458 EVAL   /Users/james/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH
c:0001 p:0000 s:0002 b:0002 l:0023e8 d:0023e8 TOP   

-- Ruby level backtrace information ----------------------------------------
/Users/james/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'
/Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:69:in `start'
/Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:69:in `catch'
/Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:70:in `block in start'
/Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:155:in `eval_input'
/Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `each_top_level_statement'
/Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in `catch'
/Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `block in each_top_level_statement'
/Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in `loop'
/Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:243:in `block (2 levels) in each_top_level_statement'
/Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:156:in `block in eval_input'
/Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:273:in `signal_status'
/Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:159:in `block (2 levels) in eval_input'
/Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/context.rb:254:in `evaluate'
/Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/workspace.rb:80:in `evaluate'
/Users/james/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/workspace.rb:80:in `eval'
(irb):9:in `irb_binding'
(irb):9:in `each_with_index'
(irb):9:in `each'
(irb):11:in `block in irb_binding'
(irb):11:in `map'
(irb):11:in `each'
(irb):11:in `each_with_index'
(irb):11:in `each'
(irb):14:in `block (2 levels) in irb_binding'
(irb):14:in `[]='

-- C level backtrace information -------------------------------------------

   See Crash Report log file under ~/Library/Logs/CrashReporter or
   /Library/Logs/CrashReporter, for the more detail of.

-- Other runtime information -----------------------------------------------
@atimin
Copy link

atimin commented Sep 10, 2012

It will be fixed but I suspect that you have some misconceptions about a list matrix.

  1. A list matrix has default value (by default it equals zero). You will get this value always when you access to non-existent element of the matrix. After creation a list matrix is empty, but we see it as filed by zero. When you assign to a cell default value you actually remove it. In this way, your code (if it works) does nothing.

  2. If you want to fill your matrix fully or bigger part. To be better use a dense matrix.

Thanks for report, if @MohawkJohn will not be against I can fix up this bug.

@jpmckinney
Copy link
Author

For (2) most of the values I'm setting are 0. I'm using a sparse matrix as a vector space model.

For (1), I don't understand, "your code (if it works) does nothing". For example, this works fine:

matrix = NMatrix.new(:list, [2, 2], :float64)
matrix.pretty_print

Prints:

0.0  0.0
0.0  0.0

Then:

matrix[0, 0] = 1
matrix.pretty_print

Gives:

1.0  0.0
0.0  0.0

My code is doing the same thing as above, except inside loops.

@atimin
Copy link

atimin commented Sep 10, 2012

(1) Sorry. I have understood what you fill a matrix by zero values. And from (2) I agree that your choice of type matrix is right.

Well.. we will fix it as soon as it possible. Thank you very mach for report again.

@jpmckinney
Copy link
Author

Forgot to mention: this is with 0.0.1 release. I will test with HEAD as soon as I can install it.

@atimin
Copy link

atimin commented Sep 10, 2012

It's very important because 0.0.2 is very differ from 0.0.1 inwardly. Actually 0.0.2 is result of deep refactoring and a big part had been rewritten in C++.

I will check up it for 0.0.2.

@atimin
Copy link

atimin commented Sep 10, 2012

For 0.0.2 this code works right:

    m = NMatrix.new(:list, [3, 3], :float64)
    [1.0,2.0,3.0].each_with_index do |x,i|
      [1.0,2.0,3.0].each_with_index do |y,j|
        m[i, j] = x+y
      end
    end
    m.pretty_print

Pleas try it again when you will have installed 0.0.2 version.

@ghost ghost assigned atimin Sep 10, 2012
@translunar
Copy link
Member

Is this working for you now, @jpmckinney? Thanks, Aleksey, for working on this.

@jpmckinney
Copy link
Author

My code works with :list now, thanks! And I'm able to cast it to :yale after building it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants