From 838ce73133c8fd691719a46ab17e6a50ed22e301 Mon Sep 17 00:00:00 2001 From: Youmu Liu <105042599+Nitload-NSI@users.noreply.github.com> Date: Mon, 20 Oct 2025 20:36:20 +0800 Subject: [PATCH] Brief solution for publishing, debuging --- .../Numeric/MatrixParallel.Execute.cs | 43 +++++++-------- .../Numeric/MatrixParallel.Publish.cs | 53 ++++++++++++------- .../Numeric/MatrixParallel.cs | 6 ++- 3 files changed, 60 insertions(+), 42 deletions(-) diff --git a/StgSharp.Mathematics/Numeric/MatrixParallel.Execute.cs b/StgSharp.Mathematics/Numeric/MatrixParallel.Execute.cs index 9a8197a0..b9e4dc90 100644 --- a/StgSharp.Mathematics/Numeric/MatrixParallel.Execute.cs +++ b/StgSharp.Mathematics/Numeric/MatrixParallel.Execute.cs @@ -1,30 +1,27 @@ //----------------------------------------------------------------------- // ----------------------------------------------------------------------- -// file="MatrixParallel.Execute.cs" +// file="MatrixParallel.Execute" // Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. +// AuthorGroup: Nitload +// Copyright (c) Nitload. 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. +// 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. // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- diff --git a/StgSharp.Mathematics/Numeric/MatrixParallel.Publish.cs b/StgSharp.Mathematics/Numeric/MatrixParallel.Publish.cs index 68e1f193..6f54e56c 100644 --- a/StgSharp.Mathematics/Numeric/MatrixParallel.Publish.cs +++ b/StgSharp.Mathematics/Numeric/MatrixParallel.Publish.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="MatrixParallel.Publish" // Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. +// AuthorGroup: Nitload +// Copyright (c) Nitload. 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 @@ -812,7 +812,7 @@ namespace StgSharp.Mathematics.Numeric PublicTaskPrivate(package); } - private static unsafe void PublicTaskPrivate(MatrixParallelTaskPackage* package) + private static unsafe Stack PublicTaskPrivate(MatrixParallelTaskPackage* package) where T: unmanaged, INumber { int primCount = package->PrimCount; @@ -822,25 +822,42 @@ namespace StgSharp.Mathematics.Numeric CapacityFixedStack wraps = LeadParallel(); - int tilePerThreadLess = tileCount / Wraps.Count; - int tileToSlice = tileCount % Wraps.Count; - int slicingPerWrap = tileToSlice / Wraps.Count; - int extraTiles = tileToSlice % Wraps.Count; + int tilePerThreadLess = tileCount / wraps.Count; + int tileToSlice = tileCount % wraps.Count; + int slicingPerWrap = tileToSlice / wraps.Count; + int extraTiles = tileToSlice % wraps.Count; int total = tileCount; - List w = new List(Wraps.Count); - - int baseTile = 0; - - // TODO 干脆用四个掩码分别表示各种部分算了。 - for (int i = 0; i < Wraps.Count; i++) + int begin = 0; + Stack taskBunch = new Stack(wraps.Count); + while (tileCount > 0) { - MatrixParallelWrap wrap = Wraps.Pop(); - wrap.WrapTask = (MatrixParallelTaskPackageNonGeneric*)package; - wrap.WrapTaskCapacity = tilePerThreadLess + (i < extraTiles ? 1 : 0); - wrap.WrapTaskSliceCount = (i * tilePerThreadLess) + Math.Min(i, extraTiles); - w.Add(wrap); + MatrixParallelWrap wrap = wraps.Pop(); + bool hasSlice = tileToSlice > 0; + bool isLargeSlice = extraTiles > 0; + + // count tiles for wrap + int toSlice = wrap.WrapTaskCapacity * slicingPerWrap; + int baseTile = wrap.WrapTaskCapacity * tilePerThreadLess; + int tileInWrap = baseTile + toSlice + (isLargeSlice ? 1 : 0); + int columnInWrap = tileInWrap * tileWidth; + + // set tasks + MatrixParallelTaskPackage* task = MatrixParallelFactory.FromExistPackage(package); + wrap.WrapTask = (MatrixParallelTaskPackageNonGeneric*)task; + task->LeftPrimOffset += columnInWrap; + task->PrimCount = columnInWrap; + + // remove tiles count + extraTiles--; + tileToSlice -= toSlice; + tileCount -= tileInWrap; + begin += tileInWrap; + taskBunch.Push(wrap); } + ReleaseParallelLeadership(ref wraps); + + return taskBunch; } } diff --git a/StgSharp.Mathematics/Numeric/MatrixParallel.cs b/StgSharp.Mathematics/Numeric/MatrixParallel.cs index f4e2d47c..8a3d1df9 100644 --- a/StgSharp.Mathematics/Numeric/MatrixParallel.cs +++ b/StgSharp.Mathematics/Numeric/MatrixParallel.cs @@ -269,7 +269,11 @@ namespace StgSharp.Mathematics.Numeric internal int WrapTaskSliceCount { get; set; } - internal int WrapTaskCapacity { get; set; } + internal int WrapTaskCapacity + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => _threads.Length; + } #endregion