Forum: Ruby-core Ruby/DL Documentation

C306d9e1df9a8b3c65830547618cc7fb?d=identicon&s=25 Aston (Guest)
on 2009-05-04 07:44
(Received via mailing list)
Hello,


recently i have been trying to learn and use dl library. while searching
i found this excellent link
http://www.jbrowse.com/text/rdl_en.html

unfortunately it is specific to 1.8.x

i wonder how people here use DL, since it is very scarcely documented. i
have been trying to
hack around ./ext/dl/*.c files but i have gained very little usable
information.

ok, my question is about passing an array from ruby to some native
libraries (IPP(http://software.intel.com/en-us/intel-ipp/) to be
specific)
on windows. yes i about other alternatives, i could embed ruby in my app
and do whatever, there is gr8 support there
but this is about quickly loading and using native libraries via dynamic
loader.

consider code below
some_fast_math_function( float* array, int len);

then how to pass 'array' argument from ruby ?
i tried this

require 'dl'

module NativeLibs
# do the job for accessing some_fast_math_function
end

array = DL.malloc(size)
NativeLibs.some_fast_math_function( array, size) # crashes :(

some how sucess is elusive for me, this crashes(core dumps) for me, i am
sure this is due to my inexperienced stunts with DL

Aston


      Own a website.Get an unlimited package.Pay next to nothing.*Go to
http://in.business.yahoo.com/
6265965a895f6581e115a041e42e9dee?d=identicon&s=25 Tadeusz Bochan (tbochan)
on 2009-05-04 12:12
(Received via mailing list)
Hi. Been there ... done some of it.
I use DL quite often on HPUX to invoke Cobol/C  modules from libraries.
The best rule I can offer is that before passing any structured
parameter,
you should 'pack' it to the
form that the target program expects.
Ruby will just pass a pointer to the packed string, and the target
program
can interpret it however the hell it wants.
eg.,
a=[3,"a1","a2","a3"]
param=a.pack("LA5A5A5") # (integer + 3x5byte array of strings) => 19
byte
string
result=routine[param].first

hth,
Tad


I located an old test script which may be of some help :
The script calls a Cobol module called FCREFNUM from a shared library
The routine expects 3 parameters passed by reference, the 3rd parameter
is
a simple array.

require "dl"
ctrargs=%w{0 1 999999}
ctrname=ARGV[0].upcase     if ARGV.size >=1
ctrargs=ARGV[1].split(":") if ARGV.size >=2
Image3k =DL.dlopen("/home/acore_dev/library/lib/libimage3k.sl")
fcrefnum=Image3k["FCREFNUM","ISSS"]
counterstart=ctrargs[0].rjust(10,"0") # 10 digit zero
counterincr =ctrargs[1].rjust(10,"0") # 10 digit zero
counterend  =ctrargs[2].rjust(10,"0") # 10 digit zero
counterparms=[counterstart,counterincr,counterend].pack("a10a10a10")
countervalue="0".rjust(10,"0")
countername=ctrname.ljust(8)

n=fcrefnum[countername,countervalue,counterparms].first # call FCREFNUM

p [countervalue,n]




           Internet
           blackapache512
           -ticket@yahoo.
Pour
           com                    ruby-core@ruby-lang.org
                                                                        cc
           04/05/2009
           07:44
Objet
                                  [ruby-core:23354] Ruby/DL
Documentation

              Veuillez
             répondre à
           ruby-core@ruby
             -lang.org






Hello,

recently i have been trying to learn and use dl library. while searching
i
found this excellent link
http://www.jbrowse.com/text/rdl_en.html

unfortunately it is specific to 1.8.x

i wonder how people here use DL, since it is very scarcely documented. i
have been trying to
hack around ./ext/dl/*.c files but i have gained very little usable
information.

ok, my question is about passing an array from ruby to some native
libraries (IPP(http://software.intel.com/en-us/intel-ipp/) to be
specific)
on windows. yes i about other alternatives, i could embed ruby in my app
and do whatever, there is gr8 support there
but this is about quickly loading and using native libraries via dynamic
loader.

consider code below
some_fast_math_function( float* array, int len);

then how to pass 'array' argument from ruby ?
i tried this

require 'dl'

module NativeLibs
# do the job for accessing some_fast_math_function
end

array = DL.malloc(size)
NativeLibs.some_fast_math_function( array, size) # crashes :(

some how sucess is elusive for me, this crashes(core dumps) for me, i am
sure this is due to my inexperienced stunts with DL

Aston



Now surf faster and smarter ! Check out the new Firefox 3 - Yahoo!
Edition
* Click here!





This message and any attachments (the "message") is
intended solely for the addressees and is confidential.
If you receive this message in error, please delete it and
immediately notify the sender. Any use not in accord with
its purpose, any dissemination or disclosure, either whole
or partial, is prohibited except formal approval. The internet
can not guarantee the integrity of this message.
BNP PARIBAS (and its subsidiaries) shall (will) not
therefore be liable for the message if modified.
Do not print this message unless it is necessary,
consider the environment.

                ---------------------------------------------

Ce message et toutes les pieces jointes (ci-apres le
"message") sont etablis a l'intention exclusive de ses
destinataires et sont confidentiels. Si vous recevez ce
message par erreur, merci de le detruire et d'en avertir
immediatement l'expediteur. Toute utilisation de ce
message non conforme a sa destination, toute diffusion
ou toute publication, totale ou partielle, est interdite, sauf
autorisation expresse. L'internet ne permettant pas
d'assurer l'integrite de ce message, BNP PARIBAS (et ses
filiales) decline(nt) toute responsabilite au titre de ce
message, dans l'hypothese ou il aurait ete modifie.
N'imprimez ce message que si necessaire,
pensez a l'environnement.
A8545ec1327b2f948abca43597230f1f?d=identicon&s=25 unknown (Guest)
on 2009-05-05 16:07
(Received via mailing list)
Hi,

It is helpful to read test/dl/test_*.rb in ruby-1.9.tar.gz.
You have to master pack and unpack anyway.
The following is an example.

$ cat sum.c
double sum(double *arry, int len)
{
  double ret = 0;
  int i;
  for(i = 0; i < len; i++){
    ret = ret + arry[i];
  }
  return ret;
}

$ gcc -shared sum.c -o libsum.so

$ cat d.rb
require 'dl/import'
module M
  extend DL::Importer
  dlload './libsum.so'
  extern 'double sum(double*, int)'
end
p M.sum([2.0, 3.0, 4.0].pack("d*"), 3)

$ ruby-1.9 -v d.rb
ruby 1.9.2dev (2009-05-02 trunk 23326) [i686-linux]
9.0


2009/5/4 Aston <blackapache512-ticket@yahoo.com>:
C306d9e1df9a8b3c65830547618cc7fb?d=identicon&s=25 Aston (Guest)
on 2009-05-06 20:05
(Received via mailing list)
thanks a lot tad.bochan@bnpparibas.com and sh@sheepman.sakura.ne.jp

tad seems to be using 1.8.x, i am using ruby19 on FreeBSD7.2

[diesalunix@raptor ~]$ uname -a
FreeBSD raptor 7.2-PRERELEASE FreeBSD 7.2-PRERELEASE #1: Sun Mar 29
19:18:27 UTC 2009     root@raptor:/usr/obj/usr/src/sys/ARGON18  amd64
[diesalunix@raptor ~]$ ruby19 -v
ruby 1.9.2dev (2009-05-02 trunk 23326) [x86_64-freebsd7.2]

as suggested by sh@ test/dl/test_*.rb are good (not gr8!) source for
learning, but given the fact there exists no other code using dl
elsewhere I am very happy I found them. thanks sh@

While googling around I discovered ruby-ffi
(http://kenai.com/projects/ruby-ffi/pages/Home), and it seems to be much
more clean and complete solution. It is supported by other VMs as well.
I would have prefered to be with dl since it is part of standard ruby C
implementation but it is very difficult to use(u got to have lot of time
and little black magic), code using it is almost non-existent and
/dev/null documentation

when reading about ruby-ffi elsewhere it appears very promising, what is
its future in regard to ruby's C implementation ? will it ever be part
of stdlib ? it appears it is still taking shape but by and large stable

Aston




________________________________
From: "sh@sheepman.sakura.ne.jp" <sh@sheepman.sakura.ne.jp>
To: ruby-core@ruby-lang.org
Sent: Tuesday, 5 May, 2009 2:06:09 PM
Subject: [ruby-core:23359] Re: Ruby/DL Documentation

Hi,

It is helpful to read test/dl/test_*.rb in ruby-1.9.tar.gz.
You have to master pack and unpack anyway.
The following is an example.

$ cat sum.c
double sum(double *arry, int len)
{
  double ret = 0;
  int i;
  for(i = 0; i < len; i++){
    ret = ret + arry[i];
  }
  return ret;
}

$ gcc -shared sum.c -o libsum.so

$ cat d.rb
require 'dl/import'
module M
  extend DL::Importer
  dlload './libsum.so'
  extern 'double sum(double*, int)'
end
p M.sum([2.0, 3.0, 4.0].pack("d*"), 3)

$ ruby-1.9 -v d.rb
ruby 1.9.2dev (2009-05-02 trunk 23326) [i686-linux]
9.0


2009/5/4 Aston <blackapache512-ticket@yahoo.com>:
> hack around ./ext/dl/*.c files but i have gained very little usable
> some_fast_math_function( float* array, int len);
> array = DL.malloc(size)
> Click here!
Bollywood news, movie reviews, film trailers and more! Go to
http://in.movies.yahoo.com/
This topic is locked and can not be replied to.