Skip to content

Commit

Permalink
Cpp accel (#3)
Browse files Browse the repository at this point in the history
* Add Cpp DLL

* Code Cleaning

* Update README.md

* bug fix

* fix utility

* Add .NET 8

* Update README
  • Loading branch information
kingsznhone authored Apr 23, 2024
1 parent de13b6e commit 098350c
Show file tree
Hide file tree
Showing 28 changed files with 591 additions and 121 deletions.
43 changes: 22 additions & 21 deletions DataConverter/DataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static List<PlanetTable> ReadData()
}
VSOP2013DATA.Add(planet);
}

ParallelLoopResult result = Parallel.For(0, 9, ip =>
{
ReadPlanet(VSOP2013DATA[ip], ip);
Expand Down Expand Up @@ -108,17 +108,16 @@ private static void ReadPlanet(PlanetTable Planet, int ip)

Planet.body = (VSOPBody)H.ip;
Planet.variables[H.iv].Body = (VSOPBody)H.ip;
Planet.variables[H.iv].iv = H.iv;
Planet.variables[H.iv].Variable = (VSOPVariable) H.iv;

Term[] buffer = new Term[H.nt];
for (int i = 0; i < H.nt; i++)
{
line = sr.ReadLine();
ReadTerm(line, ref buffer[i]);
}

Planet.variables[H.iv].PowerTables[H.it].iv = H.iv;
Planet.variables[H.iv].PowerTables[H.it].it = H.it;
Planet.variables[H.iv].PowerTables[H.it].Variable = (VSOPVariable)H.iv;
Planet.variables[H.iv].PowerTables[H.it].Power = H.it;
Planet.variables[H.iv].PowerTables[H.it].Body = (VSOPBody)H.ip;
Planet.variables[H.iv].PowerTables[H.it].Terms = buffer;
}
Expand All @@ -128,22 +127,24 @@ private static void ReadPlanet(PlanetTable Planet, int ip)

private static Header ReadHeader(string line)
{
ReadOnlySpan<char> lineSpan = line.AsSpan();
Header H = new();
int lineptr = 9;
H.ip = Convert.ToInt32(line.Substring(lineptr, 3).Trim()) - 1;
H.ip = int.Parse(lineSpan[lineptr..(lineptr + 3)].Trim()) - 1;
lineptr += 3;
H.iv = Convert.ToInt32(line.Substring(lineptr, 3).Trim()) - 1;
H.iv = int.Parse(lineSpan[lineptr..(lineptr + 3)].Trim()) - 1;
lineptr += 3;
H.it = Convert.ToInt32(line.Substring(lineptr, 3).Trim());
H.it = int.Parse(lineSpan[lineptr..(lineptr + 3)].Trim());
lineptr += 3;
H.nt = Convert.ToInt32(line.Substring(lineptr, 7).Trim());
H.nt = int.Parse(lineSpan[lineptr..(lineptr + 7)].Trim());
return H;
}

private static Term ReadTerm(string line, ref Term T)
{
ReadOnlySpan<char> lineSpan = line.AsSpan();
int lineptr;
int[] Bufferiphi = new int[17];
Span<int> Bufferiphi = stackalloc int[17];
int index = 0;
double ci;

Expand All @@ -155,7 +156,8 @@ private static Term ReadTerm(string line, ref Term T)
//
for (int counter = 0; counter < 4; counter++)
{
Bufferiphi[index] = Convert.ToInt32(line.Substring(lineptr, 3).Trim());
var debug = lineSpan[lineptr..(lineptr + 3)].Trim();
Bufferiphi[index] = int.Parse(lineSpan[lineptr..(lineptr + 3)].Trim());
index++;
lineptr += 3;
}
Expand All @@ -164,7 +166,8 @@ private static Term ReadTerm(string line, ref Term T)
//
for (int counter = 0; counter < 5; counter++)
{
Bufferiphi[index] = Convert.ToInt32(line.Substring(lineptr, 3).Trim());

Bufferiphi[index] = int.Parse(lineSpan[lineptr..(lineptr + 3)].Trim());
index++;
lineptr += 3;
}
Expand All @@ -173,39 +176,37 @@ private static Term ReadTerm(string line, ref Term T)
//
for (int counter = 0; counter < 4; counter++)
{
Bufferiphi[index] = Convert.ToInt32(line.Substring(lineptr, 4).Trim());
Bufferiphi[index] = int.Parse(lineSpan[lineptr..(lineptr + 4)].Trim());
index++;
lineptr += 4;
}
//
lineptr++;
//
Bufferiphi[index] = Convert.ToInt32(line.Substring(lineptr, 6).Trim());
Bufferiphi[index] = int.Parse(lineSpan[lineptr..(lineptr + 6)].Trim());
index++;
lineptr += 6;
//
lineptr++;
//
for (int counter = 0; counter < 3; counter++)
{
Bufferiphi[index] = Convert.ToInt32(line.Substring(lineptr, 3).Trim());
Bufferiphi[index] = int.Parse(lineSpan[lineptr..(lineptr + 3)].Trim());
index++;
lineptr += 3;
}

//T.iphi = Bufferiphi;

ci = Convert.ToDouble(line.Substring(lineptr, 20).Trim());
ci = double.Parse(lineSpan[lineptr..(lineptr + 20)].Trim());
lineptr += 20;
lineptr++;
ci *= Math.Pow(10, Convert.ToDouble(line.Substring(lineptr, 3).Trim()));
ci *= Math.Pow(10, double.Parse(lineSpan[lineptr..(lineptr + 3)].Trim()));
lineptr += 3;
T.ss = ci;

ci = Convert.ToDouble(line.Substring(lineptr, 20).Trim());
ci = double.Parse(lineSpan[lineptr..(lineptr + 20)].Trim());
lineptr += 20;
lineptr++;
ci *= Math.Pow(10, Convert.ToDouble(line.Substring(lineptr, 3).Trim()));
ci *= Math.Pow(10, double.Parse(lineSpan[lineptr..(lineptr + 3)].Trim()));

T.cc = ci;

Expand Down
2 changes: 1 addition & 1 deletion Demo/Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>x64</Platforms>
</PropertyGroup>

Expand Down
12 changes: 10 additions & 2 deletions Demo/PerfTest.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using VSOP2013;

namespace Demo
{
[SimpleJob(RuntimeMoniker.Net60)]
[SimpleJob(RuntimeMoniker.Net70)]
[SimpleJob(RuntimeMoniker.Net80)]

[MemoryDiagnoser]
public class PerfTest
{
Expand All @@ -28,11 +29,18 @@ public void init()
vTime = new VSOPTime(dt, TimeFrame.UTC);
}

[Benchmark]
[Benchmark(Baseline = true)]
public VSOPResult Compute()
{
var ell = vsop.GetPlanetPosition(VSOPBody.JUPITER, vTime);
return ell;
}

[Benchmark]
public VSOPResult Compute_Native()
{
var ell = vsop.GetPlanetPosition_Native(VSOPBody.JUPITER, vTime);
return ell;
}
}
}
41 changes: 21 additions & 20 deletions Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,24 @@ private static void Main(string[] args)

ell = vsop.GetPlanetPosition(VSOPBody.EMB, vTime);
FormattedPrint(ell, vTime);
ell = vsop.GetPlanetPosition_Native(VSOPBody.EMB, vTime);
FormattedPrint(ell, vTime);
//xyz = (VSOPResult_XYZ)ell;
//FormattedPrint(xyz, vTime);
//xyz.ReferenceFrame = ReferenceFrame.ICRSJ2000;
//FormattedPrint(xyz, vTime);

xyz = (VSOPResult_XYZ)ell;
FormattedPrint(xyz, vTime);
xyz.ReferenceFrame = ReferenceFrame.ICRSJ2000;
FormattedPrint(xyz, vTime);

lbr = (VSOPResult_LBR)ell;
FormattedPrint(lbr, vTime);
lbr.ReferenceFrame = ReferenceFrame.ICRSJ2000;
FormattedPrint(lbr, vTime);
//lbr = (VSOPResult_LBR)ell;
//FormattedPrint(lbr, vTime);
//lbr.ReferenceFrame = ReferenceFrame.ICRSJ2000;
//FormattedPrint(lbr, vTime);

//for(int i = 0; i < 1000; i++)
{
foreach (VSOPBody body in Enum.GetValues(typeof(VSOPBody)))
{
ell = vsop.GetPlanetPosition(body, vTime);
}
}
//{
// foreach (VSOPBody body in Enum.GetValues(typeof(VSOPBody)))
// {
// ell = vsop.GetPlanetPosition(body, vTime);
// }
//}

Console.WriteLine("Press Enter to Start Performance Test...");
Console.ReadLine();
Expand All @@ -63,6 +63,7 @@ private static void Main(string[] args)
#else
var summary = BenchmarkRunner.Run<PerfTest>();
#endif
Console.ReadLine();
}

public static void FormattedPrint(VSOPResult Result, VSOPTime vtime)
Expand Down Expand Up @@ -106,8 +107,8 @@ public static void FormattedPrint(VSOPResult Result, VSOPTime vtime)
WriteColorLine("Reference Frame: ", ConsoleColor.Green, $"\tICRS equinox and ecliptic J2000");
break;
}
WriteColorLine("At UTC: ", ConsoleColor.Green, $"\t\t{Result.Time.UTC.ToUniversalTime().ToString("o")}");
WriteColorLine("At TDB: ", ConsoleColor.Green, $"\t\t{Result.Time.TDB.ToString("o")}");
WriteColorLine("At UTC: ", ConsoleColor.Green, $"\t\t{Result.Time.UTC.ToUniversalTime():o}");
WriteColorLine("At TDB: ", ConsoleColor.Green, $"\t\t{Result.Time.TDB:o}");

if (Result.CoordinatesType == CoordinatesType.Elliptic)
{
Expand Down Expand Up @@ -158,8 +159,8 @@ private static void WriteColorLine(params object[] oo)
foreach (var o in oo)
if (o == null)
Console.ResetColor();
else if (o is ConsoleColor)
Console.ForegroundColor = (ConsoleColor)o;
else if (o is ConsoleColor color)
Console.ForegroundColor = color;
else
Console.Write(o.ToString());
Console.WriteLine();
Expand Down
8 changes: 8 additions & 0 deletions Demo/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"Demo": {
"commandName": "Project",
"nativeDebugging": true
}
}
}
13 changes: 13 additions & 0 deletions NativeAccelerator/NativeAccelerator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "NativeAccelerator.h"
#include "math.h"

double StartIteration(struct Term* terms, int length, double tj, double tit) {
double result = 0;
for (int n = 0; n < length; n++) {
double u = terms[n].aa + terms[n].bb * tj;
double su = sin(u);
double cu = cos(u);
result += tit * (terms[n].ss * su + terms[n].cc * cu);
}
return result;
}
26 changes: 26 additions & 0 deletions NativeAccelerator/NativeAccelerator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once


#ifdef DLLEXPORT// DLLEXPORT
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif

struct Term
{
double ss;
double cc;
double aa;
double bb;
};

#ifdef __cplusplus
extern "C" {
#endif

DLL_EXPORT double StartIteration(struct Term* terms,int length, double tj, double tit);

#ifdef __cplusplus
}
#endif
Loading

0 comments on commit 098350c

Please sign in to comment.