L4 is now very unsafe
This commit is contained in:
@@ -121,6 +121,7 @@ namespace StgSharp.HighPerformance.Memory
|
|||||||
head->_origin = address;
|
head->_origin = address;
|
||||||
head->_profile = pHandle.MapPolicy;
|
head->_profile = pHandle.MapPolicy;
|
||||||
head->_refCount = 0;
|
head->_refCount = 0;
|
||||||
|
head->_predictor = predictIndex;
|
||||||
head->_address = (nuint)(_data + index);
|
head->_address = (nuint)(_data + index);
|
||||||
|
|
||||||
// pre fetch them here
|
// pre fetch them here
|
||||||
|
|||||||
@@ -1,27 +1,30 @@
|
|||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// file="MatrixEnumerator"
|
// file="MatrixEnumerator.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
|
//
|
||||||
// copies or substantial portions of the Software.
|
// The above copyright notice and
|
||||||
//
|
// this permission notice shall be included in all copies
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
// or substantial portions of the Software.
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
//
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
// SOFTWARE.
|
// 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.
|
||||||
//
|
//
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
@@ -44,51 +47,38 @@ namespace StgSharp.Mathematics.Numeric.Runtime
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public unsafe interface IMatrixCachePredictor<TSelf, T> : IL4Predict
|
public unsafe interface IMatrixCachePredictor<TSelf, T> : IL4Predict where T: unmanaged, INumber<T>
|
||||||
where T : unmanaged, INumber<T>
|
where TSelf: class, IMatrixCachePredictor<TSelf, T>
|
||||||
where TSelf : class, IMatrixCachePredictor<TSelf, T>
|
|
||||||
{
|
{
|
||||||
|
|
||||||
public bool NeedWriteBack { get; }
|
|
||||||
|
|
||||||
public static abstract PredictPolicy Policy { get; set; }
|
public static abstract PredictPolicy Policy { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed unsafe class BufferSequentialPredictor<T> : IMatrixCachePredictor<BufferSequentialPredictor<T>, T>
|
public sealed unsafe class BufferSequentialPredictor<T> : IMatrixCachePredictor<BufferSequentialPredictor<T>, T>
|
||||||
where T : unmanaged, INumber<T>
|
where T: unmanaged, INumber<T>
|
||||||
{
|
{
|
||||||
|
|
||||||
private MatrixKernelEnumerator enumerator;
|
private MatrixParallelTask* origin;
|
||||||
|
|
||||||
public bool NeedWriteBack => throw new NotImplementedException();
|
|
||||||
|
|
||||||
public static PredictPolicy Policy { get; set; } = PredictPolicy.SquareRowMajor;
|
public static PredictPolicy Policy { get; set; } = PredictPolicy.SquareRowMajor;
|
||||||
|
|
||||||
private MatrixKernel* Buffer { get; set; }
|
public bool IsSamePolicy(uint policy, IL4Predict predict)
|
||||||
|
|
||||||
public PredictResult Predict(
|
|
||||||
Span<CacheLinePrediction> node,
|
|
||||||
out int count
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Prefetch(
|
public PredictResult Predict(Span<CacheLinePrediction> node, out int count)
|
||||||
nuint origin,
|
|
||||||
ulong policy,
|
|
||||||
Span<byte> cache
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void WriteBack(
|
public void Prefetch(nuint origin, uint policy, Span<byte> cache)
|
||||||
nuint origin,
|
{
|
||||||
ulong policy,
|
throw new NotImplementedException();
|
||||||
ReadOnlySpan<byte> cache
|
}
|
||||||
)
|
|
||||||
|
public void WriteBack(nuint origin, uint policy, ReadOnlySpan<byte> cache)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,33 @@
|
|||||||
|
//-----------------------------------------------------------------------
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// file="MatrixOperatorPolicy.cs"
|
||||||
|
// Project: StgSharp
|
||||||
|
// 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.
|
||||||
|
//
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -6,10 +36,32 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace StgSharp.Mathematics.Numeric.Runtime
|
namespace StgSharp.Mathematics.Numeric.Runtime
|
||||||
{
|
{
|
||||||
internal enum MatrixOperatorPolicy
|
internal enum MatrixCachePolicy
|
||||||
{
|
{
|
||||||
|
|
||||||
Sequential = 1,
|
None = 0,
|
||||||
|
|
||||||
|
#region usage
|
||||||
|
|
||||||
|
Left = 1,
|
||||||
|
Right = 2,
|
||||||
|
Ans = 3,
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region index direction
|
||||||
|
|
||||||
|
RowMajor = 4,
|
||||||
|
ColMajor = 5,
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region shape
|
||||||
|
|
||||||
|
Square = 8,
|
||||||
|
UpperTriangle = 9,
|
||||||
|
LowerTriangle = 10,
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +1,30 @@
|
|||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// file="MatrixParallel"
|
// file="MatrixParallel.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
|
//
|
||||||
// copies or substantial portions of the Software.
|
// The above copyright notice and
|
||||||
//
|
// this permission notice shall be included in all copies
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
// or substantial portions of the Software.
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
//
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
// SOFTWARE.
|
// 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.
|
||||||
//
|
//
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
@@ -46,9 +49,7 @@ 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 &&
|
public static bool IsParallelMeaningful => IsParallelSupported && IsInitialized && _meaningful;
|
||||||
IsInitialized &&
|
|
||||||
_meaningful;
|
|
||||||
|
|
||||||
public static int ContextCount { get; private set; }
|
public static int ContextCount { get; private set; }
|
||||||
|
|
||||||
@@ -58,7 +59,7 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
{
|
{
|
||||||
foreach (MatrixParallelThread t in _threads)
|
foreach (MatrixParallelThread t in _threads)
|
||||||
{
|
{
|
||||||
t.Retire();
|
t.Pause();
|
||||||
t.Dispose();
|
t.Dispose();
|
||||||
}
|
}
|
||||||
foreach (MatrixParallelThread t in _retiredThreads) {
|
foreach (MatrixParallelThread t in _retiredThreads) {
|
||||||
@@ -93,9 +94,7 @@ 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) ?
|
contextMap[mask] = contextMap.TryGetValue(mask, out int contextCount) ? (contextCount + 1) : 1;
|
||||||
(contextCount + 1) :
|
|
||||||
1;
|
|
||||||
globalSimd = (globalSimd.Mask != 0) ? UniteSIMDID(globalSimd, simd) : simd;
|
globalSimd = (globalSimd.Mask != 0) ? UniteSIMDID(globalSimd, simd) : simd;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,10 +112,7 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
MatrixParallelThread[] allThreads = _threads.PopRange(_threads.Count);
|
MatrixParallelThread[] allThreads = _threads.PopRange(_threads.Count);
|
||||||
Array.Sort(allThreads, static(
|
Array.Sort(allThreads, static(left, right) => CompareSIMDID(left.Simd, right.Simd));
|
||||||
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]);
|
||||||
@@ -129,37 +125,26 @@ 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(
|
private static partial int CompareSIMDID(SIMDID left, SIMDID right);
|
||||||
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(
|
private static partial void LoadIntrinsicFunction(IntrinsicContext* context, ulong id);
|
||||||
IntrinsicContext* context,
|
|
||||||
ulong id
|
|
||||||
);
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private static void RetireThread(
|
private static void RetireThread(MatrixParallelThread thread)
|
||||||
MatrixParallelThread thread
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
thread.Retire();
|
thread.Pause();
|
||||||
_retiredThreads.Push(thread);
|
_retiredThreads.Push(thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
[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(
|
private static partial SIMDID UniteSIMDID(SIMDID left, SIMDID right);
|
||||||
SIMDID left,
|
|
||||||
SIMDID right
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +1,30 @@
|
|||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// file="MatrixParallelThread"
|
// file="MatrixParallelThread.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
|
//
|
||||||
// copies or substantial portions of the Software.
|
// The above copyright notice and
|
||||||
//
|
// this permission notice shall be included in all copies
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
// or substantial portions of the Software.
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
//
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
// SOFTWARE.
|
// 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.
|
||||||
//
|
//
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
@@ -50,14 +53,14 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
{
|
{
|
||||||
|
|
||||||
private IntrinsicContext* _context;
|
private IntrinsicContext* _context;
|
||||||
private MatrixParallelTask* _currentTask;
|
private readonly MatrixParallelTask* _currentTask;
|
||||||
private volatile bool _disposed, _isRetireRequested;
|
private volatile bool _disposed;
|
||||||
private readonly ManualResetEventSlim _resetEvent;
|
private readonly ManualResetEventSlim _resetEvent;
|
||||||
|
|
||||||
|
private volatile MatrixThreadState _target;
|
||||||
private 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);
|
||||||
@@ -69,8 +72,6 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
|
|
||||||
public ThreadPriority AfterWork { get; set; }
|
public ThreadPriority AfterWork { get; set; }
|
||||||
|
|
||||||
internal bool IsSingle { get; set; }
|
|
||||||
|
|
||||||
internal MatrixThreadState State { get; private set; }
|
internal MatrixThreadState State { get; private set; }
|
||||||
|
|
||||||
internal SIMDID Simd { get; private set; }
|
internal SIMDID Simd { get; private set; }
|
||||||
@@ -88,31 +89,26 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
internal void Retire()
|
internal void Pause()
|
||||||
{
|
{
|
||||||
ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelThread));
|
ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelThread));
|
||||||
if (State == MatrixThreadState.Retired) {
|
if (State == MatrixThreadState.Retired) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_isRetireRequested = true;
|
|
||||||
_resetEvent.Set();
|
_resetEvent.Set();
|
||||||
SpinWait.SpinUntil(() => State == MatrixThreadState.Retired);
|
SpinWait.SpinUntil(() => State == MatrixThreadState.Retired);
|
||||||
_managedThread.Join();
|
_managedThread.Join();
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
internal void SetSingleThreadTask(
|
internal void SetSingleThreadTask(ref MatrixParallelTask task)
|
||||||
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)
|
||||||
{
|
{
|
||||||
@@ -152,37 +148,30 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (_isRetireRequested)
|
if (_target == MatrixThreadState.Idle)
|
||||||
{
|
{
|
||||||
State = MatrixThreadState.Retired;
|
State = MatrixThreadState.Idle;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
State = MatrixThreadState.Idle;
|
State = MatrixThreadState.Idle;
|
||||||
State = MatrixThreadState.Running;
|
State = MatrixThreadState.Running;
|
||||||
_managedThread.Priority = ThreadPriority.Highest;
|
_managedThread.Priority = ThreadPriority.Highest;
|
||||||
if (IsSingle)
|
|
||||||
{
|
|
||||||
MatrixParallelTask* outer = _currentTask;
|
|
||||||
|
|
||||||
// compute here
|
// MatrixParallelQueue taskQueue = BindedWrap.TaskQueue;
|
||||||
IsSingle = false;
|
/*
|
||||||
} else
|
if (0 > (int)p ->ComputeHandle) {
|
||||||
{
|
SpecialBufferCompute(p);
|
||||||
// MatrixParallelQueue taskQueue = BindedWrap.TaskQueue;
|
|
||||||
/*
|
|
||||||
if (0 > (int)p ->ComputeHandle) {
|
|
||||||
SpecialBufferCompute(p);
|
|
||||||
}
|
|
||||||
switch (p->ComputeMode)
|
|
||||||
{
|
|
||||||
case MatrixIndexStyle.BUFFER_INDEX:
|
|
||||||
BufferCompute(p);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
/**/
|
|
||||||
}
|
}
|
||||||
|
switch (p->ComputeMode)
|
||||||
|
{
|
||||||
|
case MatrixIndexStyle.BUFFER_INDEX:
|
||||||
|
BufferCompute(p);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/**/
|
||||||
|
|
||||||
_managedThread.Priority = AfterWork;
|
_managedThread.Priority = AfterWork;
|
||||||
State = MatrixThreadState.Idle;
|
State = MatrixThreadState.Idle;
|
||||||
}
|
}
|
||||||
@@ -191,24 +180,19 @@ namespace StgSharp.Mathematics.Numeric
|
|||||||
|
|
||||||
#pragma warning disable CA5393
|
#pragma warning disable CA5393
|
||||||
[LibraryImport(Native.LibName, EntryPoint = "sn_get_simd_level_local")]
|
[LibraryImport(Native.LibName, EntryPoint = "sn_get_simd_level_local")]
|
||||||
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
|
[UnmanagedCallConv(CallConvs = [ typeof(CallConvCdecl) ])]
|
||||||
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
|
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
|
||||||
internal static partial ulong GetSimdId();
|
internal static partial ulong GetSimdId();
|
||||||
|
|
||||||
[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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user