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
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?
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.
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
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
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
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.
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.
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.
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.
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:
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.
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.