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
+4 -18
View File
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="root.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
@@ -26,8 +26,8 @@
// 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<Area> root = new List<Area>();
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 ---------------------
}
+14
View File
@@ -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++;
@@ -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
}
}
+53 -38
View File
@@ -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<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)]
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)]
@@ -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;
}
}
}
+3 -2
View File
@@ -63,11 +63,12 @@ namespace StgSharp
{
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;
foreach (Matrix<float>.MatrixSegment lines in m.GetColumnEnumeration())
foreach (Matrix<float>.Segment lines in m.GetRowEnumeration())
{
i = 0;
foreach (MatrixKernel* item in lines)
{
i++;