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
|
|
@ -69,12 +69,19 @@ void Main()
|
|||
bool shouldUseParallelism = true;
|
||||
bool shouldUseSimd = true;
|
||||
|
||||
// compilation specific
|
||||
bool shouldRecompile = false;
|
||||
|
||||
bool isEvaluating = false;
|
||||
bool shouldEvaluate = false;
|
||||
bool shouldCancelUpdateTexture = false;
|
||||
bool shouldUpdateTexture = false;
|
||||
|
||||
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())
|
||||
{
|
||||
|
|
@ -82,25 +89,43 @@ void Main()
|
|||
|
||||
Raylib.ClearBackground(Color.White);
|
||||
|
||||
InterpreterOptions interpreterOptions = (shouldUseParallelism ? InterpreterOptions.Parallelism : default)
|
||||
| (shouldUseSimd ? InterpreterOptions.Simd : default)
|
||||
| (shouldUseCompiler
|
||||
? InterpreterOptions.CompileInnerLoop
|
||||
: default);
|
||||
|
||||
if (shouldEvaluate && isEvaluating)
|
||||
{
|
||||
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)
|
||||
{
|
||||
isEvaluating = true;
|
||||
shouldUpdateTexture = true;
|
||||
InterpreterOptions interpreterOptions = (shouldUseParallelism ? InterpreterOptions.Parallelism : default)
|
||||
| (shouldUseSimd ? InterpreterOptions.Simd : default)
|
||||
| (shouldUseCompiler
|
||||
? InterpreterOptions.CompileInnerLoop
|
||||
: default);
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
Stopwatch sw = Stopwatch.StartNew();
|
||||
EvaluationInstructions instructions = Parsing.Parse(programsProsperoVm);
|
||||
currentOutputImageData.AsSpan()[..currentOutputImageData.Length].Clear();
|
||||
|
||||
if ((interpreterOptions & InterpreterOptions.Simd) != 0)
|
||||
|
|
@ -142,7 +167,14 @@ void Main()
|
|||
{
|
||||
double evaluationTimeNanoSeconds = (lastEvaluationTimeTook * 1000.0 * 1000.0 * 1000.0);
|
||||
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))
|
||||
|
|
@ -166,11 +198,19 @@ void Main()
|
|||
if (Raylib.IsKeyPressed(KeyboardKey.S))
|
||||
{
|
||||
shouldUseSimd = !shouldUseSimd;
|
||||
if (shouldUseCompiler)
|
||||
{
|
||||
shouldRecompile = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (Raylib.IsKeyPressed(KeyboardKey.C))
|
||||
{
|
||||
shouldUseCompiler = !shouldUseCompiler;
|
||||
if (shouldUseCompiler)
|
||||
{
|
||||
shouldRecompile = true;
|
||||
}
|
||||
}
|
||||
|
||||
Raylib.EndDrawing();
|
||||
|
|
|
|||
Loading…
Reference in New Issue