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