Daily update on 20251124-010227 by Nitload Youmu. Additional Message : error in hlsf

Signed-off-by: Nitload <YoumuLiu_NSI@whv0t.onmicrosoft.com>
This commit is contained in:
Nitload
2025-11-24 01:02:27 +08:00
parent 1f05a6a962
commit c1995d119a
6 changed files with 69 additions and 50 deletions
@@ -2,8 +2,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="HLSFAllocator.Alloc" // file="HLSFAllocator.Alloc"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
@@ -74,6 +74,7 @@ namespace StgSharp.HighPerformance.Memory
SliceMemory(handle, (segmentIndex + 1) * levelSizeArray[level]); SliceMemory(handle, (segmentIndex + 1) * levelSizeArray[level]);
} }
handle->State = EntryState.Allocated; handle->State = EntryState.Allocated;
Console.WriteLine($"handle alloced at {handle->Position}");
return new hlsfHandle(handle, size); return new hlsfHandle(handle, size);
} }
@@ -199,6 +200,7 @@ namespace StgSharp.HighPerformance.Memory
{ {
newPos = (nuint)m_Buffer + _size; // Clamp to maximum buffer size newPos = (nuint)m_Buffer + _size; // Clamp to maximum buffer size
blockSize = _spareMemory->Size; blockSize = _spareMemory->Size;
p = 0;
} }
// Try to allocate new Entry from slab allocator // Try to allocate new Entry from slab allocator
@@ -208,7 +210,7 @@ namespace StgSharp.HighPerformance.Memory
handle->Level = GetLevelFromSize((uint)blockSize); handle->Level = GetLevelFromSize((uint)blockSize);
// Update spare memory position // Update spare memory position
_spareMemory->Position = newPos; // Replace Volatile.Write with direct access _spareMemory->Position = newPos;
BucketNode* b = (BucketNode*)handle->Position; BucketNode* b = (BucketNode*)handle->Position;
b->EntryRef = handle; b->EntryRef = handle;
PushLevel(handle->Level, DetermineSegmentIndex((uint)blockSize, handle->Level), b); PushLevel(handle->Level, DetermineSegmentIndex((uint)blockSize, handle->Level), b);
@@ -2,8 +2,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="HLSFAllocator.BucketNode" // file="HLSFAllocator.BucketNode"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
@@ -26,10 +26,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
using System; using System;
using System.ComponentModel.DataAnnotations;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading;
namespace StgSharp.HighPerformance.Memory namespace StgSharp.HighPerformance.Memory
{ {
@@ -41,7 +39,7 @@ namespace StgSharp.HighPerformance.Memory
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsNullOrEmpty(BucketNode* node) private static bool IsNullOrEmpty(BucketNode* node)
{ {
return node == null || node->Magic != BucketNode.MAGIC_NUMBER; return node == null;
} }
#endregion #endregion
@@ -57,7 +55,6 @@ namespace StgSharp.HighPerformance.Memory
{ {
_bucketHeads[index] = (nuint)node; _bucketHeads[index] = (nuint)node;
node->IsInBucket = true; node->IsInBucket = true;
node->Magic = BucketNode.MAGIC_NUMBER;
return; return;
} else } else
{ {
@@ -65,7 +62,6 @@ namespace StgSharp.HighPerformance.Memory
node->NextLevel = head; node->NextLevel = head;
BucketNode* headNode = (BucketNode*)head; BucketNode* headNode = (BucketNode*)head;
node->IsInBucket = true; node->IsInBucket = true;
node->Magic = BucketNode.MAGIC_NUMBER;
if (headNode != null) { if (headNode != null) {
headNode->PreviousLevel = (ulong)node; headNode->PreviousLevel = (ulong)node;
} }
@@ -115,8 +111,7 @@ namespace StgSharp.HighPerformance.Memory
return false; return false;
} }
ulong n = head->NextLevel; BucketNode* next = (BucketNode*)head->NextLevel;
BucketNode* next = (BucketNode*)n;
if (next == null) if (next == null)
{ {
@@ -128,8 +123,14 @@ namespace StgSharp.HighPerformance.Memory
node = head; node = head;
node->IsInBucket = false; node->IsInBucket = false;
node->NextLevel = EmptyHandle; node->NextLevel = EmptyHandle;
if (next != null) { if (next != null)
{
Console.WriteLine("////////////// catching error ///////////////");
Console.WriteLine($"ref next: {(ulong)next}");
Console.WriteLine($"base pos: {(ulong)m_Buffer}");
Console.WriteLine(_size);
next->PreviousLevel = EmptyHandle; next->PreviousLevel = EmptyHandle;
Console.WriteLine("///////////// no error caught ///////////////");
} }
return true; return true;
} }
@@ -167,17 +168,26 @@ namespace StgSharp.HighPerformance.Memory
internal struct BucketNode internal struct BucketNode
{ {
public const uint MAGIC_NUMBER = 0xBEEFCAFE; // Magic number for node integrity verification [FieldOffset(0)] internal Entry* EntryRef; // Pointer to corresponding Entry
[FieldOffset(32)] internal bool IsInBucket; // Whether in bucket
[FieldOffset(16)] internal Entry* EntryRef; // Pointer to corresponding Entry [FieldOffset(8)] internal ulong _nextLevel; // Bucket stack linked list: next pointer
[FieldOffset(28)] internal bool IsInBucket; // Whether in bucket [FieldOffset(16)] internal ulong LockBuffer;
[FieldOffset(36)] internal int NextLock; [FieldOffset(24)] internal ulong PreviousLevel; // Bucket stack linked list: previous pointer
[FieldOffset(32)] internal int PrevLock;
[FieldOffset(24)] internal uint Magic; // Magic number verification
[FieldOffset(32)] internal ulong LockBuffer;
[FieldOffset(0)] internal ulong NextLevel; // Bucket stack linked list: next pointer internal ulong NextLevel
[FieldOffset(8)] internal ulong PreviousLevel; // Bucket stack linked list: previous pointer {
get
{
Console.WriteLine($"get next level {_nextLevel}");
return _nextLevel;
}
set
{
Console.WriteLine($"NextLevel set to {value}.");
_nextLevel = value;
}
}
} }
@@ -2,8 +2,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="HLSFAllocator" // file="HLSFAllocator"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
@@ -34,7 +34,7 @@ namespace StgSharp.HighPerformance.Memory
public unsafe partial class HybridLayerSegregatedFitAllocator : IDisposable public unsafe partial class HybridLayerSegregatedFitAllocator : IDisposable
{ {
private static readonly nuint EmptyHandle = (nuint)(void*)null; private const nuint EmptyHandle = 0;
// Bucket system field - directly in main allocator class // Bucket system field - directly in main allocator class
private ulong[] _bucketHeads; private ulong[] _bucketHeads;
@@ -2,8 +2,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixCompute.Basic" // file="MatrixCompute.Basic"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
@@ -41,13 +41,15 @@ namespace StgSharp.Mathematics.Numeric
public static class MatrixCompute public static class MatrixCompute
{ {
public static unsafe Matrix<float> Add(this Matrix<float> left, Matrix<float> right) public static unsafe Matrix<float> Add(Matrix<float> left, Matrix<float> right, Matrix<float> ans)
{ {
if (left.ColumnLength != right.ColumnLength || left.RowLength != right.RowLength) { if (left.ColumnLength != right.ColumnLength ||
left.RowLength != right.RowLength ||
left.ColumnLength != ans.ColumnLength ||
left.RowLength != ans.RowLength) {
throw new IndexOutOfRangeException("Matrix dimensions must agree."); throw new IndexOutOfRangeException("Matrix dimensions must agree.");
} }
long count = (long)left.KernelColumnLength * left.KernelRowLength; long count = (long)left.KernelColumnLength * left.KernelRowLength;
Matrix<float> ans = Matrix.FromDefault<float>(left.ColumnLength, left.RowLength);
if (count <= 256) if (count <= 256)
{ {
MatrixKernel<float>* leftPtr = left.Buffer; MatrixKernel<float>* leftPtr = left.Buffer;
@@ -84,7 +86,7 @@ namespace StgSharp.Mathematics.Numeric
return ans; return ans;
} }
public static unsafe void Fill<T>(this 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; long count = (long)ans.KernelColumnLength * ans.KernelRowLength;
ScalarPacket* scalar = MatrixParallelFactory.CreateScalarPacket(); ScalarPacket* scalar = MatrixParallelFactory.CreateScalarPacket();
@@ -114,10 +116,12 @@ namespace StgSharp.Mathematics.Numeric
taskGroup.Dispose(); taskGroup.Dispose();
} }
public static unsafe Matrix<float> Mul(this Matrix<float> left, float right) public static unsafe Matrix<float> Mul(Matrix<float> left, float right, Matrix<float> ans)
{ {
if (left.ColumnLength != ans.ColumnLength || left.RowLength != ans.RowLength) {
throw new IndexOutOfRangeException("Matrix dimensions must agree.");
}
long count = (long)left.KernelColumnLength * left.KernelRowLength; long count = (long)left.KernelColumnLength * left.KernelRowLength;
Matrix<float> ans = Matrix.FromDefault<float>(left.ColumnLength, left.RowLength);
ScalarPacket* scalar = MatrixParallelFactory.CreateScalarPacket(); ScalarPacket* scalar = MatrixParallelFactory.CreateScalarPacket();
scalar->Data<float>(0) = right; scalar->Data<float>(0) = right;
if (count <= 256) if (count <= 256)
@@ -155,13 +159,15 @@ namespace StgSharp.Mathematics.Numeric
return ans; return ans;
} }
public static unsafe Matrix<float> Sub(this Matrix<float> left, Matrix<float> right) public static unsafe Matrix<float> Sub(Matrix<float> left, Matrix<float> right, Matrix<float> ans)
{ {
if (left.ColumnLength != right.ColumnLength || left.RowLength != right.RowLength) { if (left.ColumnLength != right.ColumnLength ||
left.RowLength != right.RowLength ||
left.ColumnLength != ans.ColumnLength ||
left.RowLength != ans.RowLength) {
throw new IndexOutOfRangeException("Matrix dimensions must agree."); throw new IndexOutOfRangeException("Matrix dimensions must agree.");
} }
long count = (long)left.KernelColumnLength * left.KernelRowLength; long count = (long)left.KernelColumnLength * left.KernelRowLength;
Matrix<float> ans = Matrix.FromDefault<float>(left.ColumnLength, left.RowLength);
if (count <= 256) if (count <= 256)
{ {
MatrixKernel<float>* leftPtr = left.Buffer; MatrixKernel<float>* leftPtr = left.Buffer;
@@ -2,8 +2,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixParallelThread" // file="MatrixParallelThread"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
@@ -45,17 +45,16 @@ namespace StgSharp.Mathematics.Numeric
private bool _affinityApplied; private bool _affinityApplied;
private bool disposedValue; private bool disposedValue;
private ManualResetEventSlim _resetEvent; private readonly ManualResetEventSlim _resetEvent;
private Thread _managedThread; private readonly Thread _managedThread;
public MatrixParallelThread() public MatrixParallelThread()
{ {
// Allocate a unique logical processor for this worker (mutually exclusive) // Allocate a unique logical processor for this worker (mutually exclusive)
_affinity = AffinityAllocator.Allocate(); _affinity = AffinityAllocator.Allocate();
_resetEvent = new ManualResetEventSlim(false);
_managedThread = new Thread(MatrixParallelWorker); _managedThread = new Thread(MatrixParallelWorker);
_managedThread.Start(); _managedThread.Start();
_resetEvent = new ManualResetEventSlim(false);
} }
// Global runtime switch: true => UMA/no-affinity; false => NUMA/affinity (auto-detected) // Global runtime switch: true => UMA/no-affinity; false => NUMA/affinity (auto-detected)
+10 -8
View File
@@ -2,8 +2,8 @@
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="Program" // file="Program"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload // AuthorGroup: Nitload Space
// Copyright (c) Nitload. All rights reserved. // Copyright (c) Nitload Space. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal // of this software and associated documentation files (the "Software"), to deal
@@ -67,25 +67,27 @@ namespace StgSharp
// 原有的矩阵测试代码 // 原有的矩阵测试代码
HybridLayerSegregatedFitAllocator hlsf = new HybridLayerSegregatedFitAllocator(64 * 4000 * 4000); HybridLayerSegregatedFitAllocator hlsf = new HybridLayerSegregatedFitAllocator((nuint)(64 * (ulong)10000 * 10000));
Matrix<float> m = Matrix.FromHlsf<float>(2000, 2000, hlsf); Matrix<float> m = Matrix.FromHlsf<float>(1000, 1000, hlsf);
m.Fill(1.0f); MatrixCompute.Fill(m, 1.0f);
Console.WriteLine(); Console.WriteLine();
Console.WriteLine(); Console.WriteLine();
Console.WriteLine(); Console.WriteLine();
Matrix<float> n = m.Add(m); Matrix<float> n = Matrix.FromHlsf<float>(1000, 1000, hlsf);
MatrixCompute.Fill(n, 0.0f);
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
{ {
for (int j = 0; j < 2; j++) for (int j = 0; j < 2; j++)
{ {
n = n.Add(m); MatrixCompute.Add(n, m, n);
Console.WriteLine($"{i} {j}"); Console.WriteLine($"{i} {j}");
} }
GC.Collect(); GC.Collect();
} }
n = n.Mul(12);
// MatrixCompute.Mul(n, 12, n);
for (int i = 0; i < 20; i++) for (int i = 0; i < 20; i++)
{ {
for (int j = 0; j < 20; j++) { for (int j = 0; j < 20; j++) {