Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4051

Re: Fast way to move C array to ruby (rb_ary_new4)?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!feeds.phibee-telecom.net!talisker.lacave.net!lacave.net!not-for-mail
From Ville Sipola <ville_sipola@hotmail.com>
Newsgroups comp.lang.ruby
Subject Re: Fast way to move C array to ruby (rb_ary_new4)?
Date Fri, 6 May 2011 14:47:05 -0500
Organization Service de news de lacave.net
Lines 53
Message-ID <28d39b6f96e5111235f7b9aa93869a9e@ruby-forum.com> (permalink)
References <7bb00dec91cd520ed45220d7d899f294@ruby-forum.com> <BANLkTimkAt-43cdMX+ZEETjJ-K7X=kSPnw@mail.gmail.com> <BANLkTinmauNPb+vZ8Tv5Fw_OSx=Xc_oT3Q@mail.gmail.com> <BANLkTi=F7J=Fg52aFeSOqtqL1XsS4VuVtg@mail.gmail.com> <104a6c52e34d44d0b57874ca7d51a4c3@ruby-forum.com>
NNTP-Posting-Host bristol.highgroove.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Trace talisker.lacave.net 1304711251 46987 65.111.164.187 (6 May 2011 19:47:31 GMT)
X-Complaints-To abuse@lacave.net
NNTP-Posting-Date Fri, 6 May 2011 19:47:31 +0000 (UTC)
In-Reply-To <104a6c52e34d44d0b57874ca7d51a4c3@ruby-forum.com>
X-Received-From This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway
X-Mail-Count 382759
X-Ml-Name ruby-talk
X-Rubymirror Yes
X-Ruby-Talk <28d39b6f96e5111235f7b9aa93869a9e@ruby-forum.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.ruby:4051

Show key headers only | View raw


Ville Sipola wrote:
> Now I only have to figure out how to use that.

Now that I have, I thought it a good idea to describe how it was done,
so that others reading the post might be able to use the information.

So this was my method.

The writing part is a bit of a hack. Masahiro Tanaka himself advices
against this approach in here: https://www.ruby-forum.com/topic/83484,
but I'm yiet to find a better solution. If there is a function for
writing c data in an narray in a safe way, I'd like to hear about it.

First, in Ruby I did something like

@x=NArray.float(@number_of_samples).fill(some_value)

to make sure that the narray is of correct size.

Then in C:
static VALUE writingfunction(VALUE class,VALUE x)
{
    struct NARRAY *xarray;
    GetNArray(x, xarray);
    double *floatpointerx = xarray->ptr;

    double c_value;

    for (counter = 0; counter < number_of_samples; counter++)
    {
       (do the actual calculation)
       floatpointerx[samplecounter] = c_value; (This is the illegal
hack)
    }
}

To read in C (this I believe is completely ok, please correct if I'm
wrong.)

static VALUE readingfunction(VALUE class, VALUE x)
{
    struct NARRAY *xarray;
    GetNArray(x, xarray);
    double *floatpointerx = xarray->ptr;
    for (counter = 0; counter < xarray->total; counter++)
    {
        a_function_that_takes_c_double(floatpointerx[counter1]);
    }
}

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Fast way to move C array to ruby (rb_ary_new4)? Ville Sipola <ville_sipola@hotmail.com> - 2011-04-27 05:46 -0500
  Re: Fast way to move C array to ruby (rb_ary_new4)? Robert Klemme <shortcutter@googlemail.com> - 2011-04-27 07:08 -0500
    Re: Fast way to move C array to ruby (rb_ary_new4)? Robert Klemme <shortcutter@googlemail.com> - 2011-04-27 08:06 -0500
      Re: Fast way to move C array to ruby (rb_ary_new4)? Cameron McBride <cameron.mcbride@gmail.com> - 2011-04-27 15:02 -0500
        Re: Fast way to move C array to ruby (rb_ary_new4)? Ville Sipola <ville_sipola@hotmail.com> - 2011-04-27 17:01 -0500
          Re: Fast way to move C array to ruby (rb_ary_new4)? Ville Sipola <ville_sipola@hotmail.com> - 2011-05-06 14:47 -0500
  Re: Fast way to move C array to ruby (rb_ary_new4)? Brian Candler <b.candler@pobox.com> - 2011-04-27 11:15 -0500

csiph-web