Fast Ruby to Julia bindings through the LLVM
| Name | Zexuan Luo |
| Github username | spacewander |
| [email protected] | |
| Timezone | Guangzhou, China, UTC +8 |
| School | South China University of Technology(SCUT) |
| Proposal Title | Fast Ruby to Julia bindings through the LLVM |
Motivation for Proposal / Goal:
Goal
Julia is a language designed to address the requirements of high-performance numerical and scientific computing. It includes efficient libraries for floating point, linear algebra, random number generation, fast Fourier transforms. If we can call Julia functions directly from Ruby, this can be a great gift to Ruby. The Ruby community will be benefited a lot in science area.
Implementation Details
First, start our journey from C world
Julia is designed to be embedded in C easily. You can call Julia code in C code, like this example:
#include <julia.h>
int main(int argc, char *argv[])
{
/* required: setup the julia context */
jl_init("/usr/bin");
/* run julia commands */
jl_eval_string("print(sqrt(2.0))");
/* strongly recommended: notify julia that the
program is about to terminate. this allows
julia time to cleanup pending write requests
and run all finalizers
*/
jl_atexit_hook();
return 0;
}
What you need is the Julia header files and Julia runtime(libjulia.so).
From the other side, calling C in Julia is possible, too. Let's quickly look at Julia's doc. Julia supports an API called ccall to call C functions.
1, 2, 3...Rabbit!
What about other languages? I mean, how can other languages work well with Julia? Some languages are implemented in C. Therefore, binding them with Julia is possible, even the binding is duplex.
Let's assume this language is X. What you need is to convert X's object into C struct, and pass it to Julia. Julia will convert it into Julia object, run Julia code in its runtime, and then pass C struct as result. Finally, X receives result and converts it back to X's object. Another question, what about GC? In Julia, you can handle objects' references, also, there is other ways to play with GC, like storing objects into global scope.
The supposition above has been implemented, the X is Python. This is what PyJulia and PyCall.jl do.(Though there is some problems with them, admittedly) Now, it should be time for Ruby.
C and more - LLVM
I have a plan. We need a mole in Julia, which handle Ruby object and Julia object, to take advantage of Julia runtime. Then, we need a Ruby gem, to wrap the (possibly dirty) details, let's name it Rumeo here. These two good friends will work like PyJulia and PyCall.jl.
What about going further? Julia is depend on LLVM, which supports its affinity for C. And Julia will be compiled into LLVM bitcode before into machine code. Hence, we can combine Ruby with Julia even in LLVM level. There is an implementation of Ruby working on LLVM, called Rubinius. We can write a gem with Rubinius, and communicate with Julia in the level of bitcode instead of C. It will benefit on speed, which makes thing better. This is why I call it fast bindings.
Drawbacks
Of course, this plan is speculative. It will be an experiment, a venture investment. Here list the possible drawback, you need to read them carefully before doing any decision:
- GC. Yes, there are some methods to handle GC, but we need to find a correct way.
- Julia's bitcode. Julia doesn't offer a way to generate its pure bitcode(code_llvm doesn't really generate pure bitcode), so, a hack can't be avoid.
- Rubinius wrapper. Rubinius offers a VM to run your code. So I have to wrap the bitcode in Ruby object, this requires well understanding on Rubinius VM.
Tentative Timeline:
Until May 25 (Community Bonding Period)
Reading docs and code of Rubinius and Julia. Try to find a way to hack. And read the code of PyCall.jl and Pyjulia. Do some design and experiments.
May 25 - June 24
Implement RbCall.jl.
June 25 - July 25
Implement Rumeo
July 26 - August 20
Implement Rumeo-llvm
August 21 - August 28
stop coding, start paperwork
Do you have other obligations from late May to early August (school, work, etc.)?
There will be some exams and some schoolwork in June. They will take some time, but could not be a problem.
About Me:
- I have been contributing to open source projects since two years ago.
- During the period of working in SCUT High-performance Computing Lab, I played a major role in the development of an application working in Tianhe-2 - the world's fastest supercomputer.
- I have experience in writing bindings for Python and Ruby(via FFI). I am also comfortable with Ruby and some other languages, such as C++, Python.
- I like learning new programming languages. It's fun to discover a new world.
The patches I sent: