buffer op brief ok, but blocks on unkown reason
This commit is contained in:
@@ -25,38 +25,21 @@
|
|||||||
//
|
//
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
using StgSharp.HighPerformance;
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using hlsfAllocator = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator;
|
using hlsfAllocator = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator;
|
||||||
|
|
||||||
// using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
|
|
||||||
|
|
||||||
namespace StgSharp.Mathematics.Numeric
|
namespace StgSharp.Mathematics.Numeric
|
||||||
{
|
{
|
||||||
public abstract unsafe partial class Matrix<T> where T: unmanaged, INumber<T>
|
public static class Matrix
|
||||||
{
|
{
|
||||||
|
|
||||||
private protected readonly int t_count = 16 / sizeof(T);
|
|
||||||
|
|
||||||
private protected readonly int t_size = sizeof(T);
|
|
||||||
|
|
||||||
private protected Matrix() { }
|
|
||||||
|
|
||||||
public abstract ref T this[int column, int row]
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract unsafe Matrix<T> this[ReadOnlySpan<MatrixIndex> column, ReadOnlySpan<MatrixIndex> row]
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static hlsfAllocator? DefaultAllocator
|
public static hlsfAllocator? DefaultAllocator
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@@ -67,192 +50,29 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
set;
|
set;
|
||||||
} = null;
|
} = null;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Length of the matrix in columns.
|
|
||||||
/// </summary>
|
|
||||||
public int ColumnLength { get; init; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Length of the matrix in rows.
|
|
||||||
/// </summary>
|
|
||||||
public int RowLength { get; init; }
|
|
||||||
|
|
||||||
public static MatrixAllocation DefaultAllocation { get; set; } = MatrixAllocation.GC;
|
public static MatrixAllocation DefaultAllocation { get; set; } = MatrixAllocation.GC;
|
||||||
|
|
||||||
internal abstract MatrixKernel<T>* Buffer { get; }
|
public static Matrix<T> FromDefault<T>(int columnLength, int rowLength) where T : unmanaged, INumber<T>
|
||||||
|
|
||||||
protected internal int KernelColumnLength { get; init; }
|
|
||||||
|
|
||||||
protected internal int KernelRowLength { get; init; }
|
|
||||||
|
|
||||||
public static Matrix<T> FromDefault(int columnLength, int rowLength)
|
|
||||||
{
|
{
|
||||||
return DefaultAllocation switch
|
return DefaultAllocation switch
|
||||||
{
|
{
|
||||||
MatrixAllocation.GC => FromGC(columnLength, rowLength),
|
MatrixAllocation.GC => FromGC<T>(columnLength, rowLength),
|
||||||
MatrixAllocation.HLSF => FromHlsf(columnLength, rowLength, DefaultAllocator!),
|
MatrixAllocation.HLSF => FromHlsf<T>(columnLength, rowLength, DefaultAllocator!),
|
||||||
_ => throw new NotSupportedException("This allocation is not supported."),
|
_ => throw new NotSupportedException("This allocation is not supported."),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Matrix<T> FromGC(int columnLength, int rowLength)
|
public static Matrix<T> FromGC<T>(int columnLength, int rowLength) where T : unmanaged, INumber<T>
|
||||||
{
|
{
|
||||||
return new GCMatrix<T>(columnLength, rowLength);
|
return new GCMatrix<T>(columnLength, rowLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static Matrix<T> FromHlsf(int columnLength, int rowLength, hlsfAllocator allocator)
|
public static Matrix<T> FromHlsf<T>(int columnLength, int rowLength, hlsfAllocator allocator)
|
||||||
|
where T : unmanaged, INumber<T>
|
||||||
{
|
{
|
||||||
return new HlsfMatrix<T>(columnLength, rowLength, allocator);
|
return new HlsfMatrix<T>(columnLength, rowLength, allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public abstract MatrixSegmentEnumeration<T> GetColumnEnumeration();
|
|
||||||
|
|
||||||
public abstract ref MatrixKernel<T> GetMatrixKernel(int column, int row);
|
|
||||||
|
|
||||||
public abstract MatrixSegmentEnumeration<T> GetRowEnumeration();
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public abstract MatrixSegmentEnumeration<T> GetSequentialEnumeration();
|
|
||||||
|
|
||||||
[Obsolete("This method does not contain boarder check, so be careful on size or use indexer instead.", false)]
|
|
||||||
public abstract ref T UnsafeIndex(int column, int row);
|
|
||||||
|
|
||||||
private protected void AssertIndex(int column, int row)
|
|
||||||
{
|
|
||||||
if (column < 0 || column >= ColumnLength) {
|
|
||||||
throw new ArgumentOutOfRangeException(nameof(column));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (row < 0 || row >= RowLength) {
|
|
||||||
throw new ArgumentOutOfRangeException(nameof(row));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public readonly unsafe ref struct MatrixSegmentEnumeration<T>(
|
|
||||||
MatrixKernel<T>* source,
|
|
||||||
int primStride,
|
|
||||||
int primSize,
|
|
||||||
int secondaryStride,
|
|
||||||
int secondarySize,
|
|
||||||
int primOffset = 0,
|
|
||||||
int secondaryOffset = 0)
|
|
||||||
where T: unmanaged, INumber<T>
|
|
||||||
{
|
|
||||||
|
|
||||||
private readonly MatrixKernel<T>* _source = source;
|
|
||||||
internal readonly int PrimOffset = primOffset;
|
|
||||||
|
|
||||||
internal readonly int PrimSize = primSize;
|
|
||||||
internal readonly int PrimStride = primStride;
|
|
||||||
internal readonly int SecondaryOffset = secondaryOffset;
|
|
||||||
internal readonly int SecondarySize = secondarySize;
|
|
||||||
internal readonly int SecondaryStride = secondaryStride;
|
|
||||||
|
|
||||||
internal readonly MatrixKernel<T>* Source
|
|
||||||
{
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
get => _source;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Enumerator GetEnumerator()
|
|
||||||
{
|
|
||||||
return new Enumerator(in this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ref struct Enumerator(in MatrixSegmentEnumeration<T> enumeration)
|
|
||||||
{
|
|
||||||
|
|
||||||
private readonly MatrixKernel<T>* _source = enumeration._source;
|
|
||||||
private int _indexCount = -1;
|
|
||||||
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(
|
|
||||||
_source + _index,
|
|
||||||
_enumeration.SecondarySize,
|
|
||||||
_enumeration.SecondaryStride,
|
|
||||||
_enumeration.PrimOffset);
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool MoveNext()
|
|
||||||
{
|
|
||||||
_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, 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;
|
|
||||||
|
|
||||||
public ref MatrixKernel<T> this[int index]
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
if (index < 0 || index >= _size) {
|
|
||||||
throw new ArgumentOutOfRangeException(nameof(index));
|
|
||||||
}
|
|
||||||
return ref _source[index * _stride];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public Enumerator GetEnumerator()
|
|
||||||
{
|
|
||||||
return new Enumerator(in this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ref struct Enumerator(in MatrixSegment<T> segment)
|
|
||||||
{
|
|
||||||
|
|
||||||
private readonly int _end = segment._size;
|
|
||||||
|
|
||||||
private int _indexCount = -1;
|
|
||||||
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 => _segment._source + _index;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool MoveNext()
|
|
||||||
{
|
|
||||||
_index += _segment._stride;
|
|
||||||
_indexCount++;
|
|
||||||
if (_index >= _bound) {
|
|
||||||
_index = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return _indexCount < _end;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-7
@@ -1,6 +1,6 @@
|
|||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// file="Matrix.GC"
|
// file="Matrix_T.GC"
|
||||||
// Project: StgSharp
|
// Project: StgSharp
|
||||||
// AuthorGroup: Nitload
|
// AuthorGroup: Nitload
|
||||||
// Copyright (c) Nitload. All rights reserved.
|
// Copyright (c) Nitload. All rights reserved.
|
||||||
@@ -94,10 +94,10 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
|
|
||||||
#region enumeration
|
#region enumeration
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
public override unsafe MatrixSegmentEnumeration<T> ColumnEnumeration
|
||||||
public override unsafe MatrixSegmentEnumeration<T> GetColumnEnumeration()
|
|
||||||
{
|
{
|
||||||
return new MatrixSegmentEnumeration<T>(_buffer, KernelColumnLength, KernelColumnLength * KernelRowLength, 1,
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get => new MatrixSegmentEnumeration<T>(_buffer, KernelColumnLength, KernelColumnLength * KernelRowLength, 1,
|
||||||
KernelColumnLength);
|
KernelColumnLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,10 +106,10 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
return new MatrixSegmentEnumeration<T>(_buffer, 1, KernelColumnLength, KernelColumnLength, KernelRowLength);
|
return new MatrixSegmentEnumeration<T>(_buffer, 1, KernelColumnLength, KernelColumnLength, KernelRowLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
public override unsafe MatrixSegmentEnumeration<T> SequentialEnumeration
|
||||||
public override unsafe MatrixSegmentEnumeration<T> GetSequentialEnumeration()
|
|
||||||
{
|
{
|
||||||
return new MatrixSegmentEnumeration<T>(_buffer, KernelRowLength * KernelColumnLength,
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get => new MatrixSegmentEnumeration<T>(_buffer, KernelRowLength * KernelColumnLength,
|
||||||
KernelColumnLength * KernelRowLength, 1,
|
KernelColumnLength * KernelRowLength, 1,
|
||||||
KernelRowLength * KernelColumnLength);
|
KernelRowLength * KernelColumnLength);
|
||||||
}
|
}
|
||||||
+8
-7
@@ -1,6 +1,6 @@
|
|||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// file="Matrix.HLSF"
|
// file="Matrix_T.HLSF"
|
||||||
// Project: StgSharp
|
// Project: StgSharp
|
||||||
// AuthorGroup: Nitload
|
// AuthorGroup: Nitload
|
||||||
// Copyright (c) Nitload. All rights reserved.
|
// Copyright (c) Nitload. All rights reserved.
|
||||||
@@ -103,10 +103,10 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
|
|
||||||
#region enumeration
|
#region enumeration
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
public override unsafe MatrixSegmentEnumeration<T> ColumnEnumeration
|
||||||
public override unsafe MatrixSegmentEnumeration<T> GetColumnEnumeration()
|
|
||||||
{
|
{
|
||||||
return new MatrixSegmentEnumeration<T>(k_buffer, KernelColumnLength, KernelColumnLength * KernelRowLength,
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get => new MatrixSegmentEnumeration<T>(k_buffer, KernelColumnLength, KernelColumnLength * KernelRowLength,
|
||||||
1, KernelColumnLength);
|
1, KernelColumnLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,10 +116,11 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
KernelRowLength);
|
KernelRowLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
public override unsafe MatrixSegmentEnumeration<T> SequentialEnumeration
|
||||||
public override unsafe MatrixSegmentEnumeration<T> GetSequentialEnumeration()
|
|
||||||
{
|
{
|
||||||
return new MatrixSegmentEnumeration<T>(k_buffer, KernelRowLength * KernelColumnLength,
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
|
||||||
|
get => new MatrixSegmentEnumeration<T>(k_buffer, KernelRowLength * KernelColumnLength,
|
||||||
KernelColumnLength * KernelRowLength, 1,
|
KernelColumnLength * KernelRowLength, 1,
|
||||||
KernelRowLength * KernelColumnLength);
|
KernelRowLength * KernelColumnLength);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,218 @@
|
|||||||
|
//-----------------------------------------------------------------------
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// file="Matrix_T"
|
||||||
|
// Project: StgSharp
|
||||||
|
// AuthorGroup: Nitload
|
||||||
|
// Copyright (c) Nitload. 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.Numerics;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
// using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
|
||||||
|
|
||||||
|
namespace StgSharp.Mathematics.Numeric
|
||||||
|
{
|
||||||
|
public abstract unsafe partial class Matrix<T> where T : unmanaged, INumber<T>
|
||||||
|
{
|
||||||
|
|
||||||
|
private protected readonly int t_count = 16 / sizeof(T);
|
||||||
|
|
||||||
|
private protected readonly int t_size = sizeof(T);
|
||||||
|
|
||||||
|
private protected Matrix() { }
|
||||||
|
|
||||||
|
public abstract ref T this[int column, int row]
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract unsafe Matrix<T> this[ReadOnlySpan<MatrixIndex> column, ReadOnlySpan<MatrixIndex> row]
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Length of the matrix in columns.
|
||||||
|
/// </summary>
|
||||||
|
public int ColumnLength { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Length of the matrix in rows.
|
||||||
|
/// </summary>
|
||||||
|
public int RowLength { get; init; }
|
||||||
|
|
||||||
|
public abstract MatrixSegmentEnumeration<T> ColumnEnumeration { get; }
|
||||||
|
|
||||||
|
public abstract MatrixSegmentEnumeration<T> SequentialEnumeration { get; }
|
||||||
|
|
||||||
|
internal abstract MatrixKernel<T>* Buffer { get; }
|
||||||
|
|
||||||
|
protected internal int KernelColumnLength { get; init; }
|
||||||
|
|
||||||
|
protected internal int KernelRowLength { get; init; }
|
||||||
|
|
||||||
|
public abstract ref MatrixKernel<T> GetMatrixKernel(int column, int row);
|
||||||
|
|
||||||
|
public abstract MatrixSegmentEnumeration<T> GetRowEnumeration();
|
||||||
|
|
||||||
|
[Obsolete("This method does not contain boarder check, so be careful on size or use indexer instead.", false)]
|
||||||
|
public abstract ref T UnsafeIndex(int column, int row);
|
||||||
|
|
||||||
|
private protected void AssertIndex(int column, int row)
|
||||||
|
{
|
||||||
|
if (column < 0 || column >= ColumnLength) {
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(column));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (row < 0 || row >= RowLength) {
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(row));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly unsafe ref struct MatrixSegmentEnumeration<T>(
|
||||||
|
MatrixKernel<T>* source,
|
||||||
|
int primStride,
|
||||||
|
int primSize,
|
||||||
|
int secondaryStride,
|
||||||
|
int secondarySize,
|
||||||
|
int primOffset = 0,
|
||||||
|
int secondaryOffset = 0)
|
||||||
|
where T : unmanaged, INumber<T>
|
||||||
|
{
|
||||||
|
|
||||||
|
private readonly MatrixKernel<T>* _source = source;
|
||||||
|
internal readonly int PrimOffset = primOffset;
|
||||||
|
|
||||||
|
internal readonly int PrimSize = primSize;
|
||||||
|
internal readonly int PrimStride = primStride;
|
||||||
|
internal readonly int SecondaryOffset = secondaryOffset;
|
||||||
|
internal readonly int SecondarySize = secondarySize;
|
||||||
|
internal readonly int SecondaryStride = secondaryStride;
|
||||||
|
|
||||||
|
internal readonly MatrixKernel<T>* Source
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get => _source;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Enumerator GetEnumerator()
|
||||||
|
{
|
||||||
|
return new Enumerator(in this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ref struct Enumerator(in MatrixSegmentEnumeration<T> enumeration)
|
||||||
|
{
|
||||||
|
|
||||||
|
private readonly MatrixKernel<T>* _source = enumeration._source;
|
||||||
|
private int _indexCount = -1;
|
||||||
|
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(_source + _index, _enumeration.SecondarySize, _enumeration.SecondaryStride,
|
||||||
|
_enumeration.PrimOffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool MoveNext()
|
||||||
|
{
|
||||||
|
_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, 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;
|
||||||
|
|
||||||
|
public ref MatrixKernel<T> this[int index]
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (index < 0 || index >= _size) {
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(index));
|
||||||
|
}
|
||||||
|
return ref _source[index * _stride];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public Enumerator GetEnumerator()
|
||||||
|
{
|
||||||
|
return new Enumerator(in this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ref struct Enumerator(in MatrixSegment<T> segment)
|
||||||
|
{
|
||||||
|
|
||||||
|
private readonly int _end = segment._size;
|
||||||
|
|
||||||
|
private int _indexCount = -1;
|
||||||
|
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 => _segment._source + _index;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool MoveNext()
|
||||||
|
{
|
||||||
|
_index += _segment._stride;
|
||||||
|
_indexCount++;
|
||||||
|
if (_index >= _bound) {
|
||||||
|
_index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _indexCount < _end;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -47,7 +47,7 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
|
||||||
}
|
}
|
||||||
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
long count = (long)left.KernelColumnLength * left.KernelRowLength;
|
||||||
Matrix<float> ans = Matrix<float>.FromDefault(left.ColumnLength, left.RowLength);
|
Matrix<float> ans = Matrix.FromDefault<float>(left.ColumnLength, left.RowLength);
|
||||||
if (count <= 256)
|
if (count <= 256)
|
||||||
{
|
{
|
||||||
MatrixKernel<float>* leftPtr = left.Buffer;
|
MatrixKernel<float>* leftPtr = left.Buffer;
|
||||||
|
|||||||
@@ -47,6 +47,16 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
MatrixParallelFactory.Release(p);
|
MatrixParallelFactory.Release(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal static unsafe void BufferComputeNoOperatorScalar(MatrixParallelTaskPackage* p)
|
||||||
|
{
|
||||||
|
int size = p->ElementSize * 16;
|
||||||
|
long count = Unsafe.As<int, long>(ref p->PrimCount);
|
||||||
|
long offset = Unsafe.As<int, long>(ref p->PrimTileOffset) * size;
|
||||||
|
((delegate* unmanaged[Cdecl]<void*, ScalarPacket*,long, void>)p->ComputeHandle)((void*)(p->Result + offset),
|
||||||
|
p->Scalar, count);
|
||||||
|
MatrixParallelFactory.Release(p);
|
||||||
|
}
|
||||||
|
|
||||||
internal static unsafe void BufferComputeUnaryScalar(MatrixParallelTaskPackage* p)
|
internal static unsafe void BufferComputeUnaryScalar(MatrixParallelTaskPackage* p)
|
||||||
{
|
{
|
||||||
int size = p->ElementSize * 16;
|
int size = p->ElementSize * 16;
|
||||||
|
|||||||
@@ -67,17 +67,21 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
int primCount = package->PrimCount;
|
int primCount = package->PrimCount;
|
||||||
int secCount = package->SecCount;
|
int secCount = package->SecCount;
|
||||||
long count = (long)primCount * secCount;
|
long count = (long)primCount * secCount;
|
||||||
|
Console.WriteLine($"amount of kernels is {count}");
|
||||||
|
|
||||||
long recommendCount = count / 1024;
|
long recommendCount = count / 1024;
|
||||||
MatrixParallelThread[] pool = LeadParallel((int)recommendCount);
|
MatrixParallelThread[] pool = LeadParallel((int)recommendCount);
|
||||||
MatrixParallelHandle handle = new MatrixParallelHandle(pool);
|
MatrixParallelHandle handle = new MatrixParallelHandle(pool);
|
||||||
if (handle.Wraps.Count() == 1)
|
if (handle.Wraps.Length == 1)
|
||||||
{
|
{
|
||||||
|
// Console.WriteLine($"Scheduling into {pool.Length} threads.");
|
||||||
ref long offsetRef = ref Unsafe.As<int, long>(ref package->PrimTileOffset);
|
ref long offsetRef = ref Unsafe.As<int, long>(ref package->PrimTileOffset);
|
||||||
offsetRef = 0;
|
offsetRef = 0;
|
||||||
ref long lengthRef = ref Unsafe.As<int, long>(ref package->PrimCount);
|
ref long lengthRef = ref Unsafe.As<int, long>(ref package->PrimCount);
|
||||||
lengthRef = count;
|
lengthRef = count;
|
||||||
handle.Wraps.First().WrapTask.Enqueue((nint)package);
|
handle.Wraps[0].WrapTask.Enqueue((nint)package);
|
||||||
|
|
||||||
|
// Console.WriteLine($"Scheduling {count} kernels into thread");
|
||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
long actual = count / pool.Length;
|
long actual = count / pool.Length;
|
||||||
@@ -85,18 +89,25 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
|
|
||||||
long offset = 0;
|
long offset = 0;
|
||||||
MatrixParallelTaskPackage* current = package;
|
MatrixParallelTaskPackage* current = package;
|
||||||
|
|
||||||
|
// Console.WriteLine($"Scheduling into {pool.Length} threads.");
|
||||||
foreach (MatrixParallelWrap wrap in handle.Wraps)
|
foreach (MatrixParallelWrap wrap in handle.Wraps)
|
||||||
{
|
{
|
||||||
long wrapTaskLength = actual * wrap.WrapTaskCapacity;
|
long wrapTaskLength = actual * wrap.WrapTaskCapacity;
|
||||||
long ex = Math.Min(extra, wrap.WrapTaskCapacity);
|
long ex = Math.Min(extra, wrap.WrapTaskCapacity);
|
||||||
|
long length = wrapTaskLength + ex;
|
||||||
extra -= ex;
|
extra -= ex;
|
||||||
ref long offsetRef = ref Unsafe.As<int, long>(ref current->PrimTileOffset);
|
ref long offsetRef = ref Unsafe.As<int, long>(ref current->PrimTileOffset);
|
||||||
offsetRef = offset;
|
offsetRef = offset;
|
||||||
ref long lengthRef = ref Unsafe.As<int, long>(ref current->PrimCount);
|
ref long lengthRef = ref Unsafe.As<int, long>(ref current->PrimCount);
|
||||||
lengthRef = wrapTaskLength + ex;
|
lengthRef = length;
|
||||||
wrap.WrapTask.Enqueue((nint) current);
|
wrap.WrapTask.Enqueue((nint) current);
|
||||||
|
|
||||||
|
Console.WriteLine($"Offset is {offset}");
|
||||||
|
Console.WriteLine($"Scheduling {length} kernels into {wrap.WrapTaskCapacity} threads");
|
||||||
|
Console.WriteLine();
|
||||||
current = MatrixParallelFactory.FromExistPackage(current);
|
current = MatrixParallelFactory.FromExistPackage(current);
|
||||||
offset += wrapTaskLength;
|
offset += length;
|
||||||
}
|
}
|
||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
@@ -117,9 +128,9 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
}
|
}
|
||||||
/**/
|
/**/
|
||||||
MatrixParallelHandle handle = new MatrixParallelHandle(pool);
|
MatrixParallelHandle handle = new MatrixParallelHandle(pool);
|
||||||
if (handle.Wraps.Count() == 1)
|
if (handle.Wraps.Length == 1)
|
||||||
{
|
{
|
||||||
handle.Wraps.First().WrapTask.Enqueue((nint)package);
|
handle.Wraps[0].WrapTask.Enqueue((nint)package);
|
||||||
return handle;
|
return handle;
|
||||||
}
|
}
|
||||||
int threadCount = pool.Length;
|
int threadCount = pool.Length;
|
||||||
@@ -129,7 +140,6 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
int extraColumnCount = columnAfterBaseTile % threadCount; // columns to be sliced
|
int extraColumnCount = columnAfterBaseTile % threadCount; // columns to be sliced
|
||||||
|
|
||||||
int total = primCount;
|
int total = primCount;
|
||||||
Stack<MatrixParallelWrap> taskBunch = new Stack<MatrixParallelWrap>(threadCount);
|
|
||||||
MatrixParallelTaskPackage* current = package;
|
MatrixParallelTaskPackage* current = package;
|
||||||
foreach (MatrixParallelWrap wrap in handle.Wraps)
|
foreach (MatrixParallelWrap wrap in handle.Wraps)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -42,7 +42,6 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
|
|
||||||
private static CapacityFixedStack<MatrixParallelThread> _threads;
|
private static CapacityFixedStack<MatrixParallelThread> _threads;
|
||||||
|
|
||||||
private static volatile int RemainThreadCount = 0;
|
|
||||||
private static readonly
|
private static readonly
|
||||||
#if NET10_0_OR_GREATER
|
#if NET10_0_OR_GREATER
|
||||||
Lock
|
Lock
|
||||||
@@ -68,13 +67,13 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
|
|
||||||
public static void LaunchParallel(MatrixParallelHandle handle, SleepMode afterWork)
|
public static void LaunchParallel(MatrixParallelHandle handle, SleepMode afterWork)
|
||||||
{
|
{
|
||||||
RemainThreadCount = _threads.Count;
|
|
||||||
_AfterWork = (ThreadPriority)afterWork;
|
_AfterWork = (ThreadPriority)afterWork;
|
||||||
MatrixParallelThread leader = handle.GetLeader();
|
MatrixParallelThread leader = handle.GetLeader();
|
||||||
foreach (MatrixParallelWrap w in handle.Wraps) {
|
foreach (MatrixParallelWrap w in handle.Wraps) {
|
||||||
w.SetScheduler();
|
w.SetScheduler();
|
||||||
}
|
}
|
||||||
leader.MatrixParallelLeader();
|
leader.MatrixParallelLeader();
|
||||||
|
handle.WaitAllWraps();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void Init()
|
internal static void Init()
|
||||||
|
|||||||
@@ -28,6 +28,7 @@
|
|||||||
using StgSharp.Collections;
|
using StgSharp.Collections;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
@@ -36,7 +37,7 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace StgSharp.Mathematics.Numeric
|
namespace StgSharp.Mathematics.Numeric
|
||||||
{
|
{
|
||||||
public struct MatrixParallelHandle : IDisposable
|
public class MatrixParallelHandle : IDisposable
|
||||||
{
|
{
|
||||||
|
|
||||||
private static readonly (int a, int b)[] LookupTable = [
|
private static readonly (int a, int b)[] LookupTable = [
|
||||||
@@ -55,10 +56,11 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
(1, 4), // 19
|
(1, 4), // 19
|
||||||
(4, 2), // 20
|
(4, 2), // 20
|
||||||
];
|
];
|
||||||
|
|
||||||
private MatrixParallelThread[] _threads;
|
private MatrixParallelThread[] _threads;
|
||||||
|
private MatrixParallelWrap[] _wraps;
|
||||||
private bool _disposed = false;
|
private bool _disposed = false;
|
||||||
private CapacityFixedStack<MatrixParallelWrap> _wraps;
|
private int _count;
|
||||||
|
private ManualResetEventSlim _leaderEvent = new ManualResetEventSlim(false);
|
||||||
|
|
||||||
internal MatrixParallelHandle(MatrixParallelThread[] threadArray)
|
internal MatrixParallelHandle(MatrixParallelThread[] threadArray)
|
||||||
{
|
{
|
||||||
@@ -78,7 +80,7 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal IEnumerable<MatrixParallelWrap> Wraps
|
internal ReadOnlySpan<MatrixParallelWrap> Wraps
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
@@ -89,21 +91,8 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal ReadOnlySpan<MatrixParallelThread> ThreadsUnsafe
|
|
||||||
{
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
get => _threads;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool IsMultithread { get; init; }
|
private bool IsMultithread { get; init; }
|
||||||
|
|
||||||
public MatrixParallelThread DefineLeader()
|
|
||||||
{
|
|
||||||
MatrixParallelThread leader = _threads[1];
|
|
||||||
leader.Role = 2;
|
|
||||||
return leader;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if (_disposed) {
|
if (_disposed) {
|
||||||
@@ -115,8 +104,27 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
|
|
||||||
public MatrixParallelThread GetLeader()
|
public MatrixParallelThread GetLeader()
|
||||||
{
|
{
|
||||||
Threads[0].Role = 2;
|
MatrixParallelThread leader = Threads[1];
|
||||||
return Threads[0];
|
leader.Role = MatrixParallelThread.RoleType.Leader;
|
||||||
|
leader.LeaderEvent = _leaderEvent;
|
||||||
|
return leader;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void WaitAllWraps()
|
||||||
|
{
|
||||||
|
if (_disposed) {
|
||||||
|
throw new ObjectDisposedException(typeof(MatrixParallelHandle).Name);
|
||||||
|
}
|
||||||
|
SpinWait.SpinUntil(() => Volatile.Read(ref _count) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
internal void ReportWrapAccomplished()
|
||||||
|
{
|
||||||
|
if (_disposed) {
|
||||||
|
throw new ObjectDisposedException(typeof(MatrixParallelHandle).Name);
|
||||||
|
}
|
||||||
|
Interlocked.Decrement(ref _count);
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
@@ -163,24 +171,27 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
int count = _threads.Length;
|
int count = _threads.Length;
|
||||||
if (count < 6)
|
if (count < 6)
|
||||||
{
|
{
|
||||||
MatrixParallelWrap wrap = new MatrixParallelWrap(_threads, 0, count);
|
MatrixParallelWrap wrap = new MatrixParallelWrap(this, 0, count);
|
||||||
_wraps = [ wrap ];
|
_wraps = [ wrap ];
|
||||||
return;
|
} else
|
||||||
}
|
|
||||||
(int c3,int c4) = FastDecompose(_threads.Length);
|
|
||||||
_wraps = new(c3 + c4);
|
|
||||||
int begin = 0;
|
|
||||||
for (int i = 0; i < c3; i++)
|
|
||||||
{
|
{
|
||||||
_wraps.Push(new MatrixParallelWrap(_threads, begin, 3));
|
(int c3, int c4) = FastDecompose(_threads.Length);
|
||||||
|
_wraps = new MatrixParallelWrap[c3 + c4];
|
||||||
|
int begin = 0;
|
||||||
|
int i = 0;
|
||||||
|
for (; i < c3; i++)
|
||||||
|
{
|
||||||
|
_wraps[i] = new MatrixParallelWrap(this, begin, 3);
|
||||||
begin += 3;
|
begin += 3;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < c4; i++)
|
for (; i < c3 + c4; i++)
|
||||||
{
|
{
|
||||||
_wraps.Push(new MatrixParallelWrap(_threads, begin, 4));
|
_wraps[i] = new MatrixParallelWrap(this, begin, 4);
|
||||||
begin += 4;
|
begin += 4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_count = _wraps.Length;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,19 +119,19 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
|
|
||||||
#region right enum
|
#region right enum
|
||||||
|
|
||||||
[FieldOffset(32)]public int RightPrimOffset; // 4
|
[FieldOffset(48)]public int RightPrimOffset; // 4
|
||||||
[FieldOffset(36)]public int RightPrimStride; // 4
|
[FieldOffset(52)]public int RightPrimStride; // 4
|
||||||
[FieldOffset(40)]public int RightSecOffset; // 4
|
[FieldOffset(56)]public int RightSecOffset; // 4
|
||||||
[FieldOffset(44)]public int RightSecStride; // 4
|
[FieldOffset(60)]public int RightSecStride; // 4
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region ans enum
|
#region ans enum
|
||||||
|
|
||||||
[FieldOffset(48)]public int ResultPrimOffset; // 4
|
[FieldOffset(64)]public int ResultPrimOffset; // 4
|
||||||
[FieldOffset(52)]public int ResultPrimStride; // 4
|
[FieldOffset(68)]public int ResultPrimStride; // 4
|
||||||
[FieldOffset(56)]public int ResultSecOffset; // 4
|
[FieldOffset(72)]public int ResultSecOffset; // 4
|
||||||
[FieldOffset(60)] public int ResultSecStride; // 4
|
[FieldOffset(76)] public int ResultSecStride; // 4
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|||||||
@@ -72,6 +72,8 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
set => field = value;
|
set => field = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal ManualResetEventSlim? LeaderEvent { get; set; }
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
Dispose(disposing:true);
|
Dispose(disposing:true);
|
||||||
@@ -102,6 +104,7 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
case MatrixOperationParam.LEFT_RIGHT_ANS_SCALAR_PARAM:
|
case MatrixOperationParam.LEFT_RIGHT_ANS_SCALAR_PARAM:
|
||||||
break;
|
break;
|
||||||
case MatrixOperationParam.ANS_SCALAR_PARAM:
|
case MatrixOperationParam.ANS_SCALAR_PARAM:
|
||||||
|
MatrixComputeModel.BufferComputeNoOperatorScalar(p);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@@ -134,22 +137,31 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void Restart()
|
public void Restart()
|
||||||
|
{
|
||||||
|
if (Role == RoleType.Leader)
|
||||||
|
{
|
||||||
|
LeaderEvent?.Set();
|
||||||
|
} else
|
||||||
{
|
{
|
||||||
_resetEvent.Set();
|
_resetEvent.Set();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
internal void MatrixParallelLeader()
|
internal void MatrixParallelLeader()
|
||||||
{
|
{
|
||||||
Thread current = Thread.CurrentThread;
|
Thread current = Thread.CurrentThread;
|
||||||
current.Priority = ThreadPriority.Highest;
|
current.Priority = ThreadPriority.Highest;
|
||||||
Thread.Sleep(0);
|
LeaderEvent?.Wait();
|
||||||
|
LeaderEvent?.Reset();
|
||||||
|
|
||||||
|
// Console.WriteLine("Leader is computing");
|
||||||
#if WINDOWS
|
#if WINDOWS
|
||||||
if (!UseUmaAffinityPath)
|
if (!UseUmaAffinityPath)
|
||||||
{
|
{
|
||||||
// NUMA/affinity path: bind to this thread's reserved mask, then restore
|
// NUMA/affinity path: bind to this thread's reserved mask, then restore
|
||||||
nuint previousMask = SetThreadAffinityMask(GetCurrentThread(), _affinity.Mask);
|
nuint previousMask = SetThreadAffinityMask(GetCurrentThread(), _affinity.Mask);
|
||||||
bool affinityChanged = previousMask != 0; // nonzero => success
|
bool affinityChanged = previousMask != 0; // nonzero => success
|
||||||
|
Thread.Sleep(0);
|
||||||
ExecuteTaskPackage(Tasks);
|
ExecuteTaskPackage(Tasks);
|
||||||
if (affinityChanged) {
|
if (affinityChanged) {
|
||||||
_ = SetThreadAffinityMask(GetCurrentThread(), previousMask);
|
_ = SetThreadAffinityMask(GetCurrentThread(), previousMask);
|
||||||
@@ -164,7 +176,11 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
ExecuteTaskPackage(Tasks);
|
ExecuteTaskPackage(Tasks);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
this.Role = 1;
|
BindedWrap.ReportThreadAccomplished();
|
||||||
|
|
||||||
|
// Console.WriteLine("leader's computing is done");
|
||||||
|
Role = RoleType.Worker;
|
||||||
|
LeaderEvent = null;
|
||||||
current.Priority = ThreadPriority.Normal;
|
current.Priority = ThreadPriority.Normal;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -228,16 +244,27 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
_resetEvent.Reset();
|
_resetEvent.Reset();
|
||||||
switch (Role)
|
switch (Role)
|
||||||
{
|
{
|
||||||
case 0://worker
|
case RoleType.Worker://worker
|
||||||
// _ = Interlocked.Decrement(ref RemainThreadCount);
|
// _ = Interlocked.Decrement(ref RemainThreadCount);
|
||||||
|
// Console.WriteLine("worker start");
|
||||||
ExecuteTaskPackage(Tasks);
|
ExecuteTaskPackage(Tasks);
|
||||||
|
BindedWrap.ReportThreadAccomplished();
|
||||||
|
|
||||||
|
// Console.WriteLine("worker end");
|
||||||
break;
|
break;
|
||||||
case 1://scheduler
|
case RoleType.Scheduler://scheduler
|
||||||
BindedWrap.PublishTaskToWorkers(BindedWrap.WrapTask);
|
Console.WriteLine("scheduler start");
|
||||||
|
BindedWrap.PublishTaskToWorkers();
|
||||||
BindedWrap.SetAllWorker();
|
BindedWrap.SetAllWorker();
|
||||||
|
|
||||||
|
Console.WriteLine("scheduler done");
|
||||||
ExecuteTaskPackage(Tasks);
|
ExecuteTaskPackage(Tasks);
|
||||||
|
BindedWrap.ReportThreadAccomplished();
|
||||||
|
BindedWrap.WaitAllWorker();
|
||||||
|
BindedWrap.ReportWrapAccomplished();
|
||||||
|
Role = RoleType.Worker;
|
||||||
break;
|
break;
|
||||||
case 2://leader, error because leader should be at outer thread but not here
|
case RoleType.Leader://leader, error because leader should be at outer thread but not here
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@@ -389,21 +416,15 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
|
|
||||||
internal Queue<IntPtr> Tasks { get; } = [];
|
internal Queue<IntPtr> Tasks { get; } = [];
|
||||||
|
|
||||||
public int Role
|
public RoleType Role { get; internal set; }
|
||||||
|
|
||||||
|
public enum RoleType : int
|
||||||
{
|
{
|
||||||
get;
|
|
||||||
internal set
|
Worker = 0,
|
||||||
{
|
Scheduler = 1,
|
||||||
if ((Role & (~3u)) == 0)
|
Leader = 2,
|
||||||
{
|
|
||||||
field = value;
|
|
||||||
} else
|
|
||||||
{
|
|
||||||
#if DEBUG
|
|
||||||
throw new ArgumentOutOfRangeException(nameof(Role));
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net.WebSockets;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -39,23 +40,22 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
public unsafe class MatrixParallelWrap
|
public unsafe class MatrixParallelWrap
|
||||||
{
|
{
|
||||||
|
|
||||||
private readonly MatrixParallelThread[] _threads;
|
|
||||||
private int _begin;
|
private int _begin;
|
||||||
|
|
||||||
internal MatrixParallelWrap(MatrixParallelThread[] threads, int begin, int count)
|
private int _remainThreadCount;
|
||||||
|
private readonly MatrixParallelHandle _handle;
|
||||||
|
|
||||||
|
internal MatrixParallelWrap(MatrixParallelHandle handle, int begin, int count)
|
||||||
{
|
{
|
||||||
WrapTaskCapacity = count;
|
WrapTaskCapacity = count;
|
||||||
_threads = threads;
|
_handle = handle;
|
||||||
_begin = begin;
|
_begin = begin;
|
||||||
_threads[_begin].Role = 1;
|
for (int i = 0; i < WrapTaskCapacity; i++)
|
||||||
for (int i = _begin; i < _begin + WrapTaskCapacity; i++) {
|
|
||||||
_threads[i].BindedWrap = this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public MatrixParallelWrap()
|
|
||||||
{
|
{
|
||||||
_threads = new MatrixParallelThread[5];
|
Threads[i].Role = MatrixParallelThread.RoleType.Worker;
|
||||||
|
Threads[i].BindedWrap = this;
|
||||||
|
}
|
||||||
|
Threads[0].Role = MatrixParallelThread.RoleType.Scheduler;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SleepMode AfterWork
|
public static SleepMode AfterWork
|
||||||
@@ -66,19 +66,41 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
|
|
||||||
internal int Magic { get; set; }
|
internal int Magic { get; set; }
|
||||||
|
|
||||||
|
private ReadOnlySpan<MatrixParallelThread> Threads
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get => _handle.Threads[_begin..(_begin + WrapTaskCapacity)];
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void ReportThreadAccomplished()
|
||||||
|
{
|
||||||
|
Interlocked.Decrement(ref _remainThreadCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void ReportWrapAccomplished()
|
||||||
|
{
|
||||||
|
_handle.ReportWrapAccomplished();
|
||||||
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public void SetAllWorker()
|
public void SetAllWorker()
|
||||||
{
|
{
|
||||||
for (int i = _begin; i < _begin + WrapTaskCapacity; i++)
|
for (int i = 1; i < WrapTaskCapacity; i++) {
|
||||||
{
|
Threads[i].Restart();
|
||||||
if (_threads[i].Role != 3) {
|
|
||||||
_threads[i].Restart();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void PublishTaskToWorkers(Queue<IntPtr> tasks)
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public void WaitAllWorker()
|
||||||
{
|
{
|
||||||
|
SpinWait.SpinUntil(() => Volatile.Read(ref _remainThreadCount) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void PublishTaskToWorkers()
|
||||||
|
{
|
||||||
|
Queue<nint> tasks = WrapTask;
|
||||||
while (tasks.TryDequeue(out nint handle))
|
while (tasks.TryDequeue(out nint handle))
|
||||||
{
|
{
|
||||||
MatrixParallelTaskPackage* task = (MatrixParallelTaskPackage*)handle;
|
MatrixParallelTaskPackage* task = (MatrixParallelTaskPackage*)handle;
|
||||||
@@ -100,45 +122,54 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
|
|
||||||
internal void SetScheduler()
|
internal void SetScheduler()
|
||||||
{
|
{
|
||||||
MatrixParallelThread thread = _threads[_begin];
|
MatrixParallelThread thread = Threads[0];
|
||||||
thread.Restart();
|
thread.Restart();
|
||||||
|
_ = Interlocked.Exchange(ref _remainThreadCount, WrapTaskCapacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void PublishBufferTaskToWorkers(MatrixParallelTaskPackage* package)
|
private void PublishBufferTaskToWorkers(MatrixParallelTaskPackage* package)
|
||||||
{
|
{
|
||||||
long count = Unsafe.As<int, long>(ref package->PrimCount);
|
ReadOnlySpan<MatrixParallelThread> pool = Threads;
|
||||||
long recommendCount = count / 1024;
|
|
||||||
MatrixParallelThread[] pool = _threads;
|
|
||||||
if (pool.Length == 1)
|
if (pool.Length == 1)
|
||||||
{
|
{
|
||||||
pool[0].Tasks.Enqueue((nint)package);
|
pool[0].Tasks.Enqueue((nint)package);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
long count = Unsafe.As<int, long>(ref package->PrimCount);
|
||||||
long actual = count / pool.Length;
|
long actual = count / pool.Length;
|
||||||
long extra = count % pool.Length;
|
long extra = count % pool.Length;
|
||||||
|
|
||||||
long offset = 0;
|
long offset = 0;
|
||||||
MatrixParallelTaskPackage* current = package;
|
MatrixParallelTaskPackage* current = package;
|
||||||
|
string msg = $"""
|
||||||
|
Total: count={count}, length={Threads.Length}{Environment.NewLine}
|
||||||
|
""";
|
||||||
foreach (MatrixParallelThread thread in pool)
|
foreach (MatrixParallelThread thread in pool)
|
||||||
{
|
{
|
||||||
long wrapTaskLength = actual;
|
long wrapTaskLength = actual;
|
||||||
long ex = extra > 0 ? 1 : 0;
|
long ex = extra > 0 ? 1 : 0;
|
||||||
|
long length = wrapTaskLength + ex;
|
||||||
extra -= ex;
|
extra -= ex;
|
||||||
ref long offsetRef = ref Unsafe.As<int, long>(ref current->PrimTileOffset);
|
ref long offsetRef = ref Unsafe.As<int, long>(ref current->PrimTileOffset);
|
||||||
offsetRef += offset;
|
offsetRef += offset;
|
||||||
ref long lengthRef = ref Unsafe.As<int, long>(ref current->PrimCount);
|
ref long lengthRef = ref Unsafe.As<int, long>(ref current->PrimCount);
|
||||||
lengthRef = wrapTaskLength + ex;
|
lengthRef = length;
|
||||||
pool[0].Tasks.Enqueue((nint)current);
|
thread.Tasks.Enqueue((nint)current);
|
||||||
|
msg += $"""
|
||||||
|
Slice to thread: total offset={offsetRef} local offset={offset}, length={lengthRef}{Environment.NewLine}
|
||||||
|
""";
|
||||||
|
|
||||||
current = MatrixParallelFactory.FromExistPackage(current);
|
current = MatrixParallelFactory.FromExistPackage(current);
|
||||||
offset += wrapTaskLength;
|
offset = length;
|
||||||
}
|
}
|
||||||
|
Console.WriteLine(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void PublishKernelTaskToWorkers(MatrixParallelTaskPackage* package)
|
private void PublishKernelTaskToWorkers(MatrixParallelTaskPackage* package)
|
||||||
{
|
{
|
||||||
int primCount = package->PrimCount;
|
int primCount = package->PrimCount;
|
||||||
int secCount = package->SecCount;
|
int secCount = package->SecCount;
|
||||||
MatrixParallelThread[] pool = _threads;
|
ReadOnlySpan<MatrixParallelThread> pool = Threads;
|
||||||
|
|
||||||
if (pool.Length == 1)
|
if (pool.Length == 1)
|
||||||
{
|
{
|
||||||
@@ -201,31 +232,10 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Handle(int inWrapIndex, MatrixParallelWrap wrap)
|
|
||||||
{
|
|
||||||
|
|
||||||
public int InWrapIndex { get; } = inWrapIndex;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Role of the thread in the wrap. 1 is Scheduler, 0 are Workers. 2 is leader, beyond
|
|
||||||
/// wrap's control.
|
|
||||||
/// </summary>
|
|
||||||
public int Role { get; internal set; }
|
|
||||||
|
|
||||||
public Queue<IntPtr> TaskCache { get; } = [];
|
|
||||||
|
|
||||||
internal ManualResetEventSlim ResetEvent { get; } = new(false);
|
|
||||||
|
|
||||||
internal MatrixParallelWrap Wrap { get; } = wrap;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#region wrap task
|
#region wrap task
|
||||||
|
|
||||||
internal unsafe Queue<nint> WrapTask { get; set; } = [];
|
internal unsafe Queue<nint> WrapTask { get; set; } = [];
|
||||||
|
|
||||||
internal int WrapTaskSliceCount { get; set; }
|
|
||||||
|
|
||||||
internal int WrapTaskCapacity { get ; init; }
|
internal int WrapTaskCapacity { get ; init; }
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ DECLARE_BUF_PROC_LEFT_RIGHT_ANS(float, sse, add)
|
|||||||
DECLARE_BUF_PROC_LEFT_RIGHT_ANS(float, avx, add)
|
DECLARE_BUF_PROC_LEFT_RIGHT_ANS(float, avx, add)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < count; i++) {
|
for (size_t i = 0; i < count; i++) {
|
||||||
|
size_t s = sizeof(MAT_KERNEL(float));
|
||||||
register __m256 c0 = _mm256_add_ps(left[i].ymm[0], right[i].ymm[0]);
|
register __m256 c0 = _mm256_add_ps(left[i].ymm[0], right[i].ymm[0]);
|
||||||
register __m256 c1 = _mm256_add_ps(left[i].ymm[1], right[i].ymm[1]);
|
register __m256 c1 = _mm256_add_ps(left[i].ymm[1], right[i].ymm[1]);
|
||||||
ans[i].ymm[0] = c0;
|
ans[i].ymm[0] = c0;
|
||||||
|
|||||||
@@ -67,10 +67,18 @@ namespace StgSharp
|
|||||||
|
|
||||||
|
|
||||||
// 原有的矩阵测试代码
|
// 原有的矩阵测试代码
|
||||||
HybridLayerSegregatedFitAllocator hlsf = new HybridLayerSegregatedFitAllocator(64 * 400 * 400);
|
HybridLayerSegregatedFitAllocator hlsf = new HybridLayerSegregatedFitAllocator(64 * 4000 * 4000);
|
||||||
|
|
||||||
Matrix<float> m = Matrix<float>.FromHlsf(200, 200, hlsf);
|
Matrix<float> m = Matrix.FromHlsf<float>(2000, 2000, hlsf);
|
||||||
m.Fill(1.0f);
|
m.Fill(1.0f);
|
||||||
|
for (int i = 0; i < 20; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < 20; j++) {
|
||||||
|
Console.Write($"{m[i, j]:0.0E+00} \t");
|
||||||
|
}
|
||||||
|
Console.WriteLine();
|
||||||
|
}
|
||||||
|
Console.WriteLine();
|
||||||
Matrix<float> n = m.Add(m);
|
Matrix<float> n = m.Add(m);
|
||||||
for (int i = 0; i < 20; i++)
|
for (int i = 0; i < 20; i++)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user