From 6bebe1ca850f554aeb255103898ee4b3483fbb53 Mon Sep 17 00:00:00 2001
From: Youmu Liu <105042599+Nitload-NSI@users.noreply.github.com>
Date: Fri, 29 Aug 2025 21:50:24 +0800
Subject: [PATCH] matrix enumeration full available
---
StgSharp.Common/Data/GlobalData/root.cs | 66 ++++++--------
StgSharp.Common/Environment/Platform.cs | 14 +++
.../Intrinsic/intrinsicContextProvider.cs | 14 +++
StgSharp.Common/Mathematics/Linear/Matrix.cs | 91 +++++++++++--------
StgSharpDebug/Program.cs | 5 +-
5 files changed, 110 insertions(+), 80 deletions(-)
diff --git a/StgSharp.Common/Data/GlobalData/root.cs b/StgSharp.Common/Data/GlobalData/root.cs
index a68410c8..93d7019d 100644
--- a/StgSharp.Common/Data/GlobalData/root.cs
+++ b/StgSharp.Common/Data/GlobalData/root.cs
@@ -1,33 +1,33 @@
//-----------------------------------------------------------------------
-//-----------------------------------------------------------------------
-// file="root.cs"
-// Project: StgSharp
-// AuthorGroup: Nitload Space
-// Copyright (c) Nitload Space. All rights reserved.
+// -----------------------------------------------------------------------
+// file="root.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:
+// 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 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.
+// 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.Internal;
using System;
@@ -38,19 +38,5 @@ using System.Threading.Tasks;
namespace StgSharp
{
- public static unsafe partial class World
- {
-
- private static byte leastUnusedArea;
- private static List root = new List();
- private static SSD defaultIdProvider = new SSD();
-
- public static byte NewArea()
- {
- byte ret = leastUnusedArea;
- leastUnusedArea++;
- return ret;
- }
-
- }//------------------ end of class ---------------------
+ public static unsafe partial class World { }//------------------ end of class ---------------------
}
diff --git a/StgSharp.Common/Environment/Platform.cs b/StgSharp.Common/Environment/Platform.cs
index ab41fb00..31a7301f 100644
--- a/StgSharp.Common/Environment/Platform.cs
+++ b/StgSharp.Common/Environment/Platform.cs
@@ -57,6 +57,16 @@ namespace StgSharp
private static uint markCount = 0;
internal static GraphicAPI API = default;
+ public static int DefaultSIMDAlignment => InternalIO.IntrinsicLevel switch
+ {
+ IntrinsicLevel.SSE => 16,
+ IntrinsicLevel.AVX2 => 32,
+ IntrinsicLevel.AVX512 => 64,
+ IntrinsicLevel.NEON => 16,
+ IntrinsicLevel.Non => 8,
+ _ => 8,
+ };
+
public static unsafe void Init()
{
mainTimeProvider = new StgSharpTime();
@@ -84,21 +94,25 @@ namespace StgSharp
InternalIO.InternalWriteLog(ex.Message, LogType.Error);
}
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void LogInfo(string log)
{
InternalIO.InternalWriteLog(log, LogType.Info);
}
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void LogWarning(string log)
{
InternalIO.InternalWriteLog(log, LogType.Warning);
}
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void LogWarning(Exception notVerySeriesException)
{
InternalIO.InternalWriteLog(notVerySeriesException.Message, LogType.Warning);
}
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Mark(bool needConsole)
{
markCount++;
diff --git a/StgSharp.Common/Internal/Intrinsic/intrinsicContextProvider.cs b/StgSharp.Common/Internal/Intrinsic/intrinsicContextProvider.cs
index e73f08eb..42659ab6 100644
--- a/StgSharp.Common/Internal/Intrinsic/intrinsicContextProvider.cs
+++ b/StgSharp.Common/Internal/Intrinsic/intrinsicContextProvider.cs
@@ -42,6 +42,8 @@ namespace StgSharp.Internal
private static readonly IntrinsicContext _intrinsicContext = new();
+ public static IntrinsicLevel IntrinsicLevel { get; set; }
+
internal static IntrinsicContext Intrinsic
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -64,6 +66,7 @@ namespace StgSharp.Internal
Environment.Exit(-1);
break;
default:
+ IntrinsicLevel = (IntrinsicLevel)statCode;
break;
}
}
@@ -73,4 +76,15 @@ namespace StgSharp.Internal
private static partial int LoadIntrinsicFunction(IntrinsicContext* context);
}
+
+ internal enum IntrinsicLevel : sbyte
+ {
+
+ Non = 0,
+ SSE = 1,
+ AVX2 = 2,
+ AVX512 = 3,
+ NEON = -1
+
+ }
}
\ No newline at end of file
diff --git a/StgSharp.Common/Mathematics/Linear/Matrix.cs b/StgSharp.Common/Mathematics/Linear/Matrix.cs
index 5bec2fdd..1084a5d0 100644
--- a/StgSharp.Common/Mathematics/Linear/Matrix.cs
+++ b/StgSharp.Common/Mathematics/Linear/Matrix.cs
@@ -32,6 +32,7 @@ using StgSharp.HighPerformance;
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
+using System.Text;
using hlsfAllocator = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator;
using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
@@ -57,18 +58,45 @@ namespace StgSharp.Mathematics
set;
}
- public int ColumnCount { get; init; }
+ public static hlsfAllocator? DefaultAllocator
+ {
+ get
+ {
+ ArgumentNullException.ThrowIfNull(field, nameof(DefaultAllocator));
+ return null;
+ }
+ set;
+ } = null;
- public int RowCount { get; init; }
+ public int ColumnLength { get; init; }
- protected int KernelColumnCount { get; init; }
+ public int RowLength { get; init; }
- protected int KernelRowCount { get; init; }
+ public static MatrixAllocation DefaultAllocation { get; set; } = MatrixAllocation.GC;
+
+ protected int KernelColumnLength { get; init; }
+
+ protected int KernelRowLength { get; init; }
+
+ public static Matrix FromDefault(int columnLength, int rowLength)
+ {
+ return DefaultAllocation switch
+ {
+ MatrixAllocation.GC => FromGC(columnLength, rowLength),
+ MatrixAllocation.HLSF => FromHlsf(columnLength, rowLength, DefaultAllocator!),
+ _ => throw new NotSupportedException("This allocation is not supported."),
+ };
+ }
+
+ public static Matrix FromGC(int columnLength, int rowLength)
+ {
+ return new GCMatrix(columnLength, rowLength);
+ }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Matrix FromHlsf(int column, int row, hlsfAllocator allocator)
+ public static Matrix FromHlsf(int columnLength, int rowLength, hlsfAllocator allocator)
{
- return new HlsfMatrix(column, row, allocator);
+ return new HlsfMatrix(columnLength, rowLength, allocator);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -86,66 +114,61 @@ namespace StgSharp.Mathematics
private protected void AssertIndex(int column, int row)
{
- if (column < 0 || column >= ColumnCount) {
+ if (column < 0 || column >= ColumnLength) {
throw new ArgumentOutOfRangeException(nameof(column));
}
- if (row < 0 || row >= RowCount) {
+ if (row < 0 || row >= RowLength) {
throw new ArgumentOutOfRangeException(nameof(row));
}
}
public readonly unsafe ref struct MatrixSegmentEnumeration(
MatrixKernel* source,
- int size,
int stride,
- int secondaryStride)
+ int size,
+ int secondaryStride,
+ int secondarySize)
{
private readonly MatrixKernel* _source = source;
+ private readonly int _secondarySize = secondarySize;
private readonly int _secondaryStride = secondaryStride;
+
private readonly int _size = size;
private readonly int _stride = stride;
public Enumerator GetEnumerator()
{
- return new Enumerator(_source, _size, _stride, _size, _secondaryStride);
+ return new Enumerator(in this);
}
- public ref struct Enumerator(MatrixKernel* source, int end, int stride, int size, int secondaryStride)
+ public ref struct Enumerator(in MatrixSegmentEnumeration enumeration)
{
- private MatrixKernel* _current = source - stride;
- private readonly MatrixKernel* _end = source + end;
- private readonly MatrixKernel* _source = source;
- private readonly int _secondaryStride = secondaryStride;
- private readonly int _size = size;
- private readonly int _stride = stride;
+ private MatrixKernel* _current = enumeration._source - enumeration._stride;
+ private readonly MatrixKernel* _end = enumeration._source + enumeration._size;
+ private readonly int _secondarySize = enumeration._secondarySize;
+ private readonly int _secondaryStride = enumeration._secondaryStride;
+ private readonly int _stride = enumeration._stride;
- public readonly MatrixSegment Current
+ public readonly Segment Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- get => new(_current, _size, _secondaryStride);
+ get => new(_current, _secondarySize, _secondaryStride);
}
- public readonly void Dispose() { }
-
public bool MoveNext()
{
_current += _stride;
return _current < _end;
}
- public void Reset()
- {
- _current = _source - _stride;
- }
-
}
}
- public readonly unsafe ref struct MatrixSegment(MatrixKernel* source, int size, int stride)
+ public readonly unsafe ref struct Segment(MatrixKernel* source, int size, int stride)
{
private readonly MatrixKernel* _source = source;
@@ -168,11 +191,10 @@ namespace StgSharp.Mathematics
return new Enumerator(_source, _size, _stride);
}
- public ref struct Enumerator(MatrixKernel* source, int end, int stride)
+ public ref struct Enumerator(MatrixKernel* source, int count, int stride)
{
- private readonly MatrixKernel* _end = source + end;
- private readonly MatrixKernel* _source = source;
+ private readonly MatrixKernel* _end = source + (count * stride);
private readonly int _stride = stride;
public MatrixKernel* Current
@@ -183,19 +205,12 @@ namespace StgSharp.Mathematics
private set;
} = source - stride;
- public void Dispose() { }
-
public bool MoveNext()
{
Current += _stride;
return Current < _end;
}
- public void Reset()
- {
- Current = _source - _stride;
- }
-
}
}
diff --git a/StgSharpDebug/Program.cs b/StgSharpDebug/Program.cs
index d0ded01c..4f561b53 100644
--- a/StgSharpDebug/Program.cs
+++ b/StgSharpDebug/Program.cs
@@ -63,11 +63,12 @@ namespace StgSharp
{
HybridLayerSegregatedFitAllocator hlsf = new HybridLayerSegregatedFitAllocator(64 * 400 * 400);
- Matrix m = Matrix.FromHlsf(400, 400, hlsf);
+ Matrix m = Matrix.FromHlsf(400, 200, hlsf);
int i = 0, ii = 0;
- foreach (Matrix.MatrixSegment lines in m.GetColumnEnumeration())
+ foreach (Matrix.Segment lines in m.GetRowEnumeration())
{
+ i = 0;
foreach (MatrixKernel* item in lines)
{
i++;