hlsf wrong alloc solved
This commit is contained in:
@@ -78,10 +78,23 @@ namespace StgSharp.HighPerformance
|
||||
return !(left == right);
|
||||
}
|
||||
|
||||
public static M64 operator <<(M64 value, int shift)
|
||||
{
|
||||
M64 result = new M64();
|
||||
result.value = value.value << shift;
|
||||
return result;
|
||||
}
|
||||
|
||||
public static bool operator ==(M64 left, M64 right)
|
||||
{
|
||||
return left.value == right.value;
|
||||
}
|
||||
public static M64 operator >>(M64 value, int shift)
|
||||
{
|
||||
M64 result = new M64();
|
||||
result.value = value.value << shift;
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="HLSFAllocator.Alloc"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
// 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
|
||||
@@ -74,7 +74,6 @@ namespace StgSharp.HighPerformance.Memory
|
||||
SliceMemory(handle, (segmentIndex + 1) * levelSizeArray[level]);
|
||||
}
|
||||
handle->State = EntryState.Allocated;
|
||||
Console.WriteLine($"handle alloced at {handle->Position}");
|
||||
return new hlsfHandle(handle, size);
|
||||
}
|
||||
|
||||
@@ -213,7 +212,8 @@ namespace StgSharp.HighPerformance.Memory
|
||||
_spareMemory->Position = newPos;
|
||||
BucketNode* b = (BucketNode*)handle->Position;
|
||||
b->EntryRef = handle;
|
||||
PushLevel(handle->Level, DetermineSegmentIndex((uint)blockSize, handle->Level), b);
|
||||
|
||||
// PushLevel(handle->Level, DetermineSegmentIndex((uint)blockSize, handle->Level), b);
|
||||
|
||||
// update position linked list
|
||||
handle->NextNear = (ulong)_spareMemory;
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="HLSFAllocator.BucketNode"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
// 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
|
||||
@@ -55,6 +55,10 @@ namespace StgSharp.HighPerformance.Memory
|
||||
{
|
||||
_bucketHeads[index] = (nuint)node;
|
||||
node->IsInBucket = true;
|
||||
node->NextLevel = EmptyHandle;
|
||||
node->PreviousLevel = EmptyHandle;
|
||||
|
||||
// Console.WriteLine($"Push to empty level of {levelIndex} {sizeIndex}, bucket pos {(ulong)node}");
|
||||
return;
|
||||
} else
|
||||
{
|
||||
@@ -62,7 +66,10 @@ namespace StgSharp.HighPerformance.Memory
|
||||
node->NextLevel = head;
|
||||
BucketNode* headNode = (BucketNode*)head;
|
||||
node->IsInBucket = true;
|
||||
if (headNode != null) {
|
||||
|
||||
// Console.WriteLine($"Push to head of level of {levelIndex} {sizeIndex}, bucket pos {(ulong)node}, previous head is at {head}");
|
||||
if (headNode != null)
|
||||
{
|
||||
headNode->PreviousLevel = (ulong)node;
|
||||
}
|
||||
return;
|
||||
@@ -111,6 +118,7 @@ namespace StgSharp.HighPerformance.Memory
|
||||
return false;
|
||||
}
|
||||
|
||||
// Console.WriteLine($"level popped from level {levelIndex} {sizeIndex}, node is at {(ulong)head}");
|
||||
BucketNode* next = (BucketNode*)head->NextLevel;
|
||||
|
||||
if (next == null)
|
||||
@@ -123,14 +131,8 @@ namespace StgSharp.HighPerformance.Memory
|
||||
node = head;
|
||||
node->IsInBucket = false;
|
||||
node->NextLevel = EmptyHandle;
|
||||
if (next != null)
|
||||
{
|
||||
Console.WriteLine("////////////// catching error ///////////////");
|
||||
Console.WriteLine($"ref next: {(ulong)next}");
|
||||
Console.WriteLine($"base pos: {(ulong)m_Buffer}");
|
||||
Console.WriteLine(_size);
|
||||
if (next != null) {
|
||||
next->PreviousLevel = EmptyHandle;
|
||||
Console.WriteLine("///////////// no error caught ///////////////");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -157,6 +159,8 @@ namespace StgSharp.HighPerformance.Memory
|
||||
node->NextLevel = EmptyHandle;
|
||||
node->PreviousLevel = EmptyHandle;
|
||||
node->IsInBucket = false;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -164,30 +168,15 @@ namespace StgSharp.HighPerformance.Memory
|
||||
/// BucketNode structure - specialized node for bucket linked lists Stored in main
|
||||
/// allocation area (user-overwritable region)
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 40)]
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
internal struct BucketNode
|
||||
{
|
||||
|
||||
[FieldOffset(0)] internal Entry* EntryRef; // Pointer to corresponding Entry
|
||||
[FieldOffset(32)] internal bool IsInBucket; // Whether in bucket
|
||||
|
||||
[FieldOffset(8)] internal ulong _nextLevel; // Bucket stack linked list: next pointer
|
||||
[FieldOffset(16)] internal ulong LockBuffer;
|
||||
[FieldOffset(24)] internal ulong PreviousLevel; // Bucket stack linked list: previous pointer
|
||||
|
||||
internal ulong NextLevel
|
||||
{
|
||||
get
|
||||
{
|
||||
Console.WriteLine($"get next level {_nextLevel}");
|
||||
return _nextLevel;
|
||||
}
|
||||
set
|
||||
{
|
||||
Console.WriteLine($"NextLevel set to {value}.");
|
||||
_nextLevel = value;
|
||||
}
|
||||
}
|
||||
[FieldOffset(8)] internal ulong NextLevel; // Bucket stack linked list: next pointer
|
||||
[FieldOffset(24)] internal ulong PreviousLevel; // Bucket stack linked list: previous pointer
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,21 @@ namespace StgSharp.HighPerformance.Memory
|
||||
public unsafe partial class HybridLayerSegregatedFitAllocator
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// check out if a bucket is within the same higher-level boundary as currentOffset
|
||||
/// </summary>
|
||||
/// <param name="currentOffset">
|
||||
///
|
||||
/// </param>
|
||||
/// <param name="target">
|
||||
///
|
||||
/// </param>
|
||||
/// <param name="level">
|
||||
///
|
||||
/// </param>
|
||||
/// <returns>
|
||||
///
|
||||
/// </returns>
|
||||
private bool AssertBackwardBoundary(ulong currentOffset, Entry* target, int level)
|
||||
{
|
||||
int nextLevelSize = levelSizeArray[level] * 4;
|
||||
@@ -45,6 +60,21 @@ namespace StgSharp.HighPerformance.Memory
|
||||
return (currentOffset / (ulong)nextLevelSize) == (targetHead / (ulong)nextLevelSize);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// check out if a bucket is within the same higher-level boundary as currentOffset
|
||||
/// </summary>
|
||||
/// <param name="currentOffset">
|
||||
///
|
||||
/// </param>
|
||||
/// <param name="target">
|
||||
///
|
||||
/// </param>
|
||||
/// <param name="level">
|
||||
///
|
||||
/// </param>
|
||||
/// <returns>
|
||||
///
|
||||
/// </returns>
|
||||
private bool AssertForwardBoundary(ulong currentOffset, Entry* target, int level)
|
||||
{
|
||||
int nextLevelSize = levelSizeArray[level] * 4;
|
||||
@@ -75,7 +105,7 @@ namespace StgSharp.HighPerformance.Memory
|
||||
/// <summary>
|
||||
/// Used in Free operations, efficiently remove from buckets and merge adjacent blocks.
|
||||
/// Current Entry will serve as the final merged block. The Merge will be in lazy
|
||||
/// strategy, any fail in acquiring spin lock will end a merging process.
|
||||
/// strategy.
|
||||
/// </summary>
|
||||
private void MergeAndRemoveFromBuckets(Entry* entry)
|
||||
{
|
||||
@@ -99,8 +129,7 @@ namespace StgSharp.HighPerformance.Memory
|
||||
ulong p = entry->PreviousNear;
|
||||
Entry* prev = (Entry*)p;
|
||||
|
||||
|
||||
// 计算当前块相对于分配空间基址的偏移量
|
||||
// compute offset of current block and base position
|
||||
ulong currentOffset = entry->Position - (ulong)m_Buffer;
|
||||
|
||||
if (prev == null ||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="HLSFAllocator"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
// 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
|
||||
@@ -28,6 +28,7 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.HighPerformance.Memory
|
||||
{
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixCompute.Basic"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
// 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
|
||||
@@ -86,7 +86,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
return ans;
|
||||
}
|
||||
|
||||
public static unsafe void Fill<T>(Matrix<float> ans, T value) where T: unmanaged, INumber<T>
|
||||
public static unsafe void Fill<T>(Matrix<float> ans, T value) where T : unmanaged, INumber<T>
|
||||
{
|
||||
long count = (long)ans.KernelColumnLength * ans.KernelRowLength;
|
||||
ScalarPacket* scalar = MatrixParallelFactory.CreateScalarPacket();
|
||||
|
||||
@@ -57,7 +57,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
MatrixParallelFactory.Release(p);
|
||||
}
|
||||
|
||||
internal static unsafe void BufferComputeUnaryScalar(MatrixParallelTaskPackage* p)
|
||||
internal static unsafe void BufferComputeUnary(MatrixParallelTaskPackage* p)
|
||||
{
|
||||
int size = p->ElementSize * 16;
|
||||
long count = Unsafe.As<int, long>(ref p->PrimCount);
|
||||
@@ -69,6 +69,17 @@ namespace StgSharp.Mathematics.Numeric
|
||||
MatrixParallelFactory.Release(p);
|
||||
}
|
||||
|
||||
internal static unsafe void BufferComputeUnaryScalar(MatrixParallelTaskPackage* p)
|
||||
{
|
||||
int size = p->ElementSize * 16;
|
||||
long count = Unsafe.As<int, long>(ref p->PrimCount);
|
||||
long offset = Unsafe.As<int, long>(ref p->PrimTileOffset) * size;
|
||||
((delegate* unmanaged[Cdecl]< void*, void*, ScalarPacket*, long, void>)p->ComputeHandle)((void*)(p->Right + offset),
|
||||
(void*)(p->Result + offset),
|
||||
p->Scalar, count);
|
||||
MatrixParallelFactory.Release(p);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal static unsafe void Compute_Binary(MatrixParallelTaskPackage* p)
|
||||
{
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallelThread"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
// 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
|
||||
@@ -93,6 +93,7 @@ namespace StgSharp.Mathematics.Numeric
|
||||
switch (p->ComputeMode.ParameterStyle)
|
||||
{
|
||||
case MatrixOperationParam.RIGHT_ANS_PARAM:
|
||||
MatrixComputeModel.BufferComputeUnary(p);
|
||||
break;
|
||||
case MatrixOperationParam.LEFT_RIGHT_ANS_PARAM:
|
||||
MatrixComputeModel.BufferComputeBinary(p);
|
||||
|
||||
@@ -0,0 +1,259 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="HlsfStressTest"
|
||||
// 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.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="HlsfStressTest" (performance benchmark)
|
||||
// Project: StgSharp
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.HighPerformance.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace StgSharp
|
||||
{
|
||||
internal static class HlsfStressTest
|
||||
{
|
||||
|
||||
private const uint MaxAllocSize = 32 * 1024 * 1024; // 32MB documented cap
|
||||
|
||||
// Allow overriding arena size via env HLSF_ARENA_MB (must be >=64MB)
|
||||
private static readonly ulong ArenaBytes = Environment.GetEnvironmentVariable("HLSF_ARENA_MB") is string mbStr &&
|
||||
int.TryParse(mbStr, out int mb) &&
|
||||
mb >= 64 ?
|
||||
(ulong)mb * 1024UL * 1024UL :
|
||||
3UL * 1024UL * 1024UL * 1024UL;
|
||||
|
||||
private static readonly uint[] SizeSweep = {
|
||||
64, 128, 192, 256, 512, 768, 1024, 2048, 3072, 4096, 8192, 12288, 16384, 32768, 49152, 65536, 131072, 196608, 262144, 524288, 786432, 1_048_576, 2_097_152, 3_145_728, 4_194_304, 8_388_608, 12_582_912, 16_777_216, 25_165_824, 33_554_432
|
||||
};
|
||||
|
||||
public static unsafe void Run()
|
||||
{
|
||||
Console.WriteLine("=== HLSF Allocator Single-Thread Performance Benchmark ===");
|
||||
Console.WriteLine($"Arena size: {ArenaBytes / (1024 * 1024)} MiB (env override: HLSF_ARENA_MB)");
|
||||
using HybridLayerSegregatedFitAllocator allocator = new HybridLayerSegregatedFitAllocator((nuint)ArenaBytes);
|
||||
|
||||
Warmup(allocator);
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("[1] Allocation throughput (no touch)");
|
||||
foreach (uint sz in SizeSweep)
|
||||
{
|
||||
uint size = sz > MaxAllocSize ? MaxAllocSize : sz;
|
||||
BenchAllocThroughput(allocator, size, touch:false);
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("[2] Allocation throughput (touch first byte)");
|
||||
foreach (uint sz in SizeSweep)
|
||||
{
|
||||
uint size = sz > MaxAllocSize ? MaxAllocSize : sz;
|
||||
BenchAllocThroughput(allocator, size, touch:true);
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("[3] Free throughput after pre-allocation (256k blocks of 1KB)");
|
||||
BenchFreeThroughput(allocator, 256 * 1024, 1024);
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("[4] Alloc+Immediate Free pairs (5,000,000 x 256B)");
|
||||
BenchAllocFreePairs(allocator, 5_000_000, 256);
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("[5] Fragmentation cycle (allocate ~1024MB random, free half, reallocate)");
|
||||
BenchFragmentationCycle(allocator, 1024);
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("=== Benchmark Complete ===");
|
||||
}
|
||||
|
||||
private static void BenchAllocFreePairs(
|
||||
HybridLayerSegregatedFitAllocator allocator,
|
||||
int pairs,
|
||||
uint blockSize)
|
||||
{
|
||||
Stopwatch sw = Stopwatch.StartNew();
|
||||
for (int i = 0; i < pairs; i++)
|
||||
{
|
||||
HybridLayerSegregatedFitAllocationHandle h = allocator.Alloc(blockSize);
|
||||
allocator.Free(h);
|
||||
}
|
||||
sw.Stop();
|
||||
double opsPerSec = pairs / sw.Elapsed.TotalSeconds;
|
||||
Console.WriteLine($" size={Fmt(blockSize),7} pairs={pairs,9} alloc+free/s={opsPerSec,12:0.0}");
|
||||
}
|
||||
|
||||
private static void BenchAllocThroughput(
|
||||
HybridLayerSegregatedFitAllocator allocator,
|
||||
uint blockSize,
|
||||
bool touch)
|
||||
{
|
||||
ulong budget = ArenaBytes / 2; // use half arena for test
|
||||
if (blockSize == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
long ops = (long)(budget / blockSize);
|
||||
ops = Math.Min(ops, blockSize >= (4 * 1024 * 1024) ? 2048 : 2_000_000);
|
||||
if (ops <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<HybridLayerSegregatedFitAllocationHandle> list = new List<HybridLayerSegregatedFitAllocationHandle>((int)ops);
|
||||
Stopwatch sw = Stopwatch.StartNew();
|
||||
for (long i = 0; i < ops; i++)
|
||||
{
|
||||
HybridLayerSegregatedFitAllocationHandle h = allocator.Alloc(blockSize);
|
||||
if (touch)
|
||||
{
|
||||
Span<byte> span = h.BufferHandle;
|
||||
if (!span.IsEmpty) {
|
||||
span[0] = (byte)(i & 0xFF);
|
||||
}
|
||||
}
|
||||
list.Add(h);
|
||||
}
|
||||
sw.Stop();
|
||||
double allocOpsPerSec = ops / sw.Elapsed.TotalSeconds;
|
||||
foreach (HybridLayerSegregatedFitAllocationHandle h in list) {
|
||||
allocator.Free(h);
|
||||
}
|
||||
|
||||
Console.WriteLine($" size={Fmt(blockSize),7} ops={ops,9} alloc/s={allocOpsPerSec,12:0.0} touch={touch}");
|
||||
}
|
||||
|
||||
private static void BenchFragmentationCycle(
|
||||
HybridLayerSegregatedFitAllocator allocator,
|
||||
int targetMiB)
|
||||
{
|
||||
int targetBytes = targetMiB * 1024 * 1024;
|
||||
Random rng = new Random(123);
|
||||
List<HybridLayerSegregatedFitAllocationHandle> live = new List<HybridLayerSegregatedFitAllocationHandle>(128 * 1024);
|
||||
long allocated = 0;
|
||||
Stopwatch swA = Stopwatch.StartNew();
|
||||
while (allocated < targetBytes)
|
||||
{
|
||||
uint sz = PickSize(rng);
|
||||
HybridLayerSegregatedFitAllocationHandle h = allocator.Alloc(sz);
|
||||
live.Add(h);
|
||||
allocated += sz;
|
||||
}
|
||||
swA.Stop();
|
||||
Console.WriteLine($" phaseA alloc {targetMiB}MB in {swA.Elapsed.TotalSeconds:0.00}s blocks={live.Count}");
|
||||
|
||||
Stopwatch swB = Stopwatch.StartNew();
|
||||
int freed = 0;
|
||||
for (int i = live.Count - 1; i >= 0; i -= 2)
|
||||
{
|
||||
allocator.Free(live[i]);
|
||||
live.RemoveAt(i);
|
||||
freed++;
|
||||
}
|
||||
swB.Stop();
|
||||
Console.WriteLine($" phaseB freed {freed} in {swB.Elapsed.TotalSeconds:0.00}s remain={live.Count}");
|
||||
|
||||
allocated = 0;
|
||||
int reCount = 0;
|
||||
Stopwatch swC = Stopwatch.StartNew();
|
||||
while (allocated < targetBytes / 2)
|
||||
{
|
||||
uint sz = PickSize(rng);
|
||||
HybridLayerSegregatedFitAllocationHandle h = allocator.Alloc(sz);
|
||||
live.Add(h);
|
||||
allocated += sz;
|
||||
reCount++;
|
||||
}
|
||||
swC.Stop();
|
||||
Console.WriteLine($" phaseC re-alloc {reCount} in {swC.Elapsed.TotalSeconds:0.00}s ~{allocated / (1024.0 * 1024.0):0}MB");
|
||||
|
||||
Stopwatch swD = Stopwatch.StartNew();
|
||||
foreach (HybridLayerSegregatedFitAllocationHandle h in live) {
|
||||
allocator.Free(h);
|
||||
}
|
||||
|
||||
swD.Stop();
|
||||
Console.WriteLine($" cleanup freed {live.Count} in {swD.Elapsed.TotalSeconds:0.00}s");
|
||||
}
|
||||
|
||||
private static void BenchFreeThroughput(
|
||||
HybridLayerSegregatedFitAllocator allocator,
|
||||
int count,
|
||||
uint blockSize)
|
||||
{
|
||||
List<HybridLayerSegregatedFitAllocationHandle> list = new List<HybridLayerSegregatedFitAllocationHandle>(count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
list.Add(allocator.Alloc(blockSize));
|
||||
}
|
||||
|
||||
Stopwatch sw = Stopwatch.StartNew();
|
||||
for (int i = 0; i < count; i++) {
|
||||
allocator.Free(list[i]);
|
||||
}
|
||||
|
||||
sw.Stop();
|
||||
double freesPerSec = count / sw.Elapsed.TotalSeconds;
|
||||
Console.WriteLine($" size={Fmt(blockSize),7} count={count,9} free/s={freesPerSec,12:0.0}");
|
||||
}
|
||||
|
||||
private static string Fmt(uint bytes)
|
||||
{
|
||||
if (bytes >= 1024 * 1024) {
|
||||
return $"{bytes / (1024 * 1024)}MB";
|
||||
}
|
||||
|
||||
if (bytes >= 1024) {
|
||||
return $"{bytes / 1024}KB";
|
||||
}
|
||||
|
||||
return $"{bytes}B";
|
||||
}
|
||||
|
||||
private static uint PickSize(Random rng)
|
||||
{
|
||||
uint raw = SizeSweep[rng.Next(SizeSweep.Length)];
|
||||
return raw > MaxAllocSize ? MaxAllocSize : raw;
|
||||
}
|
||||
|
||||
private static void Warmup(HybridLayerSegregatedFitAllocator allocator)
|
||||
{
|
||||
List<HybridLayerSegregatedFitAllocationHandle> tmp = new List<HybridLayerSegregatedFitAllocationHandle>(4096);
|
||||
for (int i = 0; i < 4096; i++) {
|
||||
tmp.Add(allocator.Alloc(256));
|
||||
}
|
||||
|
||||
foreach (HybridLayerSegregatedFitAllocationHandle h in tmp) {
|
||||
allocator.Free(h);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,198 +0,0 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixParallelTest"
|
||||
// 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 StgSharp.Mathematics;
|
||||
using StgSharp.Mathematics.Numeric;
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
|
||||
namespace StgSharp
|
||||
{
|
||||
internal partial class Program
|
||||
{
|
||||
/*
|
||||
public static void TestMatrixParallel()
|
||||
{
|
||||
// 4. 线程调度基础功能测试
|
||||
Console.WriteLine("=== 线程调度基础功能测试 ===");
|
||||
TestSchedulingBasics();
|
||||
Console.WriteLine();
|
||||
|
||||
// 5. 并行控制流测试
|
||||
Console.WriteLine("=== 并行控制流测试 ===");
|
||||
TestParallelControlFlow();
|
||||
Console.WriteLine();
|
||||
|
||||
// 6. 重复操作稳定性测试
|
||||
Console.WriteLine("=== 重复操作稳定性测试 ===");
|
||||
TestRepeatedOperations();
|
||||
Console.WriteLine();
|
||||
|
||||
// 7. 线程状态监控测试
|
||||
Console.WriteLine("=== 线程状态监控测试 ===");
|
||||
|
||||
// TestThreadStateMonitoring();
|
||||
Console.WriteLine();
|
||||
|
||||
Console.WriteLine("? MatrixParallel 分层线程调度系统空跑测试完成");
|
||||
}
|
||||
|
||||
private static void TestParallelControlFlow()
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine("测试并行控制流...");
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
Console.WriteLine($" 第 {i + 1} 轮控制流测试:");
|
||||
|
||||
|
||||
// 获取并行锁
|
||||
Console.WriteLine(" 执行 LeadParallel()...");
|
||||
MatrixParallel.LeadParallel();
|
||||
|
||||
try
|
||||
{
|
||||
// 启动并行处理
|
||||
Console.WriteLine($" 执行 LaunchParallel({(SleepMode)(i % 3 + 1)})...");
|
||||
MatrixParallel.LaunchParallel((SleepMode)(i % 3 + 1));
|
||||
|
||||
// 设置所有 Wrap
|
||||
MatrixParallel.SetAllWrap();
|
||||
|
||||
// 发布任务(空任务)
|
||||
// MatrixParallel.PublishTask();
|
||||
|
||||
// 短暂等待,模拟实际工作
|
||||
Thread.Sleep(10 + i * 5);
|
||||
|
||||
Console.WriteLine(" ? 控制流执行正常");
|
||||
}
|
||||
finally
|
||||
{
|
||||
// 释放并行锁
|
||||
Console.WriteLine(" 执行 EndParallel()...");
|
||||
MatrixParallel.EndParallel();
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("? 并行控制流测试通过");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"? 并行控制流测试失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static void TestRepeatedOperations()
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine("执行重复操作稳定性测试...");
|
||||
|
||||
Stopwatch stopwatch = Stopwatch.StartNew();
|
||||
int iterations = 50;
|
||||
int successCount = 0;
|
||||
|
||||
for (int i = 0; i < iterations; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
MatrixParallel.LeadParallel();
|
||||
|
||||
MatrixParallel.LaunchParallel(SleepMode.QuickWakeup);
|
||||
MatrixParallel.SetAllWrap();
|
||||
|
||||
// MatrixParallel.PublishTask();
|
||||
|
||||
|
||||
// 极短暂的等待
|
||||
Thread.Sleep(1);
|
||||
|
||||
successCount++;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($" 第 {i + 1} 次操作失败: {ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
MatrixParallel.EndParallel();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($" 第 {i + 1} 次 EndParallel 失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// 每10次操作显示一次进度
|
||||
if ((i + 1) % 10 == 0)
|
||||
{
|
||||
Console.WriteLine($" 已完成 {i + 1}/{iterations} 次操作");
|
||||
}
|
||||
}
|
||||
|
||||
stopwatch.Stop();
|
||||
|
||||
Console.WriteLine($"? 重复操作测试完成:");
|
||||
Console.WriteLine($" 总操作次数: {iterations}");
|
||||
Console.WriteLine($" 成功次数: {successCount}");
|
||||
Console.WriteLine($" 成功率: {(double)successCount / iterations * 100:F1}%");
|
||||
Console.WriteLine($" 总耗时: {stopwatch.ElapsedMilliseconds} ms");
|
||||
Console.WriteLine($" 平均耗时: {stopwatch.ElapsedMilliseconds / (double)iterations:F2} ms/次");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"? 重复操作测试失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static void TestSchedulingBasics()
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine("测试基本调度操作...");
|
||||
|
||||
// 测试设置所有 Wrap
|
||||
Console.WriteLine(" 执行 SetAllWrap()...");
|
||||
MatrixParallel.SetAllWrap();
|
||||
Console.WriteLine(" ? SetAllWrap() 执行成功");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"? 基本调度操作测试失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
/**/
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Program"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
// 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
|
||||
@@ -61,12 +61,15 @@ namespace StgSharp
|
||||
{
|
||||
try
|
||||
{
|
||||
World.Initialize().UseModule<NumericalModule>().UseModule<GraphicModule>().InitializeModule();
|
||||
World.Initialize()
|
||||
.UseModule<NumericalModule>()
|
||||
.UseModule<GraphicModule>()
|
||||
.InitializeModule();
|
||||
|
||||
// TestRender();
|
||||
|
||||
|
||||
// 原有的矩阵测试代码
|
||||
HlsfStressTest.Run();
|
||||
/*
|
||||
HybridLayerSegregatedFitAllocator hlsf = new HybridLayerSegregatedFitAllocator((nuint)(64 * (ulong)10000 * 10000));
|
||||
|
||||
Matrix<float> m = Matrix.FromHlsf<float>(1000, 1000, hlsf);
|
||||
@@ -87,7 +90,7 @@ namespace StgSharp
|
||||
GC.Collect();
|
||||
}
|
||||
|
||||
// MatrixCompute.Mul(n, 12, n);
|
||||
MatrixCompute.Mul(n, 12, n);
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
for (int j = 0; j < 20; j++) {
|
||||
@@ -96,13 +99,13 @@ namespace StgSharp
|
||||
Console.WriteLine();
|
||||
}
|
||||
/**/
|
||||
/**/
|
||||
/*
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
Console.ReadLine();
|
||||
/**/
|
||||
/*
|
||||
World.WriteLog($"{ex}", LogType.Error);
|
||||
Dialogue.PostError(ex);
|
||||
/**/
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="SimdDemo.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 StgSharp.Mathematics;
|
||||
|
||||
namespace StgSharp
|
||||
{
|
||||
public static class SimdDemo
|
||||
{
|
||||
|
||||
public static void TestMethod() { }
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user