From c1995d119a4a3e066a4e51539a330f2626054c4a Mon Sep 17 00:00:00 2001 From: Nitload Date: Mon, 24 Nov 2025 01:02:27 +0800 Subject: [PATCH] Daily update on 20251124-010227 by Nitload Youmu. Additional Message : error in hlsf Signed-off-by: Nitload --- .../Memory/HLSFAllocator.Alloc.cs | 8 ++-- .../Memory/HLSFAllocator.BucketNode.cs | 48 +++++++++++-------- .../HighPerformance/Memory/HLSFAllocator.cs | 6 +-- .../Numeric/MatrixCompute.Basic.cs | 28 ++++++----- .../Numeric/MatrixParallelThread.cs | 11 ++--- StgSharpDebug/Program.cs | 18 +++---- 6 files changed, 69 insertions(+), 50 deletions(-) diff --git a/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.Alloc.cs b/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.Alloc.cs index 16c77046..473e9cc2 100644 --- a/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.Alloc.cs +++ b/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.Alloc.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // 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 @@ -74,6 +74,7 @@ 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); } @@ -199,6 +200,7 @@ namespace StgSharp.HighPerformance.Memory { newPos = (nuint)m_Buffer + _size; // Clamp to maximum buffer size blockSize = _spareMemory->Size; + p = 0; } // Try to allocate new Entry from slab allocator @@ -208,7 +210,7 @@ namespace StgSharp.HighPerformance.Memory handle->Level = GetLevelFromSize((uint)blockSize); // Update spare memory position - _spareMemory->Position = newPos; // Replace Volatile.Write with direct access + _spareMemory->Position = newPos; BucketNode* b = (BucketNode*)handle->Position; b->EntryRef = handle; PushLevel(handle->Level, DetermineSegmentIndex((uint)blockSize, handle->Level), b); diff --git a/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.BucketNode.cs b/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.BucketNode.cs index cb2cf9b5..e9c14a87 100644 --- a/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.BucketNode.cs +++ b/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.BucketNode.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="HLSFAllocator.BucketNode" // 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,8 @@ // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- using System; -using System.ComponentModel.DataAnnotations; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Threading; namespace StgSharp.HighPerformance.Memory { @@ -41,7 +39,7 @@ namespace StgSharp.HighPerformance.Memory [MethodImpl(MethodImplOptions.AggressiveInlining)] private static bool IsNullOrEmpty(BucketNode* node) { - return node == null || node->Magic != BucketNode.MAGIC_NUMBER; + return node == null; } #endregion @@ -57,7 +55,6 @@ namespace StgSharp.HighPerformance.Memory { _bucketHeads[index] = (nuint)node; node->IsInBucket = true; - node->Magic = BucketNode.MAGIC_NUMBER; return; } else { @@ -65,7 +62,6 @@ namespace StgSharp.HighPerformance.Memory node->NextLevel = head; BucketNode* headNode = (BucketNode*)head; node->IsInBucket = true; - node->Magic = BucketNode.MAGIC_NUMBER; if (headNode != null) { headNode->PreviousLevel = (ulong)node; } @@ -115,8 +111,7 @@ namespace StgSharp.HighPerformance.Memory return false; } - ulong n = head->NextLevel; - BucketNode* next = (BucketNode*)n; + BucketNode* next = (BucketNode*)head->NextLevel; if (next == null) { @@ -128,8 +123,14 @@ namespace StgSharp.HighPerformance.Memory node = head; node->IsInBucket = false; 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; + Console.WriteLine("///////////// no error caught ///////////////"); } return true; } @@ -167,17 +168,26 @@ namespace StgSharp.HighPerformance.Memory 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(28)] internal bool IsInBucket; // Whether in bucket - [FieldOffset(36)] internal int NextLock; - [FieldOffset(32)] internal int PrevLock; - [FieldOffset(24)] internal uint Magic; // Magic number verification - [FieldOffset(32)] internal ulong LockBuffer; + [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 - [FieldOffset(0)] internal ulong NextLevel; // Bucket stack linked list: next pointer - [FieldOffset(8)] 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; + } + } } diff --git a/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.cs b/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.cs index b8e3abef..b801c7d2 100644 --- a/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.cs +++ b/StgSharp.Common/HighPerformance/Memory/HLSFAllocator.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // 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 @@ -34,7 +34,7 @@ namespace StgSharp.HighPerformance.Memory 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 private ulong[] _bucketHeads; diff --git a/StgSharp.Mathematics/Numeric/MatrixCompute.Basic.cs b/StgSharp.Mathematics/Numeric/MatrixCompute.Basic.cs index c70b7711..5cdd8808 100644 --- a/StgSharp.Mathematics/Numeric/MatrixCompute.Basic.cs +++ b/StgSharp.Mathematics/Numeric/MatrixCompute.Basic.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="MatrixCompute.Basic" // 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 @@ -41,13 +41,15 @@ namespace StgSharp.Mathematics.Numeric public static class MatrixCompute { - public static unsafe Matrix Add(this Matrix left, Matrix right) + public static unsafe Matrix Add(Matrix left, Matrix right, Matrix 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."); } long count = (long)left.KernelColumnLength * left.KernelRowLength; - Matrix ans = Matrix.FromDefault(left.ColumnLength, left.RowLength); if (count <= 256) { MatrixKernel* leftPtr = left.Buffer; @@ -84,7 +86,7 @@ namespace StgSharp.Mathematics.Numeric return ans; } - public static unsafe void Fill(this Matrix ans, T value) where T : unmanaged, INumber + public static unsafe void Fill(Matrix ans, T value) where T: unmanaged, INumber { long count = (long)ans.KernelColumnLength * ans.KernelRowLength; ScalarPacket* scalar = MatrixParallelFactory.CreateScalarPacket(); @@ -114,10 +116,12 @@ namespace StgSharp.Mathematics.Numeric taskGroup.Dispose(); } - public static unsafe Matrix Mul(this Matrix left, float right) + public static unsafe Matrix Mul(Matrix left, float right, Matrix ans) { + if (left.ColumnLength != ans.ColumnLength || left.RowLength != ans.RowLength) { + throw new IndexOutOfRangeException("Matrix dimensions must agree."); + } long count = (long)left.KernelColumnLength * left.KernelRowLength; - Matrix ans = Matrix.FromDefault(left.ColumnLength, left.RowLength); ScalarPacket* scalar = MatrixParallelFactory.CreateScalarPacket(); scalar->Data(0) = right; if (count <= 256) @@ -155,13 +159,15 @@ namespace StgSharp.Mathematics.Numeric return ans; } - public static unsafe Matrix Sub(this Matrix left, Matrix right) + public static unsafe Matrix Sub(Matrix left, Matrix right, Matrix 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."); } long count = (long)left.KernelColumnLength * left.KernelRowLength; - Matrix ans = Matrix.FromDefault(left.ColumnLength, left.RowLength); if (count <= 256) { MatrixKernel* leftPtr = left.Buffer; diff --git a/StgSharp.Mathematics/Numeric/MatrixParallelThread.cs b/StgSharp.Mathematics/Numeric/MatrixParallelThread.cs index 85717ef7..500f84be 100644 --- a/StgSharp.Mathematics/Numeric/MatrixParallelThread.cs +++ b/StgSharp.Mathematics/Numeric/MatrixParallelThread.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="MatrixParallelThread" // 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 @@ -45,17 +45,16 @@ namespace StgSharp.Mathematics.Numeric private bool _affinityApplied; private bool disposedValue; - private ManualResetEventSlim _resetEvent; - private Thread _managedThread; + private readonly ManualResetEventSlim _resetEvent; + private readonly Thread _managedThread; public MatrixParallelThread() { // Allocate a unique logical processor for this worker (mutually exclusive) _affinity = AffinityAllocator.Allocate(); - + _resetEvent = new ManualResetEventSlim(false); _managedThread = new Thread(MatrixParallelWorker); _managedThread.Start(); - _resetEvent = new ManualResetEventSlim(false); } // Global runtime switch: true => UMA/no-affinity; false => NUMA/affinity (auto-detected) diff --git a/StgSharpDebug/Program.cs b/StgSharpDebug/Program.cs index 38534e38..d708a55a 100644 --- a/StgSharpDebug/Program.cs +++ b/StgSharpDebug/Program.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // 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 @@ -67,25 +67,27 @@ namespace StgSharp // 原有的矩阵测试代码 - HybridLayerSegregatedFitAllocator hlsf = new HybridLayerSegregatedFitAllocator(64 * 4000 * 4000); + HybridLayerSegregatedFitAllocator hlsf = new HybridLayerSegregatedFitAllocator((nuint)(64 * (ulong)10000 * 10000)); - Matrix m = Matrix.FromHlsf(2000, 2000, hlsf); - m.Fill(1.0f); + Matrix m = Matrix.FromHlsf(1000, 1000, hlsf); + MatrixCompute.Fill(m, 1.0f); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); - Matrix n = m.Add(m); + Matrix n = Matrix.FromHlsf(1000, 1000, hlsf); + MatrixCompute.Fill(n, 0.0f); for (int i = 0; i < 5; i++) { for (int j = 0; j < 2; j++) { - n = n.Add(m); + MatrixCompute.Add(n, m, n); Console.WriteLine($"{i} {j}"); } GC.Collect(); } - n = n.Mul(12); + + // MatrixCompute.Mul(n, 12, n); for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) {