matrix pool init ok, working on task publish

This commit is contained in:
Nitload
2026-03-30 01:42:09 +08:00
parent 61303635ad
commit 24498c2717
9 changed files with 220 additions and 208 deletions
@@ -33,32 +33,22 @@ using System.Threading.Tasks;
namespace StgSharp.HighPerformance.Memory namespace StgSharp.HighPerformance.Memory
{ {
[Flags]
public enum PredictResult
{
Success = 1,
Exhausted = 2,
}
public interface IL4Predict public interface IL4Predict
{ {
bool IsExhausted { get; } PredictResult Predict(Span<CacheLinePrediction> node, out int count);
/// <summary> /// <summary>
/// Writes predicted cache line descriptors into <paramref name="node" />. /// Materializes the cache line identified by <paramref name="origin" /> into <paramref /// ///
/// </summary>
/// <param name="node">
/// Prediction output buffer.
/// </param>
/// <param name="count">
/// When this method returns, contains the number of valid entries written to <paramref ///
/// name="node" />. The value must not exceed <paramref name="node" />.Length.
/// </param>
/// <returns>
/// <see langword="true" /> if any prediction data was produced; otherwise, <see langword="false"
/// />.
/// </returns>
bool Predict(
Span<CacheLinePrediction> node,
out int count
);
/// <summary>
/// Materializes the cache line identified by <paramref name="origin" /> into <paramref ///
/// name="cache" />. /// name="cache" />.
/// </summary> /// </summary>
/// <param name="origin"> /// <param name="origin">
@@ -70,11 +60,7 @@ namespace StgSharp.HighPerformance.Memory
/// <param name="cache"> /// <param name="cache">
/// Destination cache line buffer. /// Destination cache line buffer.
/// </param> /// </param>
void Prefetch( void Prefetch(nuint origin, ulong policy, Span<byte> cache);
nuint origin,
ulong policy,
Span<byte> cache
);
/// <summary> /// <summary>
/// Writes the cache line in <paramref name="cache" /> back to <paramref name="origin" />. /// Writes the cache line in <paramref name="cache" /> back to <paramref name="origin" />.
@@ -88,32 +74,24 @@ namespace StgSharp.HighPerformance.Memory
/// <param name="cache"> /// <param name="cache">
/// Source cache line buffer. /// Source cache line buffer.
/// </param> /// </param>
void WriteBack( void WriteBack(nuint origin, ulong policy, ReadOnlySpan<byte> cache);
nuint origin,
ulong policy,
ReadOnlySpan<byte> cache
);
} }
public struct CacheLinePrediction : IEquatable<CacheLinePrediction> public struct CacheLinePrediction : IEquatable<CacheLinePrediction>
{ {
public override readonly bool Equals( public override readonly bool Equals(object? obj)
object? obj
)
{ {
return obj is CacheLinePrediction prediction && return (obj is CacheLinePrediction prediction) &&
Address.Equals(prediction.Address) && Address.Equals(prediction.Address) &&
MapPolicy == prediction.MapPolicy; (MapPolicy == prediction.MapPolicy);
} }
public readonly bool Equals( public readonly bool Equals(CacheLinePrediction other)
CacheLinePrediction other
)
{ {
return return
Address == other.Address && MapPolicy == other.MapPolicy; (Address == other.Address) && (MapPolicy == other.MapPolicy);
} }
public override readonly int GetHashCode() public override readonly int GetHashCode()
@@ -121,17 +99,11 @@ namespace StgSharp.HighPerformance.Memory
return HashCode.Combine(Address, MapPolicy); return HashCode.Combine(Address, MapPolicy);
} }
public static bool operator !=( public static bool operator !=(CacheLinePrediction left, CacheLinePrediction right)
CacheLinePrediction left,
CacheLinePrediction right
)
{ {
return !(left == right); return !(left == right);
} }
public static bool operator ==( public static bool operator ==(CacheLinePrediction left, CacheLinePrediction right)
CacheLinePrediction left,
CacheLinePrediction right
)
{ {
return left.Equals(right); return left.Equals(right);
} }
@@ -111,7 +111,7 @@ namespace StgSharp.Mathematics.Numeric.Linear
public static PredictPolicy Policy => PredictPolicy.Sequential; public static PredictPolicy Policy => PredictPolicy.Sequential;
public bool Predict( public PredictResult Predict(
Span<CacheLinePrediction> node, Span<CacheLinePrediction> node,
out int count out int count
) )
@@ -2,8 +2,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="NumericalModule" // file="NumericalModule"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. 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
@@ -61,26 +61,23 @@ namespace StgSharp.Mathematics.Numeric
public string ModuleName => "Math.Numerical"; public string ModuleName => "Math.Numerical";
internal static IntrinsicContext GlobalContext internal static IntrinsicContext GlobalContext { get; set; } = new();
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;
} = new();
internal static SIMDID GlobalIntrinsicMask { get; set; } internal static SIMDID GlobalIntrinsicMask { get; set; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void InitializeModule() public void InitializeModule()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public void InitializeModule( [MethodImpl(MethodImplOptions.AggressiveInlining)]
IModuleInitializeProfile profile public void InitializeModule(IModuleInitializeProfile profile)
)
{ {
MatrixParallel.Init(); MatrixParallel.Init();
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void UninitializeModule() public void UninitializeModule()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
@@ -2,8 +2,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixParallel.Publish" // file="MatrixParallel.Publish"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. 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
@@ -50,8 +50,8 @@ namespace StgSharp.Mathematics.Numeric
internal static unsafe MatrixParallelWrap ScheduleTask<TTask>( internal static unsafe MatrixParallelWrap ScheduleTask<TTask>(
MatrixParallelThread[] workers, MatrixParallelThread[] workers,
MatrixParallelTask* package MatrixParallelTask* package)
) where TTask : IL4Predict where TTask: IL4Predict
{ {
/* /*
int primCount = package->PrimCount; int primCount = package->PrimCount;
@@ -61,10 +61,7 @@ namespace StgSharp.Mathematics.Numeric
// Console.WriteLine($"amount of kernels is {count}"); // Console.WriteLine($"amount of kernels is {count}");
return new(workers) return new(workers);
{
TaskQueue = MatrixParallelQueue.Rent(package)
};
} }
} }
@@ -2,8 +2,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixParallel" // file="MatrixParallel"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. 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
@@ -35,14 +35,17 @@ namespace StgSharp.Mathematics.Numeric
public static unsafe partial class MatrixParallel public static unsafe partial class MatrixParallel
{ {
private const int RESERVED_THREAD_COUNT = 4;
private static bool _meaningful; private static bool _meaningful;
private static CapacityFixedStack<MatrixParallelThread> _retiredThreads;
private static CapacityFixedStack<MatrixParallelThread> _threads; private static CapacityFixedStack<MatrixParallelThread> _threads;
public static bool IsParallelSupported { get; } = Environment.ProcessorCount > 6; public static bool IsParallelSupported { get; } = Environment.ProcessorCount > 6;
public static bool IsParallelMeaningful { get; } = (Environment.ProcessorCount > 6) && public static bool IsParallelMeaningful => IsParallelSupported && IsInitialized && _meaningful;
IsInitialized;
public static int ContextCount { get; private set; } public static int ContextCount { get; private set; }
@@ -50,7 +53,7 @@ namespace StgSharp.Mathematics.Numeric
internal static void Init() internal static void Init()
{ {
Dictionary<SIMDID, int> contextMap = []; Dictionary<ulong, int> contextMap = [];
if (IsInitialized) { if (IsInitialized) {
return; return;
@@ -62,37 +65,70 @@ namespace StgSharp.Mathematics.Numeric
} }
contextMap = []; contextMap = [];
int count = Environment.ProcessorCount; int count = Environment.ProcessorCount;
bool hasGlobalSimd = false;
SIMDID globalSimd = default;
_threads = new(count); _threads = new(count);
_retiredThreads = new(RESERVED_THREAD_COUNT);
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
MatrixParallelThread thread = new(i); MatrixParallelThread thread = new(i);
_threads.Push(thread); _threads.Push(thread);
SpinWait.SpinUntil(() => thread.State == MatrixThreadState.Ready); SpinWait.SpinUntil(() => thread.State == MatrixThreadState.Ready);
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;
} }
// TODO 构建全局的保底上下文 IntrinsicContext globalContext = new();
foreach (MatrixParallelThread thread in _threads) { } LoadIntrinsicFunction(&globalContext, globalSimd.Mask);
if (contextMap.Keys.Count == 1) NumericalModule.GlobalContext = globalContext;
NumericalModule.GlobalIntrinsicMask = globalSimd;
ContextCount = contextMap.Count;
if (ContextCount == 1)
{ {
for (int i = 0; i < 4; i++) int retireCount = Math.Min(RESERVED_THREAD_COUNT, Math.Max(0, _threads.Count));
{ for (int i = 0; i < retireCount; i++) {
MatrixParallelThread thread = _threads.Pop(); RetireThread(_threads.Pop());
thread.Dispose();
} }
} else } else
{ {
// TODO 排序并丢弃四个能力最弱线程 MatrixParallelThread[] allThreads = _threads.PopRange(_threads.Count);
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]);
} }
_threads.PushRange(allThreads.AsSpan(retireCount));
}
_meaningful = _threads.Count > 1;
_awaitingLeaderHandle = new AwaitingQueue(); _awaitingLeaderHandle = new AwaitingQueue();
IsInitialized = true; IsInitialized = true;
} }
[LibraryImport(Native.LibName, EntryPoint = "sn_compare_simd")] [LibraryImport(Native.LibName, EntryPoint = "sn_compare_simd")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
private static unsafe partial ulong CompareSIMDID( [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
IntrinsicContext* context, private static partial int CompareSIMDID(SIMDID left, SIMDID right);
ulong id
); [LibraryImport(Native.LibName, EntryPoint = "load_intrinsic_function")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
private static partial void LoadIntrinsicFunction(IntrinsicContext* context, ulong id);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void RetireThread(MatrixParallelThread thread)
{
thread.Retire();
_retiredThreads.Push(thread);
}
[LibraryImport(Native.LibName, EntryPoint = "sn_get_unite_simd")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
private static partial SIMDID UniteSIMDID(SIMDID left, SIMDID right);
} }
} }
@@ -1,27 +1,30 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixParallelQueue" // file="MatrixParallelQueue.cs"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. 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
// of this software and associated documentation files (the "Software"), to deal // obtaining a copy of this software and associated documentation
// in the Software without restriction, including without limitation the rights // files (the “Software”), to deal in the Software without restriction,
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // including without limitation the rights to use, copy, modify, merge,
// copies of the Software, and to permit persons to whom the Software is // publish, distribute, sublicense, and/or sell copies of the Software,
// furnished to do so, subject to the following conditions: // 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 // The above copyright notice and
// copies or substantial portions of the Software. // 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 // THE SOFTWARE IS PROVIDED “AS IS”,
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// SOFTWARE. // ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// //
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
@@ -39,42 +42,11 @@ using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric namespace StgSharp.Mathematics.Numeric
{ {
internal unsafe class MatrixParallelQueue internal unsafe ref struct MatrixParallelQueue
{ {
private static readonly ConcurrentStack<MatrixParallelQueue> _recycle = [];
private long _cursor;
private long _maxCount;
private MatrixParallelQueue() { }
public MatrixParallelTask* BaseTask { get; private set; }
public static MatrixParallelQueue Rent(
MatrixParallelTask* pack,
long count
)
{
if (!_recycle.TryPop(out MatrixParallelQueue? queue)) {
queue = new MatrixParallelQueue();
}
queue.BaseTask = pack;
queue._maxCount = count;
queue._cursor = 0;
return queue;
}
public static void Return(
MatrixParallelQueue queue
)
{
_recycle.Push(queue);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Steal( public bool Steal(MatrixParallelTask* task)
MatrixParallelTask* task
)
{ {
// TODO 使用提取的内容填充task // TODO 使用提取的内容填充task
throw new NotImplementedException(); throw new NotImplementedException();
@@ -2,8 +2,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixParallelTask" // file="MatrixParallelTask"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. 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
@@ -32,6 +32,16 @@ using System.Runtime.InteropServices;
namespace StgSharp.Mathematics.Numeric.Runtime namespace StgSharp.Mathematics.Numeric.Runtime
{ {
[StructLayout(LayoutKind.Sequential)]
internal struct MatrixParallelSource
{
public MatrixParallelTask Task;
public ulong Count;
public ulong Cursor;
}
[StructLayout(LayoutKind.Explicit)] [StructLayout(LayoutKind.Explicit)]
internal unsafe struct MatrixParallelTask internal unsafe struct MatrixParallelTask
{ {
@@ -56,13 +66,9 @@ namespace StgSharp.Mathematics.Numeric.Runtime
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Copy( public static void Copy(MatrixParallelTask* source, MatrixParallelTask* target)
MatrixParallelTask* source,
MatrixParallelTask* target
)
{ {
Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTask), Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTask), sizeof(MatrixParallelTask));
sizeof(MatrixParallelTask));
} }
} }
@@ -2,8 +2,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixParallelThread" // file="MatrixParallelThread"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. 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
@@ -50,13 +50,11 @@ namespace StgSharp.Mathematics.Numeric
private IntrinsicContext* _context; private IntrinsicContext* _context;
private MatrixParallelTask* _currentTask; private MatrixParallelTask* _currentTask;
private volatile bool _disposed, _isRetireRequested ; private volatile bool _disposed, _isRetireRequested, _isReinitRequested;
private readonly ManualResetEventSlim _resetEvent; private readonly ManualResetEventSlim _resetEvent;
private readonly Thread _managedThread; private Thread _managedThread;
internal MatrixParallelThread( internal MatrixParallelThread(int poolId)
int poolId
)
{ {
_resetEvent = new ManualResetEventSlim(false); _resetEvent = new ManualResetEventSlim(false);
_managedThread = new Thread(MatrixParallelWorkLoad); _managedThread = new Thread(MatrixParallelWorkLoad);
@@ -91,29 +89,54 @@ namespace StgSharp.Mathematics.Numeric
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void SetSingleThreadTask( internal void ReInit()
ref MatrixParallelTask task {
) ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelThread));
if (State != MatrixThreadState.Retired) {
return;
}
BindedWrap = null!;
_currentTask = null;
IsSingle = false;
_isRetireRequested = false;
_isReinitRequested = true;
_resetEvent.Set();
_managedThread = new Thread(MatrixParallelWorkLoad);
SpinWait.SpinUntil(() => State != MatrixThreadState.Retired);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void Retire()
{
ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelThread));
if (State == MatrixThreadState.Retired) {
return;
}
_isRetireRequested = true;
_resetEvent.Set();
SpinWait.SpinUntil(() => State == MatrixThreadState.Retired);
_managedThread.Join();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void SetSingleThreadTask(ref MatrixParallelTask task)
{ {
if (IsSingle) { if (IsSingle) {
*_currentTask = task; *_currentTask = task;
} }
} }
protected virtual void Dispose( protected virtual void Dispose(bool disposing)
bool disposing
)
{ {
if (!_disposed) if (!_disposed)
{ {
if (disposing) if (disposing)
{ {
_isRetireRequested = true; _disposed = true;
_resetEvent.Set(); _resetEvent.Set();
_managedThread.Join(); _managedThread.Join();
_resetEvent.Dispose(); _resetEvent.Dispose();
} }
_disposed = true;
} }
} }
@@ -124,21 +147,36 @@ namespace StgSharp.Mathematics.Numeric
// Bind OS-thread affinity once when the worker starts running // Bind OS-thread affinity once when the worker starts running
SetThreadAffinity(PoolId); SetThreadAffinity(PoolId);
_ = Thread.Yield(); _ = Thread.Yield();
if (Simd.Mask != 0)
{
SIMDID feature = new(GetSimdId()); SIMDID feature = new(GetSimdId());
IntrinsicContext* contextPtr = stackalloc IntrinsicContext[1]; IntrinsicContext* contextPtr = stackalloc IntrinsicContext[1];
MatrixParallelTask* taskPtr = stackalloc MatrixParallelTask[4];
LoadIntrinsicFunction(contextPtr, feature.Mask); LoadIntrinsicFunction(contextPtr, feature.Mask);
Simd = feature;
_context = contextPtr; _context = contextPtr;
}
MatrixParallelTask* taskPtr = stackalloc MatrixParallelTask[4];
State = MatrixThreadState.Ready; State = MatrixThreadState.Ready;
while (true) while (true)
{ {
// Thread self = Thread.CurrentThread; // Thread self = Thread.CurrentThread;
_resetEvent.Wait(); _resetEvent.Wait();
_resetEvent.Reset(); _resetEvent.Reset();
if (_isRetireRequested) if (_disposed)
{ {
break; break;
} }
if (_isRetireRequested)
{
State = MatrixThreadState.Retired;
continue;
}
if (_isReinitRequested)
{
_isReinitRequested = false;
State = MatrixThreadState.Idle;
continue;
}
State = MatrixThreadState.Running; State = MatrixThreadState.Running;
AfterWork = BindedWrap.AfterWork; AfterWork = BindedWrap.AfterWork;
_managedThread.Priority = ThreadPriority.Highest; _managedThread.Priority = ThreadPriority.Highest;
@@ -148,7 +186,7 @@ namespace StgSharp.Mathematics.Numeric
IsSingle = false; IsSingle = false;
} else } else
{ {
MatrixParallelQueue taskQueue = BindedWrap.TaskQueue; // MatrixParallelQueue taskQueue = BindedWrap.TaskQueue;
/* /*
if (0 > (int)p ->ComputeHandle) { if (0 > (int)p ->ComputeHandle) {
SpecialBufferCompute(p); SpecialBufferCompute(p);
@@ -179,17 +217,12 @@ 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( internal static partial void SetThreadAffinity(int coreId);
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( private static unsafe partial void LoadIntrinsicFunction(IntrinsicContext* context, ulong id);
IntrinsicContext* context,
ulong id
);
#pragma warning restore CA5393 #pragma warning restore CA5393
} }
} }
@@ -1,32 +1,36 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixParallelWrap" // file="MatrixParallelWrap.cs"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. 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
// of this software and associated documentation files (the "Software"), to deal // obtaining a copy of this software and associated documentation
// in the Software without restriction, including without limitation the rights // files (the “Software”), to deal in the Software without restriction,
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // including without limitation the rights to use, copy, modify, merge,
// copies of the Software, and to permit persons to whom the Software is // publish, distribute, sublicense, and/or sell copies of the Software,
// furnished to do so, subject to the following conditions: // 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 // The above copyright notice and
// copies or substantial portions of the Software. // 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 // THE SOFTWARE IS PROVIDED “AS IS”,
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// SOFTWARE. // ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// //
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using StgSharp.Collections; using StgSharp.Collections;
using StgSharp.HighPerformance.Memory; using StgSharp.HighPerformance.Memory;
using StgSharp.Mathematics.Numeric.Runtime;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
@@ -48,16 +52,14 @@ namespace StgSharp.Mathematics.Numeric
} }
public sealed class MatrixParallelWrap : IDisposable public class MatrixParallelWrap : IDisposable
{ {
private MatrixParallelThread[] _threads; private MatrixParallelThread[] _threads;
private bool _disposed; private bool _disposed;
private readonly CountdownEvent _countdownEvent; private readonly CountdownEvent _countdownEvent;
internal MatrixParallelWrap( internal MatrixParallelWrap(MatrixParallelThread[] threadArray)
MatrixParallelThread[] threadArray
)
{ {
_threads = threadArray; _threads = threadArray;
_countdownEvent = new CountdownEvent(threadArray.Length); _countdownEvent = new CountdownEvent(threadArray.Length);
@@ -69,18 +71,17 @@ namespace StgSharp.Mathematics.Numeric
internal L4 DataCache { get; set; } internal L4 DataCache { get; set; }
internal MatrixParallelQueue TaskQueue { get; set; } internal PredictPolicy Policy { get; set; }
public void Dispose() public void Dispose()
{ {
Dispose(true); Dispose(true);
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void LaunchParallel( public void LaunchParallel(SleepMode afterWork)
SleepMode afterWork
)
{ {
ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelWrap)); ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelWrap));
AfterWork = (ThreadPriority)afterWork; AfterWork = (ThreadPriority)afterWork;
@@ -107,9 +108,7 @@ namespace StgSharp.Mathematics.Numeric
_ = _countdownEvent.Signal(); _ = _countdownEvent.Signal();
} }
private void Dispose( private void Dispose(bool disposing)
bool disposing
)
{ {
if (_disposed) { if (_disposed) {
return; return;