per thread context loading is ok, working on global context
This commit is contained in:
@@ -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,
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user