diff --git a/StgSharp.Common/Collections/RandomAccessibleStack.cs b/StgSharp.Common/Collections/RandomAccessibleStack.cs index b5871c22..036399ca 100644 --- a/StgSharp.Common/Collections/RandomAccessibleStack.cs +++ b/StgSharp.Common/Collections/RandomAccessibleStack.cs @@ -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. /// - 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). /// - 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 /// 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 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 /// 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]; } /// @@ -146,12 +146,12 @@ namespace StgSharp.Collections /// 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 /// /// Adds an item to the top of the stack. /// - 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(); diff --git a/StgSharp.Common/HighPerformance/Memory/SlabAllocator.cs b/StgSharp.Common/HighPerformance/Memory/SlabAllocator.cs index 5e81ffcf..06972bff 100644 --- a/StgSharp.Common/HighPerformance/Memory/SlabAllocator.cs +++ b/StgSharp.Common/HighPerformance/Memory/SlabAllocator.cs @@ -59,27 +59,26 @@ namespace StgSharp.HighPerformance.Memory /// Thrown when requesting unsupported configurations. /// /// - /// Important Threading Considerations: 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. - /// Sequential Layout Limitations: ///
Sequential layout SLAB - /// allocators cannot provide a single-threaded /// version because:They use contiguous memory /// layout that requires - /// buffer expansion when capacity is exceeded - /// ///Buffer expansion involves copying all /// - /// existing data to a new memory location /// - /// Without proper synchronization, concurrent access to allocated objects during /// - /// expansion would cause memory corruptionThe /// - /// expansion lock mechanism is essential even if allocation itself is single-threaded - /// ///
Chunked Layout:
- /// /// 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.
+ /// Important Threading Considerations: 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. Sequential Layout Limitations: + ///
Sequential layout SLAB allocators cannot provide a single-threaded + /// version because: They use contiguous memory + /// layout that requires buffer expansion when capacity is exceeded + /// Buffer expansion involves copying all + /// existing data to a new memory location + /// Without proper synchronization, concurrent access to allocated objects during + /// expansion would cause memory corruption The + /// expansion lock mechanism is essential even if allocation itself is single- + /// threaded
Chunked Layout: + ///
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.
///
public static SlabAllocator Create(nuint count, SlabBufferLayout layout, bool concurrentSupport = true) { diff --git a/StgSharp.Common/Internal/Intrinsic/IntrinsicContext.cs b/StgSharp.Common/Internal/Intrinsic/IntrinsicContext.cs index 61b8c30e..04dbac6f 100644 --- a/StgSharp.Common/Internal/Intrinsic/IntrinsicContext.cs +++ b/StgSharp.Common/Internal/Intrinsic/IntrinsicContext.cs @@ -40,13 +40,13 @@ namespace StgSharp.Internal.Intrinsic { public delegate* unmanaged[Cdecl] city_hash_simplify; + public delegate* unmanaged[Cdecl] f32_add; + public delegate* unmanaged[Cdecl] f32_fma; + public delegate* unmanaged[Cdecl] f32_scalar_mul; + public delegate* unmanaged[Cdecl] f32_sub; + public delegate* unmanaged[Cdecl] f32_transpose; public delegate* unmanaged[Cdecl] factorial_simd; public delegate* unmanaged[Cdecl] index_pair; - public delegate* unmanaged[Cdecl] kernel_add; - public delegate* unmanaged[Cdecl] kernel_fma; - public delegate* unmanaged[Cdecl] kernel_scalar_mul; - public delegate* unmanaged[Cdecl] kernel_sub; - public delegate* unmanaged[Cdecl] kernel_transpose; public delegate* unmanaged[Cdecl] normalize_3; public IntrinsicContext() diff --git a/StgSharp.Common/Mathematics/Linear/Matrix.Basic.cs b/StgSharp.Common/Mathematics/Linear/Matrix.Basic.cs index ccc5db76..91c914aa 100644 --- a/StgSharp.Common/Mathematics/Linear/Matrix.Basic.cs +++ b/StgSharp.Common/Mathematics/Linear/Matrix.Basic.cs @@ -51,11 +51,11 @@ namespace StgSharp.Mathematics Matrix result = GCMatrix.FromDefault(left.ColumnLength, left.RowLength); unsafe { - MatrixParallelTaskPackage* handle = MatrixParallelFactory.CreateBaseTask(); + MatrixParallelTaskPackage* handle = MatrixParallelFactory.CreateBaseTask(); 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; diff --git a/StgSharp.Common/Mathematics/Linear/Matrix.GC.cs b/StgSharp.Common/Mathematics/Linear/Matrix.GC.cs index f9b79612..7e5efd7c 100644 --- a/StgSharp.Common/Mathematics/Linear/Matrix.GC.cs +++ b/StgSharp.Common/Mathematics/Linear/Matrix.GC.cs @@ -44,13 +44,13 @@ namespace StgSharp.Mathematics internal unsafe partial class GCMatrix : Matrix where T: unmanaged, INumber { - private readonly MatrixKernel* _buffer; + private readonly MatrixKernel* _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*)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(c, r); + ref MatrixKernel 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* Buffer { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => _buffer; } - public override unsafe ref MatrixKernel GetMatrixKernel(int column, int row) + public override unsafe ref MatrixKernel 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(c, r); + ref MatrixKernel 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 GetColumnEnumeration() { - return new MatrixSegmentEnumeration(_buffer, KernelColumnLength, KernelColumnLength * KernelRowLength, 1, KernelColumnLength); + return new MatrixSegmentEnumeration(_buffer, KernelColumnLength, KernelColumnLength * KernelRowLength, 1, KernelColumnLength); } - public override unsafe MatrixSegmentEnumeration GetRowEnumeration() + public override unsafe MatrixSegmentEnumeration GetRowEnumeration() { - return new MatrixSegmentEnumeration(_buffer, 1, KernelColumnLength, KernelColumnLength, KernelRowLength); + return new MatrixSegmentEnumeration(_buffer, 1, KernelColumnLength, KernelColumnLength, KernelRowLength); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public override unsafe MatrixSegmentEnumeration GetSequentialEnumeration() + public override unsafe MatrixSegmentEnumeration GetSequentialEnumeration() { - return new MatrixSegmentEnumeration(_buffer, KernelRowLength * KernelColumnLength, KernelColumnLength * KernelRowLength, 1, KernelRowLength * KernelColumnLength); + return new MatrixSegmentEnumeration(_buffer, KernelRowLength * KernelColumnLength, KernelColumnLength * KernelRowLength, 1, KernelRowLength * KernelColumnLength); } #endregion diff --git a/StgSharp.Common/Mathematics/Linear/Matrix.HLSF.cs b/StgSharp.Common/Mathematics/Linear/Matrix.HLSF.cs index 610476a9..5901bbf0 100644 --- a/StgSharp.Common/Mathematics/Linear/Matrix.HLSF.cs +++ b/StgSharp.Common/Mathematics/Linear/Matrix.HLSF.cs @@ -45,7 +45,7 @@ namespace StgSharp.Mathematics internal unsafe partial class HlsfMatrix : Matrix where T: unmanaged, INumber { - private readonly MatrixKernel* k_buffer; + private readonly MatrixKernel* 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))); + k_buffer = (MatrixKernel*)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(c, r); + ref MatrixKernel 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* Buffer { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => k_buffer; } - public override unsafe ref MatrixKernel GetMatrixKernel(int column, int row) + public override unsafe ref MatrixKernel 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(c, r); + ref MatrixKernel 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 GetColumnEnumeration() { - return new MatrixSegmentEnumeration(k_buffer, KernelColumnLength, KernelColumnLength * KernelRowLength, 1, KernelColumnLength); + return new MatrixSegmentEnumeration(k_buffer, KernelColumnLength, KernelColumnLength * KernelRowLength, 1, KernelColumnLength); } - public override unsafe MatrixSegmentEnumeration GetRowEnumeration() + public override unsafe MatrixSegmentEnumeration GetRowEnumeration() { - return new MatrixSegmentEnumeration(k_buffer, 1, KernelColumnLength, KernelColumnLength, KernelRowLength); + return new MatrixSegmentEnumeration(k_buffer, 1, KernelColumnLength, KernelColumnLength, KernelRowLength); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public override unsafe MatrixSegmentEnumeration GetSequentialEnumeration() + public override unsafe MatrixSegmentEnumeration GetSequentialEnumeration() { - return new MatrixSegmentEnumeration(k_buffer, KernelRowLength * KernelColumnLength, KernelColumnLength * KernelRowLength, 1, KernelRowLength * KernelColumnLength); + return new MatrixSegmentEnumeration(k_buffer, KernelRowLength * KernelColumnLength, KernelColumnLength * KernelRowLength, 1, KernelRowLength * KernelColumnLength); } #endregion diff --git a/StgSharp.Common/Mathematics/Linear/Matrix.cs b/StgSharp.Common/Mathematics/Linear/Matrix.cs index a6ee7c85..d61b6dd2 100644 --- a/StgSharp.Common/Mathematics/Linear/Matrix.cs +++ b/StgSharp.Common/Mathematics/Linear/Matrix.cs @@ -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( MatrixKernel* 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 { private readonly MatrixKernel* _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 GetEnumerator() + internal readonly MatrixKernel* Source { - return new Enumerator(in this); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => _source; } - public ref struct Enumerator(in MatrixSegmentEnumeration enumeration) where T: unmanaged, INumber + public Enumerator GetEnumerator() + { + return new Enumerator(in this); + } + + public ref struct Enumerator(in MatrixSegmentEnumeration enumeration) { - 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; + private readonly MatrixKernel* _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 _enumeration; public readonly MatrixSegment 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(MatrixKernel* source, int size, int stride) + public readonly unsafe ref struct MatrixSegment(MatrixKernel* source, int size, int stride, int offset) where T: unmanaged, INumber { private readonly MatrixKernel* _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 GetEnumerator() + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Enumerator GetEnumerator() { - return new Enumerator(_source, _size, _stride); + return new Enumerator(in this); } - public ref struct Enumerator(MatrixKernel* source, int count, int stride) where T: unmanaged, INumber + public ref struct Enumerator(in MatrixSegment segment) { - private readonly MatrixKernel* _end = source + (count * stride); - private readonly int _stride = stride; + private readonly int _end = segment._size; - public MatrixKernel* 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 _segment = segment; + + public readonly MatrixKernel* 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; } } diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.Execute.cs b/StgSharp.Common/Mathematics/Linear/MatrixParallel.Execute.cs index 5d30dc10..62da90e6 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.Execute.cs +++ b/StgSharp.Common/Mathematics/Linear/MatrixParallel.Execute.cs @@ -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] op = - (delegate* unmanaged[Cdecl])p->ComputeHandle; + delegate* unmanaged[Cdecl] op = + (delegate* unmanaged[Cdecl])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] op = - (delegate* unmanaged[Cdecl])p->ComputeHandle; + delegate* unmanaged[Cdecl] op = + (delegate* unmanaged[Cdecl])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] op = - (delegate* unmanaged[Cdecl])p->ComputeHandle; + delegate* unmanaged[Cdecl] op = + (delegate* unmanaged[Cdecl])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] op = - (delegate* unmanaged[Cdecl])p->ComputeHandle; + delegate* unmanaged[Cdecl] op = + (delegate* unmanaged[Cdecl])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); } } } diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.Task.cs b/StgSharp.Common/Mathematics/Linear/MatrixParallel.Task.cs index 87698137..e4fc6056 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.Task.cs +++ b/StgSharp.Common/Mathematics/Linear/MatrixParallel.Task.cs @@ -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 where T: unmanaged, INumber + { + + public MatrixParallelTaskPackage() + { + Unsafe.SkipInit(out this); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + 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 + + #endregion + + #region ans + + // Group 2: Result kernel base address + reserved + public MatrixKernel* Result; // 8 + public int ElementSize = sizeof(T); + public int ReservedPtr0; // 4 (extension/reserved) #endregion @@ -133,4 +207,4 @@ namespace StgSharp.Mathematics #endregion } -} +} \ No newline at end of file diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskFactory.cs b/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskFactory.cs index e8e99e15..7bd20e68 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskFactory.cs +++ b/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskFactory.cs @@ -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 _scalarAllocator ; - private static SlabAllocator _wrapAllocator ; + private static SlabAllocator _wrapAllocator ; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static MatrixParallelTaskPackage* CreateBaseTask() + public static MatrixParallelTaskPackage* CreateBaseTask() where T: unmanaged, INumber { - MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)_wrapAllocator.Allocate(); - Span s = new Span((byte*)p, sizeof(MatrixParallelTaskPackage)); + MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)_wrapAllocator.Allocate(); + Span s = new Span((byte*)p, sizeof(MatrixParallelTaskPackage)); 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* FromTaskWrap(MatrixParallelTaskPackage* source, int offset) + where T: unmanaged, INumber { - MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)_wrapAllocator.Allocate(); + MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)_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(MatrixParallelTaskPackage* source) where T: unmanaged, INumber { 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.Create(64 * 1024, SlabBufferLayout.Chunked); + _wrapAllocator = SlabAllocator. + 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.Create(64, SlabBufferLayout.Chunked, true); - _wrapAllocator = SlabAllocator.Create(64, SlabBufferLayout.Chunked, true); - _intermediateResult = new hlsfAllocator((nuint)(sizeof(MatrixKernel) * 64)); ->>>>>>> 8007eee8461498a85a9515e8fd076f03cb552b64:StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskFactory.cs } } diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskPublic.cs b/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskPublic.cs new file mode 100644 index 00000000..b0b241d3 --- /dev/null +++ b/StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskPublic.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( + MatrixSegmentEnumeration left, + MatrixSegmentEnumeration right, + MatrixSegmentEnumeration ans, + delegate**, MatrixKernel*, MatrixKernel*, void> Operation) + where T: unmanaged, INumber + { + int columnCount = left.PrimSize; + int rowCount = right.PrimSize; + + MatrixParallelTaskPackage 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 + }; + } + + } +} diff --git a/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs b/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs index 5c27b23f..ea22584d 100644 --- a/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs +++ b/StgSharp.Common/Mathematics/Linear/MatrixParallel.cs @@ -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[] _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[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(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 EnqueuePackage(Span 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 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: diff --git a/StgSharpDebug/MatrixParallelTest.cs b/StgSharpDebug/MatrixParallelTest.cs index ad4b8ff3..eaa331ea 100644 --- a/StgSharpDebug/MatrixParallelTest.cs +++ b/StgSharpDebug/MatrixParallelTest.cs @@ -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(); // ¼«¶ÌÔݵĵȴý diff --git a/StgSharpDebug/MatrixParallelThreadTest.cs b/StgSharpDebug/MatrixParallelThreadTest.cs index c7de3503..e5ea7f44 100644 --- a/StgSharpDebug/MatrixParallelThreadTest.cs +++ b/StgSharpDebug/MatrixParallelThreadTest.cs @@ -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("任务å�‘布完æˆ�"); // 等待一段时间让线程执行 diff --git a/StgSharpDebug/Program.cs b/StgSharpDebug/Program.cs index 6e6d82ba..bb32398a 100644 --- a/StgSharpDebug/Program.cs +++ b/StgSharpDebug/Program.cs @@ -59,29 +59,21 @@ namespace StgSharp { try { -<<<<<<< HEAD World.Init(); TestMatrixParallel(); // 原有的矩阵测试代ç � -======= - MatrixParallelThreadTest.TestMatrixParallelThreadScheduling(); - /* ->>>>>>> 8007eee8461498a85a9515e8fd076f03cb552b64 HybridLayerSegregatedFitAllocator hlsf = new HybridLayerSegregatedFitAllocator(64 * 400 * 400); Matrix m = Matrix.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 lines in m.GetRowEnumeration()) { i = 0; - foreach (MatrixKernel* item in lines) + foreach (MatrixKernel* item in lines) { i++; Console.WriteLine(i);