per thread context loading is ok, working on global context

This commit is contained in:
Nitload
2026-02-25 00:20:09 +08:00
parent 7ee9c749e5
commit b71515b2d4
9 changed files with 225 additions and 218 deletions
+41 -3
View File
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="MatrixParallel"
// 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,10 +27,14 @@
// -----------------------------------------------------------------------
using StgSharp.Collections;
using StgSharp.HighPerformance.Memory;
using StgSharp.Internal;
using StgSharp.Internal.Intrinsic;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
@@ -43,6 +47,8 @@ namespace StgSharp.Mathematics.Numeric
private static CapacityFixedStack<MatrixParallelThread> _threads;
private static ConcurrentDictionary<SIMDID, nint> contextMap;
private static SlabAllocator<IntrinsicContext> contextAllocator;
public static bool IsParallelSupported { get; } = Environment.ProcessorCount > 6;
@@ -51,6 +57,22 @@ namespace StgSharp.Mathematics.Numeric
private static bool IsInitialized { get; set; } = false;
internal static unsafe IntrinsicContext* GetContext(SIMDID simd)
{
if (!IsParallelSupported) {
return null;
}
if (contextMap.TryGetValue(simd, out nint handle))
{
return (IntrinsicContext*)handle;
} else
{
IntrinsicContext* context = (IntrinsicContext*)contextAllocator.Allocate();
return context;
}
}
internal static void Init()
{
if (IsInitialized) {
@@ -63,16 +85,32 @@ namespace StgSharp.Mathematics.Numeric
}
MatrixParallelFactory.Init();
contextAllocator = SlabAllocator<IntrinsicContext>.Create(4, SlabBufferLayout.Chunked, false);
int count = Environment.ProcessorCount - 4;
contextMap = [];
int count = Environment.ProcessorCount;
_threads = new(count);
for (int i = 0; i < count; i++)
{
MatrixParallelThread thread = new MatrixParallelThread(i);
_threads.Push(thread);
}
if (contextMap.Keys.Count == 1)
{
for (int i = 0; i < 4; i++)
{
MatrixParallelThread thread = _threads.Pop();
thread.Dispose();
}
} else
{
// TODO 排序并丢弃四个能力最弱线程
}
_awaitingLeaderHandle = new AwaitingQueue();
IsInitialized = true;
}
[LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
private static unsafe partial void LoadIntrinsicFunction(IntrinsicContext* context, ulong id);
}
}
@@ -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
@@ -27,18 +27,20 @@
// -----------------------------------------------------------------------
using StgSharp.Internal;
using StgSharp.Internal.Intrinsic;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric
{
internal enum MatrixThreadState : int
{
Ready = 0,
Running = 1,
Idle = 2,
}
public unsafe partial class MatrixParallelThread : IDisposable
{
@@ -47,9 +49,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);
@@ -65,9 +65,11 @@ namespace StgSharp.Mathematics.Numeric
internal IntrinsicContext* Intrinsic { get; set; }
internal bool IsSingle { get; set; }
internal MatrixParallelWrap BindedWrap { get; set; }
internal RoleType Role { get; set; }
internal MatrixThreadState state { get; set; }
internal SIMDID Simd { get; set; }
@@ -78,9 +80,7 @@ namespace StgSharp.Mathematics.Numeric
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void ExecuteTaskPackage(
MatrixParallelQueue taskQueue
)
public static unsafe void ExecuteTaskPackage(MatrixParallelQueue taskQueue)
{
MatrixParallelTaskPackage* p = taskQueue.Package;
switch (p->ComputeMode.OperationStyle)
@@ -118,42 +118,22 @@ namespace StgSharp.Mathematics.Numeric
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Restart()
{
if (Role == RoleType.Leader)
{
LeaderEvent?.Set();
} else
{
_resetEvent.Set();
}
_resetEvent.Set();
}
internal void MatrixParallelLeader()
{
Thread current = Thread.CurrentThread;
current.Priority = ThreadPriority.Highest;
LeaderEvent?.Wait();
LeaderEvent?.Reset();
// Console.WriteLine("Leader is computing");
BindedWrap.ReportThreadAccomplished();
// Console.WriteLine("leader's computing is done");
Role = RoleType.Worker;
LeaderEvent = null;
current.Priority = BindedWrap.AfterWork;
}
[DllImport(
dllName: Native.LibName,
EntryPoint = "sn_get_simd_level_local",
CallingConvention = CallingConvention.Cdecl)]
internal static extern ulong GetSimdId();
[DllImport(
dllName:Native.LibName,
EntryPoint = "sn_set_thread_affinity",
CallingConvention = CallingConvention.Cdecl)]
internal static extern void SetThreadAffinity(
int coreId
);
internal static extern void SetThreadAffinity(int coreId);
protected virtual void Dispose(
bool disposing
)
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
@@ -162,7 +142,6 @@ namespace StgSharp.Mathematics.Numeric
_resetEvent.Dispose();
_managedThread.Join();
}
disposedValue = true;
}
}
@@ -171,8 +150,10 @@ namespace StgSharp.Mathematics.Numeric
{
// Bind OS-thread affinity once when the worker starts running
SetThreadAffinity(PoolId);
Thread.Sleep(0);
Thread.Sleep(1);
SIMDID feature = new();
feature.Mask = GetSimdId();
_context = (nint*)MatrixParallel.GetContext(feature);
while (true)
{
// Thread self = Thread.CurrentThread;
@@ -180,35 +161,17 @@ namespace StgSharp.Mathematics.Numeric
_resetEvent.Reset();
AfterWork = BindedWrap.AfterWork;
_managedThread.Priority = ThreadPriority.Highest;
switch (Role)
if (IsSingle)
{
case RoleType.Worker://worker
// _ = Interlocked.Decrement(ref RemainThreadCount);
// Console.WriteLine($"worker {Environment.CurrentManagedThreadId} start");
ExecuteTaskPackage(BindedWrap.TaskQueue);
BindedWrap.ReportThreadAccomplished();
// Console.WriteLine($"worker {Environment.CurrentManagedThreadId} end");
break;
case RoleType.SingleExternal:
// TODO 将外部线程绑定到当前线程上,并执行任务包
break;
default:
break;
// TODO 将外部线程绑定到当前线程上,并执行任务包
} else
{
ExecuteTaskPackage(BindedWrap.TaskQueue);
BindedWrap.ReportThreadAccomplished();
}
_managedThread.Priority = AfterWork;
}
}
internal enum RoleType : int
{
Worker = 0,
Leader = 1,
SingleExternal = 2,
}
}
}
@@ -1,27 +1,30 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixParallelWrap"
// file="MatrixParallelWrap.cs"
// 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
// 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.
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -52,12 +55,8 @@ namespace StgSharp.Mathematics.Numeric
private MatrixParallelThread[] _threads;
private bool _disposed = false;
private int _count;
private ManualResetEventSlim _leaderEvent = new ManualResetEventSlim(false);
public ThreadPriority AfterWork { get; private set; }
internal MatrixParallelWrap(
MatrixParallelThread[] threadArray
)
internal MatrixParallelWrap(MatrixParallelThread[] threadArray)
{
_threads = threadArray;
IsMultithread = threadArray.Length > 1;
@@ -77,6 +76,8 @@ namespace StgSharp.Mathematics.Numeric
}
}
public ThreadPriority AfterWork { get; private set; }
private bool IsMultithread { get; init; }
public void Dispose()
@@ -89,22 +90,14 @@ namespace StgSharp.Mathematics.Numeric
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MatrixParallelThread GetLeader()
{
MatrixParallelThread leader = Threads[1];
leader.Role = MatrixParallelThread.RoleType.Leader;
leader.LeaderEvent = _leaderEvent;
return leader;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void LaunchParallel(
SleepMode afterWork
)
public void LaunchParallel(SleepMode afterWork)
{
AfterWork = (ThreadPriority)afterWork;
MatrixParallelThread leader = GetLeader();
leader.MatrixParallelLeader();
foreach (MatrixParallelThread thread in _threads)
{
thread.AfterWork = AfterWork;
thread.BindedWrap = this;
}
WaitAllWraps();
}