hlsf goes bad

This commit is contained in:
Youmu Liu
2026-05-08 18:09:12 +08:00
parent 8aa319a120
commit d1aca8f745
47 changed files with 5020 additions and 446 deletions
+51 -20
View File
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="Vec2"
// 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
@@ -25,6 +25,7 @@
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.HighPerformance.ProcessorAbstraction;
using StgSharp.Mathematics;
using StgSharp.Mathematics.Numeric;
using System.Net.Http.Headers;
@@ -55,7 +56,9 @@ namespace StgSharp.Mathematics.Graphics
[FieldOffset(4)]
public float Y;
internal Vec2(Vector2 vec)
internal Vec2(
Vector2 vec
)
{
Unsafe.SkipInit(out reg);
Unsafe.SkipInit(out X);
@@ -63,7 +66,9 @@ namespace StgSharp.Mathematics.Graphics
v = vec;
}
internal Vec2(M128 vec)
internal Vec2(
M128 vec
)
{
Unsafe.SkipInit(out reg);
Unsafe.SkipInit(out X);
@@ -72,7 +77,10 @@ namespace StgSharp.Mathematics.Graphics
reg.Member<ulong>(1) = 0;
}
public Vec2(float x, float y)
public Vec2(
float x,
float y
)
{
v = new Vector2(x, y);
}
@@ -81,11 +89,7 @@ namespace StgSharp.Mathematics.Graphics
public static Vec2 Zero => new Vec2(0, 0);
public static Vec2 One
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new Vec2(1, 1);
}
public static Vec2 One => new Vec2(1, 1);
public Vec2 XY
{
@@ -104,56 +108,83 @@ namespace StgSharp.Mathematics.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Cross(Vec2 right)
public float Cross(
Vec2 right
)
{
return X * right.Y - Y * right.X;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Dot(Vec2 vec)
public float Dot(
Vec2 vec
)
{
return Vector2.Dot(v, vec.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec2 operator -(Vec2 left, Vec2 right)
public static Vec2 operator -(
Vec2 left,
Vec2 right
)
{
return new Vec2(left.v - right.v);
}
public static bool operator !=(Vec2 left, Vec2 right)
public static bool operator !=(
Vec2 left,
Vec2 right
)
{
return !(left == right);
}
public static Vec2 operator *(Vec2 vec, float value)
public static Vec2 operator *(
Vec2 vec,
float value
)
{
return new Vec2(vec.v * value);
}
public static Vec2 operator *(float value, Vec2 vec)
public static Vec2 operator *(
float value,
Vec2 vec
)
{
return new Vec2(vec.v * value);
}
public static Vec2 operator /(Vec2 vec, float value)
public static Vec2 operator /(
Vec2 vec,
float value
)
{
return new Vec2(vec.v / value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec2 operator +(Vec2 left, Vec2 right)
public static Vec2 operator +(
Vec2 left,
Vec2 right
)
{
return new Vec2(left.v + right.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Vec2 left, Vec2 right)
public static bool operator ==(
Vec2 left,
Vec2 right
)
{
return left.v == right.v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Vec2((float, float) tuple)
public static implicit operator Vec2(
(float, float) tuple
)
{
return new Vec2(tuple.Item1, tuple.Item2);
}
+75 -22
View File
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="Vec3"
// 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
@@ -25,6 +25,7 @@
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.HighPerformance.ProcessorAbstraction;
using StgSharp.Mathematics;
using StgSharp.Mathematics.Numeric;
using System;
@@ -50,14 +51,18 @@ namespace StgSharp.Mathematics.Graphics
[FieldOffset(4)] public float Y;
[FieldOffset(8)] public float Z;
internal Vec3(M128 vector)
internal Vec3(
M128 vector
)
{
Unsafe.SkipInit(out this);
reg = vector;
reg.Member<uint>(3) = 0;
}
internal Vec3(Vector3 vector)
internal Vec3(
Vector3 vector
)
{
v = vector;
}
@@ -67,14 +72,21 @@ namespace StgSharp.Mathematics.Graphics
Unsafe.SkipInit(out this);
}
public Vec3(Vec2 xy, float z)
public Vec3(
Vec2 xy,
float z
)
{
Unsafe.SkipInit(out this);
reg = xy.reg;
Z = z;
}
public Vec3(float x, float y, float z)
public Vec3(
float x,
float y,
float z
)
{
Unsafe.SkipInit(out this);
v = new Vector3(x, y, z);
@@ -107,28 +119,39 @@ namespace StgSharp.Mathematics.Graphics
public static Vec3 UnitZ => new(0, 0, 1);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 Cross(Vec3 left, Vec3 right)
public static Vec3 Cross(
Vec3 left,
Vec3 right
)
{
return Linear.Cross(left, right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly float Dot(Vec3 right)
public readonly float Dot(
Vec3 right
)
{
return Vector3.Dot(v, right.v);
}
public readonly bool Equals(Vec3 other)
public readonly bool Equals(
Vec3 other
)
{
return reg == other.reg;
}
public override readonly bool Equals(object obj)
public override readonly bool Equals(
object obj
)
{
return obj is not null && obj is Vec3 vec && this == vec;
}
public static Vec3 FromSpan(ReadOnlySpan<float> span)
public static Vec3 FromSpan(
ReadOnlySpan<float> span
)
{
Vec3 v = new();
span.CopyTo(MemoryMarshal.CreateSpan(ref v.X, 3));
@@ -158,7 +181,10 @@ namespace StgSharp.Mathematics.Graphics
return ret;
}
public static bool IsParallel(Vec3 left, Vec3 right)
public static bool IsParallel(
Vec3 left,
Vec3 right
)
{
return Cross(left, right) == Zero;
}
@@ -168,61 +194,88 @@ namespace StgSharp.Mathematics.Graphics
return $"[{X},{Y},{Z}]";
}
public readonly string ToString(string sample)
public readonly string ToString(
string sample
)
{
return $"[{X.ToString(sample)},{Y.ToString(sample)},{Z.ToString(sample)}]";
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 operator -(Vec3 vec)
public static Vec3 operator -(
Vec3 vec
)
{
return new Vec3(-vec.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 operator -(Vec3 left, Vec3 right)
public static Vec3 operator -(
Vec3 left,
Vec3 right
)
{
return new Vec3(left.v - right.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Vec3 left, Vec3 right)
public static bool operator !=(
Vec3 left,
Vec3 right
)
{
return left.v != right.v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 operator *(Vec3 vec, float value)
public static Vec3 operator *(
Vec3 vec,
float value
)
{
return new Vec3(vec.v * value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float operator *(Vec3 left, Vec3 right)
public static float operator *(
Vec3 left,
Vec3 right
)
{
return Vector3.Dot(left.v, right.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 operator /(Vec3 vec, float value)
public static Vec3 operator /(
Vec3 vec,
float value
)
{
return new Vec3(vec.v / value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vec3 operator +(Vec3 left, Vec3 right)
public static Vec3 operator +(
Vec3 left,
Vec3 right
)
{
return new Vec3(left.v + right.v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Vec3 left, Vec3 right)
public static bool operator ==(
Vec3 left,
Vec3 right
)
{
return left.v == right.v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe implicit operator Vec3((float, float, float) tuple)
public static unsafe implicit operator Vec3(
(float, float, float) tuple
)
{
return Unsafe.As<(float,float,float), Vec3>(ref tuple);
}
@@ -25,6 +25,7 @@
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.HighPerformance.ProcessorAbstraction;
using StgSharp.Mathematics;
using StgSharp.Mathematics.Internal;
using StgSharp.Mathematics.Numeric;
@@ -240,4 +241,4 @@ namespace StgSharp.Mathematics.Graphics
}
}
}
}
@@ -1,35 +1,32 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="Matrix.Create.cs"
// file="Matrix.Create"
// 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 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.
// 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 StgSharp.Mathematics;
using StgSharp.Mathematics.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -48,7 +45,8 @@ namespace StgSharp.Mathematics.Numeric
int columnLength,
int rowLength,
MatrixLayout layout,
HybridLayerSegregatedFitAllocator allocator)
HybridLayerSegregatedFitAllocator allocator
)
{
ArgumentNullException.ThrowIfNull(allocator);
@@ -77,10 +75,11 @@ namespace StgSharp.Mathematics.Numeric
long requiredBytes = kernelCount * MatrixKernel<T>.Size;
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(requiredBytes, nameof(requiredBytes));
hlsfHandle handle = allocator.Alloc((uint)requiredBytes);
HlsfHandle 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);
@@ -88,13 +87,18 @@ namespace StgSharp.Mathematics.Numeric
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int DivideRoundUp(int value, int divisor)
private static int DivideRoundUp(
int value,
int divisor
)
{
return (value + (divisor - 1)) / divisor;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static long TriangularKernelCount(int order)
private static long TriangularKernelCount(
int order
)
{
long n = order;
return (n * (n + 1L)) / 2L;
@@ -71,7 +71,7 @@ namespace StgSharp.Mathematics.Numeric
op(task);
return;
}
MatrixParallelWrap taskWrap = MatrixParallel.ScheduleTask<BufferQueueLeftRightAns_NoNuma>(task);
MatrixParallelWrap taskWrap = /**/ null/*MatrixParallel.ScheduleTask<BufferQueueLeftRightAns_NoNuma>(task)/**/;
taskWrap.LaunchParallel(SleepMode.DeepSleep);
taskWrap.Dispose();
}
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="MatrixStorage"
// 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
@@ -25,8 +25,8 @@
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.HighPerformance.Memory;
using StgSharp.Mathematics;
using StgSharp.Mathematics.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -35,15 +35,16 @@ using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using HLSFAllocator = global::StgSharp.Mathematics.Memory.HybridLayerSegregatedFitAllocator;
using HLSFHandle = global::StgSharp.Mathematics.Memory.hlsfHandle;
using HLSFAllocator = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator;
namespace StgSharp.Mathematics.Numeric
{
public abstract class MatrixStorage<T> : IDisposable where T: unmanaged, INumber<T>
public abstract class MatrixStorage<T> : IDisposable where T : unmanaged, INumber<T>
{
internal unsafe ref MatrixKernel<T> this[long index] => ref Unsafe.AsRef<MatrixKernel<T>>(BufferPointer + (MatrixKernel<T>.Size * index));
internal unsafe ref MatrixKernel<T> this[
long index
] => ref Unsafe.AsRef<MatrixKernel<T>>(BufferPointer + (MatrixKernel<T>.Size * index));
protected internal abstract unsafe T* BufferPointer { get; }
@@ -51,12 +52,14 @@ namespace StgSharp.Mathematics.Numeric
}
internal class MatrixStorageHLSF<T>(HLSFAllocator allocator, HLSFHandle handle) : MatrixStorage<T>
where T: unmanaged, INumber<T>
internal class MatrixStorageHLSF<T>(
HLSFAllocator allocator,
HlsfHandle handle
) : MatrixStorage<T> where T : unmanaged, INumber<T>
{
internal HLSFAllocator _allocator = allocator;
internal HLSFHandle _handle = handle;
internal readonly HLSFAllocator _allocator = allocator;
internal readonly HlsfHandle _handle = handle;
protected internal override unsafe T* BufferPointer => (T*)_handle.Pointer;
@@ -25,7 +25,6 @@
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Common.HighPerformance.ProcessorAbstraction;
using StgSharp.HighPerformance.ProcessorAbstraction;
using StgSharp.Internal;
using StgSharp.Mathematics.Numeric.Runtime;
@@ -94,11 +93,11 @@ namespace StgSharp.Mathematics.Numeric
internal void Pause()
{
ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelThread));
if (State == MatrixThreadState.Retired) {
if (State == MatrixThreadState.Idle) {
return;
}
_resetEvent.Set();
SpinWait.SpinUntil(() => State == MatrixThreadState.Retired);
SpinWait.SpinUntil(() => State == MatrixThreadState.Idle);
_managedThread.Join();
}
@@ -1,30 +1,27 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="MatrixParallelWrap.cs"
// file="MatrixParallelWrap"
// 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 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.
// 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.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -59,7 +56,9 @@ namespace StgSharp.Mathematics.Numeric
private bool _disposed;
private readonly CountdownEvent _countdownEvent;
internal MatrixParallelWrap(MatrixParallelThread[] threadArray)
internal MatrixParallelWrap(
MatrixParallelThread[] threadArray
)
{
_threads = threadArray;
_countdownEvent = new CountdownEvent(threadArray.Length);
@@ -71,8 +70,6 @@ namespace StgSharp.Mathematics.Numeric
internal L4 DataCache { get; set; }
internal PredictPolicy Policy { get; set; }
public void Dispose()
{
Dispose(true);
@@ -81,14 +78,17 @@ namespace StgSharp.Mathematics.Numeric
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void LaunchParallel(SleepMode afterWork)
public void LaunchParallel(
SleepMode afterWork
)
{
ObjectDisposedException.ThrowIf(_disposed, nameof(MatrixParallelWrap));
AfterWork = (ThreadPriority)afterWork;
foreach (MatrixParallelThread thread in _threads)
{
thread.AfterWork = AfterWork;
thread.BindedWrap = this;
// thread.BindedWrap = this;
}
WaitAllWraps();
}
@@ -108,14 +108,16 @@ namespace StgSharp.Mathematics.Numeric
_ = _countdownEvent.Signal();
}
private void Dispose(bool disposing)
private void Dispose(
bool disposing
)
{
if (_disposed) {
return;
}
if (disposing)
{
MatrixParallel.ReturnParallelResources(ref _threads);
// MatrixParallel.ReturnParallelResources(ref _threads);
_countdownEvent.Dispose();
}
_disposed = true;
@@ -36,7 +36,6 @@ namespace StgSharp.Mathematics.Numeric
internal interface IMatrixQueueFactory
{
IMatrixParallelQueue<IMatrixParallelQueue> CreateSequential();
// IMatrixParallelQueue<IMatrixParallelQueue> CreateSequential();
}
}