back to matrix, but trouble in combining L4 predict with matrixparallelqueue
This commit is contained in:
@@ -159,6 +159,14 @@ namespace StgSharp.Mathematics.Internal
|
||||
scalar);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly nint Get(
|
||||
MatrixIntrinsicHandle index
|
||||
)
|
||||
{
|
||||
return this[(int)index];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[InlineArray(MatrixElementType.IntrinsicCount)]
|
||||
@@ -171,4 +179,4 @@ namespace StgSharp.Mathematics.Internal
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,10 +36,10 @@ namespace StgSharp.Mathematics.Internal
|
||||
internal unsafe struct MatrixParallelTask
|
||||
{
|
||||
|
||||
[FieldOffset(0)]public MatrixKernel* Mat1; // 8
|
||||
[FieldOffset(0)] public MatrixKernel* Mat1; // 8
|
||||
[FieldOffset(8)] public MatrixKernel* Mat2; // 8
|
||||
[FieldOffset(16)] public MatrixKernel* Mat3; // 16
|
||||
[FieldOffset(40)] public int Common_k;
|
||||
[FieldOffset(40)] public int CommonK;
|
||||
[FieldOffset(32)] public int CommonX;
|
||||
[FieldOffset(36)] public int CommonY;
|
||||
[FieldOffset(44)] public int i;
|
||||
@@ -65,4 +65,4 @@ namespace StgSharp.Mathematics.Internal
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixCompute.Sequential"
|
||||
// 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.Memory;
|
||||
using StgSharp.Mathematics.Numeric.Runtime;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using unsafe MkProc = delegate* unmanaged[Cdecl]<StgSharp.Mathematics.Internal.MatrixParallelTask*, void>;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric.Linear
|
||||
{
|
||||
public static unsafe partial class MatrixCompute
|
||||
{
|
||||
|
||||
public static void Add(
|
||||
Matrix<float> left,
|
||||
Matrix<float> right,
|
||||
Matrix<float> ans
|
||||
)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(left, nameof(left));
|
||||
ArgumentNullException.ThrowIfNull(right, nameof(right));
|
||||
ArgumentNullException.ThrowIfNull(ans, nameof(ans));
|
||||
if ((left.ColumnLength != right.ColumnLength) ||
|
||||
(left.RowLength != right.RowLength) ||
|
||||
(left.ColumnLength != ans.ColumnLength) ||
|
||||
(left.RowLength != ans.RowLength)) {
|
||||
throw new MatrixDimensionMismatchException<float>(
|
||||
left, right, ans
|
||||
);
|
||||
}
|
||||
MatrixElementType f32 = MatrixElementType.F32;
|
||||
long count = ((long)left.KernelColumnLength) * left.KernelRowLength;
|
||||
|
||||
MatrixKernel<float>* leftPtr = left.Buffer;
|
||||
MatrixKernel<float>* rightPtr = right.Buffer;
|
||||
MatrixKernel<float>* ansPtr = ans.Buffer;
|
||||
MkProc add = (MkProc)NumericalModule.GlobalContext.
|
||||
mat_mk[f32.IntrinsicNode].
|
||||
Get(MatrixIntrinsicHandle.Add);
|
||||
MatrixParallelTask* t = stackalloc MatrixParallelTask[1];
|
||||
|
||||
t->Mat1 = (MatrixKernel*)leftPtr;
|
||||
t->Mat2 = (MatrixKernel*)rightPtr;
|
||||
t->Mat3 = (MatrixKernel*)ansPtr;
|
||||
t->ElementType = MatrixElementType.F64;
|
||||
t->Scalar.Data<long>(0) = count;
|
||||
if (count <= 256)
|
||||
{
|
||||
add(t);
|
||||
|
||||
// .buffer_add(leftPtr, rightPtr, ansPtr, count);
|
||||
|
||||
return;
|
||||
} else if (count <= 1024)
|
||||
{
|
||||
MatrixParallelThread[] pool = MatrixParallel.LeadParallel(1);
|
||||
add(t);
|
||||
MatrixParallel.ReturnParallelResources(ref pool);
|
||||
return;
|
||||
}
|
||||
MatrixParallelWrap taskGroup = MatrixParallel.ScheduleTask<SequentialPredictor>((nuint)add, t);
|
||||
|
||||
taskGroup.LaunchParallel(SleepMode.DeepSleep);
|
||||
taskGroup.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
private sealed class SequentialPredictor : MatrixPredictor<SequentialPredictor>
|
||||
{
|
||||
|
||||
public override bool Predict(
|
||||
Span<CacheLinePrediction> node,
|
||||
out int count
|
||||
)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void Prefetch(
|
||||
nuint origin,
|
||||
ulong policy,
|
||||
Span<byte> cache
|
||||
)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void WriteBack(
|
||||
nuint origin,
|
||||
ulong policy,
|
||||
ReadOnlySpan<byte> cache
|
||||
)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.HighPerformance.Memory;
|
||||
using StgSharp.Internal;
|
||||
using StgSharp.Mathematics;
|
||||
using System;
|
||||
@@ -37,7 +38,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public partial class MatrixCompute
|
||||
public static partial class MatrixCompute
|
||||
{
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@@ -45,8 +46,8 @@ namespace StgSharp.Mathematics.Numeric
|
||||
MatrixKernel<T>* buffer,
|
||||
int colLength,
|
||||
int x,
|
||||
int y)
|
||||
where T: unmanaged, INumber<T>
|
||||
int y
|
||||
) where T : unmanaged, INumber<T>
|
||||
{
|
||||
return (MatrixKernel*)(buffer + (colLength * x + y));
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ using StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Internal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
@@ -48,6 +49,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
||||
internal static unsafe MatrixParallelWrap ScheduleTask<TTask>(
|
||||
nuint computeHandle,
|
||||
MatrixParallelTask* package
|
||||
) where TTask : IPrefetchPredict
|
||||
{
|
||||
@@ -61,8 +63,10 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
long recommendCount = /*count / /**/ 1024;
|
||||
MatrixParallelThread[] pool = LeadParallel((int)recommendCount);
|
||||
MatrixParallelWrap wrap = new MatrixParallelWrap(pool);
|
||||
wrap.TaskQueue = MatrixParallelQueue.Rent(package);
|
||||
MatrixParallelWrap wrap = new(pool)
|
||||
{
|
||||
TaskQueue = MatrixParallelQueue.Rent(package)
|
||||
};
|
||||
return wrap;
|
||||
}
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
queue = new MatrixParallelQueue();
|
||||
}
|
||||
queue._package = pack;
|
||||
queue._maxCount = (ulong)pack->CommonX * (ulong)pack->CommonY;
|
||||
queue._maxCount = ((ulong)pack->CommonX) * ((ulong)pack->CommonY);
|
||||
queue._cursor = 0;
|
||||
return queue;
|
||||
}
|
||||
|
||||
@@ -47,12 +47,12 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
}
|
||||
|
||||
public class MatrixParallelWrap : IDisposable
|
||||
public sealed class MatrixParallelWrap : IDisposable
|
||||
{
|
||||
|
||||
private MatrixParallelThread[] _threads;
|
||||
private bool _disposed = false;
|
||||
private int _count;
|
||||
private bool _disposed;
|
||||
private readonly CountdownEvent _countdownEvent;
|
||||
|
||||
internal MatrixParallelWrap(
|
||||
MatrixParallelThread[] threadArray
|
||||
@@ -60,7 +60,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
_threads = threadArray;
|
||||
IsMultithread = threadArray.Length > 1;
|
||||
_count = threadArray.Length;
|
||||
_countdownEvent = new CountdownEvent(threadArray.Length);
|
||||
}
|
||||
|
||||
public ReadOnlySpan<MatrixParallelThread> Threads
|
||||
@@ -81,11 +81,8 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) {
|
||||
return;
|
||||
}
|
||||
MatrixParallel.ReturnParallelResources(ref _threads);
|
||||
_disposed = true;
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@@ -104,19 +101,30 @@ namespace StgSharp.Mathematics.Numeric
|
||||
|
||||
public void WaitAllWraps()
|
||||
{
|
||||
if (_disposed) {
|
||||
throw new ObjectDisposedException(nameof(MatrixParallelWrap));
|
||||
}
|
||||
SpinWait.SpinUntil(() => Volatile.Read(ref _count) == 0);
|
||||
ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelWrap));
|
||||
_countdownEvent.Wait();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal void ReportThreadAccomplished()
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelWrap));
|
||||
_ = _countdownEvent.Signal();
|
||||
}
|
||||
|
||||
private void Dispose(
|
||||
bool disposing
|
||||
)
|
||||
{
|
||||
if (_disposed) {
|
||||
throw new ObjectDisposedException(nameof(MatrixParallelWrap));
|
||||
return;
|
||||
}
|
||||
Interlocked.Decrement(ref _count);
|
||||
if (disposing)
|
||||
{
|
||||
MatrixParallel.ReturnParallelResources(ref _threads);
|
||||
_countdownEvent.Dispose();
|
||||
}
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixPredictor"
|
||||
// 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.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric.Runtime
|
||||
{
|
||||
public abstract class MatrixPredictor<TSelf> : IPrefetchPredict
|
||||
where TSelf : MatrixPredictor<TSelf>
|
||||
{
|
||||
|
||||
public abstract bool Predict(
|
||||
Span<CacheLinePrediction> node,
|
||||
out int count
|
||||
);
|
||||
|
||||
public abstract void Prefetch(
|
||||
nuint origin,
|
||||
ulong policy,
|
||||
Span<byte> cache
|
||||
);
|
||||
|
||||
public abstract void WriteBack(
|
||||
nuint origin,
|
||||
ulong policy,
|
||||
ReadOnlySpan<byte> cache
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixTaskFactory"
|
||||
// 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.Mathematics;
|
||||
using StgSharp.Mathematics.Internal;
|
||||
using StgSharp.Mathematics.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using hlsfAllocator = StgSharp.Mathematics.Memory.HybridLayerSegregatedFitAllocator;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
internal static unsafe class MatrixParallelFactory
|
||||
{
|
||||
|
||||
private static hlsfAllocator _intermediateResult;
|
||||
|
||||
private static
|
||||
#if NET9_0_OR_GREATER
|
||||
Lock
|
||||
#else
|
||||
object
|
||||
#endif
|
||||
_hlsfLock = new();
|
||||
|
||||
private static SlabAllocator<ScalarPacket> _scalarAllocator ;
|
||||
|
||||
private static SlabAllocator<MatrixParallelTask> _wrapAllocator;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static MatrixParallelTask* CreateBaseTask()
|
||||
{
|
||||
MatrixParallelTask* p = (MatrixParallelTask*)_wrapAllocator.Allocate();
|
||||
Span<byte> s = new Span<byte>((byte*)p, sizeof(MatrixParallelTask));
|
||||
s.Fill(0);
|
||||
return p;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ScalarPacket* CreateScalarPacket()
|
||||
{
|
||||
ScalarPacket* p = (ScalarPacket*)_scalarAllocator.Allocate();
|
||||
return p;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe MatrixParallelTask* FromExistPackage(
|
||||
MatrixParallelTask* exist
|
||||
)
|
||||
{
|
||||
MatrixParallelTask* ptr = (MatrixParallelTask*)_wrapAllocator.Allocate();
|
||||
MatrixParallelTask.Copy(exist, ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
||||
public static void Release(
|
||||
ScalarPacket* source
|
||||
)
|
||||
{
|
||||
_scalarAllocator.Free((nuint)source);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Release(
|
||||
MatrixParallelTask* source
|
||||
)
|
||||
{
|
||||
_wrapAllocator.Free((nuint)source);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static void Init()
|
||||
{
|
||||
if (_intermediateResult is null)
|
||||
{
|
||||
_intermediateResult = new hlsfAllocator(64 * 16 * 16);
|
||||
_scalarAllocator = SlabAllocator.Create<ScalarPacket>(64 * 1024, SlabBufferLayout.Chunked);
|
||||
_wrapAllocator = SlabAllocator.
|
||||
Create<MatrixParallelTask>(128 * 1024, SlabBufferLayout.Chunked);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user