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
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<Root Type="DevExpress.CodeRush.Formatting.UI.Options.SpacesOptionsContainer">
<Options Language="C#">
<Options>
<Item Id="{a4f7d021-5762-49a9-956e-040794d03b98}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.BinaryExpressionSpacesOption" />
<Item Id="{a957508f-bb2d-484c-b792-7eba9a44f0d4}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.RelationalExpressionSpacesOption" />
<Item Id="{12b5408f-751d-4709-908f-d269e40b26ab}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.BitwiseExpressionSpacesOption" />
<Item Id="{1be4039d-43ba-4867-b1c6-1453ef76638b}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.LogicalExpressionSpacesOption" />
<Item Id="{8e1642cf-2fd0-4bd8-b0f9-a289bf7f889a}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.UnaryExpressionSpacesOption" />
<Item Id="{43888577-23ae-4e8c-9103-b0e8b006dffd}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.TernaryExpressionSpacesOption" />
<Item Id="{6bd37f4f-1c00-4c06-a83a-2746936e6c8b}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.AssignmentExpressionSpacesOption" />
<Item Id="{07d202aa-0580-489b-9883-c3a9d2f750f6}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.ExpressionSpacesOption" />
<Item Id="{b6cc94db-d940-47bc-9e4c-46e7d397e829}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.StatementSpacesOption" />
<Item Id="{20314921-1cb7-4b58-a259-e7200cfd1014}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.ArgumentListSpacesOption" />
<Item Id="{b031d570-6511-4aa1-8dbb-075d09d9ed72}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.ParameterListSpacesOption" />
<Item Id="{ca5e97ed-0399-47cd-8bc2-8c2677f273ed}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.InitializerSpacesOption" />
<Item Id="{bd007738-8ed3-480b-8618-0c8841c68fb1}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.LambdaExpressionSpacesOption" />
<Item Id="{e677064b-b741-49b6-b6ad-b2634258bbed}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.ArrowExpressionSpacesOption" />
<Item Id="{1ee97312-d3a8-49b0-b557-e396e73a7898}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.TypeParameterListSpacesOption" />
<Item Id="{3d7f395a-bb87-43c2-b483-45b0e5662607}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.TypeArgumentListSpacesOption" />
<Item Id="{a8d62687-d898-460d-9322-1088c09f5d9b}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.AttributeListSpacesOption" />
<Item Id="{35f5e5b0-a043-4989-8680-da5aa592ece0}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.CoalesceExpressionSpacesOption" />
<Item Id="{c63dcf47-261a-4a10-9a4d-5ca4957f5061}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.BaseListSpacesOption" />
<Item Id="{6c1b5b15-b7c8-4b91-ad28-a52e7389dc9b}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.CtorInitSpacesOption" />
<Item Id="{10781ebe-9235-4229-a95b-a184827b9815}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.CommentSpacesOption" />
<Item Id="{30a037ea-69da-4198-98ad-bc7342242e53}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.SwitchExpressionSpacesOption" />
<Item Id="{7a5f4d31-4fc4-4c34-ba85-2baad09873b4}" Type="DevExpress.CodeRush.Formatting.UI.Options.Conversion.CollectionExpressionSpacesOption">
<WithinBrackets>False</WithinBrackets>
</Item>
</Options>
</Options>
</Root>
+33 -52
View File
@@ -1,27 +1,30 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Platform"
// file="Platform.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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -84,41 +87,31 @@ namespace StgSharp
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void LogError(
Exception ex
)
public static void LogError(Exception ex)
{
DefaultLog.InternalWriteLog(ex.Message, LogType.Error);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void LogInfo(
string log
)
public static void LogInfo(string log)
{
DefaultLog.InternalWriteLog(log, LogType.Info);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void LogWarning(
string log
)
public static void LogWarning(string log)
{
DefaultLog.InternalWriteLog(log, LogType.Warning);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void LogWarning(
Exception notVerySeriesException
)
public static void LogWarning(Exception notVerySeriesException)
{
DefaultLog.InternalWriteLog(notVerySeriesException.Message, LogType.Warning);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Mark(
bool needConsole
)
public static void Mark(bool needConsole)
{
markCount++;
if (needConsole) {
@@ -126,9 +119,7 @@ namespace StgSharp
}
}
public static void Terminate(
int exitCode
)
public static void Terminate(int exitCode)
{
MainTimeProvider.StopProvidingTime();
GraphicFramework.glfwTerminate();
@@ -141,8 +132,7 @@ namespace StgSharp
bool isConsole,
[CallerFilePath] string filePath = "",
[CallerMemberName] string callerName = "",
[CallerLineNumber] int lineNumber = 0
)
[CallerLineNumber] int lineNumber = 0)
{
Type t = target.GetType();
string log = $"Tracked object: {nameof(target)}\t\tType:{t.Name}\n";
@@ -173,17 +163,12 @@ namespace StgSharp
/// The severity of current message.
/// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteLog(
string message,
LogType logType
)
public static void WriteLog(string message, LogType logType)
{
DefaultLog.InternalWriteLog(message, logType);
}
internal static TDele ConvertAPI<TDele>(
IntPtr funcPtr
)
internal static TDele ConvertAPI<TDele>(IntPtr funcPtr)
{
if (funcPtr == IntPtr.Zero) {
throw new InvalidCastException(
@@ -199,10 +184,7 @@ namespace StgSharp
}
}
internal static Delegate ConvertAPI(
IntPtr funcPtr,
Type T
)
internal static Delegate ConvertAPI(IntPtr funcPtr, Type T)
{
if (funcPtr == IntPtr.Zero) {
throw new InvalidCastException(
@@ -238,7 +220,6 @@ namespace StgSharp
Dialogue.CreateDialogueProcessIfNotExist();
}
/**/
NativeIntrinsic.InitIntrinsicContext();
}
internal class Initializer : IStaticModule
@@ -1,27 +1,30 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="intrinsicContextProvider"
// file="intrinsicContextProvider.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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -47,6 +50,7 @@ namespace StgSharp.Internal
get => _intrinsicContext;
}
/*
internal static void InitIntrinsicContext()
{
ulong statCode = 0;
@@ -71,10 +75,8 @@ namespace StgSharp.Internal
}
[LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")]
[UnmanagedCallConv(CallConvs =[ typeof(CallConvCdecl) ])]
private static partial ulong LoadIntrinsicFunction(
IntrinsicContext* context
);
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
private static partial void LoadIntrinsicFunction(IntrinsicContext* context, ulong id);
/**/
}
}
+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();
}
+1 -1
View File
@@ -65,7 +65,7 @@ SN_API GLFWglproc SN_DECL loadGlfuncDefault(char *procName);
SN_API void SN_DECL unloadImageData(Image *out);
SN_API char *SN_DECL readLog(void);
SN_API void SN_DECL load_glfw_functions(GLFWFunctionTable *table);
SN_API SIMDID SN_DECL load_intrinsic_function(void *intrinsic_context, uint64_t id);
SN_API void SN_DECL load_intrinsic_function(void *intrinsic_context, uint64_t id);
SN_API SIMDID SN_DECL sn_get_simd_level_global();
/* Per-core variant: always re-executes CPUID, never caches.
* Must be called on the target core (after affinity is set). */
+1 -2
View File
@@ -19,7 +19,7 @@ PRIVATE void load_intrinsic_avx(sn_intrinsic *context, SIMDID id);
PRIVATE void load_intrinsic_512_dual(sn_intrinsic *context, SIMDID id);
PRIVATE void load_intrinsic_512(sn_intrinsic *context, SIMDID id);
SN_API SIMDID load_intrinsic_function(void *intrinsic_context, uint64_t id)
SN_API void load_intrinsic_function(void *intrinsic_context, uint64_t id)
{
sn_null_assert(intrinsic_context);
@@ -45,7 +45,6 @@ SN_API SIMDID load_intrinsic_function(void *intrinsic_context, uint64_t id)
load_intrinsic_512(context, simdid);
break;
}
return simdid;
}
void load_intrinsic_sse(sn_intrinsic *context, SIMDID id)
+24 -25
View File
@@ -20,33 +20,34 @@
* [63..56] uArchHi make_byte[7]
* ----------------------------------------------------------------------- */
#define FIELD_MAIN(id) ((unsigned char)(id).make_byte[1])
#define FIELD_AVX512(id) ((uint16_t)((unsigned char)(id).make_byte[2] | \
((unsigned char)(id).make_byte[3] << 8)))
#define FIELD_AMX(id) ((unsigned char)(id).make_byte[4])
#define FIELD_AVX(id) ((unsigned char)(id).make_byte[5])
#define FIELD_AVX10(id) ((unsigned char)(id).make_byte[6])
#define FIELD_UARCH(id) ((unsigned char)(id).make_byte[7])
#define FIELD_MAIN(id) ((unsigned char)(id).make_byte[1])
#define FIELD_AVX512(id) \
((uint16_t)((unsigned char)(id).make_byte[2] | ((unsigned char)(id).make_byte[3] << 8)))
#define FIELD_AMX(id) ((unsigned char)(id).make_byte[4])
#define FIELD_AVX(id) ((unsigned char)(id).make_byte[5])
#define FIELD_AVX10(id) ((unsigned char)(id).make_byte[6])
#define FIELD_UARCH(id) ((unsigned char)(id).make_byte[7])
/* Portable popcount for a 16-bit value. */
static int popcount16(uint16_t v)
{
int c = 0;
while (v) { v &= v - 1; c++; }
while (v) {
v &= v - 1;
c++;
}
return c;
}
/* Map AVX512_IMPL 2-bit field to a strength ordinal for tie-breaking.
* DUAL(1) => 0 (weakest), UNKNOWN(0) => 1, NATIVE(2) => 2 (strongest) */
* Index = impl value (0=UNKNOWN, 1=DUAL, 2=NATIVE, 3=reserved).
* Strength: DUAL(0) < UNKNOWN(1) < NATIVE(2). */
static const int impl_strength_table[4] = { 1, 0, 2, 1 };
static int impl_strength(uint16_t avx512)
{
unsigned impl = (avx512 >> SIMDID_AVX512_IMPL_SHIFT) & 0x3u;
switch (impl) {
case 1: return 0; /* DUAL — glue / downclock risk */
case 0: return 1; /* UNKNOWN — conservatively mid */
case 2: return 2; /* NATIVE — full-width ALU */
default: return 1; /* reserved, treat as unknown */
}
return impl_strength_table[impl];
}
/* -----------------------------------------------------------------------
@@ -85,11 +86,10 @@ SN_API SIMDID SN_DECL sn_get_unite_simd(SIMDID left, SIMDID right)
/* Clear the IMPL field, then set the weaker one */
int ls = impl_strength(l512);
int rs = impl_strength(r512);
unsigned weak_impl = (ls <= rs)
? ((l512 >> SIMDID_AVX512_IMPL_SHIFT) & 0x3u)
: ((r512 >> SIMDID_AVX512_IMPL_SHIFT) & 0x3u);
merged = (merged & ~(uint16_t)SIMDID_AVX512_IMPL_MASK)
| (uint16_t)(weak_impl << SIMDID_AVX512_IMPL_SHIFT);
int s = (ls <= rs) ? l512 : r512; /* pick the one with weaker strength ordinal */
unsigned weak_impl = (s >> SIMDID_AVX512_IMPL_SHIFT) & 0x3u;
merged &= ~(uint16_t)SIMDID_AVX512_IMPL_MASK;
merged |= (uint16_t)(weak_impl << SIMDID_AVX512_IMPL_SHIFT);
out.make_byte[2] = (char)(merged & 0xFF);
out.make_byte[3] = (char)((merged >> 8) & 0xFF);
@@ -171,10 +171,9 @@ SN_API int SN_DECL sn_compare_simd(SIMDID left, SIMDID right)
if (lb != rb)
return lb > rb ? 1 : -1;
/* 4. Extension popcount (mask out BASE + IMPL + reserved low nibble) */
uint16_t ext_mask = (uint16_t)~(SIMDID_AVX512_BASE
| SIMDID_AVX512_IMPL_MASK
| 0x000Fu);
// 4. Extension popcount (mask out BASE + IMPL + reserved low nibble)
int drop = ~(SIMDID_AVX512_BASE | SIMDID_AVX512_IMPL_MASK | 0x000Fu);
uint16_t ext_mask = drop & (l512 | r512); /* only consider bits set in either */
int lp = popcount16(l512 & ext_mask);
int rp = popcount16(r512 & ext_mask);
if (lp != rp)
@@ -196,4 +195,4 @@ SN_API int SN_DECL sn_compare_simd(SIMDID left, SIMDID right)
return 0;
}
#endif
#endif