Code Monkey home page Code Monkey logo

benchmark-leapfrog's Introduction

Simple N-Body with LeapFrog integrator

Implementation in C, Fortran, Go, Julia and Rust of a very simple N-Body simulator with 3 particles using a LeapFrog integrator. Presented in What can the programming language Rust do for astrophysics?, to appear in the Proceedings of the IAU Symposium 325 on Astroinformatics.

The code has evolved since publication, implementing several suggestion made by the Hacker News and Rust subreddit communities. The current times on a 1,6 GHz Intel Core i5 machine:

  • C: 2m53.504s
  • Fortran: 3m16.314s
  • Rust: 2m33.082s
  • Go: 4m10.233s
  • Julia: 3m59.223s

Output positions for the two particles after a one million year simulation:

[
    [0.00011259641961937496, 0.00011235312238407964, 0.00000982962683070781],
    [-2.986377308237493, 14588403.613911422, 1276320.0322206458]
]

The original article's conclusions are still valid for this simple user case, Rust can be as fast and precise as C or Fortran. Additionally, Rust characteristics ensures that scientific results are not affected by memory management issues and this is a great advantage for reliable scientific computation.

Compilation & execution

From each directory:

make clean
make
time target/optimized/leapfrog 

benchmark-leapfrog's People

Contributors

asmunder avatar btracey avatar freeboson avatar jdstorm avatar kristjansson avatar marblestation avatar mikeinnes avatar sbinet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

benchmark-leapfrog's Issues

Benchmarks should e.g. write output

As written, the compiled benchmarks don't measure the time taken for a n-body simulation. For example, the Fortran compiles (GNU Fortran (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11)) to

0000000000400610 <main>:
  400610:       48 83 ec 08             sub    $0x8,%rsp
  400614:       e8 d7 ff ff ff          callq  4005f0 <_gfortran_set_args@plt>
  400619:       be 70 0b 40 00          mov    $0x400b70,%esi
  40061e:       bf 07 00 00 00          mov    $0x7,%edi
  400623:       e8 98 ff ff ff          callq  4005c0 <_gfortran_set_options@plt>
  400628:       66 0f 57 c0             xorpd  %xmm0,%xmm0
  40062c:       f2 0f 10 0d 64 05 00    movsd  0x564(%rip),%xmm1        # 400b98 <options.38.2155+0x28>
  400633:       00
  400634:       f2 0f 10 15 64 05 00    movsd  0x564(%rip),%xmm2        # 400ba0 <options.38.2155+0x30>
  40063b:       00
  40063c:       0f 1f 40 00             nopl   0x0(%rax)
  400640:       f2 0f 58 c1             addsd  %xmm1,%xmm0
  400644:       f2 0f 58 c1             addsd  %xmm1,%xmm0
  400648:       66 0f 2e d0             ucomisd %xmm0,%xmm2
  40064c:       73 f2                   jae    400640 <main+0x30>
  40064e:       31 c0                   xor    %eax,%eax
  400650:       48 83 c4 08             add    $0x8,%rsp
  400654:       c3                      retq
  400655:       0f 1f 00                nopl   (%rax)

The loop from 400640 to 40064c corresponds to the time = time + half_time_step lines only i.e. the entire n-body simulation is optimised away. For these benchmarks to be at all useful, you must ensure that the compiler is unable to prove that virtually all the work is unnecessary. The easiest way to do this is to e.g. write out the results at the end, and measure only the computation time if that's what's of interest to you.

Bug in C implementation.

double dx = x[i][0] - x[j][0];
double dy = x[i][0] - x[j][0];
double dz = x[i][0] - x[j][0];

This isn't correct, right? Shouldn't it be:

            double dx = x[i][0] - x[j][0];
            double dy = x[i][1] - x[j][1];
            double dz = x[i][2] - x[j][2];

Rust isn't actually doing any work?

I'm not an expert on rust, but I added to the end: println!("{:?}",x); (to check the answer), to the end of main and now the runtime on my machine has gone from 11 seconds to more than 2 minutes. Still running.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.