diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskWrapFactory.cs b/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskWrapFactory.cs index 84922aaa..84873e72 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskWrapFactory.cs +++ b/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskWrapFactory.cs @@ -53,10 +53,12 @@ namespace StgSharp.Mathematics public static MatrixParallelTaskPackage* FromTaskWrap(MatrixParallelTaskPackage* source, int offset) { MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)_wrapAllocator.Allocate(); + /* *p = *source; - p->left += offset * p->leftStride; - p->right += offset * p->rightStride; - p->result += offset * p->resultStride; + p->Left += offset * p->LeftStride; + p->Right += offset * p->RightStride; + p->Result += offset * p->ResultStride; + /**/ return p; } @@ -67,8 +69,8 @@ namespace StgSharp.Mathematics public static void Release(MatrixParallelTaskPackage* source) { - if (source->scalerPacket != null) { - _scalarAllocator.Free((nuint)source->scalerPacket); + if (source->Scalar != null) { + _scalarAllocator.Free((nuint)source->Scalar); } _wrapAllocator.Free((nuint)source); } @@ -77,23 +79,31 @@ namespace StgSharp.Mathematics { int count = group.Length; group[0] = (nint)source; - int l = source->leftStride, r = source->rightStride, res = source->resultStride, c = source->count / count; - source->leftStride *= count; - source->rightStride *= count; - source->resultStride *= count; - int remain = source->count - (c * count); + int l = source->LeftPrimStride, r = source->RightPrimStride, res = source->ResultPrimStride, c = source->PrimCount / count; + source->LeftPrimStride *= count; + source->RightPrimStride *= count; + source->ResultPrimStride *= count; + source->PrimCount = c; + int remain = source->PrimCount - (c * count); for (int i = 1; i < count; i++) { nint handle = (nint)_wrapAllocator.Allocate(); MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)handle; *p = *source; - p->left += l; - p->right += r; - p->result += res; + p->Left += l * i; + p->Right += r * i; + p->Result += res * i; + p->PrimCount = c; group[i] = handle; } - for (int i = 0; i < remain; i++) { - ((MatrixParallelTaskPackage*)group[^i])->count++; + + // Distribute remaining work to the last few tasks + for (int i = 0; i < remain; i++) + { + int targetIndex = count - 1 - i; + if (targetIndex >= 0) { + ((MatrixParallelTaskPackage*)group[targetIndex])->PrimCount++; + } } } diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs b/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs index 71590d62..a96ab2b0 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs +++ b/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs @@ -286,4 +286,4 @@ namespace StgSharp.Mathematics DeepSleep = ThreadPriority.Lowest, } -} +} \ No newline at end of file diff --git a/StgSharpDebug/MatrixParallelTest.cs b/StgSharpDebug/MatrixParallelTest.cs new file mode 100644 index 00000000..ad4b8ff3 --- /dev/null +++ b/StgSharpDebug/MatrixParallelTest.cs @@ -0,0 +1,229 @@ +//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// file="MatrixParallelTest.cs" +// Project: StgSharp +// AuthorGroup: Nitload Space +// Copyright (c) Nitload Space. All rights reserved. +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the “Software”), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, +// and to permit persons to whom the Software is furnished to do so, +// subject to the following conditions: +// +// The above copyright notice and +// this permission notice shall be included in all copies +// or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED “AS IS”, +// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH +// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// +// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +using StgSharp.Mathematics; +using System; +using System.Diagnostics; +using System.Threading; + +namespace StgSharp +{ + internal partial class Program + { + + public static void TestMatrixParallel() + { + // 4. 线程调度基础功能测试 + Console.WriteLine("=== 线程调度基础功能测试 ==="); + TestSchedulingBasics(); + Console.WriteLine(); + + // 5. 并行控制流测试 + Console.WriteLine("=== 并行控制流测试 ==="); + TestParallelControlFlow(); + Console.WriteLine(); + + // 6. 重复操作稳定性测试 + Console.WriteLine("=== 重复操作稳定性测试 ==="); + TestRepeatedOperations(); + Console.WriteLine(); + + // 7. 线程状态监控测试 + Console.WriteLine("=== 线程状态监控测试 ==="); + TestThreadStateMonitoring(); + Console.WriteLine(); + + Console.WriteLine("? MatrixParallel 分层线程调度系统空跑测试完成"); + } + + private static bool TestInitialization() + { + if (!MatrixParallel.SupportParallel) + { + Console.WriteLine("? 跳过初始化测试 - 系统不支持并行处理"); + return false; + } + + try + { + Console.WriteLine("正在初始化 MatrixParallel..."); + MatrixParallel.Init(); + Console.WriteLine("? MatrixParallel 初始化成功"); + + + // 检查 LeaderHandle 是否正确设置 + if (MatrixParallel.LeaderHandle != null) + { + Console.WriteLine($"? LeaderHandle 已创建,Wrap ID: {MatrixParallel.LeaderHandle.Wrap.ID}"); + } else + { + Console.WriteLine("? LeaderHandle 未正确设置"); + } + + return true; + } + catch (Exception ex) + { + Console.WriteLine($"? 初始化失败: {ex.Message}"); + return false; + } + } + + private static void TestParallelControlFlow() + { + try + { + Console.WriteLine("测试并行控制流..."); + + for (int i = 0; i < 5; i++) + { + Console.WriteLine($" 第 {i + 1} 轮控制流测试:"); + + + // 获取并行锁 + Console.WriteLine(" 执行 LeadParallel()..."); + MatrixParallel.LeadParallel(); + + try + { + // 启动并行处理 + Console.WriteLine($" 执行 LaunchParallel({(SleepMode)(i % 3 + 1)})..."); + MatrixParallel.LaunchParallel((SleepMode)(i % 3 + 1)); + + // 设置所有 Wrap + MatrixParallel.SetAllWrap(); + + // 发布任务(空任务) + MatrixParallel.PublishTask(); + + // 短暂等待,模拟实际工作 + Thread.Sleep(10 + i * 5); + + Console.WriteLine(" ? 控制流执行正常"); + } + finally + { + // 释放并行锁 + Console.WriteLine(" 执行 EndParallel()..."); + MatrixParallel.EndParallel(); + } + } + + Console.WriteLine("? 并行控制流测试通过"); + } + catch (Exception ex) + { + Console.WriteLine($"? 并行控制流测试失败: {ex.Message}"); + } + } + + private static void TestRepeatedOperations() + { + try + { + Console.WriteLine("执行重复操作稳定性测试..."); + + Stopwatch stopwatch = Stopwatch.StartNew(); + int iterations = 50; + int successCount = 0; + + for (int i = 0; i < iterations; i++) + { + try + { + MatrixParallel.LeadParallel(); + + MatrixParallel.LaunchParallel(SleepMode.QuickWakeup); + MatrixParallel.SetAllWrap(); + MatrixParallel.PublishTask(); + + + // 极短暂的等待 + Thread.Sleep(1); + + successCount++; + } + catch (Exception ex) + { + Console.WriteLine($" 第 {i + 1} 次操作失败: {ex.Message}"); + } + finally + { + try + { + MatrixParallel.EndParallel(); + } + catch (Exception ex) + { + Console.WriteLine($" 第 {i + 1} 次 EndParallel 失败: {ex.Message}"); + } + } + + // 每10次操作显示一次进度 + if ((i + 1) % 10 == 0) + { + Console.WriteLine($" 已完成 {i + 1}/{iterations} 次操作"); + } + } + + stopwatch.Stop(); + + Console.WriteLine($"? 重复操作测试完成:"); + Console.WriteLine($" 总操作次数: {iterations}"); + Console.WriteLine($" 成功次数: {successCount}"); + Console.WriteLine($" 成功率: {(double)successCount / iterations * 100:F1}%"); + Console.WriteLine($" 总耗时: {stopwatch.ElapsedMilliseconds} ms"); + Console.WriteLine($" 平均耗时: {stopwatch.ElapsedMilliseconds / (double)iterations:F2} ms/次"); + } + catch (Exception ex) + { + Console.WriteLine($"? 重复操作测试失败: {ex.Message}"); + } + } + + private static void TestSchedulingBasics() + { + try + { + Console.WriteLine("测试基本调度操作..."); + + // 测试设置所有 Wrap + Console.WriteLine(" 执行 SetAllWrap()..."); + MatrixParallel.SetAllWrap(); + Console.WriteLine(" ? SetAllWrap() 执行成功"); + } + catch (Exception ex) + { + Console.WriteLine($"? 基本调度操作测试失败: {ex.Message}"); + } + } + + } +} \ No newline at end of file diff --git a/StgSharpDebug/Program.cs b/StgSharpDebug/Program.cs index 4f561b53..3d550487 100644 --- a/StgSharpDebug/Program.cs +++ b/StgSharpDebug/Program.cs @@ -57,16 +57,19 @@ namespace StgSharp internal static unsafe void Main(string[] args) { - World.Init(); - /**/ try { + World.Init(); + + TestMatrixParallel(); + + // 鍘熸湁鐨勭煩闃垫祴璇曚唬鐮� HybridLayerSegregatedFitAllocator hlsf = new HybridLayerSegregatedFitAllocator(64 * 400 * 400); Matrix m = Matrix.FromHlsf(400, 200, hlsf); int i = 0, ii = 0; - foreach (Matrix.Segment lines in m.GetRowEnumeration()) + foreach (Segment lines in m.GetRowEnumeration()) { i = 0; foreach (MatrixKernel* item in lines)