 |
Forums |
Admin Discussion Forums: help Start New Thread
By: Paul Brannan
RE: How should I wrap this? [ reply ] 2009-12-07 21:36
|
FFI only needs to get the function pointer when the library is initialized; after that it is not much slower than a normal C call (on my machine I'm seeing 600ns for ffi vs. 300ns for a normal call from ruby into C). Any cost of ffi will most likely be dwarfed by the cost of method calls and dynamic allocations in the ruby interpreter itself.
I agree with Jason that ffi is the way to go here; this lets your code run on all the major ruby implementations without modification.
|
By: Matt Someone
RE: How should I wrap this? [ reply ] 2009-12-07 20:59
|
Thank you for your response =)
I've considered RubyFFI but I'm not quite convinced that its fast enough for something like numerical computation (I imagine grabing pointers to functions from libraries at runtime may be a little slow).
Since I thought on just wrapping to C++ the most interesting part of GSL (just Vector/Matrix stuff, maybe Random distributions), it may not be quite an effort to do it.
In any case, taking your suggestion into account, and given the fact that rbplusplus and rice are under heavy development, I might use RubyFFI for the time being (at least so I can test speed and such).
Thank you!
|
By: Jason Roelofs
RE: How should I wrap this? [ reply ] 2009-12-07 20:51
|
There are two ways you can go about this:
1) Build a comprehensive C++ wrapper around the entire GSL library, then expose that into Ruby with Rb++ / Rice.
2) Use Ruby-FFI[1] to expose the direct C into Ruby then write a Ruby library wrapping up the ugly details of dealing w/ the C to end up with a clean OO API.
Given that you're starting with C, and that building anything in C++ is a big, error-prone task, I would highly recommend going down path #2. Trying to go down path #1 is pretty much twice the effort as #2 as you have to build and maintain the C++ wrapper while also trying to get it wrapped nicely into Ruby.
My thoughts: Rice is not the right fit for you. Grab Ruby-FFI and play around with it some. I think it will work out a lot better than the other way.
[1]http://wiki.github.com/ffi/ffi
|
By: Matt Someone
How should I wrap this? [ reply ] 2009-12-07 19:54
|
Hi,
I'm trying to create a Ruby wrapper to the GSL (GNU Scientific Library). Some wrappers already exist, but they're composed of huge C. files that provide the necessary binding by hand.
Since GSL is a C interface (for example, a Vector is allocated with gsl_vector_alloc and freed with gsl_vector_free), everything is done with C calls and pointers to structures (like a gsl_vector*).
I wanted to create classes like Vector that automatically call the alloc/free functions when necessary, and also include all gsl_vector related functions as instance methods.
I came up with two ideas (both involving rice, since that's what I wan't to use =b):
1) Create a C++ Vector class (the constructor calls the alloc and stores the pointer, the destructor calls free). Then, I would define instance methods by hand (which route calls to gsl_vector_* methods passing the stored pointer) in the C++ class.
2) Just provide the class to do the memory managment (i.e.: just define the constructor and destructor in the C++ class) and somehow define the methods programatically from a script (using rbplusplus?).
I know 1) is possible, but it requires defining each method by hand at C++ level (which is easier than programming the C binding by hand, but still).
I don't know if (2) is actually possible somehow.
Do you have any suggestions on how can I use rice/rbplusplus for this case? I think it may be useful in other cases where one wants to wrap a C library in an OO way.
Thanks,
Matt
|
|
 |