cache the compiled inner loop whenever the options change, so it does not have to recompile every time when evaluated
This commit is contained in:
parent
006ad8c4f7
commit
54bb37852a
54
Program.cs
54
Program.cs
|
|
@ -68,6 +68,9 @@ void Main()
|
||||||
bool shouldUseCompiler = false;
|
bool shouldUseCompiler = false;
|
||||||
bool shouldUseParallelism = true;
|
bool shouldUseParallelism = true;
|
||||||
bool shouldUseSimd = true;
|
bool shouldUseSimd = true;
|
||||||
|
|
||||||
|
// compilation specific
|
||||||
|
bool shouldRecompile = false;
|
||||||
|
|
||||||
bool isEvaluating = false;
|
bool isEvaluating = false;
|
||||||
bool shouldEvaluate = false;
|
bool shouldEvaluate = false;
|
||||||
|
|
@ -75,32 +78,54 @@ void Main()
|
||||||
bool shouldUpdateTexture = false;
|
bool shouldUpdateTexture = false;
|
||||||
|
|
||||||
float lastEvaluationTimeTook = 0.0f;
|
float lastEvaluationTimeTook = 0.0f;
|
||||||
|
float lastCompilationTimeTook = 0.0f;
|
||||||
|
|
||||||
|
// cache the instructions, the program doesn't change after all
|
||||||
|
EvaluationInstructions instructions = Parsing.Parse(programsProsperoVm);
|
||||||
|
|
||||||
while (!Raylib.WindowShouldClose())
|
while (!Raylib.WindowShouldClose())
|
||||||
{
|
{
|
||||||
Raylib.BeginDrawing();
|
Raylib.BeginDrawing();
|
||||||
|
|
||||||
Raylib.ClearBackground(Color.White);
|
Raylib.ClearBackground(Color.White);
|
||||||
|
|
||||||
|
InterpreterOptions interpreterOptions = (shouldUseParallelism ? InterpreterOptions.Parallelism : default)
|
||||||
|
| (shouldUseSimd ? InterpreterOptions.Simd : default)
|
||||||
|
| (shouldUseCompiler
|
||||||
|
? InterpreterOptions.CompileInnerLoop
|
||||||
|
: default);
|
||||||
|
|
||||||
if (shouldEvaluate && isEvaluating)
|
if (shouldEvaluate && isEvaluating)
|
||||||
{
|
{
|
||||||
shouldEvaluate = false;
|
shouldEvaluate = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (shouldRecompile)
|
||||||
|
{
|
||||||
|
Compiler.CompilerCache<Vector<float>>.CachedPrograms.Clear();
|
||||||
|
Compiler.CompilerCache<float>.CachedPrograms.Clear();
|
||||||
|
|
||||||
|
Stopwatch sw = Stopwatch.StartNew();
|
||||||
|
if ((interpreterOptions & InterpreterOptions.Simd) != 0)
|
||||||
|
{
|
||||||
|
Compiler.Compile<Vector<float>>(instructions);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Compiler.Compile<float>(instructions);
|
||||||
|
}
|
||||||
|
lastCompilationTimeTook = (float)sw.Elapsed.TotalSeconds;
|
||||||
|
shouldRecompile = false;
|
||||||
|
}
|
||||||
|
|
||||||
if (shouldEvaluate && !isEvaluating)
|
if (shouldEvaluate && !isEvaluating)
|
||||||
{
|
{
|
||||||
isEvaluating = true;
|
isEvaluating = true;
|
||||||
shouldUpdateTexture = true;
|
shouldUpdateTexture = true;
|
||||||
InterpreterOptions interpreterOptions = (shouldUseParallelism ? InterpreterOptions.Parallelism : default)
|
|
||||||
| (shouldUseSimd ? InterpreterOptions.Simd : default)
|
|
||||||
| (shouldUseCompiler
|
|
||||||
? InterpreterOptions.CompileInnerLoop
|
|
||||||
: default);
|
|
||||||
|
|
||||||
Task.Run(() =>
|
Task.Run(() =>
|
||||||
{
|
{
|
||||||
Stopwatch sw = Stopwatch.StartNew();
|
Stopwatch sw = Stopwatch.StartNew();
|
||||||
EvaluationInstructions instructions = Parsing.Parse(programsProsperoVm);
|
|
||||||
currentOutputImageData.AsSpan()[..currentOutputImageData.Length].Clear();
|
currentOutputImageData.AsSpan()[..currentOutputImageData.Length].Clear();
|
||||||
|
|
||||||
if ((interpreterOptions & InterpreterOptions.Simd) != 0)
|
if ((interpreterOptions & InterpreterOptions.Simd) != 0)
|
||||||
|
|
@ -142,7 +167,14 @@ void Main()
|
||||||
{
|
{
|
||||||
double evaluationTimeNanoSeconds = (lastEvaluationTimeTook * 1000.0 * 1000.0 * 1000.0);
|
double evaluationTimeNanoSeconds = (lastEvaluationTimeTook * 1000.0 * 1000.0 * 1000.0);
|
||||||
double nanoSecondsPerPixel = evaluationTimeNanoSeconds / (currentOutputImageSize * currentOutputImageSize);
|
double nanoSecondsPerPixel = evaluationTimeNanoSeconds / (currentOutputImageSize * currentOutputImageSize);
|
||||||
Raylib.DrawText($" - evaluation took: {lastEvaluationTimeTook:0.0} s ({nanoSecondsPerPixel:0.0} ns / pixel)", 12, 52, 20, Color.White);
|
if (shouldUseCompiler)
|
||||||
|
{
|
||||||
|
Raylib.DrawText($" - evaluation took: {lastEvaluationTimeTook:0.0} s ({nanoSecondsPerPixel:0.0} ns / pixel) (compilation: {lastCompilationTimeTook:0.0} s)", 12, 52, 20, Color.White);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Raylib.DrawText($" - evaluation took: {lastEvaluationTimeTook:0.0} s ({nanoSecondsPerPixel:0.0} ns / pixel)", 12, 52, 20, Color.White);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Raylib.IsKeyPressed(KeyboardKey.R))
|
if (Raylib.IsKeyPressed(KeyboardKey.R))
|
||||||
|
|
@ -166,11 +198,19 @@ void Main()
|
||||||
if (Raylib.IsKeyPressed(KeyboardKey.S))
|
if (Raylib.IsKeyPressed(KeyboardKey.S))
|
||||||
{
|
{
|
||||||
shouldUseSimd = !shouldUseSimd;
|
shouldUseSimd = !shouldUseSimd;
|
||||||
|
if (shouldUseCompiler)
|
||||||
|
{
|
||||||
|
shouldRecompile = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Raylib.IsKeyPressed(KeyboardKey.C))
|
if (Raylib.IsKeyPressed(KeyboardKey.C))
|
||||||
{
|
{
|
||||||
shouldUseCompiler = !shouldUseCompiler;
|
shouldUseCompiler = !shouldUseCompiler;
|
||||||
|
if (shouldUseCompiler)
|
||||||
|
{
|
||||||
|
shouldRecompile = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Raylib.EndDrawing();
|
Raylib.EndDrawing();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue