merge from giga
This commit is contained in:
@@ -1,40 +0,0 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="KernelParallel.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 System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics
|
||||
{
|
||||
internal class KernelParallel { }
|
||||
}
|
||||
@@ -49,7 +49,30 @@ namespace StgSharp.Mathematics
|
||||
try
|
||||
{
|
||||
Matrix<float> result = GCMatrix<float>.FromDefault(left.ColumnLength, left.RowLength);
|
||||
MatrixParallel.PublishTask();
|
||||
unsafe
|
||||
{
|
||||
MatrixParallelTaskPackage* handle = MatrixParallelFactory.CreateBaseTask();
|
||||
handle->Left = left.Buffer;
|
||||
handle->Right = right.Buffer;
|
||||
handle->Result = result.Buffer;
|
||||
handle->ComputeHandle = (IntPtr)InternalIO.Intrinsic.kernel_add;
|
||||
handle->LeftPrimOffset = 0;
|
||||
handle->RightPrimOffset = 0;
|
||||
handle->LeftPrimStride = left.KernelColumnLength;
|
||||
handle->LeftSecOffset = 1;
|
||||
handle->LeftPrimOffset = 0;
|
||||
handle->RightPrimOffset = 0;
|
||||
handle->RightPrimStride = right.KernelColumnLength;
|
||||
handle->RightSecOffset = 1;
|
||||
handle->RightPrimOffset = 0;
|
||||
handle->ResultPrimOffset = 0;
|
||||
handle->ResultPrimStride = result.KernelColumnLength;
|
||||
handle->ResultSecOffset = 1;
|
||||
handle->ResultPrimOffset = 0;
|
||||
handle->PrimCount = left.KernelRowLength;
|
||||
handle->SecCount = left.KernelColumnLength;
|
||||
}
|
||||
|
||||
MatrixParallel.LaunchParallel(SleepMode.DeepSleep);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -71,6 +71,12 @@ namespace StgSharp.Mathematics
|
||||
set => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
internal override unsafe MatrixKernel* Buffer
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _buffer;
|
||||
}
|
||||
|
||||
public override unsafe ref MatrixKernel GetMatrixKernel(int column, int row)
|
||||
{
|
||||
AssertIndex(column, row);
|
||||
|
||||
@@ -75,6 +75,12 @@ namespace StgSharp.Mathematics
|
||||
set => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
internal override unsafe MatrixKernel* Buffer
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => k_buffer;
|
||||
}
|
||||
|
||||
public override unsafe ref MatrixKernel GetMatrixKernel(int column, int row)
|
||||
{
|
||||
AssertIndex(column, row);
|
||||
|
||||
@@ -32,9 +32,9 @@ using StgSharp.HighPerformance;
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using hlsfAllocator = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator;
|
||||
using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
|
||||
|
||||
// using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
|
||||
|
||||
namespace StgSharp.Mathematics
|
||||
{
|
||||
@@ -68,15 +68,23 @@ namespace StgSharp.Mathematics
|
||||
set;
|
||||
} = 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;
|
||||
|
||||
protected int KernelColumnLength { get; init; }
|
||||
internal abstract MatrixKernel<T>* Buffer { get; }
|
||||
|
||||
protected int KernelRowLength { get; init; }
|
||||
protected internal int KernelColumnLength { get; init; }
|
||||
|
||||
protected internal int KernelRowLength { get; init; }
|
||||
|
||||
public static Matrix<T> FromDefault(int columnLength, int rowLength)
|
||||
{
|
||||
@@ -100,14 +108,14 @@ namespace StgSharp.Mathematics
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public abstract MatrixSegmentEnumeration GetColumnEnumeration();
|
||||
public abstract MatrixSegmentEnumeration<T> GetColumnEnumeration();
|
||||
|
||||
public abstract ref MatrixKernel GetMatrixKernel(int column, int row);
|
||||
public abstract ref MatrixKernel<T> GetMatrixKernel(int column, int row);
|
||||
|
||||
public abstract MatrixSegmentEnumeration GetRowEnumeration();
|
||||
public abstract MatrixSegmentEnumeration<T> GetRowEnumeration();
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public abstract MatrixSegmentEnumeration GetSequentialEnumeration();
|
||||
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);
|
||||
@@ -125,38 +133,39 @@ namespace StgSharp.Mathematics
|
||||
|
||||
}
|
||||
|
||||
public readonly unsafe ref struct MatrixSegmentEnumeration(
|
||||
MatrixKernel* source,
|
||||
public readonly unsafe ref struct MatrixSegmentEnumeration<T>(
|
||||
MatrixKernel<T>* source,
|
||||
int stride,
|
||||
int size,
|
||||
int secondaryStride,
|
||||
int secondarySize)
|
||||
where T: unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
private readonly MatrixKernel* _source = source;
|
||||
private readonly MatrixKernel<T>* _source = source;
|
||||
private readonly int _secondarySize = secondarySize;
|
||||
private readonly int _secondaryStride = secondaryStride;
|
||||
|
||||
private readonly int _size = size;
|
||||
private readonly int _stride = stride;
|
||||
|
||||
public Enumerator GetEnumerator()
|
||||
public Enumerator<T> GetEnumerator()
|
||||
{
|
||||
return new Enumerator(in this);
|
||||
return new Enumerator<T>(in this);
|
||||
}
|
||||
|
||||
public ref struct Enumerator(in MatrixSegmentEnumeration enumeration)
|
||||
public ref struct Enumerator<T>(in MatrixSegmentEnumeration<T> enumeration) where T: unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
private MatrixKernel* _current = enumeration._source - enumeration._stride;
|
||||
private readonly MatrixKernel* _end = enumeration._source + enumeration._size;
|
||||
private MatrixKernel<T>* _current = enumeration._source - enumeration._stride;
|
||||
private readonly MatrixKernel<T>* _end = enumeration._source + enumeration._size;
|
||||
private readonly int _secondarySize = enumeration._secondarySize;
|
||||
private readonly int _secondaryStride = enumeration._secondaryStride;
|
||||
private readonly int _stride = enumeration._stride;
|
||||
|
||||
public readonly Segment Current
|
||||
public readonly MatrixSegment<T> Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new(_current, _secondarySize, _secondaryStride);
|
||||
}
|
||||
|
||||
@@ -170,14 +179,15 @@ namespace StgSharp.Mathematics
|
||||
|
||||
}
|
||||
|
||||
public readonly unsafe ref struct Segment(MatrixKernel* source, int size, int stride)
|
||||
public readonly unsafe ref struct MatrixSegment<T>(MatrixKernel<T>* source, int size, int stride)
|
||||
where T: unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
private readonly MatrixKernel* _source = source;
|
||||
private readonly MatrixKernel<T>* _source = source;
|
||||
private readonly int _size = size;
|
||||
private readonly int _stride = stride;
|
||||
|
||||
public ref MatrixKernel this[int index]
|
||||
public ref MatrixKernel<T> this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
@@ -188,22 +198,22 @@ namespace StgSharp.Mathematics
|
||||
}
|
||||
}
|
||||
|
||||
public Enumerator GetEnumerator()
|
||||
public Enumerator<T> GetEnumerator()
|
||||
{
|
||||
return new Enumerator(_source, _size, _stride);
|
||||
return new Enumerator<T>(_source, _size, _stride);
|
||||
}
|
||||
|
||||
public ref struct Enumerator(MatrixKernel* source, int count, int stride)
|
||||
public ref struct Enumerator<T>(MatrixKernel<T>* source, int count, int stride) where T: unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
private readonly MatrixKernel* _end = source + (count * stride);
|
||||
private readonly MatrixKernel<T>* _end = source + (count * stride);
|
||||
private readonly int _stride = stride;
|
||||
|
||||
public MatrixKernel* Current
|
||||
public MatrixKernel<T>* Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private set;
|
||||
} = source - stride;
|
||||
|
||||
|
||||
@@ -62,6 +62,11 @@ namespace StgSharp.Mathematics
|
||||
internal unsafe struct MatrixParallelTaskPackage
|
||||
{
|
||||
|
||||
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
|
||||
@@ -126,6 +131,6 @@ namespace StgSharp.Mathematics
|
||||
public long ReservedLong0; // 8
|
||||
public long ReservedLong1; // 8
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
+40
-3
@@ -28,28 +28,54 @@
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.HighPerformance;
|
||||
using StgSharp.HighPerformance.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using hlsfAllocator = StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator;
|
||||
|
||||
namespace StgSharp.Mathematics
|
||||
{
|
||||
internal static unsafe class MatrixParallelFactory
|
||||
{
|
||||
|
||||
private static readonly SlabAllocator<ScalarPacket> _scalarAllocator = SlabAllocator<ScalarPacket>.Create(64, SlabBufferLayout.Chunked, true);
|
||||
private static hlsfAllocator _intermediateResult;
|
||||
|
||||
private static readonly SlabAllocator<MatrixParallelTaskPackage> _wrapAllocator = SlabAllocator<MatrixParallelTaskPackage>.Create(64, SlabBufferLayout.Chunked, true);
|
||||
private static
|
||||
#if NET9_0_OR_GREATER
|
||||
Lock
|
||||
#else
|
||||
object
|
||||
#endif
|
||||
_hlsfLock = new();
|
||||
|
||||
private static SlabAllocator<ScalarPacket> _scalarAllocator ;
|
||||
|
||||
private static SlabAllocator<MatrixParallelTaskPackage> _wrapAllocator ;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static MatrixParallelTaskPackage* CreateBaseTask()
|
||||
{
|
||||
MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)_wrapAllocator.Allocate();
|
||||
Span<byte> s = new Span<byte>((byte*)p, sizeof(MatrixParallelTaskPackage));
|
||||
s.Fill(0);
|
||||
return p;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ScalarPacket* CreateScalarPacket()
|
||||
{
|
||||
ScalarPacket* p = (ScalarPacket*)_scalarAllocator.Allocate();
|
||||
return p;
|
||||
}
|
||||
|
||||
<<<<<<< HEAD:StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskWrapFactory.cs
|
||||
public static MatrixParallelTaskPackage* FromTaskWrap(MatrixParallelTaskPackage* source, int offset)
|
||||
{
|
||||
MatrixParallelTaskPackage* p = (MatrixParallelTaskPackage*)_wrapAllocator.Allocate();
|
||||
@@ -62,11 +88,15 @@ 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)
|
||||
{
|
||||
if (source->Scalar != null) {
|
||||
@@ -75,8 +105,10 @@ namespace StgSharp.Mathematics
|
||||
_wrapAllocator.Free((nuint)source);
|
||||
}
|
||||
|
||||
public static void SliceTask(MatrixParallelTaskPackage* source, Span<IntPtr> group)
|
||||
[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;
|
||||
@@ -105,6 +137,11 @@ namespace StgSharp.Mathematics
|
||||
((MatrixParallelTaskPackage*)group[targetIndex])->PrimCount++;
|
||||
}
|
||||
}
|
||||
=======
|
||||
_scalarAllocator = SlabAllocator<ScalarPacket>.Create(64, SlabBufferLayout.Chunked, true);
|
||||
_wrapAllocator = SlabAllocator<MatrixParallelTaskPackage>.Create(64, SlabBufferLayout.Chunked, true);
|
||||
_intermediateResult = new hlsfAllocator((nuint)(sizeof(MatrixKernel) * 64));
|
||||
>>>>>>> 8007eee8461498a85a9515e8fd076f03cb552b64:StgSharp.Common/Mathematics/Linear/MatrixParallel.TaskFactory.cs
|
||||
}
|
||||
|
||||
}
|
||||
@@ -33,6 +33,7 @@ using StgSharp.HighPerformance.Memory;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO.Pipes;
|
||||
using System.Linq;
|
||||
@@ -68,10 +69,7 @@ namespace StgSharp.Mathematics
|
||||
|
||||
private static Thread[] _threads;
|
||||
private static MatrixParallelWrap[] _wraps;
|
||||
private static readonly (int a, int b)[] LookupTable =
|
||||
[(1, 1), (0, 2), (3, 0), (2, 1),
|
||||
(1, 2), (0, 3), (3, 1), (2, 2), (1, 3),
|
||||
(0, 4), (3, 2), (2, 3), (1, 4), (0, 5),];
|
||||
private static readonly (int a, int b)[] LookupTable = [ (1, 1), (0, 2), (3, 0), (2, 1), (1, 2), (0, 3), (3, 1), (2, 2), (1, 3), (0, 4), (3, 2), (2, 3), (1, 4), (0, 5), ];
|
||||
private static volatile int RemainThreadCount = 0;
|
||||
|
||||
private static readonly object _lock = new();
|
||||
@@ -79,9 +77,13 @@ namespace StgSharp.Mathematics
|
||||
|
||||
public static bool SupportParallel { get; } = Environment.ProcessorCount > 6;
|
||||
|
||||
public static bool IsParallelMeaningful { get; } = Environment.ProcessorCount > 6;
|
||||
public static bool IsParallelMeaningful { get; } = Environment.ProcessorCount > 6 && IsInitialized;
|
||||
|
||||
internal static MatrixParallelWrap.Handle LeaderHandle { get; private set; }
|
||||
public static MatrixParallelWrap.Handle LeaderHandle { get; private/**/ set; }
|
||||
|
||||
private static bool IsInitialized { get; set; } = false;
|
||||
|
||||
private static Queue<IntPtr> LeaderTask { get; set; }
|
||||
|
||||
public static void EndParallel()
|
||||
{
|
||||
@@ -105,18 +107,41 @@ namespace StgSharp.Mathematics
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
if (IsInitialized) {
|
||||
return;
|
||||
}
|
||||
MatrixParallelFactory.Init();
|
||||
int count = Environment.ProcessorCount - 4;
|
||||
count = count == 5 ? 4 : count;
|
||||
int _3wrap = 0, _4wrap = 0;
|
||||
_threads = new Thread[count];
|
||||
if (count < 6) { } else if (count == 6) { } else { }
|
||||
if (count < 6)
|
||||
{
|
||||
_wraps = [ new MatrixParallelWrap(0, count, true) ];
|
||||
} else if (count == 6)
|
||||
{
|
||||
_wraps = [ new MatrixParallelWrap(0, 3, true), new MatrixParallelWrap(3, 3, false) ];
|
||||
} else
|
||||
{
|
||||
(int _3wrap, int _4wrap) = FastDecompose(count);
|
||||
_wraps = new MatrixParallelWrap[_3wrap + _4wrap];
|
||||
int id = 0, idx = 0;
|
||||
for (; idx < _3wrap; idx++, id += 3) {
|
||||
_wraps[idx] = new MatrixParallelWrap(idx, 3, id == 0);
|
||||
}
|
||||
for (; idx < _3wrap + _4wrap; idx++, id += 4) {
|
||||
_wraps[idx] = new MatrixParallelWrap(id, 4, id == 0);
|
||||
}
|
||||
}
|
||||
LeaderHandle = new MatrixParallelWrap.Handle(0, _wraps[0]);
|
||||
LeaderTask = LeaderHandle.Wrap.GetWorkerTaskQueue(0);
|
||||
IsInitialized = true;
|
||||
}
|
||||
|
||||
public static void LaunchParallel(SleepMode afterWork)
|
||||
{
|
||||
RemainThreadCount = _threads.Length;
|
||||
_AfterWork = (ThreadPriority)afterWork;
|
||||
SetAllWrap();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@@ -125,10 +150,22 @@ namespace StgSharp.Mathematics
|
||||
Monitor.Enter(_lock);
|
||||
}
|
||||
|
||||
public static void PublishTask() { }
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void MatrixParallelLeaderWork()
|
||||
{
|
||||
MatrixParallelWrap.ExecuteTaskPackage(LeaderTask);
|
||||
_ = Interlocked.Decrement(ref RemainThreadCount);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void ResetThreadCount()
|
||||
{
|
||||
RemainThreadCount = _threads.Length;
|
||||
}
|
||||
|
||||
public static void SetAllWrap()
|
||||
{
|
||||
ResetThreadCount();
|
||||
foreach (MatrixParallelWrap item in _wraps) {
|
||||
_ = item.SchedulerReset.Set();
|
||||
}
|
||||
@@ -243,12 +280,13 @@ namespace StgSharp.Mathematics
|
||||
// TODO task publication to worker in wrap
|
||||
wrap.SetAllWorker();
|
||||
ExecuteTaskPackage(taskQueue);
|
||||
_ = Interlocked.Decrement(ref RemainThreadCount);
|
||||
self.Priority = _AfterWork;
|
||||
} while (true);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static unsafe void ExecuteTaskPackage(Queue<IntPtr> taskQueue)
|
||||
public static unsafe void ExecuteTaskPackage(Queue<IntPtr> taskQueue)
|
||||
{
|
||||
while (taskQueue.TryDequeue(out nint handle))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user