some cleanup

This commit is contained in:
profan 2026-06-06 22:02:02 +01:00
parent 54bb37852a
commit 6ac115fe0b
2 changed files with 48 additions and 52 deletions

View File

@ -160,21 +160,18 @@ void Main()
Raylib.DrawTexture(currentOutputTexture, 0, 0, Color.White); Raylib.DrawTexture(currentOutputTexture, 0, 0, Color.White);
Raylib.EndShaderMode(); Raylib.EndShaderMode();
Raylib.DrawText("Sharpero (press R to evaluate, O to output to file)", 12, 12, 20, Color.White); Raylib.DrawText("Sharpero (press R to evaluate, O to output to file)", 12, 12, 20, Color.RayWhite);
Raylib.DrawText($" - parallelism {(shouldUseParallelism ? "enabled" : "disabled")} (P to toggle), simd {(shouldUseSimd ? "enabled" : "disabled")} (S to toggle), compile {(shouldUseCompiler? "enabled" : "disabled")} (C to toggle)", 12, 32, 20, Color.White); Raylib.DrawText($" - parallelism {(shouldUseParallelism ? "enabled" : "disabled")} (P to toggle), simd {(shouldUseSimd ? "enabled" : "disabled")} (S to toggle), compile {(shouldUseCompiler? "enabled" : "disabled")} (C to toggle)", 12, 32, 20, Color.RayWhite);
if (lastEvaluationTimeTook != 0.0f) if (lastEvaluationTimeTook != 0.0f)
{ {
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);
if (shouldUseCompiler) Raylib.DrawText(
{ shouldUseCompiler
Raylib.DrawText($" - evaluation took: {lastEvaluationTimeTook:0.0} s ({nanoSecondsPerPixel:0.0} ns / pixel) (compilation: {lastCompilationTimeTook:0.0} s)", 12, 52, 20, Color.White); ? $" - evaluation took: {lastEvaluationTimeTook:0.0} s ({nanoSecondsPerPixel:0.0} ns / pixel) (compilation: {lastCompilationTimeTook:0.0} s)"
} : $" - evaluation took: {lastEvaluationTimeTook:0.0} s ({nanoSecondsPerPixel:0.0} ns / pixel)", 12,
else 52, 20, Color.White);
{
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))
@ -186,7 +183,7 @@ void Main()
{ {
Task.Run(() => Task.Run(() =>
{ {
GenerateOutputImage(currentImageSize: currentOutputImageSize, shouldWriteOutputImage: true); GenerateOutputImage(currentImageSize: currentOutputImageSize);
}); });
} }
@ -221,43 +218,16 @@ void Main()
Raylib.CloseWindow(); Raylib.CloseWindow();
} }
float[] GenerateOutputImage(int currentImageSize, bool shouldWriteOutputImage = false) void GenerateOutputImage(int currentImageSize)
{ {
string combinedOutputString = string.Empty; string outputImagePath = "prospero.jpg";
InterpreterOptions interpreterOptions = InterpreterOptions.Parallelism | InterpreterOptions.Simd;
(float[] result, double totalTimeTakenSecondsOutput) = BenchmarkFunction(() => (bool result, double timeTakenInSeconds) = BenchmarkFunction(() =>
{
(float[] result, double timeTakenSecondsEvaluate) = BenchmarkFunction(() =>
{ {
EvaluationInstructions instructions = Parsing.Parse(programsProsperoVm); EvaluationInstructions instructions = Parsing.Parse(programsProsperoVm);
InterpreterOptions interpreterOptions = InterpreterOptions.Parallelism | InterpreterOptions.Simd; float[] result = Interpreter.Evaluate<Vector<float>>(instructions, imageSize: currentImageSize, interpreterOptions);
return Interpreter.Evaluate<Vector<float>>(instructions, imageSize: currentImageSize, interpreterOptions); WriteOutputImage(currentImageSize, result, outputImagePath);
});
combinedOutputString += $" - took: {timeTakenSecondsEvaluate} seconds to evaluate the image!" + Environment.NewLine;
if (shouldWriteOutputImage)
{
(bool success, double timeTakenSecondsOutput) = BenchmarkFunction(() =>
{
WriteOutputImage(currentImageSize, result, "prospero.jpg");
return true;
});
combinedOutputString += $" - took: {timeTakenSecondsOutput} seconds to write out image!" + Environment.NewLine;
}
return result;
});
Console.Write($"Sharpero took: {totalTimeTakenSecondsOutput} seconds to evaluate {currentImageSize}x{currentImageSize} image!" + Environment.NewLine + combinedOutputString);
(T, double) BenchmarkFunction<T>(Func<T> benchmarkedFunction)
{
var sw = Stopwatch.StartNew();
T benchmarkedResult = benchmarkedFunction();
return (benchmarkedResult, sw.Elapsed.TotalSeconds);
}
void WriteOutputImage(int imageSize, float[] imageData, string imageOutputPath) void WriteOutputImage(int imageSize, float[] imageData, string imageOutputPath)
{ {
@ -268,7 +238,17 @@ float[] GenerateOutputImage(int currentImageSize, bool shouldWriteOutputImage =
data.SaveTo(stream); data.SaveTo(stream);
} }
return result; return true;
});
Console.WriteLine($"Sharpero wrote {currentImageSize}x{currentImageSize} to {outputImagePath} in {timeTakenInSeconds} seconds ({interpreterOptions})");
}
(T, double) BenchmarkFunction<T>(Func<T> benchmarkedFunction)
{
var sw = Stopwatch.StartNew();
T benchmarkedResult = benchmarkedFunction();
return (benchmarkedResult, sw.Elapsed.TotalSeconds);
} }
internal enum OpCode { VarX, VarY, Const, Add, Sub, Mul, Max, Min, Neg, Square, Sqrt } internal enum OpCode { VarX, VarY, Const, Add, Sub, Mul, Max, Min, Neg, Square, Sqrt }
@ -352,6 +332,11 @@ internal static class Parsing
var identifiers = new Dictionary<string, Operand>(); var identifiers = new Dictionary<string, Operand>();
foreach (string line in File.ReadAllLines(filename)) foreach (string line in File.ReadAllLines(filename))
{ {
if (line.StartsWith('#'))
{
continue;
}
switch (line.Split(" ")) switch (line.Split(" "))
{ {
case [{ } @out, "const", not null]: case [{ } @out, "const", not null]:
@ -375,6 +360,11 @@ internal static class Parsing
List<Instruction> instructions = []; List<Instruction> instructions = [];
foreach (string line in File.ReadAllLines(filename)) foreach (string line in File.ReadAllLines(filename))
{ {
if (line.StartsWith('#'))
{
continue;
}
Instruction? parsedInstruction = line.Split(" ") switch Instruction? parsedInstruction = line.Split(" ") switch
{ {
[{ } @out, "var-x"] => new Instruction(identifiers[@out], OpCode.VarX), [{ } @out, "var-x"] => new Instruction(identifiers[@out], OpCode.VarX),

6
Programs/simple.vm Normal file
View File

@ -0,0 +1,6 @@
# 1/4th filled in?
_0 var-x
_1 var-y
_2 add _0 _1
_3 const 1
_4 sub _2 _3