Daily update on 20250921-173333 by Nitload Youmu. Additional Message : New design of matrix enumeration, capable for parallel task publication.

Signed-off-by: Nitload <YoumuLiu_NSI@whv0t.onmicrosoft.com>
This commit is contained in:
Nitload
2025-09-21 17:33:33 +08:00
parent 0404ecf044
commit b8a51c13dd
15 changed files with 463 additions and 304 deletions
@@ -1,33 +1,33 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="RandomAccessibleStack.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// -----------------------------------------------------------------------
// file="RandomAccessibleStack.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 System;
using System.Collections;
using System.Collections.Generic;
@@ -60,11 +60,11 @@ namespace StgSharp.Collections
/// Initializes a new instance of the RandomAccessStack class that is empty and has the
/// specified initial capacity.
/// </summary>
public RandomAccessibleStack( int capacity )
public RandomAccessibleStack(int capacity)
{
if( capacity < 0 ) {
if (capacity < 0) {
throw new ArgumentOutOfRangeException(
nameof( capacity ), "Capacity must be non-negative." );
nameof(capacity), "Capacity must be non-negative.");
}
_array = new T[capacity];
@@ -74,20 +74,20 @@ namespace StgSharp.Collections
/// Gets or sets the element at the specified index in the stack (0 = bottom, Count-1 =
/// top).
/// </summary>
public T this[ int index ]
public T this[int index]
{
get
{
if( index < 0 || index >= _size ) {
throw new ArgumentOutOfRangeException( nameof( index ) );
if (index < 0 || index >= _size) {
throw new ArgumentOutOfRangeException(nameof(index));
}
return _array[ index ];
return _array[index];
}
/*
set
{
if( index < 0 || index >= _size ) {
if( index < 0 || index >= PrimSize ) {
throw new ArgumentOutOfRangeException( nameof( index ) );
}
_array[ index ] = value;
@@ -106,7 +106,7 @@ namespace StgSharp.Collections
/// </summary>
public void Clear()
{
Array.Clear( _array, 0, _size );
Array.Clear(_array, 0, _size);
_size = 0;
_version++;
}
@@ -118,14 +118,14 @@ namespace StgSharp.Collections
public IEnumerator<T> GetEnumerator()
{
int expectedVersion = _version;
for( int i = _size - 1; i >= 0; i-- )
for (int i = _size - 1; i >= 0; i--)
{
if( expectedVersion != _version ) {
if (expectedVersion != _version) {
throw new InvalidOperationException(
"Collection was modified during enumeration." );
"Collection was modified during enumeration.");
}
yield return _array[ i ];
yield return _array[i];
}
}
@@ -134,11 +134,11 @@ namespace StgSharp.Collections
/// </summary>
public T Peek()
{
if( _size == 0 ) {
throw new InvalidOperationException( "Stack is empty." );
if (_size == 0) {
throw new InvalidOperationException("Stack is empty.");
}
return _array[ _size - 1 ];
return _array[_size - 1];
}
/// <summary>
@@ -146,12 +146,12 @@ namespace StgSharp.Collections
/// </summary>
public T Pop()
{
if( _size == 0 ) {
throw new InvalidOperationException( "Stack is empty." );
if (_size == 0) {
throw new InvalidOperationException("Stack is empty.");
}
T item = _array[ --_size ];
_array[ _size ] = default!;
T item = _array[--_size];
_array[_size] = default!;
_version++;
return item;
}
@@ -159,24 +159,24 @@ namespace StgSharp.Collections
/// <summary>
/// Adds an item to the top of the stack.
/// </summary>
public void Push( T item )
public void Push(T item)
{
if( _size == _array.Length ) {
EnsureCapacity( _size + 1 );
if (_size == _array.Length) {
EnsureCapacity(_size + 1);
}
_array[ _size++ ] = item;
_array[_size++] = item;
_version++;
}
private void EnsureCapacity( int min )
private void EnsureCapacity(int min)
{
int newCapacity = _array.Length == 0 ? DefaultCapacity : _array.Length * 2;
if( newCapacity < min ) {
if (newCapacity < min) {
newCapacity = min;
}
Array.Resize( ref _array, newCapacity );
Array.Resize(ref _array, newCapacity);
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
@@ -59,27 +59,26 @@ namespace StgSharp.HighPerformance.Memory
/// Thrown when requesting unsupported configurations.
/// </exception>
/// <remarks>
/// <para><strong> Important Threading Considerations: </strong></para> <para>While SLAB
/// /// allocators themselves may be designed for single-threaded use, allocated objects
/// are /// frequently shared across multiple threads in real-world applications. Due
/// to the /// special implementation of expandable concurrent sequential SLAB
/// allocators, they rely /// on internal expansion locks to ensure that memory buffer
/// expansions (which involve /// copying the entire buffer to a new location) do not
/// occur while other threads are /// reading from allocated regions. </para>
/// <para><strong>Sequential Layout Limitations: ///</strong><br /> Sequential layout SLAB
/// allocators cannot provide a single-threaded /// version because:<list
/// type="bullet"><item><description>They use contiguous memory /// layout that requires
/// buffer expansion when capacity is exceeded
/// ///</description></item><item><description>Buffer expansion involves copying all ///
/// existing data to a new memory location</description></item><item><description> ///
/// Without proper synchronization, concurrent access to allocated objects during ///
/// expansion would cause memory corruption</description></item><item><description>The ///
/// expansion lock mechanism is essential even if allocation itself is single-threaded
/// ///</description></item></list></para> <para><strong> Chunked Layout: </strong><br />
/// /// Chunked layout allocators can optionally provide single-threaded versions since
/// they /// don't require buffer expansion and copying. Each chunk is allocated
/// independently, /// avoiding the memory relocation issues present in sequential
/// layouts. </para>
/// <para><strong> Important Threading Considerations: </strong></para> <para> While SLAB
/// allocators themselves may be designed for single-threaded use, allocated objects are
/// frequently shared across multiple threads in real-world applications. Due to the
/// special implementation of expandable concurrent sequential SLAB allocators, they rely
/// on internal expansion locks to ensure that memory buffer expansions (which involve
/// copying the entire buffer to a new location) do not occur while other threads are
/// reading from allocated regions.</para> <para><strong> Sequential Layout Limitations:
/// </strong><br /> Sequential layout SLAB allocators cannot provide a single-threaded
/// version because:<list type="bullet"><item><description> They use contiguous memory
/// layout that requires buffer expansion when capacity is exceeded
/// </description></item><item><description> Buffer expansion involves copying all
/// existing data to a new memory location </description></item><item><description>
/// Without proper synchronization, concurrent access to allocated objects during
/// expansion would cause memory corruption </description></item><item><description> The
/// expansion lock mechanism is essential even if allocation itself is single-
/// threaded</description></item></list></para> <para><strong> Chunked Layout:
/// </strong><br /> Chunked layout allocators can optionally provide single-threaded
/// versions since they don't require buffer expansion and copying. Each chunk is
/// allocated independently, avoiding the memory relocation issues present in sequential
/// layouts.</para>
/// </remarks>
public static SlabAllocator<T> Create(nuint count, SlabBufferLayout layout, bool concurrentSupport = true)
{
@@ -40,13 +40,13 @@ namespace StgSharp.Internal.Intrinsic
{
public delegate* unmanaged[Cdecl]<char*, int, int> city_hash_simplify;
public delegate* unmanaged[Cdecl]<void*, void*, void*, void> f32_add;
public delegate* unmanaged[Cdecl]<void*, void*, void*, void> f32_fma;
public delegate* unmanaged[Cdecl]<void*, float, void*, void> f32_scalar_mul;
public delegate* unmanaged[Cdecl]<void*, void*, void*, void> f32_sub;
public delegate* unmanaged[Cdecl]<void*, void*, void> f32_transpose;
public delegate* unmanaged[Cdecl]<int, ulong> factorial_simd;
public delegate* unmanaged[Cdecl]<char*, int, int, int> index_pair;
public delegate* unmanaged[Cdecl]<void*, void*, void*, void> kernel_add;
public delegate* unmanaged[Cdecl]<void*, void*, void*, void> kernel_fma;
public delegate* unmanaged[Cdecl]<MatrixKernel*, float, MatrixKernel*, void> kernel_scalar_mul;
public delegate* unmanaged[Cdecl]<void*, void*, void*, void> kernel_sub;
public delegate* unmanaged[Cdecl]<MatrixKernel*, MatrixKernel*, void> kernel_transpose;
public delegate* unmanaged[Cdecl]<Vector4*, Vector4*, void> normalize_3;
public IntrinsicContext()
@@ -51,11 +51,11 @@ namespace StgSharp.Mathematics
Matrix<float> result = GCMatrix<float>.FromDefault(left.ColumnLength, left.RowLength);
unsafe
{
MatrixParallelTaskPackage* handle = MatrixParallelFactory.CreateBaseTask();
MatrixParallelTaskPackage<float>* handle = MatrixParallelFactory.CreateBaseTask<float>();
handle->Left = left.Buffer;
handle->Right = right.Buffer;
handle->Result = result.Buffer;
handle->ComputeHandle = (IntPtr)InternalIO.Intrinsic.kernel_add;
handle->ComputeHandle = (IntPtr)InternalIO.Intrinsic.f32_add;
handle->LeftPrimOffset = 0;
handle->RightPrimOffset = 0;
handle->LeftPrimStride = left.KernelColumnLength;
+14 -14
View File
@@ -44,13 +44,13 @@ namespace StgSharp.Mathematics
internal unsafe partial class GCMatrix<T> : Matrix<T> where T: unmanaged, INumber<T>
{
private readonly MatrixKernel* _buffer;
private readonly MatrixKernel<T>* _buffer;
public GCMatrix(int column, int row)
{
KernelColumnLength = (column + 3) / 4;
KernelRowLength = (row + 3) / t_count;
_buffer = (MatrixKernel*)NativeMemory.AlignedAlloc((nuint)(column * row * sizeof(T)), (nuint)World.DefaultSIMDAlignment);
_buffer = (MatrixKernel<T>*)NativeMemory.AlignedAlloc((nuint)(column * row * sizeof(T)), (nuint)World.DefaultSIMDAlignment);
}
public override ref T this[int column, int row]
@@ -60,8 +60,8 @@ namespace StgSharp.Mathematics
AssertIndex(column, row);
int kernelColumn = column / 4, c = column % 4;
int kernelRow = row / t_count, r = row % t_count;
ref MatrixKernel kernel = ref _buffer[kernelColumn + KernelColumnLength * kernelRow];
return ref kernel.UnsafeIndex<T>(c, r);
ref MatrixKernel<T> kernel = ref _buffer[kernelColumn + KernelColumnLength * kernelRow];
return ref kernel.UnsafeIndex(c, r);
}
}
@@ -71,13 +71,13 @@ namespace StgSharp.Mathematics
set => throw new NotImplementedException();
}
internal override unsafe MatrixKernel* Buffer
internal override unsafe MatrixKernel<T>* Buffer
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _buffer;
}
public override unsafe ref MatrixKernel GetMatrixKernel(int column, int row)
public override unsafe ref MatrixKernel<T> GetMatrixKernel(int column, int row)
{
AssertIndex(column, row);
int kernelColumn = column / 4;
@@ -89,27 +89,27 @@ namespace StgSharp.Mathematics
{
int kernelColumn = column / 4, c = column % 4;
int kernelRow = row / t_count, r = row % t_count;
ref MatrixKernel kernel = ref _buffer[kernelColumn + (KernelColumnLength * kernelRow)];
return ref kernel.UnsafeIndex<T>(c, r);
ref MatrixKernel<T> kernel = ref _buffer[kernelColumn + (KernelColumnLength * kernelRow)];
return ref kernel.UnsafeIndex(c, r);
}
#region enumeration
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override unsafe MatrixSegmentEnumeration GetColumnEnumeration()
public override unsafe MatrixSegmentEnumeration<T> GetColumnEnumeration()
{
return new MatrixSegmentEnumeration(_buffer, KernelColumnLength, KernelColumnLength * KernelRowLength, 1, KernelColumnLength);
return new MatrixSegmentEnumeration<T>(_buffer, KernelColumnLength, KernelColumnLength * KernelRowLength, 1, KernelColumnLength);
}
public override unsafe MatrixSegmentEnumeration GetRowEnumeration()
public override unsafe MatrixSegmentEnumeration<T> GetRowEnumeration()
{
return new MatrixSegmentEnumeration(_buffer, 1, KernelColumnLength, KernelColumnLength, KernelRowLength);
return new MatrixSegmentEnumeration<T>(_buffer, 1, KernelColumnLength, KernelColumnLength, KernelRowLength);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override unsafe MatrixSegmentEnumeration GetSequentialEnumeration()
public override unsafe MatrixSegmentEnumeration<T> GetSequentialEnumeration()
{
return new MatrixSegmentEnumeration(_buffer, KernelRowLength * KernelColumnLength, KernelColumnLength * KernelRowLength, 1, KernelRowLength * KernelColumnLength);
return new MatrixSegmentEnumeration<T>(_buffer, KernelRowLength * KernelColumnLength, KernelColumnLength * KernelRowLength, 1, KernelRowLength * KernelColumnLength);
}
#endregion
@@ -45,7 +45,7 @@ namespace StgSharp.Mathematics
internal unsafe partial class HlsfMatrix<T> : Matrix<T> where T: unmanaged, INumber<T>
{
private readonly MatrixKernel* k_buffer;
private readonly MatrixKernel<T>* k_buffer;
private readonly hlsfAllocator allocator;
private readonly hlsfHandle handle;
@@ -53,8 +53,8 @@ namespace StgSharp.Mathematics
{
KernelColumnLength = (column + 3) / 4;
KernelRowLength = (row + 3) / t_count;
this.handle = allocator.Alloc((uint)(KernelColumnLength * KernelRowLength * sizeof(MatrixKernel)));
k_buffer = (MatrixKernel*)handle.Pointer;
this.handle = allocator.Alloc((uint)(KernelColumnLength * KernelRowLength * sizeof(MatrixKernel<T>)));
k_buffer = (MatrixKernel<T>*)handle.Pointer;
}
public override ref T this[int column, int row]
@@ -64,8 +64,8 @@ namespace StgSharp.Mathematics
AssertIndex(column, row);
int kernelColumn = column / 4, c = column % 4;
int kernelRow = row / t_count, r = row % t_count;
ref MatrixKernel kernel = ref k_buffer[(kernelColumn * kernelColumn) + kernelRow];
return ref kernel.UnsafeIndex<T>(c, r);
ref MatrixKernel<T> kernel = ref k_buffer[(kernelColumn * kernelColumn) + kernelRow];
return ref kernel.UnsafeIndex(c, r);
}
}
@@ -75,13 +75,13 @@ namespace StgSharp.Mathematics
set => throw new NotImplementedException();
}
internal override unsafe MatrixKernel* Buffer
internal override unsafe MatrixKernel<T>* Buffer
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => k_buffer;
}
public override unsafe ref MatrixKernel GetMatrixKernel(int column, int row)
public override unsafe ref MatrixKernel<T> GetMatrixKernel(int column, int row)
{
AssertIndex(column, row);
int kernelColumn = column / 4, c = column % 4;
@@ -93,8 +93,8 @@ namespace StgSharp.Mathematics
{
int kernelColumn = column / 4, c = column % 4;
int kernelRow = row / t_count, r = row % t_count;
ref MatrixKernel kernel = ref k_buffer[(kernelColumn * kernelColumn) + kernelRow];
return ref kernel.UnsafeIndex<T>(c, r);
ref MatrixKernel<T> kernel = ref k_buffer[(kernelColumn * kernelColumn) + kernelRow];
return ref kernel.UnsafeIndex(c, r);
}
~HlsfMatrix()
@@ -105,20 +105,20 @@ namespace StgSharp.Mathematics
#region enumeration
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override unsafe MatrixSegmentEnumeration GetColumnEnumeration()
public override unsafe MatrixSegmentEnumeration<T> GetColumnEnumeration()
{
return new MatrixSegmentEnumeration(k_buffer, KernelColumnLength, KernelColumnLength * KernelRowLength, 1, KernelColumnLength);
return new MatrixSegmentEnumeration<T>(k_buffer, KernelColumnLength, KernelColumnLength * KernelRowLength, 1, KernelColumnLength);
}
public override unsafe MatrixSegmentEnumeration GetRowEnumeration()
public override unsafe MatrixSegmentEnumeration<T> GetRowEnumeration()
{
return new MatrixSegmentEnumeration(k_buffer, 1, KernelColumnLength, KernelColumnLength, KernelRowLength);
return new MatrixSegmentEnumeration<T>(k_buffer, 1, KernelColumnLength, KernelColumnLength, KernelRowLength);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override unsafe MatrixSegmentEnumeration GetSequentialEnumeration()
public override unsafe MatrixSegmentEnumeration<T> GetSequentialEnumeration()
{
return new MatrixSegmentEnumeration(k_buffer, KernelRowLength * KernelColumnLength, KernelColumnLength * KernelRowLength, 1, KernelRowLength * KernelColumnLength);
return new MatrixSegmentEnumeration<T>(k_buffer, KernelRowLength * KernelColumnLength, KernelColumnLength * KernelRowLength, 1, KernelRowLength * KernelColumnLength);
}
#endregion
+63 -31
View File
@@ -32,6 +32,8 @@ using StgSharp.HighPerformance;
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using hlsfAllocator = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator;
// using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
@@ -135,55 +137,77 @@ namespace StgSharp.Mathematics
public readonly unsafe ref struct MatrixSegmentEnumeration<T>(
MatrixKernel<T>* source,
int stride,
int size,
int primStride,
int primSize,
int secondaryStride,
int secondarySize)
int secondarySize,
int primOffset = 0,
int secondaryOffset = 0)
where T: unmanaged, INumber<T>
{
private readonly MatrixKernel<T>* _source = source;
private readonly int _secondarySize = secondarySize;
private readonly int _secondaryStride = secondaryStride;
internal readonly int SecondaryStride = secondaryStride;
internal readonly int PrimOffset = primOffset;
private readonly int _size = size;
private readonly int _stride = stride;
internal readonly int PrimSize = primSize;
internal readonly int PrimStride = primStride;
internal readonly int SecondaryOffset = secondaryOffset;
internal readonly int SecondarySize = secondarySize;
public Enumerator<T> GetEnumerator()
internal readonly MatrixKernel<T>* Source
{
return new Enumerator<T>(in this);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _source;
}
public ref struct Enumerator<T>(in MatrixSegmentEnumeration<T> enumeration) where T: unmanaged, INumber<T>
public Enumerator GetEnumerator()
{
return new Enumerator(in this);
}
public ref struct Enumerator(in MatrixSegmentEnumeration<T> enumeration)
{
private MatrixKernel<T>* _current = enumeration._source - enumeration._stride;
private readonly MatrixKernel<T>* _end = enumeration._source + enumeration._size;
private readonly int _secondarySize = enumeration._secondarySize;
private readonly int _secondaryStride = enumeration._secondaryStride;
private readonly int _stride = enumeration._stride;
private readonly MatrixKernel<T>* _source = enumeration._source;
private int _indexCount = 0;
private readonly int _stride = enumeration.PrimStride;
private readonly long _bound = (long)enumeration.PrimSize * enumeration.PrimStride;
private long _index = (long)enumeration.PrimOffset * enumeration.PrimStride;
private readonly MatrixSegmentEnumeration<T> _enumeration;
public readonly MatrixSegment<T> Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(_current, _secondarySize, _secondaryStride);
get => new(
_source + _index,
_enumeration.SecondarySize,
_enumeration.SecondaryStride,
_enumeration.PrimOffset);
}
public bool MoveNext()
{
_current += _stride;
return _current < _end;
_indexCount++;
_index += _stride;
if (_index >= _bound) {
_index = 0;
}
return _indexCount >= _enumeration.PrimSize;
}
}
}
public readonly unsafe ref struct MatrixSegment<T>(MatrixKernel<T>* source, int size, int stride)
public readonly unsafe ref struct MatrixSegment<T>(MatrixKernel<T>* source, int size, int stride, int offset)
where T: unmanaged, INumber<T>
{
private readonly MatrixKernel<T>* _source = source;
private readonly int _offset = offset;
private readonly int _secondarySize = size;
private readonly int _size = size;
private readonly int _stride = stride;
@@ -198,29 +222,37 @@ namespace StgSharp.Mathematics
}
}
public Enumerator<T> GetEnumerator()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Enumerator GetEnumerator()
{
return new Enumerator<T>(_source, _size, _stride);
return new Enumerator(in this);
}
public ref struct Enumerator<T>(MatrixKernel<T>* source, int count, int stride) where T: unmanaged, INumber<T>
public ref struct Enumerator(in MatrixSegment<T> segment)
{
private readonly MatrixKernel<T>* _end = source + (count * stride);
private readonly int _stride = stride;
private readonly int _end = segment._size;
public MatrixKernel<T>* Current
private int _indexCount = 0;
private readonly long _bound = (long)segment._size * segment._stride;
private long _index = (long)segment._offset * segment._stride;
private readonly MatrixSegment<T> _segment = segment;
public readonly MatrixKernel<T>* Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private set;
} = source - stride;
get => _segment._source + _index;
}
public bool MoveNext()
{
Current += _stride;
return Current < _end;
_index += _segment._stride;
_indexCount++;
if (_index >= _bound) {
_index = 0;
}
return _indexCount < _end;
}
}
@@ -33,42 +33,43 @@ using System;
using System.Collections.Generic;
using System.Formats.Asn1;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics
{
internal static partial class MatrixParallel
public static partial class MatrixParallel
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe void Compute_Binary(MatrixParallelTaskPackage* p)
internal static unsafe void Compute_Binary(MatrixParallelTaskPackageNonGeneric* p)
{
MatrixKernel* l = p->Left, r = p->Right, a = p->Result;
long
lCount = p->LeftPrimOffset * p->LeftPrimStride,
rCount = p->RightPrimOffset * p->RightPrimStride,
aCount = p->ResultPrimOffset * p->ResultPrimStride,
pCount = p->PrimCount;
int eSize = p->ElementSize;
nint l = p->Left, r = p->Right, a = p->Result;
long
lCount = (long)p->LeftPrimOffset * p->LeftPrimStride,
rCount = (long)p->RightPrimOffset * p->RightPrimStride,
aCount = (long)p->ResultPrimOffset * p->ResultPrimStride,
pCount = p->PrimCount,
lLimit = pCount - p->LeftPrimOffset,
rLimit = pCount - p->RightPrimOffset,
aLimit = pCount - p->ResultPrimOffset;
delegate* unmanaged[Cdecl]<MatrixKernel*, MatrixKernel*, MatrixKernel*, void> op =
(delegate* unmanaged[Cdecl]<MatrixKernel*, MatrixKernel*, MatrixKernel*, void>)p->ComputeHandle;
delegate* unmanaged[Cdecl]<IntPtr, IntPtr, IntPtr, void> op =
(delegate* unmanaged[Cdecl]<IntPtr, IntPtr, IntPtr, void>)p->ComputeHandle;
for (long i = 0; i < pCount; i++)
{
lCount = i < lLimit ? lCount + p->LeftPrimOffset : 0;
rCount = i < rLimit ? rCount + p->RightPrimOffset : 0;
aCount = i < rLimit ? aCount + p->ResultPrimOffset : 0;
MatrixKernel* left = l + lCount;
MatrixKernel* right = r + rCount;
MatrixKernel* ans = a + aCount;
nint left = l + (nint)(lCount * eSize);
nint right = r + (nint)(rCount * eSize);
nint ans = a + (nint)(aCount * eSize);
long
lc = p->LeftSecOffset * p->LeftSecStride,
rc = p->RightSecOffset * p->RightSecStride,
ac = p->ResultSecOffset * p->ResultSecStride,
lc = (long)p->LeftSecOffset * p->LeftSecStride,
rc = (long)p->RightSecOffset * p->RightSecStride,
ac = (long)p->ResultSecOffset * p->ResultSecStride,
sCount = p->SecCount;
long
ll = sCount - p->LeftSecOffset,
@@ -79,15 +80,15 @@ namespace StgSharp.Mathematics
lc = j < ll ? lc + p->LeftSecOffset : 0;
rc = j < rl ? rc + p->RightSecOffset : 0;
ac = j < al ? ac + p->ResultSecOffset : 0;
op(left + lc, right + rc, ans + ac);
op(left + (nint)lc, right + (nint)rc, ans + (nint)ac);
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe void Compute_BinaryScalar(MatrixParallelTaskPackage* p)
internal static unsafe void Compute_BinaryScalar(MatrixParallelTaskPackageNonGeneric* p)
{
MatrixKernel* l = p->Left, r = p->Right, a = p->Result;
nint l = p->Left, r = p->Right, a = p->Result;
long
lCount = p->LeftPrimOffset * p->LeftPrimStride,
rCount = p->RightPrimOffset * p->RightPrimStride,
@@ -97,23 +98,23 @@ namespace StgSharp.Mathematics
lLimit = pCount - p->LeftPrimOffset,
rLimit = pCount - p->RightPrimOffset,
aLimit = pCount - p->ResultPrimOffset;
delegate* unmanaged[Cdecl]<MatrixKernel*, MatrixKernel*, MatrixKernel*, ScalarPacket*, void> op =
(delegate* unmanaged[Cdecl]<MatrixKernel*, MatrixKernel*, MatrixKernel*, ScalarPacket*, void>)p->ComputeHandle;
delegate* unmanaged[Cdecl]<nint,nint,nint, ScalarPacket*, void> op =
(delegate* unmanaged[Cdecl]<nint, nint, nint, ScalarPacket*, void>)p->ComputeHandle;
ScalarPacket* scalar = p->Scalar;
for (long i = 0; i < pCount; i++)
{
lCount = i < lLimit ? lCount + p->LeftPrimOffset : 0;
rCount = i < rLimit ? rCount + p->RightPrimOffset : 0;
aCount = i < rLimit ? aCount + p->ResultPrimOffset : 0;
MatrixKernel* left = l + lCount;
MatrixKernel* right = r + rCount;
MatrixKernel* ans = a + aCount;
nint
left = l + (nint)lCount,
right = r + (nint)rCount,
ans = a + (nint)aCount;
long
lc = p->LeftSecOffset * p->LeftSecStride,
rc = p->RightSecOffset * p->RightSecStride,
ac = p->ResultSecOffset * p->ResultSecStride,
sCount = p->SecCount;
long
sCount = p->SecCount,
ll = sCount - p->LeftSecOffset,
rl = sCount - p->RightSecOffset,
al = sCount - p->ResultSecOffset;
@@ -122,28 +123,27 @@ namespace StgSharp.Mathematics
lc = j < ll ? lc + p->LeftSecOffset : 0;
rc = j < rl ? rc + p->RightSecOffset : 0;
ac = j < al ? ac + p->ResultSecOffset : 0;
op(left + lc, right + rc, ans + ac, scalar);
op(left + (nint)lc, right + (nint)rc, ans + (nint)ac, scalar);
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe void Compute_Unary(MatrixParallelTaskPackage* p)
internal static unsafe void Compute_Unary(MatrixParallelTaskPackageNonGeneric* p)
{
MatrixKernel* s = p->Right, a = p->Result;
nint s = p->Right, a = p->Result;
long rCount = p->RightPrimOffset * p->RightPrimStride,
aCount = p->ResultPrimOffset * p->ResultPrimStride,
pCount = p->PrimCount;
aCount = p->ResultPrimOffset * p->ResultPrimStride,
pCount = p->PrimCount;
long rLimit = pCount - p->RightPrimOffset,
aLimit = pCount - p->ResultPrimOffset;
delegate* unmanaged[Cdecl]<MatrixKernel*, MatrixKernel*, void> op =
(delegate* unmanaged[Cdecl]<MatrixKernel*, MatrixKernel*, void>)p->ComputeHandle;
delegate* unmanaged[Cdecl]<nint, nint, void> op =
(delegate* unmanaged[Cdecl]<nint, nint, void>)p->ComputeHandle;
for (long i = 0; i < pCount; i++)
{
rCount = i < rLimit ? rCount + p->RightPrimOffset : 0;
aCount = i < rLimit ? aCount + p->ResultPrimOffset : 0;
MatrixKernel* source = s + rCount;
MatrixKernel* ans = a + aCount;
nint source = s + (nint)rCount, ans = a + (nint)aCount;
long rc = p->RightSecOffset * p->RightSecStride,
ac = p->ResultSecOffset * p->ResultSecStride,
sCount = p->SecCount;
@@ -153,29 +153,29 @@ namespace StgSharp.Mathematics
{
rc = j < rl ? rc + p->RightSecOffset : 0;
ac = j < al ? ac + p->ResultSecOffset : 0;
op(source + rc, ans + ac);
op(source + (nint)rc, ans + (nint)ac);
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe void Compute_UnaryScalar(MatrixParallelTaskPackage* p)
internal static unsafe void Compute_UnaryScalar(MatrixParallelTaskPackageNonGeneric* p)
{
MatrixKernel* s = p->Right, a = p->Result;
nint s = p->Right, a = p->Result;
long rCount = p->RightPrimOffset * p->RightPrimStride,
aCount = p->ResultPrimOffset * p->ResultPrimStride,
pCount = p->PrimCount;
long rLimit = pCount - p->RightPrimOffset,
aLimit = pCount - p->ResultPrimOffset;
delegate* unmanaged[Cdecl]<MatrixKernel*, MatrixKernel*, ScalarPacket*, void> op =
(delegate* unmanaged[Cdecl]<MatrixKernel*, MatrixKernel*, ScalarPacket*, void>)p->ComputeHandle;
delegate* unmanaged[Cdecl]<nint, nint, ScalarPacket*, void> op =
(delegate* unmanaged[Cdecl]<nint, nint, ScalarPacket*, void>)p->ComputeHandle;
ScalarPacket* scalar = p->Scalar;
for (long i = 0; i < pCount; i++)
{
rCount = i < rLimit ? rCount + p->RightPrimOffset : 0;
aCount = i < rLimit ? aCount + p->ResultPrimOffset : 0;
MatrixKernel* source = s + rCount;
MatrixKernel* ans = a + aCount;
nint source = s + (nint)rCount;
nint ans = a + (nint)aCount;
long rc = p->RightSecOffset * p->RightSecStride,
ac = p->ResultSecOffset * p->ResultSecStride,
sCount = p->SecCount;
@@ -185,7 +185,7 @@ namespace StgSharp.Mathematics
{
rc = j < rl ? rc + p->RightSecOffset : 0;
ac = j < al ? ac + p->ResultSecOffset : 0;
op(source + rc, ans + ac, scalar);
op(source + (nint)rc, ans + (nint)ac, scalar);
}
}
}
@@ -31,14 +31,9 @@
using StgSharp.HighPerformance;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Text.Json.Serialization;
using System.Threading;
namespace StgSharp.Mathematics
{
@@ -59,27 +54,106 @@ namespace StgSharp.Mathematics
}
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct MatrixParallelTaskPackage
internal readonly unsafe struct MatrixParallelTaskPackageNonGeneric
{
public static unsafe void Copy(MatrixParallelTaskPackage* source, MatrixParallelTaskPackage* target)
{
Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTaskPackage), sizeof(MatrixParallelTaskPackage));
}
#region source
// Group 1: Left/Right matrix kernel base address
public MatrixKernel* Left; // 8
public MatrixKernel* Right; // 8
public readonly IntPtr Left; // 8
public readonly IntPtr Right; // 8
#endregion
#region ans
// Group 2: Result kernel base address + reserved
public MatrixKernel* Result; // 8
public void* ReservedPtr0; // 8 (extension/reserved)
public readonly IntPtr Result; // 8
public readonly int ElementSize;
public readonly int ReservedPtr0; // 4 (extension/reserved)
#endregion
#region left enum
public readonly int LeftPrimOffset; // 4
public readonly int LeftPrimStride; // 4
public readonly int LeftSecOffset; // 4
public readonly int LeftSecStride; // 4
#endregion
#region right enum
public readonly int RightPrimOffset; // 4
public readonly int RightPrimStride; // 4
public readonly int RightSecOffset; // 4
public readonly int RightSecStride; // 4
#endregion
#region ans enum
public readonly int ResultPrimOffset; // 4
public readonly int ResultPrimStride; // 4
public readonly int ResultSecOffset; // 4
public readonly int ResultSecStride; // 4
#endregion
#region scalar/compute
public readonly ScalarPacket* Scalar; // 8
public readonly IntPtr ComputeHandle; // 8
#endregion
#region global profile
public readonly int PrimCount; // 4
public readonly int ComputeMode; // 4
public readonly int SecCount; // 4
private readonly int ReservedInt0; // 4
#endregion
#region reserve
public readonly long ReservedLong0; // 8
public readonly long ReservedLong1; // 8
#endregion
}
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct MatrixParallelTaskPackage<T> where T: unmanaged, INumber<T>
{
public MatrixParallelTaskPackage()
{
Unsafe.SkipInit(out this);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Copy(MatrixParallelTaskPackage<T>* source, MatrixParallelTaskPackage<T>* target)
{
Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTaskPackage<T>), sizeof(MatrixParallelTaskPackage<T>));
}
#region source
// Group 1: Left/Right matrix kernel base address
public MatrixKernel<T>* Left; // 8
public MatrixKernel<T>* Right; // 8
#endregion
#region ans
// Group 2: Result kernel base address + reserved
public MatrixKernel<T>* Result; // 8
public int ElementSize = sizeof(T);
public int ReservedPtr0; // 4 (extension/reserved)
#endregion
@@ -133,4 +207,4 @@ namespace StgSharp.Mathematics
#endregion
}
}
}
@@ -1,6 +1,6 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixParallel.TaskWrapFactory.cs"
// file="MatrixParallel.TaskFactory.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
@@ -33,6 +33,7 @@ using StgSharp.HighPerformance.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
@@ -57,13 +58,13 @@ namespace StgSharp.Mathematics
private static SlabAllocator<ScalarPacket> _scalarAllocator ;
private static SlabAllocator<MatrixParallelTaskPackage> _wrapAllocator ;
private static SlabAllocator<MatrixParallelTaskPackageNonGeneric> _wrapAllocator ;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MatrixParallelTaskPackage* CreateBaseTask()
public static MatrixParallelTaskPackage<T>* CreateBaseTask<T>() where T: unmanaged, INumber<T>
{
MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)_wrapAllocator.Allocate();
Span<byte> s = new Span<byte>((byte*)p, sizeof(MatrixParallelTaskPackage));
MatrixParallelTaskPackage<T>* p = (MatrixParallelTaskPackage<T>*)_wrapAllocator.Allocate();
Span<byte> s = new Span<byte>((byte*)p, sizeof(MatrixParallelTaskPackage<T>));
s.Fill(0);
return p;
}
@@ -75,10 +76,10 @@ namespace StgSharp.Mathematics
return p;
}
<<<<<<< HEAD:StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskWrapFactory.cs
public static MatrixParallelTaskPackage* FromTaskWrap(MatrixParallelTaskPackage* source, int offset)
public static MatrixParallelTaskPackage<T>* FromTaskWrap<T>(MatrixParallelTaskPackage<T>* source, int offset)
where T: unmanaged, INumber<T>
{
MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)_wrapAllocator.Allocate();
MatrixParallelTaskPackage<T>* p = (MatrixParallelTaskPackage<T>*)_wrapAllocator.Allocate();
/*
*p = *source;
p->Left += offset * p->LeftStride;
@@ -88,16 +89,24 @@ namespace StgSharp.Mathematics
return p;
}
=======
[MethodImpl(MethodImplOptions.AggressiveInlining)]
>>>>>>> 8007eee8461498a85a9515e8fd076f03cb552b64:StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskFactory.cs
public static void Release(ScalarPacket* source)
{
_scalarAllocator.Free((nuint)source);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Release(MatrixParallelTaskPackage* source)
public static void Release(MatrixParallelTaskPackageNonGeneric* source)
{
if (source->Scalar != null) {
_scalarAllocator.Free((nuint)source->Scalar);
}
_wrapAllocator.Free((nuint)source);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Release<T>(MatrixParallelTaskPackage<T>* source) where T: unmanaged, INumber<T>
{
if (source->Scalar != null) {
_scalarAllocator.Free((nuint)source->Scalar);
@@ -108,40 +117,13 @@ namespace StgSharp.Mathematics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void Init()
{
<<<<<<< HEAD:StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskWrapFactory.cs
int count = group.Length;
group[0] = (nint)source;
int l = source->LeftPrimStride, r = source->RightPrimStride, res = source->ResultPrimStride, c = source->PrimCount / count;
source->LeftPrimStride *= count;
source->RightPrimStride *= count;
source->ResultPrimStride *= count;
source->PrimCount = c;
int remain = source->PrimCount - (c * count);
for (int i = 1; i < count; i++)
if (_intermediateResult is null)
{
nint handle = (nint)_wrapAllocator.Allocate();
MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)handle;
*p = *source;
p->Left += l * i;
p->Right += r * i;
p->Result += res * i;
p->PrimCount = c;
group[i] = handle;
_intermediateResult = new hlsfAllocator(64 * 16 * 16);
_scalarAllocator = SlabAllocator<ScalarPacket>.Create(64 * 1024, SlabBufferLayout.Chunked);
_wrapAllocator = SlabAllocator<MatrixParallelTaskPackageNonGeneric>.
Create(128 * 1024, SlabBufferLayout.Chunked);
}
// Distribute remaining work to the last few tasks
for (int i = 0; i < remain; i++)
{
int targetIndex = count - 1 - i;
if (targetIndex >= 0) {
((MatrixParallelTaskPackage*)group[targetIndex])->PrimCount++;
}
}
=======
_scalarAllocator = SlabAllocator<ScalarPacket>.Create(64, SlabBufferLayout.Chunked, true);
_wrapAllocator = SlabAllocator<MatrixParallelTaskPackage>.Create(64, SlabBufferLayout.Chunked, true);
_intermediateResult = new hlsfAllocator((nuint)(sizeof(MatrixKernel) * 64));
>>>>>>> 8007eee8461498a85a9515e8fd076f03cb552b64:StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskFactory.cs
}
}
@@ -0,0 +1,79 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixParallel.TaskPublic.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:
//
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.HighPerformance;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics
{
public static partial class MatrixParallel
{
public static unsafe void PublicTask<T>(
MatrixSegmentEnumeration<T> left,
MatrixSegmentEnumeration<T> right,
MatrixSegmentEnumeration<T> ans,
delegate*<MatrixKernel<T>*, MatrixKernel<T>*, MatrixKernel<T>*, void> Operation)
where T: unmanaged, INumber<T>
{
int columnCount = left.PrimSize;
int rowCount = right.PrimSize;
MatrixParallelTaskPackage<T> package = new()
{
Left = left.Source,
Right = right.Source,
Result = ans.Source,
LeftPrimOffset = left.PrimOffset,
RightPrimOffset = right.PrimOffset,
ResultPrimOffset = ans.PrimOffset,
LeftPrimStride = left.PrimStride,
RightPrimStride = right.PrimStride,
ResultPrimStride = ans.PrimStride,
LeftSecOffset = left.SecondaryOffset,
RightSecOffset = right.SecondaryOffset,
ResultSecOffset = ans.SecondaryOffset,
LeftSecStride = left.SecondaryStride,
RightSecStride = right.SecondaryStride,
ResultSecStride = ans.SecondaryStride,
PrimCount = left.PrimSize,
SecCount = left.SecondarySize,
ComputeHandle = (nint)Operation
};
}
}
}
@@ -64,7 +64,7 @@ namespace StgSharp.Mathematics
* we allow each wrap to contain either 3 or 4 threads, providing flexibility while maintaining efficiency.
*/
internal static partial class MatrixParallel
public static partial class MatrixParallel
{
private static Thread[] _threads;
@@ -167,7 +167,7 @@ namespace StgSharp.Mathematics
{
ResetThreadCount();
foreach (MatrixParallelWrap item in _wraps) {
_ = item.SchedulerReset.Set();
item.SchedulerReset.Set();
}
}
@@ -176,17 +176,17 @@ namespace StgSharp.Mathematics
private readonly Queue<IntPtr>[] _cache;
private readonly Thread[] _threads;
private readonly AutoResetEvent[] _workerReset;
private readonly ManualResetEventSlim[] _workerReset;
public MatrixParallelWrap(int id, int wrapSize, bool isFirst)
{
ID = id;
int begin = isFirst ? 1 : 0, end = wrapSize - 1;
_cache = new Queue<IntPtr>[wrapSize];
_workerReset = new AutoResetEvent[end];
_workerReset = new ManualResetEventSlim[end];
_threads = new Thread[wrapSize];
for (int i = 0; i < end; i++) {
_workerReset[i] = new AutoResetEvent(false);
_workerReset[i] = new ManualResetEventSlim(false);
}
for (int i = 0; i < _cache.Length; i++) {
_cache[i] = new Queue<IntPtr>(8);
@@ -212,7 +212,7 @@ namespace StgSharp.Mathematics
public int ID { get; init; }
internal AutoResetEvent SchedulerReset { get; } = new(false);
internal ManualResetEventSlim SchedulerReset { get; } = new(false);
public Span<IntPtr> EnqueuePackage(Span<IntPtr> packageSpan)
{
@@ -224,7 +224,7 @@ namespace StgSharp.Mathematics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public AutoResetEvent GetWorkerResetEvent(int id)
public ManualResetEventSlim GetWorkerResetEvent(int id)
{
return _workerReset[id];
}
@@ -238,8 +238,8 @@ namespace StgSharp.Mathematics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetAllWorker()
{
foreach (AutoResetEvent item in _workerReset) {
_ = item.Set();
foreach (ManualResetEventSlim item in _workerReset) {
item.Set();
}
}
@@ -253,10 +253,11 @@ namespace StgSharp.Mathematics
MatrixParallelWrap wrap = handle.Wrap;
int inWrapIndex = handle.InWrapIndex;
Thread self = Thread.CurrentThread;
AutoResetEvent reset = wrap.GetWorkerResetEvent(inWrapIndex);
ManualResetEventSlim reset = wrap.GetWorkerResetEvent(inWrapIndex);
do
{
_ = reset.WaitOne();
reset.Wait();
reset.Reset();
self.Priority = ThreadPriority.Highest;
ExecuteTaskPackage(wrap.GetWorkerTaskQueue(inWrapIndex));
_ = Interlocked.Decrement(ref RemainThreadCount);
@@ -270,11 +271,12 @@ namespace StgSharp.Mathematics
MatrixParallelWrap wrap = handle.Wrap;
int inWrapIndex = handle.InWrapIndex;
Queue<IntPtr> taskQueue = wrap.GetWorkerTaskQueue(inWrapIndex);
AutoResetEvent resetEvent = wrap.SchedulerReset;
ManualResetEventSlim resetEvent = wrap.SchedulerReset;
Thread self = Thread.CurrentThread;
do
{
_ = resetEvent.WaitOne();
resetEvent.Wait();
resetEvent.Reset();
self.Priority = ThreadPriority.Highest;
// TODO task publication to worker in wrap
@@ -290,7 +292,7 @@ namespace StgSharp.Mathematics
{
while (taskQueue.TryDequeue(out nint handle))
{
MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)handle;
MatrixParallelTaskPackageNonGeneric* p = (MatrixParallelTaskPackageNonGeneric*)handle;
switch (p->ComputeMode)
{
case 1:
+5 -36
View File
@@ -57,45 +57,13 @@ namespace StgSharp
// 7. 线程状态监控测试
Console.WriteLine("=== 线程状态监控测试 ===");
TestThreadStateMonitoring();
// TestThreadStateMonitoring();
Console.WriteLine();
Console.WriteLine("? MatrixParallel 分层线程调度系统空跑测试完成");
}
private static bool TestInitialization()
{
if (!MatrixParallel.SupportParallel)
{
Console.WriteLine("? 跳过初始化测试 - 系统不支持并行处理");
return false;
}
try
{
Console.WriteLine("正在初始化 MatrixParallel...");
MatrixParallel.Init();
Console.WriteLine("? MatrixParallel 初始化成功");
// 检查 LeaderHandle 是否正确设置
if (MatrixParallel.LeaderHandle != null)
{
Console.WriteLine($"? LeaderHandle 已创建,Wrap ID: {MatrixParallel.LeaderHandle.Wrap.ID}");
} else
{
Console.WriteLine("? LeaderHandle 未正确设置");
}
return true;
}
catch (Exception ex)
{
Console.WriteLine($"? 初始化失败: {ex.Message}");
return false;
}
}
private static void TestParallelControlFlow()
{
try
@@ -121,7 +89,7 @@ namespace StgSharp
MatrixParallel.SetAllWrap();
// 发布任务(空任务)
MatrixParallel.PublishTask();
// MatrixParallel.PublishTask();
// 短暂等待,模拟实际工作
Thread.Sleep(10 + i * 5);
@@ -162,7 +130,8 @@ namespace StgSharp
MatrixParallel.LaunchParallel(SleepMode.QuickWakeup);
MatrixParallel.SetAllWrap();
MatrixParallel.PublishTask();
// MatrixParallel.PublishTask();
// 极短暂的等待
+31 -1
View File
@@ -1,3 +1,33 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixParallelThreadTest.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:
//
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Mathematics;
using System;
using System.Threading;
@@ -59,7 +89,7 @@ namespace StgSharp
Console.WriteLine("已通知所有Wrap开始工作");
// 发布任务(当前为空实现)
MatrixParallel.PublishTask();
// MatrixParallel.PublishTask();
Console.WriteLine("任务发布完成");
// 等待一段时间让线程执行
+3 -11
View File
@@ -59,29 +59,21 @@ namespace StgSharp
{
try
{
<<<<<<< HEAD
World.Init();
TestMatrixParallel();
// 原有的矩阵测试代码
=======
MatrixParallelThreadTest.TestMatrixParallelThreadScheduling();
/*
>>>>>>> 8007eee8461498a85a9515e8fd076f03cb552b64
HybridLayerSegregatedFitAllocator hlsf = new HybridLayerSegregatedFitAllocator(64 * 400 * 400);
Matrix<float> m = Matrix<float>.FromHlsf(400, 200, hlsf);
int i = 0, ii = 0;
<<<<<<< HEAD
foreach (Segment lines in m.GetRowEnumeration())
=======
foreach (MatrixSegment lines in m.GetRowEnumeration())
>>>>>>> 8007eee8461498a85a9515e8fd076f03cb552b64
foreach (MatrixSegment<float> lines in m.GetRowEnumeration())
{
i = 0;
foreach (MatrixKernel* item in lines)
foreach (MatrixKernel<float>* item in lines)
{
i++;
Console.WriteLine(i);