add multi predictor support to l4, working for ver 5 of mat parallel

This commit is contained in:
Youmu Liu
2026-04-09 18:07:49 +08:00
parent 0bad6bb480
commit f7187ca7fd
19 changed files with 653 additions and 187 deletions
@@ -0,0 +1,100 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="ConcurrentList"
// Project: StgSharp
// 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
// 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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Common.Collections
{
public class ConcurrentFlexibleArray<T>(
int capacity = 4
)
{
private volatile T[] _array = new T[capacity];
private readonly ReaderWriterLockSlim _rwLock = new();
public T this[
int index
]
{
get
{
_rwLock.EnterReadLock();
try
{
if ((uint)index >= (uint)_array.Length) {
throw new ArgumentOutOfRangeException(nameof(index));
}
return _array[index];
}
finally
{
_rwLock.ExitReadLock();
}
}
set
{
if ((uint)index >= (uint)_array.Length)
{
_rwLock.EnterWriteLock();
try
{
if ((uint)index >= (uint)_array.Length)
{
int newSize = Math.Max(_array.Length * 2, index + 1);
T[] newArray = new T[newSize];
Array.Copy(_array, newArray, _array.Length);
_array = newArray;
_array[index] = value;
}
}
finally
{
_rwLock.ExitWriteLock();
}
} else
{
_rwLock.EnterReadLock();
try
{
_array[index] = value;
}
finally
{
_rwLock.ExitReadLock();
}
}
}
}
}
}
+28 -11
View File
@@ -1,4 +1,4 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="ListSegment" // file="ListSegment"
// Project: StgSharp // Project: StgSharp
@@ -50,7 +50,11 @@ namespace StgSharp.Collections
private int _length; private int _length;
private List<T> _collection; private List<T> _collection;
public ListSegment(List<T> collection, int begin, int length) public ListSegment(
List<T> collection,
int begin,
int length
)
{ {
ArgumentNullException.ThrowIfNull(collection); ArgumentNullException.ThrowIfNull(collection);
ArgumentOutOfRangeException.ThrowIfLessThan(begin, 0); ArgumentOutOfRangeException.ThrowIfLessThan(begin, 0);
@@ -61,7 +65,9 @@ namespace StgSharp.Collections
_length = length; _length = length;
} }
public T this[int indexValue] public T this[
int indexValue
]
{ {
get get
{ {
@@ -71,11 +77,13 @@ namespace StgSharp.Collections
} }
} }
public T this[Index i] public T this[
Index i
]
{ {
get get
{ {
int offset = i.IsFromEnd ? _length - i.Value : i.Value; int offset = i.IsFromEnd ? (_length - i.Value) : i.Value;
ArgumentOutOfRangeException.ThrowIfLessThan(offset, 0); ArgumentOutOfRangeException.ThrowIfLessThan(offset, 0);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(offset, _length); ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(offset, _length);
return _collection[offset + _begin]; return _collection[offset + _begin];
@@ -86,17 +94,22 @@ namespace StgSharp.Collections
public int Count => _length; public int Count => _length;
public bool Contains(T item) public bool Contains(
T item
)
{ {
int index = _collection.IndexOf(item); int index = _collection.IndexOf(item);
return index >= _begin && index < _length + index; return (index >= _begin) && (index < _length + index);
} }
public void CopyTo(T[] array, int arrayIndex) public void CopyTo(
T[] array,
int arrayIndex
)
{ {
ArgumentNullException.ThrowIfNull(array); ArgumentNullException.ThrowIfNull(array);
Span<T> span = CollectionsMarshal.AsSpan<T>(_collection).Slice(_begin, _length); Span<T> span = CollectionsMarshal.AsSpan<T>(_collection).Slice(_begin, _length);
Span<T> target = new Span<T>(array, arrayIndex, _length); Span<T> target = new(array, arrayIndex, _length);
span.CopyTo(target); span.CopyTo(target);
} }
@@ -107,7 +120,9 @@ namespace StgSharp.Collections
} }
} }
void ICollection<T>.Add(T item) void ICollection<T>.Add(
T item
)
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }
@@ -117,7 +132,9 @@ namespace StgSharp.Collections
throw new NotSupportedException(); throw new NotSupportedException();
} }
bool ICollection<T>.Remove(T item) bool ICollection<T>.Remove(
T item
)
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }
@@ -107,8 +107,6 @@ namespace StgSharp.Collections
byte ctrl = (byte)((hash >> EntryMaskWidth) & 0x7F); // H2: upper 8 bits (highest bit is naturally 0) byte ctrl = (byte)((hash >> EntryMaskWidth) & 0x7F); // H2: upper 8 bits (highest bit is naturally 0)
Vector128<byte> ctrlVector = Vector128.Create(ctrl); Vector128<byte> ctrlVector = Vector128.Create(ctrl);
int firstAvailableSlot = -1; // Record the first insertable slot
Vector128<byte> bucketCtrl = Sse2.LoadVector128( Vector128<byte> bucketCtrl = Sse2.LoadVector128(
(byte*)&bucket->Control); (byte*)&bucket->Control);
+4 -1
View File
@@ -25,6 +25,7 @@
// //
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using StgSharp.Common.HighPerformance.ProcessorAbstraction;
using StgSharp.Mathematics; using StgSharp.Mathematics;
using System; using System;
using System.Reflection; using System.Reflection;
@@ -211,6 +212,8 @@ namespace StgSharp
if (Dialogue.NeedShowWhenStartup) { if (Dialogue.NeedShowWhenStartup) {
Dialogue.CreateDialogueProcessIfNotExist(); Dialogue.CreateDialogueProcessIfNotExist();
} }
Numa.Initialize();
/**/ /**/
} }
@@ -228,7 +231,7 @@ namespace StgSharp
public void UninitializeModule() public void UninitializeModule()
{ {
throw new NotImplementedException(); return;
} }
} }
@@ -1,30 +1,27 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="L4.CacheLine.cs" // file="L4.CacheLine"
// 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 // Permission is hereby granted, free of charge, to any person obtaining a copy
// obtaining a copy of this software and associated documentation // of this software and associated documentation files (the "Software"), to deal
// files (the “Software”), to deal in the Software without restriction, // in the Software without restriction, including without limitation the rights
// including without limitation the rights to use, copy, modify, merge, // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// publish, distribute, sublicense, and/or sell copies of the Software, // copies of the Software, and to permit persons to whom the Software is
// and to permit persons to whom the Software is furnished to do so, // furnished to do so, subject to the following conditions:
// subject to the following conditions:
// //
// The above copyright notice and // The above copyright notice and this permission notice shall be included in all
// this permission notice shall be included in all copies // copies or substantial portions of the Software.
// or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED “AS IS”, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// ARISING FROM, OUT OF OR IN CONNECTION WITH // SOFTWARE.
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// //
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
@@ -51,8 +48,8 @@ namespace StgSharp.HighPerformance.Memory
internal int _refCount; internal int _refCount;
internal nuint _address; internal nuint _address;
internal nuint _origin; internal nuint _origin;
internal uint _predictor;
internal ulong _profile; internal uint _profile;
} }
@@ -42,16 +42,7 @@ namespace StgSharp.HighPerformance.Memory
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void AgeAll() private unsafe void AgeAll()
{ {
Vector128<byte> one = Vector128.Create((byte)0x01); _intrinsic.AgeAll(_eviction);
Vector128<byte> max = Vector128.Create((byte)0x03);
byte* p = (byte*)_eviction;
for (int i = 0; i < CacheLineCount; i += 16)
{
Vector128<byte> v = Sse2.LoadVector128(p + i);
v = Sse2.AddSaturate(v, one);
v = Sse2.Min(v, max);
Sse2.Store(p + i, v);
}
} }
/// <summary> /// <summary>
@@ -69,15 +60,12 @@ namespace StgSharp.HighPerformance.Memory
) )
{ {
int remain = count; int remain = count;
Vector128<byte> one = Vector128.Create((byte)0x01);
Vector128<byte> three = Vector128.Create((byte)0x03); Vector128<byte> three = Vector128.Create((byte)0x03);
byte* p = (byte*)_eviction; byte* p = (byte*)_eviction;
for (int i = 0; i < CacheLineCount && remain > 0; i += 16) for (int i = 0; (i < CacheLineCount) && (remain > 0); i += 16)
{ {
Vector128<byte> v = Sse2.LoadVector128(p + i); int evictMask = _intrinsic.ScanEvict(p + i, three);
Vector128<byte> mask = Sse2.CompareEqual(v, three); while ((evictMask != 0) && (remain > 0))
int evictMask = Sse2.MoveMask(mask);
while (evictMask != 0 && remain > 0)
{ {
int bitPos = BitOperations.TrailingZeroCount(evictMask); int bitPos = BitOperations.TrailingZeroCount(evictMask);
int index = bitPos + i; int index = bitPos + i;
@@ -86,7 +74,10 @@ namespace StgSharp.HighPerformance.Memory
{ {
// EvictLine(i + index); // EvictLine(i + index);
_ = _map.TryRemove(head->_origin, out _); _ = _map.TryRemove(head->_origin, out _);
PrefetchPredict.WriteBack(head->_origin, head->_profile, new ReadOnlySpan<byte>((byte*)head->_address, Unsafe.SizeOf<CacheLineData>())); int id = (int)head->_predictor;
IL4Predict predict = _predictions[id] ??
throw new L4PredictUnregisteredException(id);
predict.WriteBack(head->_origin, head->_profile, new ReadOnlySpan<byte>((byte*)head->_address, Unsafe.SizeOf<CacheLineData>()));
RecycleLine(index); RecycleLine(index);
remain--; remain--;
} }
@@ -1,6 +1,6 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixParallelQueue.Predictor" // file="L4.Exceptions"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload. All rights reserved.
@@ -25,25 +25,21 @@
// //
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using StgSharp.HighPerformance.Memory;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric.Runtime namespace StgSharp.HighPerformance.Memory
{ {
internal interface IMatrixPredictor<TSelf> : IL4Predict public sealed class L4PredictUnregisteredException : Exception
where TSelf : unmanaged, IMatrixPredictor<TSelf>
{ {
static abstract PredictPolicy Policy { get; }
public L4PredictUnregisteredException(
int id
)
: base($"L4 predictor with id {id} is null. Maybe it has been unregistered.") { }
} }
internal enum PredictPolicy
{
Transparent = 0,
Sequential = 1,
}
} }
@@ -0,0 +1,105 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="L4.IntrinsicKernel"
// Project: StgSharp
// 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
// 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.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.HighPerformance.Memory
{
public unsafe partial class L4
{
private interface IIntrinsicKernel
{
static IIntrinsicKernel Instance { get; } = Create();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AgeAll(
EvictionArray* eviction
);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int ScanEvict(
byte* vPtr,
Vector128<byte> three
);
private static IIntrinsicKernel Create()
{
if (Sse42.IsSupported)
{
return new SseKernel();
} else
{
throw new PlatformNotSupportedException("No supported intrinsic kernel found for current platform.");
}
}
}
private struct SseKernel : IIntrinsicKernel
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AgeAll(
EvictionArray* eviction
)
{
Vector128<byte> one = Vector128.Create((byte)0x01);
Vector128<byte> max = Vector128.Create((byte)0x03);
byte* p = (byte*)eviction;
for (int i = 0; i < CacheLineCount; i += 16)
{
Vector128<byte> v = Sse2.LoadVector128(p + i);
v = Sse2.AddSaturate(v, one);
v = Sse2.Min(v, max);
Sse2.Store(p + i, v);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int ScanEvict(
byte* vPtr,
Vector128<byte> three
)
{
Vector128<byte> v = Sse2.LoadVector128(vPtr);
Vector128<byte> mask = Sse2.CompareEqual(v, three);
return Sse2.MoveMask(mask);
}
}
}
}
@@ -2,8 +2,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="L4.Predict" // file="L4.Predict"
// 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
@@ -25,6 +25,8 @@
// //
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using StgSharp.Common.Collections;
using StgSharp.Mathematics.Memory;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -33,85 +35,65 @@ using System.Threading.Tasks;
namespace StgSharp.HighPerformance.Memory namespace StgSharp.HighPerformance.Memory
{ {
[Flags] public partial class L4
public enum PredictResult
{ {
Success = 1, private ConcurrentBufferStack<int> _idReuse = new(32);
Exhausted = 2,
} private ConcurrentFlexibleArray<IL4Predict> _predictions = new(32);
public interface IL4Predict private int _largestId = 0;
private Queue<int> _predictionQueue = [];
private IL4Predict CurrentPredict { get; set; }
public int RegisterPredict(
IL4Predict predict
)
{ {
if (predict is null) {
PredictResult Predict(Span<CacheLinePrediction> node, out int count); return -1;
}
/// <summary> if (!_idReuse.TryPop(out int id)) {
/// Materializes the cache line identified by <paramref name="origin" /> into <paramref /// /// id = _largestId++;
/// name="cache" />. }
/// </summary> _predictions[id] = predict;
/// <param name="origin"> _predictionQueue.Enqueue(id);
/// Source address associated with the cache line. return id;
/// </param>
/// <param name="policy">
/// Mapping policy associated with the cache line.
/// </param>
/// <param name="cache">
/// Destination cache line buffer.
/// </param>
void Prefetch(nuint origin, ulong policy, Span<byte> cache);
/// <summary>
/// Writes the cache line in <paramref name="cache" /> back to <paramref name="origin" />.
/// </summary>
/// <param name="origin">
/// Source address associated with the cache line.
/// </param>
/// <param name="policy">
/// Mapping policy associated with the cache line.
/// </param>
/// <param name="cache">
/// Source cache line buffer.
/// </param>
void WriteBack(nuint origin, ulong policy, ReadOnlySpan<byte> cache);
} }
public struct CacheLinePrediction : IEquatable<CacheLinePrediction> public IL4Predict? UnregisterPredict(
int id
)
{ {
if (id < 0) {
return null;
}
IL4Predict p = _predictions[id];
if (p == null) {
return p;
}
_predictions[id] = null!;
public override readonly bool Equals(object? obj) // TODO: scan and write back
_idReuse.Push(id);
return p;
}
private void UseNewPredict()
{ {
return (obj is CacheLinePrediction prediction) && if (_predictionQueue.Count == 0) {
Address.Equals(prediction.Address) && return;
(MapPolicy == prediction.MapPolicy);
} }
if (_predictionQueue.TryDequeue(out int id))
public readonly bool Equals(CacheLinePrediction other)
{ {
return IL4Predict predict = _predictions[id] ??
(Address == other.Address) && (MapPolicy == other.MapPolicy); throw new L4PredictUnregisteredException(id);
CurrentPredict = predict;
}
} }
public override readonly int GetHashCode()
{
return HashCode.Combine(Address, MapPolicy);
}
public static bool operator !=(CacheLinePrediction left, CacheLinePrediction right)
{
return !(left == right);
}
public static bool operator ==(CacheLinePrediction left, CacheLinePrediction right)
{
return left.Equals(right);
}
#pragma warning disable CA1051
public nuint Address;
public ulong MapPolicy;
#pragma warning restore CA1051
} }
} }
@@ -0,0 +1,138 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="L4.Predictor"
// Project: StgSharp
// 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
// 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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.HighPerformance.Memory
{
[Flags]
public enum PredictResult
{
Success = 1,
Exhausted = 2,
}
public interface IL4Predict
{
PredictResult Predict(
Span<CacheLinePrediction> node,
out int count
);
/// <summary>
/// Materializes the cache line identified by <paramref name="origin" /> into <paramref
/// name="cache" />.
/// </summary>
/// <param name="origin">
/// Source address associated with the cache line.
/// </param>
/// <param name="policy">
/// Mapping policy associated with the cache line.
/// </param>
/// <param name="cache">
/// Destination cache line buffer.
/// </param>
void Prefetch(
nuint origin,
ulong policy,
Span<byte> cache
);
/// <summary>
/// Writes the cache line in <paramref name="cache" /> back to <paramref name="origin" />.
/// </summary>
/// <param name="origin">
/// Source address associated with the cache line.
/// </param>
/// <param name="policy">
/// Mapping policy associated with the cache line.
/// </param>
/// <param name="cache">
/// Source cache line buffer.
/// </param>
void WriteBack(
nuint origin,
uint policy,
ReadOnlySpan<byte> cache
);
}
public struct CacheLinePrediction : IEquatable<CacheLinePrediction>
{
public override readonly bool Equals(
object? obj
)
{
return (obj is CacheLinePrediction prediction) &&
Address.Equals(prediction.Address) &&
(MapPolicy == prediction.MapPolicy);
}
public readonly bool Equals(
CacheLinePrediction other
)
{
return
(Address == other.Address) && (MapPolicy == other.MapPolicy);
}
public override readonly int GetHashCode()
{
return HashCode.Combine(Address, MapPolicy);
}
public static bool operator !=(
CacheLinePrediction left,
CacheLinePrediction right
)
{
return !(left == right);
}
public static bool operator ==(
CacheLinePrediction left,
CacheLinePrediction right
)
{
return left.Equals(right);
}
#pragma warning disable CA1051
public nuint Address;
public uint MapPolicy;
internal uint Predictor;
#pragma warning restore CA1051
}
}
@@ -36,14 +36,15 @@ namespace StgSharp.HighPerformance.Memory
private partial void Prefetch() private partial void Prefetch()
{ {
if (PrefetchPredict is null) { if (CurrentPredict is null) {
return; return;
} }
if (_activeCount > MaxActivateCacheLineCount) { if (_activeCount > MaxActivateCacheLineCount) {
throw new OverflowException("Too many activated cache line in external reference."); throw new OverflowException("Too many activated cache line in external reference.");
} }
Span<CacheLinePrediction> span = new(_prediction, PredictionCount); Span<CacheLinePrediction> span = new(_prediction, PredictionCount);
if (!PrefetchPredict.Predict(span, out int actualCount) && (actualCount <= 0)) PredictResult result = PrefetchPredict.Predict(span, out int actualCount);
if ((result == PredictResult.Exhausted) && (actualCount <= 0))
{ {
_exhausted = true; _exhausted = true;
return; return;
@@ -87,7 +88,7 @@ namespace StgSharp.HighPerformance.Memory
head->_origin = address; head->_origin = address;
head->_profile = handle.MapPolicy; head->_profile = handle.MapPolicy;
head->_refCount = 0; head->_refCount = 0;
head->_address = nuint & _data[index]; head->_address = (nuint)(_data + index);
// pre fetch them here // pre fetch them here
PrefetchPredict.Prefetch(head->_origin, head->_profile, new Span<byte>((byte*)head->_address, Unsafe.SizeOf<CacheLineData>())); PrefetchPredict.Prefetch(head->_origin, head->_profile, new Span<byte>((byte*)head->_address, Unsafe.SizeOf<CacheLineData>()));
+6 -4
View File
@@ -38,12 +38,14 @@ namespace StgSharp.HighPerformance.Memory
private const int PredictionCount = 2048; private const int PredictionCount = 2048;
private const int PredictionLeastAhead = 1024; private const int PredictionLeastAhead = 1024;
private static readonly IIntrinsicKernel _intrinsic = IIntrinsicKernel.Instance;
private readonly CacheLineData* _data; private readonly CacheLineData* _data;
private readonly EvictionArray* _eviction; private readonly EvictionArray* _eviction;
private readonly CacheLineHead* _head; private readonly CacheLineHead* _head;
private readonly CacheLinePrediction* _prediction; private readonly CacheLinePrediction* _prediction;
private bool _disposed; private bool _disposed;
private bool _exhausted;
private int _activeCount; private int _activeCount;
private int _aheadCount; private int _aheadCount;
@@ -84,7 +86,7 @@ namespace StgSharp.HighPerformance.Memory
ptr += stackSize; ptr += stackSize;
} }
public IL4Predict PrefetchPredict { get; set; } // public IL4Predict PrefetchPredict { get; set; }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ContinueOnce( public void ContinueOnce(
@@ -121,7 +123,7 @@ namespace StgSharp.HighPerformance.Memory
nuint origin nuint origin
) )
{ {
if ((_aheadCount < PredictionLeastAhead) && !PrefetchPredict.IsExhausted) if ((_aheadCount < PredictionLeastAhead) && !_exhausted)
{ {
if (Interlocked.CompareExchange(ref _prefetching, 1, 0) == 0) if (Interlocked.CompareExchange(ref _prefetching, 1, 0) == 0)
{ {
@@ -193,7 +195,7 @@ namespace StgSharp.HighPerformance.Memory
_activeCount = ref activeCount; _activeCount = ref activeCount;
} }
[Obsolete("You should not create a CacheLineHandle outside a L4", true)] [Obsolete("You should not create a CacheLineHandle instance outside a L4", true)]
public L4CacheLine() { } public L4CacheLine() { }
public readonly nuint Address => _handle; public readonly nuint Address => _handle;
@@ -42,7 +42,6 @@ namespace StgSharp.Common.HighPerformance.ProcessorAbstraction
internal NumaNode( internal NumaNode(
uint numaId, uint numaId,
int count, int count,
Span<int> logicalCoreIndex,
Span<uint> logicalCoreIds Span<uint> logicalCoreIds
) )
@@ -90,10 +90,10 @@ namespace StgSharp.Mathematics.Numeric
int yKernel int yKernel
) )
{ {
return xKernel >= 0 && return (xKernel >= 0) &&
yKernel >= 0 && (yKernel >= 0) &&
xKernel < enumerator.KernelColumnLength && (xKernel < enumerator.KernelColumnLength) &&
yKernel < enumerator.KernelRowLength; (yKernel < enumerator.KernelRowLength);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -104,7 +104,7 @@ namespace StgSharp.Mathematics.Numeric
) )
{ {
long order = GetTriangularOrder(enumerator); long order = GetTriangularOrder(enumerator);
return xKernel >= 0 && yKernel >= 0 && xKernel < order && yKernel < order; return (xKernel >= 0) && (yKernel >= 0) && (xKernel < order) && (yKernel < order);
} }
private sealed class DenseSquareKernelBoarder : MatrixKernelBoarder private sealed class DenseSquareKernelBoarder : MatrixKernelBoarder
@@ -122,7 +122,7 @@ namespace StgSharp.Mathematics.Numeric
long col = enumerator.KernelColumnLength; long col = enumerator.KernelColumnLength;
// Console.Write($"Attempt to get offset at ({x},{y}), offset is {x * col + y} "); // Console.Write($"Attempt to get offset at ({x},{y}), offset is {x * col + y} ");
return x * col + y; return (x * col) + y;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -151,7 +151,7 @@ namespace StgSharp.Mathematics.Numeric
long x = xKernel; long x = xKernel;
long y = yKernel; long y = yKernel;
long prefixColumns = x * order - (x * (x - 1L)) / 2L; long prefixColumns = (x * order) - ((x * (x - 1L)) / 2L);
return prefixColumns + (y - x); return prefixColumns + (y - x);
} }
@@ -162,7 +162,8 @@ namespace StgSharp.Mathematics.Numeric
in MatrixSize enumerator in MatrixSize enumerator
) )
{ {
return IsWithinTriangularDomain(enumerator, xKernel, yKernel) && yKernel >= xKernel; return IsWithinTriangularDomain(enumerator, xKernel, yKernel) &&
(yKernel >= xKernel);
} }
} }
@@ -191,7 +192,8 @@ namespace StgSharp.Mathematics.Numeric
in MatrixSize enumerator in MatrixSize enumerator
) )
{ {
return IsWithinTriangularDomain(enumerator, xKernel, yKernel) && yKernel <= xKernel; return IsWithinTriangularDomain(enumerator, xKernel, yKernel) &&
(yKernel <= xKernel);
} }
} }
@@ -0,0 +1,97 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixEnumerator"
// Project: StgSharp
// 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
// 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 StgSharp.HighPerformance.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric.Runtime
{
public enum PredictPolicy
{
Transparent = 0,
BufferSequential = 1,
SquareRowMajor = 2,
SquareColMajor = 3,
}
public unsafe interface IMatrixCachePredictor<TSelf, T> : IL4Predict
where T : unmanaged, INumber<T>
where TSelf : class, IMatrixCachePredictor<TSelf, T>
{
public bool NeedWriteBack { get; }
public static abstract PredictPolicy Policy { get; set; }
}
public sealed unsafe class BufferSequentialPredictor<T> : IMatrixCachePredictor<BufferSequentialPredictor<T>, T>
where T : unmanaged, INumber<T>
{
private MatrixKernelEnumerator enumerator;
public bool NeedWriteBack => throw new NotImplementedException();
public static PredictPolicy Policy { get; set; } = PredictPolicy.SquareRowMajor;
private MatrixKernel* Buffer { get; set; }
public PredictResult Predict(
Span<CacheLinePrediction> node,
out int count
)
{
throw new NotImplementedException();
}
public void Prefetch(
nuint origin,
ulong policy,
Span<byte> cache
)
{
throw new NotImplementedException();
}
public void WriteBack(
nuint origin,
ulong policy,
ReadOnlySpan<byte> cache
)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric.Runtime
{
internal enum MatrixOperatorPolicy
{
Sequential = 1,
}
}
@@ -1,30 +1,27 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixParallelQueue.cs" // file="MatrixParallelQueue"
// 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 // Permission is hereby granted, free of charge, to any person obtaining a copy
// obtaining a copy of this software and associated documentation // of this software and associated documentation files (the "Software"), to deal
// files (the “Software”), to deal in the Software without restriction, // in the Software without restriction, including without limitation the rights
// including without limitation the rights to use, copy, modify, merge, // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// publish, distribute, sublicense, and/or sell copies of the Software, // copies of the Software, and to permit persons to whom the Software is
// and to permit persons to whom the Software is furnished to do so, // furnished to do so, subject to the following conditions:
// subject to the following conditions:
// //
// The above copyright notice and // The above copyright notice and this permission notice shall be included in all
// this permission notice shall be included in all copies // copies or substantial portions of the Software.
// or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED “AS IS”, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// ARISING FROM, OUT OF OR IN CONNECTION WITH // SOFTWARE.
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// //
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
@@ -42,11 +39,14 @@ using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric namespace StgSharp.Mathematics.Numeric
{ {
internal unsafe ref struct MatrixParallelQueue<TPredictor> where TPredictor: unmanaged, IMatrixPredictor<TPredictor> internal unsafe class MatrixParallelQueue<TPredictor, T> where T : unmanaged, INumber<T>
where TPredictor : class, IMatrixCachePredictor<TPredictor, T>
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Steal(MatrixParallelTask* task) public bool Steal(
MatrixParallelTask* task
)
{ {
// TODO 使用提取的内容填充task // TODO 使用提取的内容填充task
throw new NotImplementedException(); throw new NotImplementedException();
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric.Runtime
{
internal class MatrixParallelThread
{
}
}
@@ -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
@@ -55,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);
@@ -103,14 +105,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)
{ {
@@ -172,7 +178,7 @@ namespace StgSharp.Mathematics.Numeric
{ {
case PredictPolicy.Transparent: case PredictPolicy.Transparent:
break; break;
case PredictPolicy.Sequential: case PredictPolicy.BufferSequential:
break; break;
default: default:
break; break;
@@ -209,12 +215,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
} }
} }