diff --git a/StgSharp.Common/Collections/NotEnumerableList.cs b/StgSharp.Common/Collections/LinearBag.cs similarity index 71% rename from StgSharp.Common/Collections/NotEnumerableList.cs rename to StgSharp.Common/Collections/LinearBag.cs index 2f5e0513..7c9fa99d 100644 --- a/StgSharp.Common/Collections/NotEnumerableList.cs +++ b/StgSharp.Common/Collections/LinearBag.cs @@ -1,30 +1,27 @@ //----------------------------------------------------------------------- // ----------------------------------------------------------------------- -// file="NotEnumerableList.cs" +// file="LinearBag" // 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. // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- @@ -41,7 +38,9 @@ namespace StgSharp.Collections /// Build a from a read-only span. Capacity will exactly fit /// the input. /// - public static LinearBag FromSpan(ReadOnlySpan values) + public static LinearBag Create( + ReadOnlySpan values + ) { if (values.IsEmpty) { return new LinearBag(4); @@ -63,8 +62,10 @@ namespace StgSharp.Collections /// /// Element type. /// - [CollectionBuilder(typeof(LinearBagBuilder), nameof(LinearBagBuilder.FromSpan))] - public class LinearBag(int capacity = 4) + [CollectionBuilder(typeof(LinearBagBuilder), nameof(LinearBagBuilder.Create))] + public class LinearBag( + int capacity = 4 + ) { // Invariant: valid elements occupy indexes [0, Count-1]. Count is the number of live elements. @@ -73,17 +74,20 @@ namespace StgSharp.Collections /// /// Access element by index (0..Count-1). Throws if out of range. /// - public T this[int index] + public T this[ + int index + ] { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => (uint)index >= (uint)Count ? throw new ArgumentOutOfRangeException(nameof(index)) : _items[index]; + get + { + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); + return _items[index]; + } [MethodImpl(MethodImplOptions.AggressiveInlining)] set { - if ((uint)index >= (uint)Count) { - throw new ArgumentOutOfRangeException(nameof(index)); - } - + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count); _items[index] = value; } } @@ -102,7 +106,9 @@ namespace StgSharp.Collections /// Adds an item (unordered). Expands internal array if needed. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Add(T item) + public void Add( + T item + ) { if (Count == _items.Length) { EnsureCapacity(Count + 1); @@ -144,7 +150,9 @@ namespace StgSharp.Collections /// /// Removes the first occurrence of value (unordered). Does nothing if not found. /// - public void Remove(T value) + public void Remove( + T value + ) { EqualityComparer comparer = EqualityComparer.Default; for (int i = 0; i < Count; i++) @@ -162,7 +170,9 @@ namespace StgSharp.Collections /// last). Preserves O(1) removal at cost of ordering. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void RemoveAt(int index) + public void RemoveAt( + int index + ) { if ((uint)index >= (uint)Count) { throw new ArgumentOutOfRangeException(nameof(index)); @@ -181,13 +191,15 @@ namespace StgSharp.Collections /// Ensures capacity is at least . Doubles size when growing. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void EnsureCapacity(int min) + private void EnsureCapacity( + int min + ) { if (_items.Length >= min) { return; } - int newCapacity = _items.Length == 0 ? 4 : _items.Length * 2; + int newCapacity = (_items.Length == 0) ? 4 : (_items.Length * 2); if (newCapacity < min) { newCapacity = min; } diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixOperatorPolicy.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixOperatorPolicy.cs index b00db5ce..807a11c3 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixOperatorPolicy.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixOperatorPolicy.cs @@ -1,30 +1,27 @@ //----------------------------------------------------------------------- // ----------------------------------------------------------------------- -// file="MatrixOperatorPolicy.cs" +// file="MatrixOperatorPolicy" // 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. // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- @@ -36,7 +33,7 @@ using System.Threading.Tasks; namespace StgSharp.Mathematics.Numeric.Runtime { - internal enum MatrixCachePolicy + internal enum MatrixCachePolicy : short { None = 0, diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Numa.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Numa.cs index 57a1cd39..6219b9e7 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Numa.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Numa.cs @@ -25,6 +25,7 @@ // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- +using StgSharp.Collections; using StgSharp.HighPerformance.Memory; using System; using System.Collections.Generic; @@ -41,7 +42,8 @@ namespace StgSharp.Mathematics.Numeric private static NumaMeasurement[] _numaMeasurements; private static int[] _numaPayload; - private static MatrixParallelThread[][] _numaThreads; + private static LinearBag[] _numaSyncHeavy; + private static LinearBag[] _numaWaveFront; private static MatrixParallelThread[] _threads; private static void UpdateThreadPayload() diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Publish.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Publish.cs index c098c53c..033ee528 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Publish.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Publish.cs @@ -65,12 +65,12 @@ namespace StgSharp.Mathematics.Numeric ulong additionalPayload = 0; for (int i = 0; i < _numaMeasurements.Length; i++) { - MatrixParallelThread[] numa = _numaThreads[i]; + MatrixParallelThread[] numa = _numaWaveFront[i]; if ((numa is null) || (numa.Length == 0)) { continue; } - ulong total = (ulong)_numaThreads[i].Length; + ulong total = (ulong)_numaWaveFront[i].Length; NumaMeasurement measure = _numaMeasurements[i]; ulong numaPayload = measure.ActiveTasks; uint numaActive = measure.ActiveThreadCount; @@ -82,7 +82,7 @@ namespace StgSharp.Mathematics.Numeric } } L4 cache = _numaBuffer[payloadIndex]; - MatrixParallelThread[] selectedNuma = _numaThreads[payloadIndex]; + MatrixParallelThread[] selectedNuma = _numaWaveFront[payloadIndex]; throw new NotImplementedException(); } diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.cs index f0dbddb3..2f8daace 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.cs @@ -84,7 +84,7 @@ namespace StgSharp.Mathematics.Numeric _retiredThreads = new(RESERVED_THREAD_COUNT); // init thread on all logical cores - _numaThreads = Numa.CreateForeachThread( + MatrixParallelThread[][] numaWaveOrigin = Numa.CreateForeachThread( core => { MatrixParallelThread thread = new(core); SpinWait.SpinUntil(() => thread.State == MatrixThreadState.Ready); @@ -108,15 +108,15 @@ namespace StgSharp.Mathematics.Numeric // retire threads if (ContextCount == 1) { - MatrixParallelThread[] threadArr = _numaThreads[0]; + MatrixParallelThread[] threadArr = numaWaveOrigin[0]; int retireCount = Math.Min(RESERVED_THREAD_COUNT, Math.Max(0, threadArr.Length)); for (int i = 1; i <= retireCount; i++) { RetireThread(threadArr[^i]); } - _numaThreads[0] = threadArr[0..^retireCount]; + _numaWaveFront[0] = [.. threadArr[0..^retireCount]]; } else { - RetireWhenMultipleSIMDID(); + RetireWhenMultipleSIMDID(numaWaveOrigin); } _meaningful = _threads.Length > 1; @@ -134,11 +134,13 @@ namespace StgSharp.Mathematics.Numeric _retiredThreads.Push(thread); } - private static void RetireWhenMultipleSIMDID() + private static void RetireWhenMultipleSIMDID( + MatrixParallelThread[][] threads + ) { MatrixParallelThread[] allThreads = new MatrixParallelThread[Environment.ProcessorCount]; int offset = 0; - foreach (MatrixParallelThread[] threadArr in _numaThreads) + foreach (MatrixParallelThread[] threadArr in threads) { Array.Copy(threadArr, 0, allThreads, offset, threadArr.Length); offset += threadArr.Length; @@ -159,9 +161,9 @@ namespace StgSharp.Mathematics.Numeric } // rebuild numa active arrays - for (int numa = 0; numa < _numaThreads.Length; numa++) + for (int numa = 0; numa < _numaWaveFront.Length; numa++) { - MatrixParallelThread[] source = _numaThreads[numa]; + MatrixParallelThread[] source = threads[numa]; int keepCount = 0; for (int i = 0; i < source.Length; i++) @@ -181,7 +183,7 @@ namespace StgSharp.Mathematics.Numeric } } - _numaThreads[numa] = active; + _numaWaveFront[numa] = [.. active]; } _threads = allThreads[retireCount..]; diff --git a/StgSharp.Native/sn-head/sn_intrinsic.std.h b/StgSharp.Native/sn-head/sn_intrinsic.std.h index 37124ffb..046c1ce0 100644 --- a/StgSharp.Native/sn-head/sn_intrinsic.std.h +++ b/StgSharp.Native/sn-head/sn_intrinsic.std.h @@ -17,18 +17,18 @@ #define GET_MK_PROC_STD(T_type, T_arch, T_feature, T_op) \ ((UNI_MK_PROC)MK_PROC_NAME_STD(T_type, T_arch, T_feature, T_op)) #define SN_MK_PROC_DECL(T_type, P_name) INTERNAL void SN_DECL P_name(MAT_TASK(T_type) const *task) -#define SN_MK_PROC_DECL_STD(T_type, T_arch, T_feature, T_op) \ - INTERNAL NOINLINE SN_STD_TARGET_ATTR(T_arch, T_feature) void SN_DECL \ - MK_PROC_NAME_STD(T_type, T_arch, T_feature, T_op)(MAT_TASK(T_type) const *task) +#define SN_MK_PROC_DECL_STD(T_type, T_arch, T_feature, T_op) \ + INTERNAL NOINLINE SN_STD_TARGET_ATTR(T_arch, T_feature) void SN_DECL MK_PROC_NAME_STD( \ + T_type, T_arch, T_feature, T_op)(MAT_TASK(T_type) const *task) #define SK_PROC_NAME_STD(T_type, T_arch, T_feature, T_op) \ T_type##_single_kernel_proc_##T_op##_##T_arch##_##T_feature #define GET_SK_PROC_STD(T_type, T_arch, T_feature, T_op) \ ((UNI_SK_PROC)SK_PROC_NAME_STD(T_type, T_arch, T_feature, T_op)) -#define SN_SK_PROC_DECL_STD(T_type, T_arch, T_feature, T_op) \ - INTERNAL NOINLINE SN_STD_TARGET_ATTR(T_arch, T_feature) void SN_DECL \ - SK_PROC_NAME_STD(T_type, T_arch, T_feature, T_op)( \ - MAT_KERNEL(T_type) const *left, MAT_KERNEL(T_type) const *right, \ - MAT_KERNEL(T_type) *restrict ans, uint64_t scalar) +#define SN_SK_PROC_DECL_STD(T_type, T_arch, T_feature, T_op) \ + INTERNAL NOINLINE SN_STD_TARGET_ATTR(T_arch, T_feature) void SN_DECL SK_PROC_NAME_STD( \ + T_type, T_arch, T_feature, \ + T_op)(MAT_KERNEL(T_type) const *left, MAT_KERNEL(T_type) const *right, \ + MAT_KERNEL(T_type) *restrict ans, uint64_t scalar) #define _SN_STD__PROBE_0 0 #define _SN_STD__PROBE_1 1 @@ -43,27 +43,27 @@ #define _SN_MATPROC_RIGHT_ANS_SCALAR 1 #define _SN_MATPROC_ANS_SCALAR 1 -#define _SN_MK_EXPAND_RIGHT_ANS \ +#define _SN_MK_EXPAND_RIGHT_ANS \ SN_MAT_KERNEL_LOCAL const *right = (SN_MAT_KERNEL_LOCAL const *)task->mat_1; \ SN_MAT_KERNEL_LOCAL *restrict ans = (SN_MAT_KERNEL_LOCAL *)task->mat_2; -#define _SN_MK_EXPAND_LEFT_RIGHT_ANS \ +#define _SN_MK_EXPAND_LEFT_RIGHT_ANS \ SN_MAT_KERNEL_LOCAL const *left = (SN_MAT_KERNEL_LOCAL const *)task->mat_0; \ SN_MAT_KERNEL_LOCAL const *right = (SN_MAT_KERNEL_LOCAL const *)task->mat_1; \ SN_MAT_KERNEL_LOCAL *restrict ans = (SN_MAT_KERNEL_LOCAL *)task->mat_2; -#define _SN_MK_EXPAND_RIGHT_ANS_SCALAR \ - SN_MAT_KERNEL_LOCAL const *right = (SN_MAT_KERNEL_LOCAL const *)task->mat_1; \ - SN_MAT_KERNEL_LOCAL *restrict ans = (SN_MAT_KERNEL_LOCAL *)task->mat_2; \ +#define _SN_MK_EXPAND_RIGHT_ANS_SCALAR \ + SN_MAT_KERNEL_LOCAL const *right = (SN_MAT_KERNEL_LOCAL const *)task->mat_1; \ + SN_MAT_KERNEL_LOCAL *restrict ans = (SN_MAT_KERNEL_LOCAL *)task->mat_2; \ scalar_pack *restrict scalar = (scalar_pack *restrict)task->scalar; -#define _SN_MK_EXPAND_LEFT_RIGHT_ANS_SCALAR \ - SN_MAT_KERNEL_LOCAL const *left = (SN_MAT_KERNEL_LOCAL const *)task->mat_0; \ - SN_MAT_KERNEL_LOCAL const *right = (SN_MAT_KERNEL_LOCAL const *)task->mat_1; \ - SN_MAT_KERNEL_LOCAL *restrict ans = (SN_MAT_KERNEL_LOCAL *)task->mat_2; \ +#define _SN_MK_EXPAND_LEFT_RIGHT_ANS_SCALAR \ + SN_MAT_KERNEL_LOCAL const *left = (SN_MAT_KERNEL_LOCAL const *)task->mat_0; \ + SN_MAT_KERNEL_LOCAL const *right = (SN_MAT_KERNEL_LOCAL const *)task->mat_1; \ + SN_MAT_KERNEL_LOCAL *restrict ans = (SN_MAT_KERNEL_LOCAL *)task->mat_2; \ scalar_pack *restrict scalar = (scalar_pack *restrict)task->scalar; -#define _SN_MK_EXPAND_ANS_SCALAR \ +#define _SN_MK_EXPAND_ANS_SCALAR \ SN_MAT_KERNEL_LOCAL *restrict ans = (SN_MAT_KERNEL_LOCAL *)task->mat_2; \ scalar_pack *restrict scalar = (scalar_pack *restrict)&(task->padding); @@ -74,9 +74,14 @@ #pragma region mem layout +#define _SN_MATPROC_LAYOUT_BUFFER 1 #define _SN_MATPROC_LAYOUT_TILE 1 #define _SN_MATPROC_LAYOUT_KERNEL 1 +#define _MK_MKPROC_EXPAND_INDEX_BUFFER \ + size_t offset = UNSAFE_AS(size_t, task->padding.data[0]); \ + size_t count = UNSAFE_AS(size_t, task->padding.data[1]); + #define _MK_MKPROC_EXPAND_INDEX_BUFFER \ size_t offset = UNSAFE_AS(size_t, task->padding.data[0]); \ size_t count = UNSAFE_AS(size_t, task->padding.data[1]); @@ -99,13 +104,13 @@ #pragma endregion -#define __mk_param_std(mem, param, T) \ - typedef MAT_KERNEL(T) SN_MAT_KERNEL_LOCAL; \ +#define __mk_param_std(mem, param, T) \ + typedef MAT_KERNEL(T) SN_MAT_KERNEL_LOCAL; \ matrix_task const *_ = (_SN_PARAM_STYLE(param), (_SN_MEM_LAYOUT(mem)), task); \ - _SN_MK_EXPAND_PARAM(param); \ + _SN_MK_EXPAND_PARAM(param); \ _SN_MK_EXPAND_MEM(mem) -#define __sk_param_std(T) \ +#define __sk_param_std(T) \ typedef MAT_KERNEL(T) SN_MAT_KERNEL_LOCAL; \ (void)scalar #endif // !SN_INTRINSIC_PARAM_EXPAND diff --git a/StgSharp.UserInterface/basic-arch.drawio b/StgSharp.UserInterface/basic-arch.drawio index 31ff810e..0901ed8c 100644 --- a/StgSharp.UserInterface/basic-arch.drawio +++ b/StgSharp.UserInterface/basic-arch.drawio @@ -1,37 +1,34 @@ - + - + - - - - + - + - + - - + + @@ -54,11 +51,12 @@ - + - - + + + @@ -69,7 +67,7 @@ - + @@ -77,30 +75,30 @@ - + - + - - + - - + + + - + @@ -216,34 +214,36 @@ - + - - + + + - + - + - + - + + - + @@ -298,13 +298,16 @@ - + - + - + + + + @@ -335,12 +338,12 @@ - + - - + +