scientific computing on jruby
TRANSCRIPT
![Page 1: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/1.jpg)
Scientific Computing on JRuby
github.com/prasunanand
![Page 2: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/2.jpg)
Objective●A Scientific library is memory intensive and speed counts.How to
use JRuby effectively to create a great tool/gem.
●A General Purpose GPU library for Ruby that can be used by industry in production and academia for research.
![Page 3: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/3.jpg)
●Ruby Science Foundation
●SciRuby has been trying to push Ruby for scientific computing.
●Popular Rubygems:
1.NMatrix
2.Daru
3.Mixed_models
4.Iruby_notebook
![Page 4: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/4.jpg)
NMatrixNMatrix is SciRuby’s numerical matrix core, implementing dense matrices as well as two types of sparse (linked-list-based and Yale/CSR).
It currently relies on ATLAS/CBLAS/CLAPACK and standard LAPACK for several of its linear algebra operations.
![Page 5: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/5.jpg)
![Page 6: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/6.jpg)
Daru
![Page 7: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/7.jpg)
Mixed_models
![Page 8: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/8.jpg)
Nyaplot
![Page 9: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/9.jpg)
Why nya?
![Page 10: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/10.jpg)
![Page 11: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/11.jpg)
Contributors wanted
●IRC #sciruby
●Slack-channel #sciruby
●Google-group #sciruby
![Page 12: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/12.jpg)
Known for performance JRuby is 10 times faster than CRuby.
With truffle it’s around 40 times faster than CRuby.
![Page 13: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/13.jpg)
Say hello
![Page 14: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/14.jpg)
NMatrix for JRuby●Not a unified interface for Sciruby gems: MDArray.
●MDArray is a great gem for Linear Algebra.
●However, every gem that used NMatrix as dependency needed to
be reimplemented with MDArray.
●Hence, putting in effort for optimization.
●MdArray used Parallel colt that was depreceated.
![Page 15: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/15.jpg)
NMatrix for JRuby●Parallelism=> No Global Interpreter Lock as in case of MRI
●Easy Deployment(Warbler gem)
![Page 16: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/16.jpg)
How NMatrix works●N-Dimensional
●2-Dimensional NMatrix
![Page 17: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/17.jpg)
N-dimensional NMatrixN-dimensional matrices are stored as a one-dimensional Array.
![Page 18: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/18.jpg)
![Page 19: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/19.jpg)
Elementwise Operation●Iterate through the elements
●Access the array; do the operation, return it
●[:add, :subtract, :sin, :gamma]
![Page 20: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/20.jpg)
Determinants and Factoriztion●Two dimensional matrix operations
●In NMatrix-MRI, BLAS-III and LAPACK routines are implemented
using their respective libraries
●NMatrix-JRuby depends on Java functions.
![Page 21: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/21.jpg)
Mixed models●After NMAtrix for doubles was ready, I tested it with mixed_models.
![Page 22: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/22.jpg)
Challenges●Autoboxing and Multiple data type
●Minimise copying of data
●Handling large array
![Page 23: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/23.jpg)
Autoboxing● :float64 => double only
● Strict dtypes => creating data type in Java: not guessing
●Errors => that can’t be reproduced :P
[ 0. 11, 0.05, 0.34, 0.14 ] + [ 0. 21,0.05, 0.14, 0.14 ] = [ 0, 0, 0, 0]
([ 0. 11, 0.05, 0.34, 0.14 ] + 5) + ([ 0. 21, 0.05, 0.14, 0.14 ] + 5) - 10 =
[ 0.32, 0.1, 0.48, 0.28]
![Page 24: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/24.jpg)
Minimise copying of data●Make sure you make copies of data
![Page 25: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/25.jpg)
Handling large arrays●Array Size
●Accessing elements
●Chaining to java method
●Speed and Memory Required
![Page 26: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/26.jpg)
Ruby Codeindex =0puts Benchmark.measure{ (0...15000).each do |i| (0...15000).each do |j| c[i][j] = b[i][j] index+=1 end end}
#67.790000 0.070000 67.860000 ( 65.126546)#RAM consumed => 5.4GB
b = Java::double[15_000,15_000].newc = Java::double[15_000,15_000].newindex=0puts Benchmark.measure{ (0...15000).each do |i| (0...15000).each do |j| b[i][j] = index index+=1 end end}#43.260000 3.250000 46.510000 ( 39.606356)
![Page 27: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/27.jpg)
![Page 28: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/28.jpg)
Java Codepublic class MatrixGenerator{public static void test2(){for (int index=0, i=0; i < row ; i++){ for (int j=0; j < col; j++){ c[i][j]= b[i][j]; index++; } }
}puts Benchmark.measure{MatrixGenerator.test2}
#0.034000 0.001000 00.034000 ( 00.03300)#RAM consumed => 300MB
public class MatrixGenerator{public static void test1(){
double[][] b = new double[15000][15000];double[][] c = new double[15000][15000];for (int index=0, i=0; i < row ; i++){ for (int j=0; j < col; j++){ b[i][j]= index; index++; } }
}puts Benchmark.measure{MatrixGenerator.test1}#0.032000 0.001000 00.032000 ( 00.03100)
![Page 29: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/29.jpg)
ResultsImproves:
●1000 times the speed
●10times the memory
![Page 30: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/30.jpg)
Benchmarking NMatrix functionalities
![Page 31: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/31.jpg)
System Specifications●CPU: AMD FX8350 0ctacore 4.2GHz
●RAM: 16GB
![Page 32: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/32.jpg)
Addition
![Page 33: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/33.jpg)
Subtraction
![Page 34: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/34.jpg)
Gamma
![Page 35: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/35.jpg)
Matrix Multiplication
![Page 36: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/36.jpg)
Determinant
![Page 37: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/37.jpg)
Factorization
![Page 38: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/38.jpg)
Benchmark conclusion●NMatrix-JRuby is incredibly faster for N-dimensional matrices when
elementwise operations are concerned.
●NMatrix-MRI is faster for 2-dimensional matrix when calculating matrix multiplication, determinant calculation and factorization.
![Page 39: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/39.jpg)
Improvements●Make NMatrix-JRuby faster than NMatrix-MRI using BLAS level-3 and
LAPACK routines.
●How?
●Why not JBlas?
![Page 40: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/40.jpg)
Future Work●Add support for complex dtype.
●Convert NMatrix-JRuby Enumerators to Java code.
●Add sparse support.
![Page 41: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/41.jpg)
Am I done?
![Page 42: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/42.jpg)
Nope!
![Page 43: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/43.jpg)
![Page 44: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/44.jpg)
![Page 45: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/45.jpg)
![Page 46: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/46.jpg)
Enter GPU
![Page 47: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/47.jpg)
A General-Purpose GPU library●Combine the beauty of Ruby with transparent GPU processing
●This will work both on client computers and on servers that make use of TESLA's and Intel Xeon Phi solutions.
● Developer activity and support for the current projects is mixed at best, and they are tough to use as they involve writing kernels and require a lot of effort to be put in buffer/RAM optimisation.
![Page 48: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/48.jpg)
ArrayFire-rb●Wraps ArrayFire library
![Page 49: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/49.jpg)
Using ArrayFire
![Page 50: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/50.jpg)
MRI●C extension
●Architecture is inspired by NMatrix and NArray
●The C++ function is placed in a namespace (e.g., namespace af { }) or is declared static if possible. The C function receives the prefix af_, e.g., af_multiply() (this function also happens to be static).
●C macros are capitalized and generally have the prefix AF_, as with AF_DTYPE().
●C functions (and macros, for consistency) are placed within extern "C" { } blocks to turn off C++ mangling.
●C macros (in extern blocks) may represent C++ constants (which are always defined in namespace af {} or a child thereof).
![Page 51: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/51.jpg)
JRuby●The approach is same as NMatrix JRuby.
●Java Native Interface( JNI )
●Work on ArrayFire-Java
![Page 52: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/52.jpg)
Benchmarking ArrayFire
![Page 53: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/53.jpg)
System SpecificationCPU: AMD FX Octacore 4.2GHz
RAM: 16GB
GPU: Nvidia GTX 750Ti
GPU RAM : 4GB DDR5
![Page 54: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/54.jpg)
Matrix Addition
![Page 55: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/55.jpg)
Matrix Multiplication
![Page 56: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/56.jpg)
Matrix Determinant
![Page 57: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/57.jpg)
Factorization
![Page 58: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/58.jpg)
Transparency●Integrate with Narray
●Integrate with NMatrix
●Integrate with Rails
![Page 59: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/59.jpg)
Applications●Endless possibilities ;)
●Bioinformatics
●Integrate Tensorflow
●Image Processing
●Computational Fluid Dynamics
![Page 60: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/60.jpg)
Conclusion
![Page 61: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/61.jpg)
Useful Links●https://github.com/sciruby/nmatrix
●https://github.com/arrayfire/arrayfire-rb
●https://github.com/prasunanand/arrayfire-rb/tree/temp
![Page 62: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/62.jpg)
Acknowlegements1.Pjotr Prins
2.Charles Nutter
3.John Woods
4.Alexej Gossmann
5.Sameer Deshmukh
6.Pradeep Garigipati
![Page 63: Scientific computing on jruby](https://reader035.vdocument.in/reader035/viewer/2022062903/589985b61a28abb97c8b66ab/html5/thumbnails/63.jpg)
Thank You
Github: prasunanandTwitter: @prasun_anandBlog: prasunanand.com