[jruby-user] [annonce] ffi-glpk extension demonstrator for jruby

15 views
Skip to first unread message

Diamantini Maurice

unread,
Jan 27, 2009, 3:16:38 PM1/27/09
to Help...@gnu.org, us...@jruby.codehaus.org, Diamantini Maurice
Bonjour à tous,

I'm trying to test a new extension provide with jruby which allows
to write a binding for a C library directly in ruby without
writing a C line of code.
This (ffi) extension is now part of the standard Jruby version.
That allows to use mixing of java library (like choco constraint
solver), C extensions (like glpk) directly in (j)ruby code.

As Pierre asked (on the Glpk list) for a java or other binding
for Glpk, I write this "pseudo-annonce" although ffi-glpk is just
a demonstrator for now.
But it allows to write the glpk documentation minimal example
in **full (j)ruby** code!

The demonstrator is available at :
http://www.ensta.fr/~diam/ruby/online/pub/

The code provided is the mimimum for writing the glpk doc mini
example in ruby.

Hope this help,
-- Maurice Diamantini

Here is the README :
==============================================================
## what is it

This directory contains a test exemple to validate Glpk interface to
(J)Ruby
thanks to the new ffi Ruby package.

- Glpk is the Gnu Library tookit for Linear Programming written
in C
by Andrew Makhorin,
http://www.gnu.org/software/glpk/

- Ruby is a wonderfull true objet oriented programming scripting
language
written in C

- JRuby is a Ruby (100% compatible) version written in Java and
fully
supported by Sun. It is also fully integrated with the java
world and is
as fast as the new native Ruby-1.9 version
http://jruby.org/

- FFI is a new package which allow to write 100% Ruby interface
to C
library!! FFI is currently fully integrated with JRuby language.
and can be installed for Ruby native language.
http://kenai.com/projects/ruby-ffi/pages/Home

## The provided exemple allow to solve the following problem :

max z = 10x1 + 6x2 + 4x3
s.c.
x1 + x2 + x3 <= 100
10x1 + 4x2 + 5x3 <= 600
2x1 + 2x2 + 6x3 <= 300

x1 >= 0
x2 >= 0
x3 >= 0


This directory contains :
- a file "miniglpk.c" which is exactly the glpk exemple provide with
the glpk
documentation.
You should be able to test this programme to make sure glpk is
installed on
your system, but a compilator is not required for using glpk from
(J)ruby

- a file "ffi-glpk.rb" wich contains the minimum for writing the low
level
API used in the tutorial example,

- the file "mini_glpk.rb" which is the the ruby version of the tutorial.


## Execution of the test the C version

For compiling and running the C exemple, you should set two environment
variables :

- GLPK_LIB should be set the path were the library file libglpk.so or
libglpk.dylib is located,
For sample
export GLPK_LIB=/usr/local/lib

- GLPK_INCLUDE should be set the path were the library file glpk.h
is located
For sample
export GLPK_LIB=/usr/local/lib

Then compile by:
gcc -I$GLPK_INCLUDE -L$GLPK_LIB -l glpk -o mini_glpk mini_glpk.c
and run by:
./mini_glpk
The following result should appear:

* 0: obj = 0.000000000e+00 infeas = 0.000e+00 (0)
* 2: obj = 7.333333333e+02 infeas = 0.000e+00 (0)
OPTIMAL SOLUTION FOUND
z = 733.333; x1 = 33.3333; x2 = 66.6667; x3 = 0


## Execution the jruby test

You should have the last jruby version (jruby-1.6) which contains
FFI out of the box, or ruby and install ffi with
gem install ffi (not tested)

Also, the glpk lib should be discover aumoticaly on osx leopard or
recent
linux distrib, but for now, you can specify the path path (i.e. bu
using the previous GLPK_LIB variable.
See the start "ffi-glpk.rb" if you have problems

then run
./mini_glpk.rb

It's all !

## Remark about the "ffi-glpk.rb" interface file

This kind of file could become the low level interface to the glpk
library.

It seems that the FFI community converge to the idea that there should
be one standard **low** level interface to a C library. It is essentialy
a sort of mapping of the include file and should be easy to maintain
(ideally automically generated from the include file).

Then it could exist several higher level API to make this interface more
Object Oriented ou more Ruby frendly.

## ./

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Charles Oliver Nutter

unread,
Jan 28, 2009, 3:16:32 PM1/28/09
to us...@jruby.codehaus.org, Help...@gnu.org, Diamantini Maurice
Diamantini Maurice wrote:
> Bonjour à tous,
>
> I'm trying to test a new extension provide with jruby which allows
> to write a binding for a C library directly in ruby without
> writing a C line of code.
> This (ffi) extension is now part of the standard Jruby version.
> That allows to use mixing of java library (like choco constraint
> solver), C extensions (like glpk) directly in (j)ruby code.
>
> As Pierre asked (on the Glpk list) for a java or other binding
> for Glpk, I write this "pseudo-annonce" although ffi-glpk is just
> a demonstrator for now.
> But it allows to write the glpk documentation minimal example
> in **full (j)ruby** code!

Wow, I missed this email before, but it sounds great! A very practical
use of FFI to be sure. Will you release this as a gem?

- Charlie

Maurice Diamantini

unread,
Jan 28, 2009, 4:26:12 PM1/28/09
to us...@jruby.codehaus.org, help...@gnu.org
2009/1/28 Charles Oliver Nutter <charles...@sun.com>:

> Diamantini Maurice wrote:
>>
>> Bonjour à tous,
>>
>> I'm trying to test a new extension provide with jruby which allows
>> to write a binding for a C library directly in ruby without
>> writing a C line of code.
>> This (ffi) extension is now part of the standard Jruby version.
>> That allows to use mixing of java library (like choco constraint
>> solver), C extensions (like glpk) directly in (j)ruby code.
>>
>> As Pierre asked (on the Glpk list) for a java or other binding
>> for Glpk, I write this "pseudo-announce" although ffi-glpk is just

>> a demonstrator for now.
>> But it allows to write the glpk documentation minimal example
>> in **full (j)ruby** code!
>
> Wow, I missed this email before, but it sounds great! A very practical use
> of FFI to be sure. Will you release this as a gem?

Sorry, no (I've never build gem yet).

Also, It's just a demonstrator which implement just the few API that
allowed me to write the little glpk sample.
So I should first build a more realistic example (which use more
glpk feature), then build build a more OO shell over the ffi-glpk to
make it more higher level.

But it already shows how easy it is to call a scientific C library
directly from jruby.

However I don't use glpk very much and I hope a regular user
would be more able to do that. Also, most part of ffi-glpk is very
similar to the glpk.h file (which is big with many feature !).

So It should be a mean to automatically generate the big part of ffi-glpk,
perhaps with some reformatting of the glpk.h.

-- Maurice

Diamantini Maurice

unread,
Nov 15, 2009, 3:48:52 PM11/15/09
to us...@jruby.codehaus.org, Maurice Diamantini
Bonjour à tous,

Some month ago, i tested an ffi-glpk validator.

The full test files are available from the original announce:
http://www.ruby-forum.com/topic/176913
Or directly at:
http://www.ensta.fr/~diam/ruby/online/pub


It was working (and keeps working) with jruby-1.2.0RC2 but it
display wrong result with the last JRuby-1.4.0 version.

The normal result (as it displays from JRuby-1.2.0RC2) is:

result is: z = 733.333; x1 = 33.3333; x2 = 66.6667; x3 = 0
expected: z = 733.333; x1 = 33.3333; x2 = 66.6667; x3 = 0

But now (with JRuby-1.4.0), I silently get:

result is: z = 300.731; x1 = 0.0732422; x2 = 49.9997; x3 = 0
expected: z = 733.333; x1 = 33.3333; x2 = 66.6667; x3 = 0

I suspect some pointer changes in ffi behavior?

Thank you for any help!
-- Maurice

Vladimir Sizikov

unread,
Dec 3, 2009, 5:32:21 PM12/3/09
to us...@jruby.codehaus.org, Maurice Diamantini
Hi Maurice,

I've filed a bug for that: http://jira.codehaus.org/browse/JRUBY-4303

And fixed it in the master branch. I've verified that your sample
works fine now on Linux.

Thanks for the report and the simple testcase. In the future though,
if you see any problems/bugs in JRuby, don't hesitate to file the JIRA
issues directly. That way, they won't get lost in the email.

Thanks,
--Vladimir

Diamantini Maurice

unread,
Dec 4, 2009, 3:39:21 PM12/4/09
to us...@jruby.codehaus.org, Vladimir Sizikov, Diamantini Maurice

Le 3 déc. 2009 à 23:32, Vladimir Sizikov a écrit :

> Hi Maurice,
>
> I've filed a bug for that: http://jira.codehaus.org/browse/JRUBY-4303
>
> And fixed it in the master branch. I've verified that your sample
> works fine now on Linux.
>
> Thanks for the report and the simple testcase. In the future though,
> if you see any problems/bugs in JRuby, don't hesitate to file the JIRA
> issues directly. That way, they won't get lost in the email.
>
> Thanks,
> --Vladimir

Thank you very much.
But as I was not sure if my jruby code was correct or not, so I just
expected a confirmation (or not) of this incorrect behavior.
In fact I thought I made use of some bad old ffi API, but
i didn't found doc or samples to help me.

I'll try jruby-1.5 when as soon at it is available.

Thank you for you help !

Reply all
Reply to author
Forward
0 new messages