Numerical Ruby

Hi all,

I am now using Ruby to test my numerical schemes. Using Ruby for this
task was a real pleasure (I usually code in F90). The computations run
just fine but I would like to have a result a little faster.

My code uses lots of loops as in the following example.

def faraday
for y in 1…(maxY-1)
for z in 1…(maxZ-1)
@Bx[y,z]+=-(dt/dy)(@Ez[y+1,z]-@Ez[y,z])+(dt/dz)(@Ey[y,z+1]-@Ey[y,z])
@By[y,z]+= - (dt/dz)(@Ex[y,z+1]-@Ex[y,z])
@Bz[y,z]+=(dt/dy)
(@Ex[y+1,z]-@Ex[y,z])
end
end
end

I am using NArray from Masahiro Tanaka
(http://www.ir.isas.ac.jp/~masa/ruby/index-e.html) to represent the
electromagnetic field.

Is there any way to speed up this type of computations? (I am not sure
how a C extension could help in this case.)

BTW, is there any trend to develop the use of Ruby in the field of
scientific computations (as it seems to be the case with Python)? Will
NArray be included in the standard library?

Thanks!
- Olivier

“Olivier Saut” Olivier.Saut@gmm.insa-tlse.fr wrote in message
news:20021201001712.48f6cb67.Olivier.Saut@gmm.insa-tlse.fr

Hi all,

I am now using Ruby to test my numerical schemes. Using Ruby for this
task was a real pleasure (I usually code in F90). The computations run
just fine but I would like to have a result a little faster.

My code uses lots of loops as in the following example.

Is there any way you could use Ruby to implement a symbolic evaluation
instead of an explicit evaluation? Ruby is stronger in this aspect. Given a
symbolic definition you could generate code on the fly - for example using
inline C - but I guess that would add complexity rather than remove
complexity from you implementation which may be beside the point of using
Ruby.

In the end it depends on whether you use Ruby because it is easier as an
implementation language, or becuase you want the end users to be able to
access the library from Ruby. The latter approach is good, the former is
bound to be slow.

There is also the NML language by dmcclaim which generates compiled ocaml
code on the fly resulting in (claimed) rather fast evaluation of numerical
applications even compared to C libraries because of the dynamic compilation
of expressions. It might be a backend for a Ruby solution.

http://home.mindspring.com/~dmcclain1/nmlpromo.htm
the source is not online although listed:
http://home.mindspring.com/~dmcclain1/nml.zip
so you’d have to email him.

Mikkel

In article 20021201001712.48f6cb67.Olivier.Saut@gmm.insa-tlse.fr,

···

Olivier Saut Olivier.Saut@gmm.insa-tlse.fr wrote:

Hi all,

I am now using Ruby to test my numerical schemes. Using Ruby for this
task was a real pleasure (I usually code in F90). The computations run
just fine but I would like to have a result a little faster.

My code uses lots of loops as in the following example.

def faraday
for y in 1…(maxY-1)
for z in 1…(maxZ-1)
@Bx[y,z]+=-(dt/dy)(@Ez[y+1,z]-@Ez[y,z])+(dt/dz)(@Ey[y,z+1]-@Ey[y,z])
@By[y,z]+= - (dt/dz)(@Ex[y,z+1]-@Ex[y,z])
@Bz[y,z]+=(dt/dy)
(@Ex[y+1,z]-@Ex[y,z])
end
end
end

I am using NArray from Masahiro Tanaka
(http://www.ir.isas.ac.jp/~masa/ruby/index-e.html) to represent the
electromagnetic field.

Is there any way to speed up this type of computations? (I am not sure
how a C extension could help in this case.)

BTW, is there any trend to develop the use of Ruby in the field of
scientific computations (as it seems to be the case with Python)?

There should be. Actually, there are a few folks out there that seems to
be using Ruby in scientific computation.

Phil

“Or perhaps the truth is less interesting than the facts?”
Amy Weiss (accusing theregister.co.uk of engaging in ‘tabloid journalism’)
Senior VP, Communications
Recording Industry Association of America

Hi,

From: Olivier Saut Olivier.Saut@gmm.insa-tlse.fr

My code uses lots of loops as in the following example.

def faraday
for y in 1…(maxY-1)
for z in 1…(maxZ-1)
@Bx[y,z]+=-(dt/dy)(@Ez[y+1,z]-@Ez[y,z])+(dt/dz)(@Ey[y,z+1]-@Ey[y,z])
@By[y,z]+= - (dt/dz)(@Ex[y,z+1]-@Ex[y,z])
@Bz[y,z]+=(dt/dy)
(@Ex[y+1,z]-@Ex[y,z])
end
end
end

I am using NArray from Masahiro Tanaka
(http://www.ir.isas.ac.jp/~masa/ruby/index-e.html) to represent the
electromagnetic field.

Is there any way to speed up this type of computations? (I am not sure
how a C extension could help in this case.)

If you use NArray, “writing without loop” is the way to speed up.
Your ‘faraday’ method can be rewritten as:

def faraday2
dt,dx,dy,dz = [1.0]4
@Bx[1…-2,1…-2] += -(dt/dy)
(@Ez[2…-1,1…-2] - @Ez[1…-2,1…-2]) +
(dt/dz)(@Ey[1…-2,2…-1] - @Ey[1…-2,1…-2])
@By[1…-2,1…-2] += -(dt/dz)
(@Ex[1…-2,2…-1] - @Ex[1…-2,1…-2])
@Bz[1…-2,1…-2] += (dt/dy)*(@Ex[2…-1,1…-2] - @Ex[1…-2,1…-2])
end

For 500x500 array, the result of benchmark on my machine is:

with loop: 8.69 sec
without loop: 0.31 sec

Regards,
Masahiro Tanaka

“MikkelFJ” mikkelfj-anti-spam@bigfoot.com wrote in message
news:3de94bd0$0$47406$edfadb0f@dtext01.news.tele.dk…

Is there any way you could use Ruby to implement a symbolic evaluation
instead of an explicit evaluation?

In my long tradition of replying to myself,
one could imagine something like the following:

class SymScalar
attr_reader :var
def initialize name
@var = name
end
def + a
puts “#{@var} + #{a.var};”
end
end

sx = SymScalar.new “x”
sy = SymScalar.new “y”

sx + sy ==> x + y;

Mikkel

In the end it depends on whether you use Ruby because it is easier as an
implementation language, or becuase you want the end users to be able to
access the library from Ruby. The latter approach is good, the former is
bound to be slow.

I use Ruby to shorten my development time. Beside the numerical
computations, Ruby dramatically reduces the pain to write
the input/output/interface methods of the code.

There is also the NML language by dmcclaim which generates compiled ocaml
code on the fly resulting in (claimed) rather fast evaluation of numerical
applications even compared to C libraries because of the dynamic compilation
of expressions. It might be a backend for a Ruby solution.

Great, I will have a look a it.

Thanks!
- Olivier

···

On Sun, 01 Dec 2002, MikkelFJ mikkelfj-anti-spam@bigfoot.com wrote:

Great that was exactly what I was hoping for.

Thanks a lot!
- Olivier

···

On Sun, 01 Dec 2002, Masahiro TANAKA masa@ir.isas.ac.jp wrote:

Hi,

If you use NArray, “writing without loop” is the way to speed up.
Your ‘faraday’ method can be rewritten as:

def faraday2
dt,dx,dy,dz = [1.0]4
@Bx[1…-2,1…-2] += -(dt/dy)
(@Ez[2…-1,1…-2] - @Ez[1…-2,1…-2]) +
(dt/dz)(@Ey[1…-2,2…-1] - @Ey[1…-2,1…-2])
@By[1…-2,1…-2] += -(dt/dz)
(@Ex[1…-2,2…-1] - @Ex[1…-2,1…-2])
@Bz[1…-2,1…-2] += (dt/dy)*(@Ex[2…-1,1…-2] - @Ex[1…-2,1…-2])
end

For 500x500 array, the result of benchmark on my machine is:

with loop: 8.69 sec
without loop: 0.31 sec

“Masahiro TANAKA” masa@ir.isas.ac.jp wrote in message
news:20021201.192929.41625961.masa@ir.isas.ac.jp…

@Bz[1..-2,1..-2] +=  (dt/dy)*(@Ex[2..-1,1..-2] - @Ex[1..-2,1..-2])

end

For 500x500 array, the result of benchmark on my machine is:

with loop: 8.69 sec
without loop: 0.31 sec

This is actually pretty cool - now how much code would you need in C to get
the same performance.

There is only one tiny small step from here to use the message passing
interface MPI and have parallel computations on computer grids - I think
there are few around which you can hook up to, although I’ve never tried it.

Mikkel

“Masahiro TANAKA” masa@ir.isas.ac.jp wrote in message
news:20021201.192929.41625961.masa@ir.isas.ac.jp…

@Bz[1..-2,1..-2] +=  (dt/dy)*(@Ex[2..-1,1..-2] - @Ex[1..-2,1..-2])

end

For 500x500 array, the result of benchmark on my machine is:

with loop: 8.69 sec
without loop: 0.31 sec

This is actually pretty cool - now how much code would you need in C to get
the same performance.

There is only one tiny small step from here to use the message passing
interface MPI and have parallel computations on computer grids - I think
there are few around which you can hook up to, although I’ve never tried it.

Mikkel

“Phil Tomson” ptkwt@shell1.aracnet.com wrote in message
news:asbq900tvg@enews1.newsguy.com

In article 20021201001712.48f6cb67.Olivier.Saut@gmm.insa-tlse.fr,

Hi all,

I am now using Ruby to test my numerical schemes. Using Ruby for this
task was a real pleasure (I usually code in F90). The computations run
just fine but I would like to have a result a little faster.

I am using NArray from Masahiro Tanaka
(http://www.ir.isas.ac.jp/~masa/ruby/index-e.html) to represent the
electromagnetic field.

I’m sure NArray is a fine application, I’d just point out Gnu Scientific
Library (GSL) because I recently stumpled over it:

http://sources.redhat.com/gsl/

  • on the page is mentioned a partial ruby binding, but the link is dead.

Mikkel

···

Olivier Saut Olivier.Saut@gmm.insa-tlse.fr wrote:

Yes. After a rewrite following Masahiro’s suggestion, my code is 50
times faster!

		- Olivier
···

On Sun, 01 Dec 2002, MikkelFJ mikkelfj-anti-spam@bigfoot.com wrote:

“Masahiro TANAKA” masa@ir.isas.ac.jp wrote in message
news:20021201.192929.41625961.masa@ir.isas.ac.jp…

@Bz[1..-2,1..-2] +=  (dt/dy)*(@Ex[2..-1,1..-2] - @Ex[1..-2,1..-2])

end

For 500x500 array, the result of benchmark on my machine is:

with loop: 8.69 sec
without loop: 0.31 sec

This is actually pretty cool - now how much code would you need in C to get
the same performance.

Can you estimate how its speed compares to something written in, say,
Fortran?

Could Ruby be made suitable for scientific apps if it were used as a
glue to bring together extensions written in C? And could it possibly be
made even faster if Ruby objects were smart enough to do some
optimization based on their properties?

Bringing together Ruby’s convenience and expressive power with
acceptable speed would be absolutely awesome.

···

On Mon, Dec 02, 2002 at 06:50:41PM +0900, Olivier Saut wrote:

On Sun, 01 Dec 2002, MikkelFJ mikkelfj-anti-spam@bigfoot.com wrote:

“Masahiro TANAKA” masa@ir.isas.ac.jp wrote in message
news:20021201.192929.41625961.masa@ir.isas.ac.jp…

@Bz[1..-2,1..-2] +=  (dt/dy)*(@Ex[2..-1,1..-2] - @Ex[1..-2,1..-2])

end

For 500x500 array, the result of benchmark on my machine is:

with loop: 8.69 sec
without loop: 0.31 sec

This is actually pretty cool - now how much code would you need in C to get
the same performance.

Yes. After a rewrite following Masahiro’s suggestion, my code is 50
times faster!


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

martin@bdsi.com (no longer valid - where are you now, Martin?)
– from /usr/src/linux/drivers/cdrom/mcd.c

Yes. After a rewrite following Masahiro’s suggestion, my code is 50
times faster!

Can you estimate how its speed compares to something written in, say,
Fortran?

I will write a similar code in f90 in the next weeks, then I’ll be able
to compare the two. I’d say f90 should not be noticeably faster that the
ruby code using the full power of NArray. NArray is written in C. If
there is some interest, I’ll post the results to the list.
The only difference I see is that using f90 I can get parallelism to
speed up the computations (through OpenMP or MPI).

Could Ruby be made suitable for scientific apps if it were used as a
glue to bring together extensions written in C? And could it possibly be
made even faster if Ruby objects were smart enough to do some
optimization based on their properties?

IMHO, with extensions like NArray, Ruby is suitable for scientific apps.
The heavy numerical computations are performed at the speed of a C
program, and you keep Ruby to write your input/output methods and to
make a GUI. However I have tested only with codes able to run on a
personnal workstation. I don’t know how it would scale for larger codes
(fluid dynamics…). Perhaps some japanese Rubyists are already using
Ruby for large projects as many Ruby extensions for computational
physics are written by japanese people. Python starts to be mentioned
in some of the conferences (in applied mathematics) I attend, there is
no reason Ruby could not be used as well.

Bringing together Ruby’s convenience and expressive power with
acceptable speed would be absolutely awesome.

I could not agree more.

Best regards,

		- Olivier
···

On Mon, 02 Dec 2002, Mauricio Fernández batsman.geo@yahoo.com wrote:

Yes, please post your results.

···

On Monday, 2 December 2002 at 21:23:05 +0900, Olivier Saut wrote:

On Mon, 02 Dec 2002, Mauricio Fernández batsman.geo@yahoo.com wrote:

Yes. After a rewrite following Masahiro’s suggestion, my code is 50
times faster!

Can you estimate how its speed compares to something written in, say,
Fortran?

I will write a similar code in f90 in the next weeks, then I’ll be able
to compare the two. I’d say f90 should not be noticeably faster that the
ruby code using the full power of NArray. NArray is written in C. If
there is some interest, I’ll post the results to the list.


Jim Freeze