few change

This commit is contained in:
Youmu Liu
2026-04-01 18:03:04 +08:00
parent 24498c2717
commit 8f515a2a50
4 changed files with 87 additions and 30 deletions
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="NumericalModule"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// 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
@@ -66,13 +66,15 @@ namespace StgSharp.Mathematics.Numeric
internal static SIMDID GlobalIntrinsicMask { get; set; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void InitializeModule()
public static void InitializeModule()
{
throw new NotImplementedException();
MatrixParallel.Init();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void InitializeModule(IModuleInitializeProfile profile)
public void InitializeModule(
IModuleInitializeProfile profile
)
{
MatrixParallel.Init();
}
@@ -80,7 +82,7 @@ namespace StgSharp.Mathematics.Numeric
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void UninitializeModule()
{
throw new NotImplementedException();
MatrixParallel.Deinit();
}
}
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="MatrixParallel"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// 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
@@ -29,6 +29,7 @@ using StgSharp.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace StgSharp.Mathematics.Numeric
{
@@ -45,12 +46,28 @@ namespace StgSharp.Mathematics.Numeric
public static bool IsParallelSupported { get; } = Environment.ProcessorCount > 6;
public static bool IsParallelMeaningful => IsParallelSupported && IsInitialized && _meaningful;
public static bool IsParallelMeaningful => IsParallelSupported &&
IsInitialized &&
_meaningful;
public static int ContextCount { get; private set; }
private static bool IsInitialized { get; set; }
internal static void Deinit()
{
foreach (MatrixParallelThread t in _threads)
{
t.Retire();
t.Dispose();
}
foreach (MatrixParallelThread t in _retiredThreads) {
t.Dispose();
}
ContextCount = 0;
IsInitialized = false;
}
internal static void Init()
{
Dictionary<ulong, int> contextMap = [];
@@ -65,8 +82,7 @@ namespace StgSharp.Mathematics.Numeric
}
contextMap = [];
int count = Environment.ProcessorCount;
bool hasGlobalSimd = false;
SIMDID globalSimd = default;
SIMDID globalSimd = new SIMDID(0);
_threads = new(count);
_retiredThreads = new(RESERVED_THREAD_COUNT);
for (int i = 0; i < count; i++)
@@ -77,9 +93,10 @@ namespace StgSharp.Mathematics.Numeric
SIMDID simd = thread.Simd;
ulong mask = simd.Mask;
contextMap[mask] = contextMap.TryGetValue(mask, out int contextCount) ? (contextCount + 1) : 1;
globalSimd = hasGlobalSimd ? UniteSIMDID(globalSimd, simd) : simd;
hasGlobalSimd = true;
contextMap[mask] = contextMap.TryGetValue(mask, out int contextCount) ?
(contextCount + 1) :
1;
globalSimd = (globalSimd.Mask != 0) ? UniteSIMDID(globalSimd, simd) : simd;
}
IntrinsicContext globalContext = new();
@@ -96,7 +113,10 @@ namespace StgSharp.Mathematics.Numeric
} else
{
MatrixParallelThread[] allThreads = _threads.PopRange(_threads.Count);
Array.Sort(allThreads, static(left, right) => CompareSIMDID(left.Simd, right.Simd));
Array.Sort(allThreads, static(
left,
right
) => CompareSIMDID(left.Simd, right.Simd));
int retireCount = Math.Min(RESERVED_THREAD_COUNT, Math.Max(0, allThreads.Length - 1));
for (int i = 0; i < retireCount; i++) {
RetireThread(allThreads[i]);
@@ -111,15 +131,23 @@ namespace StgSharp.Mathematics.Numeric
[LibraryImport(Native.LibName, EntryPoint = "sn_compare_simd")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
private static partial int CompareSIMDID(SIMDID left, SIMDID right);
private static partial int CompareSIMDID(
SIMDID left,
SIMDID right
);
[LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
private static partial void LoadIntrinsicFunction(IntrinsicContext* context, ulong id);
private static partial void LoadIntrinsicFunction(
IntrinsicContext* context,
ulong id
);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void RetireThread(MatrixParallelThread thread)
private static void RetireThread(
MatrixParallelThread thread
)
{
thread.Retire();
_retiredThreads.Push(thread);
@@ -128,7 +156,10 @@ namespace StgSharp.Mathematics.Numeric
[LibraryImport(Native.LibName, EntryPoint = "sn_get_unite_simd")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
private static partial SIMDID UniteSIMDID(SIMDID left, SIMDID right);
private static partial SIMDID UniteSIMDID(
SIMDID left,
SIMDID right
);
}
}
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="MatrixParallelTask"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// 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
@@ -49,7 +49,13 @@ namespace StgSharp.Mathematics.Numeric.Runtime
[FieldOffset(0)] public MatrixKernel* Mat1;
[FieldOffset(8)] public MatrixKernel* Mat2;
[FieldOffset(16)] public MatrixKernel* Mat3;
/// <summary>
/// Being offset of first kernel in a L4 cache line.
/// </summary>
[FieldOffset(24)] public int CacheIndex;
/// <summary>
/// Count of kernels to be calculated in a L4 cache line.
/// </summary>
[FieldOffset(28)] public int CacheLength;
[FieldOffset(32)] public int CommonK;
[FieldOffset(36)] public int CommonX;
@@ -66,9 +72,13 @@ namespace StgSharp.Mathematics.Numeric.Runtime
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Copy(MatrixParallelTask* source, MatrixParallelTask* target)
public static void Copy(
MatrixParallelTask* source,
MatrixParallelTask* target
)
{
Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTask), sizeof(MatrixParallelTask));
Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTask),
sizeof(MatrixParallelTask));
}
}
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="MatrixParallelThread"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// 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
@@ -31,6 +31,7 @@ using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric
{
@@ -54,7 +55,9 @@ namespace StgSharp.Mathematics.Numeric
private readonly ManualResetEventSlim _resetEvent;
private Thread _managedThread;
internal MatrixParallelThread(int poolId)
internal MatrixParallelThread(
int poolId
)
{
_resetEvent = new ManualResetEventSlim(false);
_managedThread = new Thread(MatrixParallelWorkLoad);
@@ -119,14 +122,18 @@ namespace StgSharp.Mathematics.Numeric
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void SetSingleThreadTask(ref MatrixParallelTask task)
internal void SetSingleThreadTask(
ref MatrixParallelTask task
)
{
if (IsSingle) {
*_currentTask = task;
}
}
protected virtual void Dispose(bool disposing)
protected virtual void Dispose(
bool disposing
)
{
if (!_disposed)
{
@@ -183,6 +190,8 @@ namespace StgSharp.Mathematics.Numeric
if (IsSingle)
{
MatrixParallelTask* outer = _currentTask;
// compute here
IsSingle = false;
} else
{
@@ -217,12 +226,17 @@ namespace StgSharp.Mathematics.Numeric
[LibraryImport(Native.LibName, EntryPoint = "sn_set_thread_affinity")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
internal static partial void SetThreadAffinity(int coreId);
internal static partial void SetThreadAffinity(
int coreId
);
[LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
private static unsafe partial void LoadIntrinsicFunction(IntrinsicContext* context, ulong id);
private static unsafe partial void LoadIntrinsicFunction(
IntrinsicContext* context,
ulong id
);
#pragma warning restore CA5393
}
}