Fast Element Wise Operations

33 views
Skip to first unread message

Henry Chinner

unread,
Apr 7, 2015, 4:26:33 PM4/7/15
to sciru...@googlegroups.com
I believe at the moment that matrix addition, subtraction and scalar multiplication is implemented in Ruby space. This results in a huge bottleneck in HPC applications. I implemented a neural network in NMatrix and 50% of the computation time is spent adding up matrices.

Would it be possible to implement a c type iterator that takes as arguments 2 dense matrices and then applies an arbitrary function on each element pair?

Something like mat1.zip(mat2).map!{|el el[0] + el[1] } would be equivalent to mat1 + mat2, but you can provide any function in the block, and this will be executed in c.

I have been going over the code to see if I can implement it myself but the code is a bit overwhelming as I do not know c or c++.

Do you guys have any pointers for me to get started on this to try and implement this function?

Also, are there other people that need this type functionality or am I missing something obvious?

Thanks,
Henry


Rodrigo Botafogo

unread,
Apr 8, 2015, 5:51:04 PM4/8/15
to sciru...@googlegroups.com
Henry,

May I suggest that you try MDArray with JRuby, or also SciCom?  In MDArray, array operations are done in Java and I believe that it should be close to C performance, but I haven´t really profiled it.  MDArray also provides classe MDMatrix that uses Parallel Colt for processing, in this case you could also get the benefit of parallel processing if your matrices are large.  In SciCom, matrix operations are delayed to the last necessary time, so if you have large operations that do not need to be performed immediately then you could get a huge performance improvement.

Finally, if you need to apply an arbitrary function to a dense matrix, this shouldn´t be to hard to implement in MDArray.  I could take a look, although I´m really short in time lately.

Let me know if I can help in any other way.


Cheers....  

John Woods

unread,
Apr 8, 2015, 6:03:45 PM4/8/15
to sciru...@googlegroups.com
Sorry for the slow response.

Yes. I think that would be pretty simple.

I think this is the place in the history where we started changing to Ruby-based elementwise operations:


So you can get an idea how some of the templates worked there. I'd suggest taking out that code and attaching it to a different function, so the Ruby elementwise functions are still accessible — but so that you can instead call, like, NMatrix#c_add, and have it do it all in C.

You'd want to restrict it so it wouldn't work on the Ruby object type, and probably also on rationals. Heck, you could basically define it for just double if you wanted, and have all other stypes attempting to call c_add raise an exception. That would enable you to cut out the template code.

But look through those changes. They're much simpler than the current elementwise code — so maybe you'll have more luck reading it.

John

--
You received this message because you are subscribed to the Google Groups "SciRuby Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sciruby-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Henry Chinner

unread,
Apr 9, 2015, 2:38:36 AM4/9/15
to sciru...@googlegroups.com
Great, I did not know that JRuby had an equivalent. I'll give MDArray a spin to see what the performance gap is, if any.

Thanks,
Henry

Henry Chinner

unread,
Apr 9, 2015, 6:07:59 AM4/9/15
to sciru...@googlegroups.com
Thanks John, I had a look. Things seem a bit more clear. I may come back with some additional questions if you do not mind.

John Woods

unread,
Apr 9, 2015, 8:25:57 AM4/9/15
to sciru...@googlegroups.com
Not at all. Working out these issues is how we improve NMatrix. :)

Rodrigo Botafogo

unread,
Apr 9, 2015, 2:46:58 PM4/9/15
to sciru...@googlegroups.com
Great,

I would love to get some feedback on performance, and let me know if you need any help.... there isn´t to much documentation yet.
Reply all
Reply to author
Forward
0 new messages