The Computer Language
Benchmarks Game

n-body C# .NET Core #9 program

source code

/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/

   Contributed by Robert F. Tobler
   Modified to use SIMD vectors by Anthony Lloyd and Paul Westcott
*/

using System;
using System.Numerics;
using System.Runtime.CompilerServices;

public static class NBody
{
    const double dt = 0.01;
    
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    static double[] createBodies()
    {
        const double Pi = 3.141592653589793;
        const double Solarmass = 4 * Pi * Pi;
        const double DaysPeryear = 365.24;
        const double jmass = 9.54791938424326609e-04 * Solarmass;
        const double jvx = 1.66007664274403694e-03 * DaysPeryear;
        const double jvy = 7.69901118419740425e-03 * DaysPeryear;
        const double jvz = -6.90460016972063023e-05 * DaysPeryear;
        const double smass = 2.85885980666130812e-04 * Solarmass;
        const double svx = -2.76742510726862411e-03 * DaysPeryear;
        const double svy = 4.99852801234917238e-03 * DaysPeryear;
        const double svz = 2.30417297573763929e-05 * DaysPeryear;
        const double umass = 4.36624404335156298e-05 * Solarmass;
        const double uvx = 2.96460137564761618e-03 * DaysPeryear;
        const double uvy = 2.37847173959480950e-03 * DaysPeryear;
        const double uvz = -2.96589568540237556e-05 * DaysPeryear;
        const double nmass = 5.15138902046611451e-05 * Solarmass;
        const double nvx = 2.68067772490389322e-03 * DaysPeryear;
        const double nvy = 1.62824170038242295e-03 * DaysPeryear;
        const double nvz = -9.51592254519715870e-05 * DaysPeryear;
        return new double[] {
            // sun
            (jvx * jmass + svx * smass + uvx * umass + nvx * nmass)/-Solarmass,
            (jvy * jmass + svy * smass + uvy * umass + nvy * nmass)/-Solarmass,
            (jvz * jmass + svz * smass + uvz * umass + nvz * nmass)/-Solarmass,
            0.0,
            0.0, 0.0, 0.0, 0.0,
            Solarmass,
            // jupiter
            jvx, jvy, jvz, 0.0,
            4.84143144246472090e+00, -1.16032004402742839e+00, -1.03622044471123109e-01, 0.0,
            jmass,
            // saturn
            svx, svy, svz, 0.0,
            8.34336671824457987e+00, 4.12479856412430479e+00, -4.03523417114321381e-01, 0.0,
            smass,
            // uranus
            uvx, uvy, uvz, 0.0,
            1.28943695621391310e+01, -1.51111514016986312e+01, -2.23307578892655734e-01, 0.0,
            umass,
            // neptune
            nvx, nvy, nvz, 0.0,
            1.53796971148509165e+01, -2.59193146099879641e+01, 1.79258772950371181e-01, 0.0,
            nmass,
        };
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    static double energy(double[] bodies)
    {
        double e = 0.0;
        for(int i=0; i<bodies.Length; i+=9)
        {
            var Vi = new Vector<double>(bodies,i);
            var Xi = new Vector<double>(bodies,i+4);
            var imass = bodies[i+8];
            e += 0.5 * imass * Vector.Dot(Vi,Vi);
            for(int j=i+9; j<bodies.Length; j+=9)
            {
                var DX = Xi - new Vector<double>(bodies,j+4);
                e -= imass * bodies[j+8] / Math.Sqrt(Vector.Dot(DX,DX));
            }
        }
        return e;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    static void advance(double[] bodies)
    {
        for(int i=0; i<bodies.Length; i+=9)
        {
            var Vi = new Vector<double>(bodies,i);
            var Xi = new Vector<double>(bodies,i+4);
            var MASSi = new Vector<double>(bodies[i+8]);
            for(int j=i+9; j<bodies.Length; j+=9)
            {
                var DX = new Vector<double>(bodies,j+4) - Xi;
                var d2 = Vector.Dot(DX, DX);
                var MAG = new Vector<double>(dt / (d2 * Math.Sqrt(d2)));
                var MASSj = new Vector<double>(bodies[j+8]);
                Vi += DX * MASSj * MAG;
                (new Vector<double>(bodies,j) - DX * MASSi * MAG).CopyTo(bodies,j);
            }
            (Vi * dt + Xi).CopyTo(bodies,i+4);
            Vi.CopyTo(bodies,i);
        }
    }

    public static void Main(String[] args)
    {
        var bodies = createBodies();
        Console.Out.WriteLineAsync(energy(bodies).ToString("F9"));
        int n = args.Length > 0 ? Int32.Parse(args[0]) : 10000;
        while(n-->0) advance(bodies);
        Console.Out.WriteLineAsync(energy(bodies).ToString("F9"));
    }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
2.0.0 cdcd1928c9
"System.GC.Server": true


Tue, 29 Aug 2017 15:27:22 GMT

MAKE:
cp nbody.csharpcore-9.csharpcore Program.cs
cp Include/csharpcore/tmp.csproj .
cp Include/csharpcore/runtimeconfig.template.json .
mkdir obj
cp Include/csharpcore/tmp.csproj.nuget.g.props ./obj
cp Include/csharpcore/tmp.csproj.nuget.g.targets ./obj
/usr/bin/dotnet build -c Release
Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  tmp -> /home/dunham/benchmarksgame_quadcore/nbody/tmp/bin/Release/netcoreapp2.0/tmp.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:05.50

12.39s to complete and log all make actions

COMMAND LINE:
/usr/bin/dotnet ./bin/Release/netcoreapp2.0/tmp.dll 500000

UNEXPECTED OUTPUT 

1c1
< -0.169198882
--- field 1	absolute error 1.24e-04
> -0.169075164
2c2
< -0.169136443
--- field 1	absolute error 3.99e-05
> -0.169096567

PROGRAM OUTPUT:
-0.169198882
-0.169136443