Refactor job system and update project configuration
Added: - Added `JobExecutor.cs` for job execution management. - Added `JobInfo.cs` to hold job execution information. - Added `TestJobSystem.cs` for unit tests of the job system. - Added `TestJobs.cs` for additional job implementation tests. - Added `WorkerThread.cs` to manage worker threads for jobs. Changed: - Changed `AssemblyInfo.cs.cs` to include a global using directive for `unsafe JobExecuteFunc`. - Changed `IJob.cs` to include an overload of the `Execute` method with a `threadIndex` parameter. - Changed `JobHandle.cs` to include an `IsValid` property and updated internal structure. - Changed `JobScheduler.cs` to improve job scheduling and management. - Changed `JobsUtility.cs` to enhance job management functions. - Changed `MemoryBlock.cs` to reference the heap from which memory was allocated. - Changed `ParallelNoiseBenchmark.cs` to include benchmarks for the job system. - Changed `Program.cs` to execute benchmarks instead of previous test code. Removed: - Removed `.gitignore` entries for default ignored files. - Removed `JobBase.cs` to shift from structs to classes for jobs. - Removed `JobExtensions.cs` indicating a change in job scheduling. - Removed `JobStruct.cs` indicating a change in job structure. - Removed `encodings.xml`, `indexLayout.xml`, and `vcs.xml` files to simplify project configuration. - Removed fields from `JobData.cs` to simplify the job data structure. - Removed `TestJobSystem.csproj` entries related to old project structure.
This commit is contained in:
@@ -34,7 +34,7 @@ public unsafe class CollectionBenchmark
|
||||
array[i] = i;
|
||||
}
|
||||
|
||||
((ArenaAllocator*)AllocationManager.TempHandle.Allocator)->Reset();
|
||||
AllocationManager.ResetTempAllocator();
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using Misaki.HighPerformance.Jobs;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using Misaki.HighPerformance.Test.Jobs;
|
||||
@@ -9,61 +10,62 @@ namespace Misaki.HighPerformance.Test.Benchmark;
|
||||
[MemoryDiagnoser]
|
||||
public class ParallelNoiseBenchmark
|
||||
{
|
||||
private const int _WIDTH = 512;
|
||||
private const int _HEIGHT = 512;
|
||||
private const int _WIDTH = 64;
|
||||
private const int _HEIGHT = 64;
|
||||
private const int _LENGTH = _WIDTH * _HEIGHT;
|
||||
|
||||
//[GlobalSetup]
|
||||
//public void Setup()
|
||||
//{
|
||||
// JobScheduler.Initialize();
|
||||
//}
|
||||
internal JobScheduler _jobScheduler = null!;
|
||||
private UnsafeArray<float> _buffers;
|
||||
|
||||
//[GlobalCleanup]
|
||||
//public void Cleanup()
|
||||
//{
|
||||
// JobScheduler.Shutdown();
|
||||
//}
|
||||
[GlobalSetup]
|
||||
public void Setup()
|
||||
{
|
||||
_jobScheduler = new JobScheduler(Environment.ProcessorCount - 1);
|
||||
_buffers = new UnsafeArray<float>(_LENGTH, Allocator.Persistent);
|
||||
}
|
||||
|
||||
//[Benchmark]
|
||||
//public void JobSystem()
|
||||
//{
|
||||
// using var buffers = new UnsafeArray<float>(_LENGTH, Allocator.Persistent, AllocationOption.None);
|
||||
// var job = new NoiseJob()
|
||||
// {
|
||||
// buffers = buffers,
|
||||
// width = _WIDTH,
|
||||
// height = _HEIGHT
|
||||
// };
|
||||
[GlobalCleanup]
|
||||
public void Cleanup()
|
||||
{
|
||||
_jobScheduler.Dispose();
|
||||
_buffers.Dispose();
|
||||
}
|
||||
|
||||
// var handle = job.Schedule(_LENGTH, 64);
|
||||
// handle.Complete();
|
||||
//}
|
||||
[Benchmark]
|
||||
public void JobSystem()
|
||||
{
|
||||
var job = new NoiseJob()
|
||||
{
|
||||
buffers = _buffers,
|
||||
width = _WIDTH,
|
||||
height = _HEIGHT
|
||||
};
|
||||
|
||||
var handle = _jobScheduler.ScheduleParallel(ref job, _LENGTH, 64, -1);
|
||||
_jobScheduler.WaitComplete(handle);
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void ParallelFor()
|
||||
{
|
||||
using var buffers = new UnsafeArray<float>(_LENGTH, Allocator.Persistent, AllocationOption.None);
|
||||
|
||||
Parallel.For(0, _LENGTH, i =>
|
||||
{
|
||||
var x = i % _WIDTH;
|
||||
var y = i / _HEIGHT;
|
||||
var uv = new Vector2(x, y);
|
||||
buffers[i] = NoiseJob.GradientNoise(uv);
|
||||
_buffers[i] = NoiseJob.GradientNoise(uv);
|
||||
});
|
||||
}
|
||||
|
||||
[Benchmark(Baseline = true)]
|
||||
public void For()
|
||||
{
|
||||
using var buffers = new UnsafeArray<float>(_LENGTH, Allocator.Persistent, AllocationOption.None);
|
||||
for (var i = 0; i < _LENGTH; i++)
|
||||
{
|
||||
var x = i % _WIDTH;
|
||||
var y = i / _HEIGHT;
|
||||
var uv = new Vector2(x, y);
|
||||
buffers[i] = NoiseJob.GradientNoise(uv);
|
||||
_buffers[i] = NoiseJob.GradientNoise(uv);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,184 +0,0 @@
|
||||
using Misaki.HighPerformance.Jobs;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
|
||||
namespace Misaki.HighPerformance.Test.Jobs;
|
||||
|
||||
/// <summary>
|
||||
/// Simple job that adds a value to each element in an array.
|
||||
/// </summary>
|
||||
public unsafe class AddValueJob : IJobParallelFor
|
||||
{
|
||||
public float* Data;
|
||||
public float Value;
|
||||
|
||||
public void Execute(int index)
|
||||
{
|
||||
Data[index] += Value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Simple job that multiplies each element in an array by a value.
|
||||
/// </summary>
|
||||
public unsafe class MultiplyJob : IJobParallelFor
|
||||
{
|
||||
public float* Data;
|
||||
public float Multiplier;
|
||||
|
||||
public void Execute(int index)
|
||||
{
|
||||
Data[index] *= Multiplier;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Simple job that computes the sum of an array (single-threaded).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This job uses the Kahan summation algorithm to reduce numerical error.
|
||||
/// </remarks>
|
||||
public unsafe class KahanSumJob : IJob
|
||||
{
|
||||
public float* Data;
|
||||
public int Length;
|
||||
public float* Result;
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
var sum = 0f;
|
||||
var c = 0f; // Compensation for lost low-order bits
|
||||
|
||||
for (var i = 0; i < Length; i++)
|
||||
{
|
||||
var y = Data[i] - c; // So far, so good: c is zero
|
||||
var t = sum + y; // Alas, sum is big, y small, so low-order digits of y are lost
|
||||
c = (t - sum) - y; // (t - sum) cancels the high-order part of y; subtracting y recovers negative (low part of y)
|
||||
sum = t; // Algebraically, c should always be zero. Beware overly-clever compilers!
|
||||
}
|
||||
|
||||
*Result = sum;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Example program demonstrating the job system with dependencies.
|
||||
/// </summary>
|
||||
public static class JobSystemExample
|
||||
{
|
||||
public static unsafe void RunExample()
|
||||
{
|
||||
Console.WriteLine("=== Job System Example ===");
|
||||
|
||||
const int arraySize = 10000;
|
||||
|
||||
// Create test data
|
||||
using var array = new UnsafeArray<float>(arraySize, Allocator.Persistent);
|
||||
|
||||
// Initialize with values 1, 2, 3, ...
|
||||
for (var i = 0; i < arraySize; i++)
|
||||
{
|
||||
array[i] = i + 1;
|
||||
}
|
||||
|
||||
Console.WriteLine($"Initial sum: {ComputeSum((float*)array.GetUnsafePtr(), arraySize)}");
|
||||
|
||||
// Job 1: Add 10 to each element
|
||||
var addJob = new AddValueJob
|
||||
{
|
||||
Data = (float*)array.GetUnsafePtr(),
|
||||
Value = 10f
|
||||
};
|
||||
|
||||
// Job 2: Multiply each element by 2 (depends on addJob)
|
||||
var multiplyJob = new MultiplyJob
|
||||
{
|
||||
Data = (float*)array.GetUnsafePtr(),
|
||||
Multiplier = 2f
|
||||
};
|
||||
|
||||
// Job 3: Compute final sum (depends on multiplyJob)
|
||||
var result = stackalloc float[1];
|
||||
var sumJob = new KahanSumJob
|
||||
{
|
||||
Data = (float*)array.GetUnsafePtr(),
|
||||
Length = arraySize,
|
||||
Result = result
|
||||
};
|
||||
|
||||
Console.WriteLine("Scheduling jobs with dependencies...");
|
||||
|
||||
// Schedule jobs with dependencies
|
||||
var addHandle = addJob.ScheduleParallel(arraySize, 64);
|
||||
var multiplyHandle = multiplyJob.ScheduleParallel(arraySize, 64, addHandle);
|
||||
var sumHandle = sumJob.Schedule(multiplyHandle);
|
||||
|
||||
// Wait for all jobs to complete
|
||||
sumHandle.Complete();
|
||||
|
||||
Console.WriteLine($"Final sum: {*result}");
|
||||
Console.WriteLine($"Expected sum: {ComputeExpectedSum(arraySize)}");
|
||||
Console.WriteLine("Jobs completed successfully!");
|
||||
|
||||
// Test dependency combination
|
||||
Console.WriteLine("\n=== Testing Dependency Combination ===");
|
||||
|
||||
// Reset array
|
||||
for (var i = 0; i < arraySize; i++)
|
||||
{
|
||||
array[i] = 1f;
|
||||
}
|
||||
|
||||
// Create multiple independent jobs
|
||||
var basePtr = (float*)array.GetUnsafePtr();
|
||||
var job1 = new AddValueJob { Data = basePtr, Value = 1f };
|
||||
var job2 = new AddValueJob { Data = basePtr + arraySize / 2, Value = 2f };
|
||||
var job3 = new AddValueJob { Data = basePtr + arraySize / 4, Value = 3f };
|
||||
|
||||
var handle1 = job1.ScheduleParallel(arraySize / 2, 32);
|
||||
var handle2 = job2.ScheduleParallel(arraySize / 2, 32);
|
||||
var handle3 = job3.ScheduleParallel(arraySize / 4, 32);
|
||||
|
||||
// Combine dependencies
|
||||
var combinedHandle = JobHandle.CombineDependencies(handle1, handle2, handle3);
|
||||
|
||||
// Final job that depends on all previous jobs
|
||||
var finalSum = stackalloc float[1];
|
||||
var finalSumJob = new KahanSumJob
|
||||
{
|
||||
Data = (float*)array.GetUnsafePtr(),
|
||||
Length = arraySize,
|
||||
Result = finalSum
|
||||
};
|
||||
|
||||
var finalHandle = finalSumJob.Schedule(combinedHandle);
|
||||
finalHandle.Complete();
|
||||
|
||||
Console.WriteLine($"Final sum after combined dependencies: {*finalSum}");
|
||||
Console.WriteLine("Dependency combination test completed!");
|
||||
}
|
||||
|
||||
private static unsafe float ComputeSum(float* data, int length)
|
||||
{
|
||||
var sum = 0f;
|
||||
for (var i = 0; i < length; i++)
|
||||
{
|
||||
sum += data[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
private static float ComputeExpectedSum(int arraySize)
|
||||
{
|
||||
// Original sum: 1 + 2 + 3 + ... + n = n(n+1)/2
|
||||
var originalSum = arraySize * (arraySize + 1) / 2f;
|
||||
|
||||
// After adding 10: each element increases by 10, so total increases by 10 * n
|
||||
var afterAdd = originalSum + (10f * arraySize);
|
||||
|
||||
// After multiplying by 2: everything doubles
|
||||
var afterMultiply = afterAdd * 2f;
|
||||
|
||||
return afterMultiply;
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Misaki.HighPerformance.Test.Jobs;
|
||||
|
||||
internal unsafe struct NoiseJob : IJobParallelFor
|
||||
internal struct NoiseJob : IJobParallelFor
|
||||
{
|
||||
public UnsafeArray<float> buffers;
|
||||
public int width;
|
||||
@@ -41,11 +41,11 @@ internal unsafe struct NoiseJob : IJobParallelFor
|
||||
return float.Lerp(float.Lerp(d00, d10, fp.Y), float.Lerp(d01, d11, fp.Y), fp.X);
|
||||
}
|
||||
|
||||
public void Execute(int index)
|
||||
public void Execute(int loopIndex, int threadIndex)
|
||||
{
|
||||
var x = index % width;
|
||||
var y = index / height;
|
||||
var uv = new Vector2(x, y);
|
||||
buffers[index] = float.Clamp(GradientNoise(uv), 0.0f, 1.0f);
|
||||
var x = loopIndex % width;
|
||||
var y = loopIndex / height;
|
||||
var uv = new Vector2(x, y) / new Vector2(width, height);
|
||||
buffers[loopIndex] = GradientNoise(uv);
|
||||
}
|
||||
}
|
||||
@@ -22,8 +22,4 @@
|
||||
<ProjectReference Include="..\Misaki.HighPerformance\Misaki.HighPerformance.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="UnitTest\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,12 +1,46 @@
|
||||
using Misaki.HighPerformance.Test.Benchmark;
|
||||
using Misaki.HighPerformance.Test.Jobs;
|
||||
//var threadCount = 8;
|
||||
//var map = new ConcurrentSlotMap<int>();
|
||||
|
||||
// Test the job system
|
||||
JobSystemExample.RunExample();
|
||||
//var barrier = new Barrier(threadCount);
|
||||
|
||||
Console.WriteLine("\nPress any key to run benchmarks...");
|
||||
Console.ReadKey();
|
||||
//Parallel.For(0, threadCount, threadIndex =>
|
||||
//{
|
||||
// barrier.SignalAndWait();
|
||||
// for (var i = 0; i < 1000; i++)
|
||||
// {
|
||||
// var id = map.Add(i + threadIndex * 1000, out var gen);
|
||||
// if (i % 100 == 0)
|
||||
// {
|
||||
// map.Remove(id, gen);
|
||||
// }
|
||||
// }
|
||||
//});
|
||||
|
||||
BenchmarkDotNet.Running.BenchmarkRunner.Run<MathematicsBenchmark>();
|
||||
//var b = new MathematicsBenchmark();
|
||||
//b.Vector2Add();
|
||||
//Console.WriteLine($"Count should be {threadCount * 990}, actual: {map.Count}");
|
||||
|
||||
using Misaki.HighPerformance.Test.Benchmark;
|
||||
|
||||
//BenchmarkDotNet.Running.BenchmarkRunner.Run<ParallelNoiseBenchmark>();
|
||||
|
||||
var benchmark = new ParallelNoiseBenchmark();
|
||||
var sw = new System.Diagnostics.Stopwatch();
|
||||
|
||||
benchmark.Setup();
|
||||
|
||||
for (var i = 0; i < 1024; i++)
|
||||
{
|
||||
benchmark.JobSystem();
|
||||
}
|
||||
|
||||
sw.Start();
|
||||
|
||||
for (var i = 0; i < 1024; i++)
|
||||
{
|
||||
benchmark.JobSystem();
|
||||
}
|
||||
|
||||
sw.Stop();
|
||||
|
||||
benchmark.Cleanup();
|
||||
|
||||
Console.WriteLine($"JobSystem: {sw.Elapsed.TotalMilliseconds / 1024.0} ms");
|
||||
|
||||
201
Misaki.HighPerformance.Test/UnitTest/Jobs/TestJobSystem.cs
Normal file
201
Misaki.HighPerformance.Test/UnitTest/Jobs/TestJobSystem.cs
Normal file
@@ -0,0 +1,201 @@
|
||||
using Misaki.HighPerformance.Jobs;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using Misaki.HighPerformance.LowLevel.Helpers;
|
||||
|
||||
namespace Misaki.HighPerformance.Test.UnitTest.Jobs;
|
||||
|
||||
[TestClass]
|
||||
public unsafe class TestJobSystem
|
||||
{
|
||||
private JobScheduler _jobScheduler = null!;
|
||||
|
||||
[TestInitialize]
|
||||
public void Initialize()
|
||||
{
|
||||
_jobScheduler = new JobScheduler(Environment.ProcessorCount);
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void Cleanup()
|
||||
{
|
||||
_jobScheduler.Dispose();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SingleJob()
|
||||
{
|
||||
var result = stackalloc float[1];
|
||||
var job = new TwoSumJob
|
||||
{
|
||||
value1 = 1.5f,
|
||||
value2 = 2.5f,
|
||||
result = result
|
||||
};
|
||||
|
||||
var handle = _jobScheduler.Schedule(ref job, -1);
|
||||
_jobScheduler.WaitComplete(handle);
|
||||
|
||||
Assert.AreEqual(4.0f, *result);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void JobDependency()
|
||||
{
|
||||
var result = stackalloc float[1];
|
||||
var job1 = new TwoSumJob
|
||||
{
|
||||
value1 = 1.5f,
|
||||
value2 = 2.5f,
|
||||
result = result
|
||||
};
|
||||
|
||||
var handle1 = _jobScheduler.Schedule(ref job1, -1);
|
||||
|
||||
var job2 = new AddJob
|
||||
{
|
||||
value = 4.0f,
|
||||
result = result
|
||||
};
|
||||
|
||||
var handle2 = _jobScheduler.Schedule(ref job2, -1, handle1);
|
||||
_jobScheduler.WaitComplete(handle2);
|
||||
|
||||
Assert.AreEqual(8.0f, *result);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CompletedDependency()
|
||||
{
|
||||
var result = stackalloc float[1];
|
||||
var job1 = new TwoSumJob
|
||||
{
|
||||
value1 = 1.5f,
|
||||
value2 = 2.5f,
|
||||
result = result
|
||||
};
|
||||
|
||||
var handle1 = _jobScheduler.Schedule(ref job1, -1);
|
||||
_jobScheduler.WaitComplete(handle1);
|
||||
|
||||
var job2 = new AddJob
|
||||
{
|
||||
value = 4.0f,
|
||||
result = result
|
||||
};
|
||||
|
||||
var handle2 = _jobScheduler.Schedule(ref job2, -1, handle1);
|
||||
_jobScheduler.WaitComplete(handle2);
|
||||
|
||||
Assert.AreEqual(8.0f, *result);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CombineDependencies()
|
||||
{
|
||||
var result = stackalloc float[1];
|
||||
var job1 = new TwoSumJob
|
||||
{
|
||||
value1 = 2.5f,
|
||||
value2 = 2.5f,
|
||||
result = result
|
||||
};
|
||||
|
||||
var handle1 = _jobScheduler.Schedule(ref job1, -1);
|
||||
|
||||
var job2 = new AddJob
|
||||
{
|
||||
value = 4.0f,
|
||||
result = result
|
||||
};
|
||||
|
||||
var handle2 = _jobScheduler.Schedule(ref job2, -1, handle1);
|
||||
|
||||
var job3 = new AddJob
|
||||
{
|
||||
value = 10.0f,
|
||||
result = result
|
||||
};
|
||||
|
||||
var combinedHandle = _jobScheduler.CombineDependencies(handle1, handle2);
|
||||
var handle3 = _jobScheduler.Schedule(ref job3, -1, combinedHandle);
|
||||
|
||||
_jobScheduler.WaitComplete(handle3);
|
||||
|
||||
Assert.AreEqual(19.0f, *result);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SingleParallelJob()
|
||||
{
|
||||
const int size = 1000;
|
||||
var result = stackalloc float[size];
|
||||
MemoryUtilities.MemSet(result, 0, sizeof(float) * size);
|
||||
var job = new ParallelAddJob
|
||||
{
|
||||
value = 1.0f,
|
||||
inout = result
|
||||
};
|
||||
|
||||
var handle = _jobScheduler.ScheduleParallel(ref job, size, 64, -1, JobHandle.Invalid);
|
||||
_jobScheduler.WaitComplete(handle);
|
||||
|
||||
Assert.AreEqual(1.0f, result[500]);
|
||||
}
|
||||
|
||||
private static float ComputeExpectedSum(int arraySize)
|
||||
{
|
||||
// Original sum: 1 + 2 + 3 + ... + n = n(n+1)/2
|
||||
var originalSum = arraySize * (arraySize + 1) / 2f;
|
||||
|
||||
// After adding 10: each element increases by 10, so total increases by 10 * n
|
||||
var afterAdd = originalSum + (10f * arraySize);
|
||||
|
||||
// After multiplying by 2: everything doubles
|
||||
var afterMultiply = afterAdd * 2f;
|
||||
|
||||
return afterMultiply;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ChainJob()
|
||||
{
|
||||
const int arraySize = 10000;
|
||||
|
||||
using var array = new UnsafeArray<float>(arraySize, Allocator.Persistent);
|
||||
|
||||
for (var i = 0; i < arraySize; i++)
|
||||
{
|
||||
array[i] = i + 1;
|
||||
}
|
||||
|
||||
var addJob = new ParallelAddJob
|
||||
{
|
||||
value = 10f,
|
||||
inout = (float*)array.GetUnsafePtr()
|
||||
};
|
||||
|
||||
var multiplyJob = new ParallelMultiplyJob
|
||||
{
|
||||
multiplier = 2f,
|
||||
inout = (float*)array.GetUnsafePtr()
|
||||
};
|
||||
|
||||
var result = stackalloc float[1];
|
||||
var sumJob = new KahanSumJob
|
||||
{
|
||||
input = (float*)array.GetUnsafePtr(),
|
||||
length = arraySize,
|
||||
output = result
|
||||
};
|
||||
|
||||
var handle1 = _jobScheduler.ScheduleParallel(ref addJob, arraySize, 64, -1, JobHandle.Invalid);
|
||||
var handle2 = _jobScheduler.ScheduleParallel(ref multiplyJob, arraySize, 64, -1, handle1);
|
||||
var handle3 = _jobScheduler.Schedule(ref sumJob, -1, handle2);
|
||||
|
||||
_jobScheduler.WaitComplete(handle3);
|
||||
|
||||
var expected = ComputeExpectedSum(arraySize);
|
||||
Assert.AreEqual(expected, *result, 0.01f);
|
||||
}
|
||||
}
|
||||
73
Misaki.HighPerformance.Test/UnitTest/Jobs/TestJobs.cs
Normal file
73
Misaki.HighPerformance.Test/UnitTest/Jobs/TestJobs.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using Misaki.HighPerformance.Jobs;
|
||||
|
||||
namespace Misaki.HighPerformance.Test.UnitTest.Jobs;
|
||||
|
||||
internal unsafe struct TwoSumJob : IJob
|
||||
{
|
||||
public float value1;
|
||||
public float value2;
|
||||
|
||||
public float* result;
|
||||
|
||||
public void Execute(int threadIndex)
|
||||
{
|
||||
*result = value1 + value2;
|
||||
}
|
||||
}
|
||||
|
||||
internal unsafe struct AddJob : IJob
|
||||
{
|
||||
public float value;
|
||||
|
||||
public float* result;
|
||||
|
||||
public void Execute(int threadIndex)
|
||||
{
|
||||
*result += value;
|
||||
}
|
||||
}
|
||||
|
||||
internal unsafe struct KahanSumJob : IJob
|
||||
{
|
||||
public float* input;
|
||||
public int length;
|
||||
public float* output;
|
||||
|
||||
public void Execute(int threadIndex)
|
||||
{
|
||||
var sum = 0f;
|
||||
var c = 0f; // Compensation for lost low-order bits
|
||||
|
||||
for (var i = 0; i < length; i++)
|
||||
{
|
||||
var y = input[i] - c; // So far, so good: c is zero
|
||||
var t = sum + y; // Alas, sum is big, y small, so low-order digits of y are lost
|
||||
c = (t - sum) - y; // (t - sum) cancels the high-order part of y; subtracting y recovers negative (low part of y)
|
||||
sum = t; // Algebraically, c should always be zero. Beware overly-clever compilers!
|
||||
}
|
||||
|
||||
*output = sum;
|
||||
}
|
||||
}
|
||||
|
||||
internal unsafe struct ParallelAddJob : IJobParallelFor
|
||||
{
|
||||
public float value;
|
||||
public float* inout;
|
||||
|
||||
public void Execute(int loopIndex, int threadIndex)
|
||||
{
|
||||
inout[loopIndex] += value;
|
||||
}
|
||||
}
|
||||
|
||||
internal unsafe struct ParallelMultiplyJob : IJobParallelFor
|
||||
{
|
||||
public float multiplier;
|
||||
public float* inout;
|
||||
|
||||
public void Execute(int loopIndex, int threadIndex)
|
||||
{
|
||||
inout[loopIndex] *= multiplier;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user