back to matrix, but trouble in combining L4 predict with matrixparallelqueue

This commit is contained in:
Youmu Liu
2026-03-20 20:04:45 +08:00
parent 48c308ae3c
commit 0ac7b2b71f
5 changed files with 113 additions and 15 deletions
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric.Runtime
{
internal class MatrixParallel
{
public MatrixParallelTask BaseTask { get; set; }
public
}
}
@@ -0,0 +1,71 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixParallelQueue.Concurrent"
// 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric.Runtime
{
internal unsafe class ConcurrentPointerQueue
{
private nuint* _current;
private int[] _length;
private nuint[] _queue;
private int _bucketIndex;
private int _head;
private int _tail;
public ConcurrentPointerQueue()
{
_queue = new nuint[8];
}
public void Enqueue(
nuint* pointer,
int count
)
{
_head++;
_head %= _queue.Length;
_bucketIndex = 0;
_queue[_head] = (nuint)pointer;
_length[_head] = count;
}
public int TryDequeue(
Span<nuint> span
)
{
throw new NotImplementedException();
}
}
}
@@ -2,8 +2,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixParallelQueue" // file="MatrixParallelQueue"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload Space // AuthorGroup: Nitload
// Copyright (c) Nitload Space. All rights reserved. // Copyright (c) Nitload. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
@@ -42,14 +42,13 @@ namespace StgSharp.Mathematics.Numeric
internal unsafe class MatrixParallelQueue internal unsafe class MatrixParallelQueue
{ {
private static ConcurrentStack<MatrixParallelQueue> _recycle = []; private static readonly ConcurrentStack<MatrixParallelQueue> _recycle = [];
private MatrixParallelTask* _package;
private ulong _cursor; private ulong _cursor;
private ulong _maxCount; private ulong _maxCount;
private MatrixParallelQueue() { } private MatrixParallelQueue() { }
public MatrixParallelTask* Package => _package; public MatrixParallelTask* BaseTask { get; private set; }
public MatrixElementType ElementType { get; private set; } public MatrixElementType ElementType { get; private set; }
@@ -58,12 +57,13 @@ namespace StgSharp.Mathematics.Numeric
public static MatrixParallelQueue Rent( public static MatrixParallelQueue Rent(
MatrixElementType elementType, MatrixElementType elementType,
MatrixIntrinsicHandle handle, MatrixIntrinsicHandle handle,
MatrixParallelTask* pack) MatrixParallelTask* pack
)
{ {
if (!_recycle.TryPop(out MatrixParallelQueue? queue)) { if (!_recycle.TryPop(out MatrixParallelQueue? queue)) {
queue = new MatrixParallelQueue(); queue = new MatrixParallelQueue();
} }
queue._package = pack; queue.BaseTask = pack;
queue._maxCount = ((ulong)pack->CommonX) * ((ulong)pack->CommonY); queue._maxCount = ((ulong)pack->CommonX) * ((ulong)pack->CommonY);
queue._cursor = 0; queue._cursor = 0;
queue.ElementType = elementType; queue.ElementType = elementType;
@@ -71,13 +71,17 @@ namespace StgSharp.Mathematics.Numeric
return queue; return queue;
} }
public static void Return(MatrixParallelQueue queue) public static void Return(
MatrixParallelQueue queue
)
{ {
_recycle.Push(queue); _recycle.Push(queue);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Steal(MatrixParallelTask* task) public bool Steal(
MatrixParallelTask* task
)
{ {
// TODO 使用提取的内容填充task // TODO 使用提取的内容填充task
throw new NotImplementedException(); throw new NotImplementedException();
@@ -2,8 +2,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixParallelThread" // file="MatrixParallelThread"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload Space // AuthorGroup: Nitload
// Copyright (c) Nitload Space. All rights reserved. // Copyright (c) Nitload. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
@@ -53,7 +53,9 @@ namespace StgSharp.Mathematics.Numeric
private readonly ManualResetEventSlim _resetEvent; private readonly ManualResetEventSlim _resetEvent;
private readonly Thread _managedThread; private readonly Thread _managedThread;
internal MatrixParallelThread(int poolId) internal MatrixParallelThread(
int poolId
)
{ {
_resetEvent = new ManualResetEventSlim(false); _resetEvent = new ManualResetEventSlim(false);
_managedThread = new Thread(MatrixParallelWorkLoad); _managedThread = new Thread(MatrixParallelWorkLoad);
@@ -95,9 +97,13 @@ namespace StgSharp.Mathematics.Numeric
[LibraryImport(Native.LibName, EntryPoint = "sn_set_thread_affinity")] [LibraryImport(Native.LibName, EntryPoint = "sn_set_thread_affinity")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] [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) if (!_disposed)
{ {
@@ -143,7 +149,7 @@ namespace StgSharp.Mathematics.Numeric
} else } else
{ {
MatrixParallelQueue taskQueue = BindedWrap.TaskQueue; MatrixParallelQueue taskQueue = BindedWrap.TaskQueue;
MatrixParallelTask* p = taskQueue.Package; MatrixParallelTask* p = taskQueue.BaseTask;
/* /*
if (0 > (int)p ->ComputeHandle) { if (0 > (int)p ->ComputeHandle) {
SpecialBufferCompute(p); SpecialBufferCompute(p);