find solution to combinition of predictor and queue

This commit is contained in:
Nitload
2026-03-20 00:15:58 +08:00
parent dcc2bb699a
commit 48c308ae3c
14 changed files with 116 additions and 159 deletions
@@ -1,9 +1,9 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Cordination"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// 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
@@ -25,7 +25,7 @@
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
namespace StgSharp.Mathematics.Numeric
namespace StgSharp.Mathematics.Numeric.Graphics
{
public enum CoordinatePlain
{
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="MatrixCompute.Sequential"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// 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
@@ -33,18 +33,14 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using unsafe MkProc = delegate* unmanaged[Cdecl]<StgSharp.Mathematics.Internal.MatrixParallelTask*, void>;
using unsafe MkProc = delegate* unmanaged[Cdecl]<StgSharp.Mathematics.Numeric.Runtime.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
)
public static void Add(Matrix<float> left, Matrix<float> right, Matrix<float> ans)
{
ArgumentNullException.ThrowIfNull(left, nameof(left));
ArgumentNullException.ThrowIfNull(right, nameof(right));
@@ -97,28 +93,17 @@ namespace StgSharp.Mathematics.Numeric.Linear
private sealed class SequentialPredictor : MatrixPredictor<SequentialPredictor>
{
public override bool Predict(
Span<CacheLinePrediction> node,
out int count
)
public override bool Predict(Span<CacheLinePrediction> node, out int count)
{
throw new NotImplementedException();
}
public override void Prefetch(
nuint origin,
ulong policy,
Span<byte> cache
)
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
)
public override void WriteBack(nuint origin, ulong policy, ReadOnlySpan<byte> cache)
{
throw new NotImplementedException();
}
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="MatrixParallel.Publish"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// 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
@@ -28,7 +28,7 @@
using StgSharp.Collections;
using StgSharp.HighPerformance.Memory;
using StgSharp.Mathematics;
using StgSharp.Mathematics.Internal;
using StgSharp.Mathematics.Numeric.Runtime;
using System;
using System.Collections.Generic;
using System.ComponentModel;
@@ -48,10 +48,8 @@ namespace StgSharp.Mathematics.Numeric
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe MatrixParallelWrap ScheduleTask<TTask>(
nuint computeHandle,
MatrixParallelTask* package
) where TTask : IPrefetchPredict
internal static unsafe MatrixParallelWrap ScheduleTask<TTask>(nuint computeHandle, MatrixParallelTask* package)
where TTask: IPrefetchPredict
{
/*
int primCount = package->PrimCount;
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="MatrixParallelQueue"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// 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
@@ -27,7 +27,7 @@
// -----------------------------------------------------------------------
using Microsoft.VisualBasic.FileIO;
using StgSharp.HighPerformance.Memory;
using StgSharp.Mathematics.Internal;
using StgSharp.Mathematics.Numeric.Runtime;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
@@ -42,8 +42,6 @@ namespace StgSharp.Mathematics.Numeric
internal unsafe class MatrixParallelQueue
{
// TODO 为各种不同的计算策略设计专门的预测器,以适配L4
private static ConcurrentStack<MatrixParallelQueue> _recycle = [];
private MatrixParallelTask* _package;
private ulong _cursor;
@@ -53,9 +51,14 @@ namespace StgSharp.Mathematics.Numeric
public MatrixParallelTask* Package => _package;
public MatrixElementType ElementType { get; private set; }
public MatrixIntrinsicHandle Handle { get; private set; }
public static MatrixParallelQueue Rent(
MatrixParallelTask* pack
)
MatrixElementType elementType,
MatrixIntrinsicHandle handle,
MatrixParallelTask* pack)
{
if (!_recycle.TryPop(out MatrixParallelQueue? queue)) {
queue = new MatrixParallelQueue();
@@ -63,23 +66,21 @@ namespace StgSharp.Mathematics.Numeric
queue._package = pack;
queue._maxCount = ((ulong)pack->CommonX) * ((ulong)pack->CommonY);
queue._cursor = 0;
queue.ElementType = elementType;
queue.Handle = handle;
return queue;
}
public static void Return(
MatrixParallelQueue queue
)
public static void Return(MatrixParallelQueue queue)
{
_recycle.Push(queue);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (ulong count, ulong offset) Steal(
ulong request
)
public bool Steal(MatrixParallelTask* task)
{
ulong cursor = Interlocked.Add(ref _cursor, request) - request;
return (Math.Min(request, Math.Max(0, _maxCount - cursor)), cursor);
// TODO 使用提取的内容填充task
throw new NotImplementedException();
}
}
@@ -0,0 +1,64 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixParallelTask"
// 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.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Numeric.Runtime
{
[StructLayout(LayoutKind.Explicit)]
internal unsafe struct MatrixParallelTask
{
[FieldOffset(0)] public MatrixKernel* Mat1; // 8
[FieldOffset(8)] public MatrixKernel* Mat2; // 8
[FieldOffset(16)] public MatrixKernel* Mat3; // 16
[FieldOffset(40)] public int CommonK;
[FieldOffset(32)] public int CommonX;
[FieldOffset(36)] public int CommonY;
[FieldOffset(44)] public int i;
[FieldOffset(48)] public int j;
[FieldOffset(52)] public int k;
[FieldOffset(56)] public MatrixElementType ElementType; // 56
[FieldOffset(24)] public nint Span; // 24
[FieldOffset(64)] public ScalarPacket Scalar;
public MatrixParallelTask()
{
Unsafe.SkipInit(out this);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Copy(MatrixParallelTask* source, MatrixParallelTask* target)
{
Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTask), sizeof(MatrixParallelTask));
}
}
}
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="MatrixParallelThread"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// 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
@@ -26,7 +26,7 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Internal;
using StgSharp.Mathematics.Internal;
using StgSharp.Mathematics.Numeric.Runtime;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -53,9 +53,7 @@ namespace StgSharp.Mathematics.Numeric
private readonly ManualResetEventSlim _resetEvent;
private readonly Thread _managedThread;
internal MatrixParallelThread(
int poolId
)
internal MatrixParallelThread(int poolId)
{
_resetEvent = new ManualResetEventSlim(false);
_managedThread = new Thread(MatrixParallelWorkLoad);
@@ -97,13 +95,9 @@ namespace StgSharp.Mathematics.Numeric
[LibraryImport(Native.LibName, EntryPoint = "sn_set_thread_affinity")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
internal static partial void SetThreadAffinity(
int coreId
);
internal static partial void SetThreadAffinity(int coreId);
protected virtual void Dispose(
bool disposing
)
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="MatrixPredictor"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// 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
@@ -34,26 +34,16 @@ using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric.Runtime
{
public abstract class MatrixPredictor<TSelf> : IPrefetchPredict
where TSelf : MatrixPredictor<TSelf>
public abstract class MatrixPredictor<TSelf> : IPrefetchPredict where TSelf: MatrixPredictor<TSelf>
{
public abstract bool Predict(
Span<CacheLinePrediction> node,
out int count
);
protected MatrixPredictor() { }
public abstract void Prefetch(
nuint origin,
ulong policy,
Span<byte> cache
);
public abstract bool Predict(Span<CacheLinePrediction> node, out int count);
public abstract void WriteBack(
nuint origin,
ulong policy,
ReadOnlySpan<byte> cache
);
public abstract void Prefetch(nuint origin, ulong policy, Span<byte> cache);
public abstract void WriteBack(nuint origin, ulong policy, ReadOnlySpan<byte> cache);
}
}