Skip to content

C n-body Jeremy Zerfas

CONTRIBUTE SOURCE CODE

C n-body Jeremy Zerfas

This is based on the C #4 program but also includes techniques that I saw being used in several other programs. Altogether this program should be about 35-45% faster than the fastest C program, it should have the lowest (or close to it) memory usage of all the programs, and the program size should also be close to the lowest of the top 20 fastest programs. The performance should still be around 10-15% slower than the Rust #7 program, LLVM is doing a better job of optimizing than GCC is in this case (FYI, I was able to beat the Rust #7 program's performance when using Clang/LLVM). Additionally this program is also much better commented and uses far less intrinsic functions. The following is a list of the more notable changes I made to address different aspects:

Performance

-Use -funroll-loops compiler option to tweak loop unrolling like the Rust #2 programs does.

-In the advance() function a 2D array is used to store the deltas/differences in positions of the bodies like the Fortran #6 program does and all the X, Y, and Z values are stored sequentially in the arrays like the C #7 program does. Having the X, Y, and Z values stored sequentially allows loads/stores from these arrays to be done more efficiently.

-The loop that updates the bodies' velocities precomputes two values like is done by the Rust #1 program. This allows for less recalculations to be done in the loop.

Memory

-Use -static compiler option to use static linking like the Fortran programs do.

-Removed padding from the body structure.

-The arrays in the advance() function are sized to only be as large as they need to be.

Program Size

-Use of C99 and C11 features and syntax.

-Used a provided constant for PI instead of defining it like the Ruby #2 program does.

-The timestep value is just directly placed where needed instead of defining it and passing it to functions like is done in the PHP #3 program.

-Explicitly defined the number of bodies in the system rather than calculating it like the Rust #7 program does.

-The function that calculates the energy of the system also outputs the energy like the Python #2 program does.

Attach your source code file

N-Body.c

Provide an example build command-line

gcc -O3 -funroll-loops -march=native -static N-Body.c -o N-Body -lm