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)?

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 2011-05-06 14:47 -0500
Organization Service de news de lacave.net
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>

Show all headers | 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