Files
Misaki.HighPerformance/Misaki.HighPerformance.Test/Benchmark/SPMDBenchmark.cs
2026-03-30 12:47:29 +09:00

101 lines
2.3 KiB
C#

using BenchmarkDotNet.Attributes;
using Misaki.HighPerformance.Jobs;
using Misaki.HighPerformance.Mathematics.SPMD;
using System.Runtime.InteropServices;
namespace Misaki.HighPerformance.Test.Benchmark;
public unsafe class SPMDBenchmark
{
private const int _SIZE = 512;
private JobScheduler _scheduler = null!;
private float* _buf;
[GlobalSetup]
public void Setup()
{
_scheduler = new JobScheduler(Environment.ProcessorCount);
_buf = (float*)NativeMemory.Alloc(sizeof(float) * _SIZE * _SIZE);
}
[GlobalCleanup]
public void Cleanup()
{
_scheduler.Dispose();
NativeMemory.Free(_buf);
}
[Benchmark(Baseline = true)]
public void SPMDNoise()
{
var job = new Jobs.NoiseJobMathSPMD
{
buffers = _buf,
width = _SIZE,
height = _SIZE,
};
var handle = _scheduler.ScheduleParallelSPDM<Jobs.NoiseJobMathSPMD, float>(ref job, _SIZE * _SIZE, 64, -1, JobHandle.Invalid);
_scheduler.Wait(handle);
}
[Benchmark]
public void JobNoise()
{
var job = new Jobs.NoiseJobVectorFor
{
buffers = _buf,
width = _SIZE,
height = _SIZE,
};
var handle = _scheduler.ScheduleParallelFor(ref job, _SIZE * _SIZE, 64, -1, JobHandle.Invalid);
_scheduler.Wait(handle);
}
//[Benchmark]
public void MathJobNoise()
{
var job = new Jobs.NoiseJobMath
{
buffers = _buf,
width = _SIZE,
height = _SIZE,
};
var handle = _scheduler.ScheduleParallel(ref job, _SIZE * _SIZE, 64, -1, JobHandle.Invalid);
_scheduler.Wait(handle);
}
//[Benchmark]
public void ParallelNoise()
{
var job = new Jobs.NoiseJobVectorFor
{
buffers = _buf,
width = _SIZE,
height = _SIZE,
};
Parallel.For(0, _SIZE * _SIZE, (i) =>
{
job.Execute(i, default);
});
}
[Benchmark]
public void SingleThreadNoise()
{
var job = new Jobs.NoiseJobVectorFor
{
buffers = _buf,
width = _SIZE,
height = _SIZE,
};
var ctx = new JobExecutionContext(-1, _scheduler);
job.Run(_SIZE * _SIZE, in ctx);
}
}