List Info

Thread: Extensions and symbols




Extensions and symbols
user name
2006-06-28 13:36:46

Hello,

I write ruby extensions in C for speed (mathematical treatment of data), and I recently needed to access another's extension's internals inside my own, for performance issues. I found that this is a difficult thing to do in Ruby, and it's a pity.

To make things a bit more clear, I am working on an extension providing fast evaluation of arbitrary mathematical expressions (say, only two times less fast than equivalent C code). I want to be able to use it directly on types providing arrays of floats (such as NArray, ruby-gsl vectors ...). But, there comes my trouble: I have no easy way of accessing the double * data of those. Even if I modify the original code of these extensions, I find it hard to carry a pointer (or any C type) across ruby code.

I did think about few different ways to deal with that:

* create an underlying C library that takes care of the conversion VALUE -> (types of interest in a C program). This is definitely not a solution, as it demands quite a bit of work and a lot of restructuring, and it doesn't make sense to burden the system with a C library to deal only with ruby objects.

* define some of the functions to access the data as inline in a header file. This is not too bad, as most of the ruby extensions dealing with C data already use internal functions to convert from VALUE to their own types of interest. However, this is not perfect since changing the internal format of the extension would break binary compatibility with extensions depending on it;

* then, I did come with the idea of a ruby class/module being able to export some C functions to provide an access to data. For instance, an extension like Narray could provide a function like

const double * get_data_for_read(VALUE *);

and the extensions would import the adress of that function and use it. I've used that for an extension: I store the adress of the C functions in a instance hash of the module (I represent pointers by BigNum with an appropriate cast) and I simply use the adresses to call the functions later. This approach did work for me on several architectures (32 and 64 bits x86 linux, macosX - both processor types, cygwin). (see CVS of tioga on rubyforge http://rubyforge.org/scm/?group_id=701 ).

I wanted to ask: is this a good thing to do ? Is there a better way to do in ruby ? I wanted to raise this question, as for now not that many scientific packages are available and wide spread for Ruby (I mean no offense to anyone) especially when one compares with Python. There's no reason why Ruby should be behind Python on any domain !

Thanks for reading this pretty long email !

Vincent Fourmond



----------------------------------------------------------------
Avec Neuf, on peut envoyer jusqu'à 9Go par mail.
Découvrez Neuf Giga
http://www.neufgiga.com
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )