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:
2025-09-08 23:17:22 +09:00
parent a2a760594e
commit 07c99b8a5a
31 changed files with 1392 additions and 1204 deletions

View 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);
}
}

View 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;
}
}