matrix enumeration full available

This commit is contained in:
Youmu Liu
2025-08-29 21:50:24 +08:00
parent 78f97fb314
commit 6bebe1ca85
5 changed files with 110 additions and 80 deletions
+26 -40
View File
@@ -1,33 +1,33 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
//----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="root.cs" // file="root.cs"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload Space // AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved. // Copyright (c) Nitload Space. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without restriction, // files (the “Software”), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, // including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, // publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so, // and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions: // subject to the following conditions:
// //
// The above copyright notice and // The above copyright notice and
// this permission notice shall be included in all copies // this permission notice shall be included in all copies
// or substantial portions of the Software. // or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED “AS IS”, // THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // 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, // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH // ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// //
//----------------------------------------------------------------------- // -----------------------------------------------------------------------
//----------------------------------------------------------------------- // -----------------------------------------------------------------------
using StgSharp.Internal; using StgSharp.Internal;
using System; using System;
@@ -38,19 +38,5 @@ using System.Threading.Tasks;
namespace StgSharp namespace StgSharp
{ {
public static unsafe partial class World public static unsafe partial class World { }//------------------ end of class ---------------------
{
private static byte leastUnusedArea;
private static List<Area> root = new List<Area>();
private static SSD defaultIdProvider = new SSD();
public static byte NewArea()
{
byte ret = leastUnusedArea;
leastUnusedArea++;
return ret;
}
}//------------------ end of class ---------------------
} }
+14
View File
@@ -57,6 +57,16 @@ namespace StgSharp
private static uint markCount = 0; private static uint markCount = 0;
internal static GraphicAPI API = default; 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() public static unsafe void Init()
{ {
mainTimeProvider = new StgSharpTime(); mainTimeProvider = new StgSharpTime();
@@ -84,21 +94,25 @@ namespace StgSharp
InternalIO.InternalWriteLog(ex.Message, LogType.Error); InternalIO.InternalWriteLog(ex.Message, LogType.Error);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void LogInfo(string log) public static void LogInfo(string log)
{ {
InternalIO.InternalWriteLog(log, LogType.Info); InternalIO.InternalWriteLog(log, LogType.Info);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void LogWarning(string log) public static void LogWarning(string log)
{ {
InternalIO.InternalWriteLog(log, LogType.Warning); InternalIO.InternalWriteLog(log, LogType.Warning);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void LogWarning(Exception notVerySeriesException) public static void LogWarning(Exception notVerySeriesException)
{ {
InternalIO.InternalWriteLog(notVerySeriesException.Message, LogType.Warning); InternalIO.InternalWriteLog(notVerySeriesException.Message, LogType.Warning);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Mark(bool needConsole) public static void Mark(bool needConsole)
{ {
markCount++; markCount++;
@@ -42,6 +42,8 @@ namespace StgSharp.Internal
private static readonly IntrinsicContext _intrinsicContext = new(); private static readonly IntrinsicContext _intrinsicContext = new();
public static IntrinsicLevel IntrinsicLevel { get; set; }
internal static IntrinsicContext Intrinsic internal static IntrinsicContext Intrinsic
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -64,6 +66,7 @@ namespace StgSharp.Internal
Environment.Exit(-1); Environment.Exit(-1);
break; break;
default: default:
IntrinsicLevel = (IntrinsicLevel)statCode;
break; break;
} }
} }
@@ -73,4 +76,15 @@ namespace StgSharp.Internal
private static partial int LoadIntrinsicFunction(IntrinsicContext* context); private static partial int LoadIntrinsicFunction(IntrinsicContext* context);
} }
internal enum IntrinsicLevel : sbyte
{
Non = 0,
SSE = 1,
AVX2 = 2,
AVX512 = 3,
NEON = -1
}
} }
+53 -38
View File
@@ -32,6 +32,7 @@ using StgSharp.HighPerformance;
using System; using System;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text;
using hlsfAllocator = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator; using hlsfAllocator = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator;
using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle; using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
@@ -57,18 +58,45 @@ namespace StgSharp.Mathematics
set; 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<T> 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<T> FromGC(int columnLength, int rowLength)
{
return new GCMatrix<T>(columnLength, rowLength);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix<T> FromHlsf(int column, int row, hlsfAllocator allocator) public static Matrix<T> FromHlsf(int columnLength, int rowLength, hlsfAllocator allocator)
{ {
return new HlsfMatrix<T>(column, row, allocator); return new HlsfMatrix<T>(columnLength, rowLength, allocator);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -86,66 +114,61 @@ namespace StgSharp.Mathematics
private protected void AssertIndex(int column, int row) private protected void AssertIndex(int column, int row)
{ {
if (column < 0 || column >= ColumnCount) { if (column < 0 || column >= ColumnLength) {
throw new ArgumentOutOfRangeException(nameof(column)); throw new ArgumentOutOfRangeException(nameof(column));
} }
if (row < 0 || row >= RowCount) { if (row < 0 || row >= RowLength) {
throw new ArgumentOutOfRangeException(nameof(row)); throw new ArgumentOutOfRangeException(nameof(row));
} }
} }
public readonly unsafe ref struct MatrixSegmentEnumeration( public readonly unsafe ref struct MatrixSegmentEnumeration(
MatrixKernel* source, MatrixKernel* source,
int size,
int stride, int stride,
int secondaryStride) int size,
int secondaryStride,
int secondarySize)
{ {
private readonly MatrixKernel* _source = source; private readonly MatrixKernel* _source = source;
private readonly int _secondarySize = secondarySize;
private readonly int _secondaryStride = secondaryStride; private readonly int _secondaryStride = secondaryStride;
private readonly int _size = size; private readonly int _size = size;
private readonly int _stride = stride; private readonly int _stride = stride;
public Enumerator GetEnumerator() 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 MatrixKernel* _current = enumeration._source - enumeration._stride;
private readonly MatrixKernel* _end = source + end; private readonly MatrixKernel* _end = enumeration._source + enumeration._size;
private readonly MatrixKernel* _source = source; private readonly int _secondarySize = enumeration._secondarySize;
private readonly int _secondaryStride = secondaryStride; private readonly int _secondaryStride = enumeration._secondaryStride;
private readonly int _size = size; private readonly int _stride = enumeration._stride;
private readonly int _stride = stride;
public readonly MatrixSegment Current public readonly Segment Current
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(_current, _size, _secondaryStride); get => new(_current, _secondarySize, _secondaryStride);
} }
public readonly void Dispose() { }
public bool MoveNext() public bool MoveNext()
{ {
_current += _stride; _current += _stride;
return _current < _end; 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; private readonly MatrixKernel* _source = source;
@@ -168,11 +191,10 @@ namespace StgSharp.Mathematics
return new Enumerator(_source, _size, _stride); 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* _end = source + (count * stride);
private readonly MatrixKernel* _source = source;
private readonly int _stride = stride; private readonly int _stride = stride;
public MatrixKernel* Current public MatrixKernel* Current
@@ -183,19 +205,12 @@ namespace StgSharp.Mathematics
private set; private set;
} = source - stride; } = source - stride;
public void Dispose() { }
public bool MoveNext() public bool MoveNext()
{ {
Current += _stride; Current += _stride;
return Current < _end; return Current < _end;
} }
public void Reset()
{
Current = _source - _stride;
}
} }
} }
+3 -2
View File
@@ -63,11 +63,12 @@ namespace StgSharp
{ {
HybridLayerSegregatedFitAllocator hlsf = new HybridLayerSegregatedFitAllocator(64 * 400 * 400); HybridLayerSegregatedFitAllocator hlsf = new HybridLayerSegregatedFitAllocator(64 * 400 * 400);
Matrix<float> m = Matrix<float>.FromHlsf(400, 400, hlsf); Matrix<float> m = Matrix<float>.FromHlsf(400, 200, hlsf);
int i = 0, ii = 0; int i = 0, ii = 0;
foreach (Matrix<float>.MatrixSegment lines in m.GetColumnEnumeration()) foreach (Matrix<float>.Segment lines in m.GetRowEnumeration())
{ {
i = 0;
foreach (MatrixKernel* item in lines) foreach (MatrixKernel* item in lines)
{ {
i++; i++;