HLSF use long as aloloc size

This commit is contained in:
Nitload
2026-02-23 12:32:15 +08:00
parent 4bd88ccbca
commit c155cab510
552 changed files with 154266 additions and 154330 deletions
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="IBidirectionalDictionary"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// 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
@@ -32,9 +32,7 @@ namespace StgSharp.Collections
public interface IBidirectionalDictionary<TFirst, TSecond> : IDictionary<TFirst, TSecond>
{
TFirst this[
TSecond key
] { get; set; }
TFirst this[TSecond key] { get; set; }
ICollection<TFirst> FirstIndex { get; }
@@ -55,9 +53,7 @@ namespace StgSharp.Collections
/// true if the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains an element with
/// the specified key; otherwise, false.
/// </returns>
bool Contains(
TSecond key
);
bool Contains(TSecond key);
/// <summary>
/// Determines whether the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains the
@@ -70,23 +66,16 @@ namespace StgSharp.Collections
/// true if the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains an element with
/// the specified key; otherwise, false.
/// </returns>
bool Contains(
TFirst key
);
bool Contains(TFirst key);
bool TryGetValue(
TSecond key,
out TFirst value
);
bool TryGetValue(TSecond key, out TFirst value);
#pragma warning disable CA1033
ICollection<TFirst> IDictionary<TFirst, TSecond>.Keys => FirstIndex;
ICollection<TSecond> IDictionary<TFirst, TSecond>.Values => SecondIndex;
bool IDictionary<TFirst, TSecond>.ContainsKey(
TFirst key
)
bool IDictionary<TFirst, TSecond>.ContainsKey(TFirst key)
{
return Contains(key);
}
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="HLSFAllocationHandle"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// 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
@@ -40,9 +40,9 @@ namespace StgSharp.HighPerformance.Memory
{
internal readonly Allocator.Entry* EntryHandle;
public readonly uint AllocSize;
public readonly long AllocSize;
internal HybridLayerSegregatedFitAllocationHandle(Allocator.Entry* handle, uint s)
internal HybridLayerSegregatedFitAllocationHandle(Allocator.Entry* handle, long s)
{
EntryHandle = handle;
Pointer = (byte*)handle->Position;
@@ -56,7 +56,7 @@ namespace StgSharp.HighPerformance.Memory
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual<uint>(index, AllocSize);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, AllocSize);
return ref Pointer[index];
}
}
@@ -1,9 +1,9 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="HLSFAllocator.Alloc"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// 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
@@ -26,10 +26,7 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Diagnostics.Tracing;
using System.Numerics;
using System.Runtime.InteropServices;
using hlsfHandle = StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
namespace StgSharp.HighPerformance.Memory
@@ -37,9 +34,7 @@ namespace StgSharp.HighPerformance.Memory
public unsafe partial class HybridLayerSegregatedFitAllocator
{
public hlsfHandle Alloc(
uint size
)
public hlsfHandle Alloc(long size)
{
if (size > _maxSize) {
throw new InvalidOperationException("Requested size exceeds maximum allocatable size.");
@@ -96,10 +91,7 @@ namespace StgSharp.HighPerformance.Memory
/// <param name="sizeToSlice">
///
/// </param>
private void SliceMemory(
Entry* e,
long sizeToSlice
)
private void SliceMemory(Entry* e, long sizeToSlice)
{
Entry* next = e;
long remain = sizeToSlice; // remained size to slice
@@ -197,9 +189,7 @@ namespace StgSharp.HighPerformance.Memory
/// <returns>
/// True if allocation succeeded
/// </returns>
private bool TryAllocateNew32MBBlock(
out Entry* handle
)
private bool TryAllocateNew32MBBlock(out Entry* handle)
{
nuint pos = _spareMemory->Position;
long remain = _spareMemory->Size;
@@ -229,11 +219,7 @@ namespace StgSharp.HighPerformance.Memory
/// <summary>
/// Helper method for allocation - converts BucketNode to Entry
/// </summary>
private bool TryPopLevelForAllocation(
int levelIndex,
int sizeIndex,
out Entry* entry
)
private bool TryPopLevelForAllocation(int levelIndex, int sizeIndex, out Entry* entry)
{
if (TryPopLevel(levelIndex, sizeIndex, out BucketNode* bucketNode))
{
@@ -1,9 +1,9 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="HLSFAllocator.Level"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// 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
@@ -40,42 +40,19 @@ namespace StgSharp.HighPerformance.Memory
private const int MaxLevel = 9;
private int[] _levelSizeArray = [
64,
256,
1024,
4096,
16384,
65536,
262144,
1048576,
4194304,
16777216
];
private int[] _levelSizeArray = [ 64, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216 ];
public int AlignOfLevel(
int bucketLevel
)
public int AlignOfLevel(int bucketLevel)
{
int blockSize = _levelSizeArray[bucketLevel];
return int.Min(blockSize, _align);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetLevelFromSize(
long size
)
{
return GetLevelFromSize((uint)size);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetLevelFromSize(
uint size
)
private static int GetLevelFromSize(long size)
{
/*
* Level sizes: [64, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216]
* Level sizes: [64, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, ......]
* Pattern: 64 * 4^level = 2^6 * 2^(2*level) = 2^(6+2*level)
*
* For size S, we need: 2^(6+2*level) >= S
@@ -83,9 +60,9 @@ namespace StgSharp.HighPerformance.Memory
* Therefore: level >= (log2(S) - 6) / 2
*
* We use hardware LZCNT instruction via BitOperations.LeadingZeroCount
* log2(S) = 31 - LeadingZeroCount(S) for 32-bit values
* log2(S) = 63 - LeadingZeroCount(S) for 64-bit values
*/
int log2Size = 31 - BitOperations.LeadingZeroCount(size);
int log2Size = 63 - BitOperations.LeadingZeroCount((ulong)size);
// Calculate level: level = max(0, (log2Size - 6) / 2)
int level = Math.Max(0, (log2Size - 6) / 2);
@@ -94,6 +71,12 @@ namespace StgSharp.HighPerformance.Memory
return level;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetLevelFromSize(uint size)
{
return GetLevelFromSize(size);
}
}
public enum CommonMemoryAlign : int
@@ -1,9 +1,9 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="HLSFAllocator"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// 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
@@ -44,13 +44,11 @@ namespace StgSharp.HighPerformance.Memory
private readonly byte* m_Buffer;
private bool disposedValue;
private readonly int _align;
private readonly long _maxSize;
private readonly nuint _size;
private readonly SlabAllocator<Entry> _entries;
private readonly uint _maxSize;
public HybridLayerSegregatedFitAllocator(
nuint byteSize
)
public HybridLayerSegregatedFitAllocator(nuint byteSize)
{
if (byteSize < 64) {
throw new InvalidOperationException();
@@ -72,13 +70,10 @@ namespace StgSharp.HighPerformance.Memory
_maxSize = 32U * 1024U * 1024U;
}
public HybridLayerSegregatedFitAllocator(
nuint byteSize,
uint maxBlockSize
)
public HybridLayerSegregatedFitAllocator(nuint byteSize, uint maxBlockSize)
{
_entries = SlabAllocator<Entry>.Create(64, SlabBufferLayout.Chunked, false);
m_Buffer = (byte *)NativeMemory.AlignedAlloc(byteSize, (nuint)16);
m_Buffer = (byte *)NativeMemory.AlignedAlloc(byteSize, 16);
_align = 16;
_size = byteSize;
@@ -92,7 +87,7 @@ namespace StgSharp.HighPerformance.Memory
int levelCountOrigin = _levelSizeArray.Length;
int levelCount = GetLevelFromSize(maxBlockSize) + 1;
Array.Resize(ref _levelSizeArray, levelCount);
_bucketHeads = new BucketNode *[levelCount * 3 + 1];
_bucketHeads = new BucketNode *[(levelCount * 3) + 1];
int size = _levelSizeArray[^1];
for (int i = levelCountOrigin; i < levelCount; i++)
{
@@ -102,10 +97,7 @@ namespace StgSharp.HighPerformance.Memory
_maxSize = maxBlockSize;
}
public HybridLayerSegregatedFitAllocator(
nuint byteSize,
int align
)
public HybridLayerSegregatedFitAllocator(nuint byteSize, int align)
{
if (align < 16 || (align & (align - 1)) != 0) {
throw new ArgumentException(
@@ -128,11 +120,7 @@ namespace StgSharp.HighPerformance.Memory
_maxSize = 32U * 1024U * 1024U;
}
public HybridLayerSegregatedFitAllocator(
nuint byteSize,
int align,
uint maxBlockSize
)
public HybridLayerSegregatedFitAllocator(nuint byteSize, int align, long maxBlockSize)
{
if (align < 16 || (align & (align - 1)) != 0) {
throw new ArgumentException(
@@ -169,9 +157,7 @@ namespace StgSharp.HighPerformance.Memory
GC.SuppressFinalize(this);
}
protected virtual void Dispose(
bool disposing
)
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
@@ -1,9 +1,9 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Matrix.Create"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// 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
@@ -72,15 +72,12 @@ namespace StgSharp.Mathematics.Numeric
};
long requiredBytes = kernelCount * MatrixKernel<T>.Size;
if (requiredBytes <= 0 || requiredBytes > uint.MaxValue) {
throw new ArgumentOutOfRangeException(nameof(requiredBytes), "Requested matrix exceeds allocator capacity.");
}
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(requiredBytes, nameof(requiredBytes));
HybridLayerSegregatedFitAllocationHandle handle = allocator.Alloc((uint)requiredBytes);
MatrixStorageHLSF<T> storage = new MatrixStorageHLSF<T>(allocator, handle);
Matrix<T> matrix = new Matrix<T>(MatrixKernelBoarder.Create(layout), new MatrixSize(columnLength,
rowLength,
Matrix<T> matrix = new Matrix<T>(MatrixKernelBoarder.Create(layout), new MatrixSize(columnLength, rowLength,
kernelColumns,
kernelRows), storage);
+12 -13
View File
@@ -1,9 +1,9 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixTest"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// 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
@@ -42,7 +42,9 @@ namespace StgSharp
internal static partial class Program
{
private static int sideLength = 20;
private const long size = 1024L * 1024L * 1024L * 4L;
private static int sideLength = 1000;
internal static unsafe void TestMatrix(
)
@@ -51,16 +53,13 @@ namespace StgSharp
/**/
HybridLayerSegregatedFitAllocator hlsf;
unchecked {
hlsf = new((nuint)(4 * (ulong)1024 * 1024 * 1024), 32, 1024U * 1024U * 256U);
hlsf = new((nuint)size, 32, size);
}
Matrix<T> m = Matrix<T>.Create(sideLength, sideLength, MatrixLayout.DenseRectangle,
hlsf);
Matrix<T> m = Matrix<T>.Create(sideLength, sideLength, MatrixLayout.DenseRectangle, hlsf);
MatrixCompute.Fill(m, 1.0f);
Matrix<T> n = Matrix<T>.Create(sideLength, sideLength, MatrixLayout.DenseRectangle,
hlsf);
Matrix<T> n = Matrix<T>.Create(sideLength, sideLength, MatrixLayout.DenseRectangle, hlsf);
MatrixCompute.Fill(n, 1.0f);
Matrix<T> p = Matrix<T>.Create(sideLength, sideLength, MatrixLayout.DenseRectangle,
hlsf);
Matrix<T> p = Matrix<T>.Create(sideLength, sideLength, MatrixLayout.DenseRectangle, hlsf);
MatrixCompute.Fill(p, 0.0f);
/*
@@ -88,9 +87,9 @@ namespace StgSharp
// value = T.Abs(value) < 1e-6 ? 0 : value;
Console.Write($"{p.GetElementOffset(j,i)}{extra}");
// Console.Write($"{p.GetElementOffset(j,i)}{extra}");
// Console.Write($"{value:000}{extra}");
Console.Write($"{value:000}{extra}");
}
Console.WriteLine();
}
+10 -14
View File
@@ -1,9 +1,9 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Program"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// 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
@@ -39,25 +39,20 @@ namespace StgSharp
private static string testExpPath = @"D:\Programmer\stepcode\data\ap203\ap203.exp";
internal static unsafe void Main(
string[] args
)
internal static unsafe void Main(string[] args)
{
try
{
World.Initialize()
.UseModule<NumericalModule>()
.UseModule<GraphicModule>()
.InitializeModule();
World.Initialize().UseModule<NumericalModule>().UseModule<GraphicModule>().InitializeModule();
TestRegexAnalyse.Run();
// TestRegexAnalyse.Run();
// HlsfStressTest.Run();
// TestMatrix();
TestMatrix();
// --- Logarithmic time sequence test: print seconds at each emitted span ---
/*
}
catch (Exception ex)
{
@@ -67,7 +62,8 @@ namespace StgSharp
World.WriteLog($"{ex}", LogType.Error);
Dialogue.PostError(ex);
/**/
World.Terminate(-1);
// World.Terminate(-1);
/**/
}
finally
+6 -6
View File
@@ -1,9 +1,9 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="TestRegexAnalyse"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
// 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
@@ -39,10 +39,10 @@ namespace StgSharp
{
private static string[] regexSource = [
@"http|https",
@"http|https|ftp|ftps",
@"abc[0-9]x|abc[0-9]y|abd[0-9]x|abd[0-9]y|xyz",
@"get_name|get_value|get_type|set_name|set_value|set_type"
];
@"get_name|get_value|get_type|set_name|set_value|set_type",
@"helix|hello|help|hero|hers|her" ];
public static void Run()
{