From 48c308ae3cd202e22f899d3a75ae5f85bea9eaee Mon Sep 17 00:00:00 2001 From: Nitload Date: Fri, 20 Mar 2026 00:15:58 +0800 Subject: [PATCH] find solution to combinition of predictor and queue --- StgSharp.Mathematics/AssemblyInfo.cs | 14 +++++-- .../Define/PlainInstancingBuffer.cs | 37 ++++--------------- .../Internal/IntrinsicContext.cs | 31 +++++----------- .../{Linear => Graphics}/Cordination.cs | 8 ++-- .../Linear/MatrixCompute.Sequential.cs | 29 ++++----------- .../Numeric/Runtime/MatrixParallel.Publish.cs | 12 +++--- .../Numeric/Runtime/MatrixParallelQueue.cs | 31 ++++++++-------- .../Runtime}/MatrixParallelTask.cs | 14 +++---- .../Numeric/Runtime/MatrixParallelThread.cs | 18 +++------ .../Numeric/Runtime/MatrixPredictor.cs | 26 ++++--------- StgSharp.UserInterface/Service/IService.cs | 12 ++++++ .../State/IState.cs | 23 ++++++------ .../StgSharp.UnserInterface.csproj | 8 +--- .../View/PresentorDescription.cs | 12 ++++++ 14 files changed, 116 insertions(+), 159 deletions(-) rename StgSharp.Mathematics/Numeric/{Linear => Graphics}/Cordination.cs (87%) rename StgSharp.Mathematics/{Internal => Numeric/Runtime}/MatrixParallelTask.cs (88%) create mode 100644 StgSharp.UserInterface/Service/IService.cs rename StgSharp.Mathematics/Global.cs => StgSharp.UserInterface/State/IState.cs (80%) create mode 100644 StgSharp.UserInterface/View/PresentorDescription.cs diff --git a/StgSharp.Mathematics/AssemblyInfo.cs b/StgSharp.Mathematics/AssemblyInfo.cs index b7896aec..3c6e853e 100644 --- a/StgSharp.Mathematics/AssemblyInfo.cs +++ b/StgSharp.Mathematics/AssemblyInfo.cs @@ -1,9 +1,9 @@ -//----------------------------------------------------------------------- +//----------------------------------------------------------------------- // ----------------------------------------------------------------------- // file="AssemblyInfo" // 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 @@ -25,7 +25,13 @@ // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- +global using StgSharp.Internal; +global using StgSharp.Mathematics.Internal; +global using System; +global using System.Numerics; +global using System.Threading; using System.Runtime.CompilerServices; -[assembly: InternalsVisibleTo("StgSharpDebug")] \ No newline at end of file +[assembly: InternalsVisibleTo("StgSharp.Graphics")] +[assembly: InternalsVisibleTo("StgSharpDebug")] diff --git a/StgSharp.Mathematics/Geometries/PlainGeometry/Define/PlainInstancingBuffer.cs b/StgSharp.Mathematics/Geometries/PlainGeometry/Define/PlainInstancingBuffer.cs index c6839b4f..79183ce9 100644 --- a/StgSharp.Mathematics/Geometries/PlainGeometry/Define/PlainInstancingBuffer.cs +++ b/StgSharp.Mathematics/Geometries/PlainGeometry/Define/PlainInstancingBuffer.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="PlainInstancingBuffer" // 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 @@ -27,8 +27,7 @@ // ----------------------------------------------------------------------- using StgSharp.Mathematics; using StgSharp.Mathematics.Graphics; -using StgSharp.Mathematics.Numeric; - +using StgSharp.Mathematics.Numeric.Graphics; using System; using System.Collections; using System.Collections.Generic; @@ -66,35 +65,15 @@ namespace StgSharp.Geometries mutex = new Mutex(); } - public List InstanceList - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => instanceList; - } + public List InstanceList => instanceList; - public List CoordAndRotationList - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => coordList; - } + public List CoordAndRotationList => coordList; - public List ScalingList - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => scalingList; - } + public List ScalingList => scalingList; - public Span CoordAndRotationSpan - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => CollectionsMarshal.AsSpan(coordList); - } + public Span CoordAndRotationSpan => CollectionsMarshal.AsSpan(coordList); - public Span ScalingSpan - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => CollectionsMarshal.AsSpan(scalingList); - } + public Span ScalingSpan => CollectionsMarshal.AsSpan(scalingList); public int CreateInstanceID() { diff --git a/StgSharp.Mathematics/Internal/IntrinsicContext.cs b/StgSharp.Mathematics/Internal/IntrinsicContext.cs index 3da2ede2..6932eade 100644 --- a/StgSharp.Mathematics/Internal/IntrinsicContext.cs +++ b/StgSharp.Mathematics/Internal/IntrinsicContext.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="IntrinsicContext" // 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 @@ -25,6 +25,7 @@ // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- +using StgSharp.Mathematics.Numeric.Runtime; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -67,9 +68,7 @@ namespace StgSharp.Mathematics.Internal [FieldOffset(0)] public readonly ushort Size; - private MatrixElementType( - int mask - ) + private MatrixElementType(int mask) { Unsafe.SkipInit(out this); Unsafe.SkipInit(out Size); @@ -106,19 +105,14 @@ namespace StgSharp.Mathematics.Internal private nint handle; //f32_panel_mul [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly void Call( - MatrixIntrinsicHandle index, - MatrixParallelTask* task - ) + public readonly void Call(MatrixIntrinsicHandle index, MatrixParallelTask* task) { nint ptr = this[(int)index]; ((delegate* unmanaged[Cdecl])ptr)(task); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly nint Get( - MatrixIntrinsicHandle index - ) + public readonly nint Get(MatrixIntrinsicHandle index) { return this[(int)index]; } @@ -149,20 +143,15 @@ namespace StgSharp.Mathematics.Internal MatrixKernel* left, MatrixKernel* right, MatrixKernel* ans, - ulong scalar - ) + ulong scalar) { nint ptr = this[(int)index]; - ((delegate* unmanaged[Cdecl])ptr)(left, - right, - ans, - scalar); + ((delegate* unmanaged[Cdecl])ptr)(left, right, + ans, scalar); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly nint Get( - MatrixIntrinsicHandle index - ) + public readonly nint Get(MatrixIntrinsicHandle index) { return this[(int)index]; } diff --git a/StgSharp.Mathematics/Numeric/Linear/Cordination.cs b/StgSharp.Mathematics/Numeric/Graphics/Cordination.cs similarity index 87% rename from StgSharp.Mathematics/Numeric/Linear/Cordination.cs rename to StgSharp.Mathematics/Numeric/Graphics/Cordination.cs index c40da654..3e572ad8 100644 --- a/StgSharp.Mathematics/Numeric/Linear/Cordination.cs +++ b/StgSharp.Mathematics/Numeric/Graphics/Cordination.cs @@ -1,9 +1,9 @@ -//----------------------------------------------------------------------- +//----------------------------------------------------------------------- // ----------------------------------------------------------------------- // file="Cordination" // 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 @@ -25,7 +25,7 @@ // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- -namespace StgSharp.Mathematics.Numeric +namespace StgSharp.Mathematics.Numeric.Graphics { public enum CoordinatePlain { diff --git a/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.Sequential.cs b/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.Sequential.cs index 614b61cf..7dc12e6e 100644 --- a/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.Sequential.cs +++ b/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.Sequential.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="MatrixCompute.Sequential" // 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 @@ -33,18 +33,14 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -using unsafe MkProc = delegate* unmanaged[Cdecl]; +using unsafe MkProc = delegate* unmanaged[Cdecl]; namespace StgSharp.Mathematics.Numeric.Linear { public static unsafe partial class MatrixCompute { - public static void Add( - Matrix left, - Matrix right, - Matrix ans - ) + public static void Add(Matrix left, Matrix right, Matrix ans) { ArgumentNullException.ThrowIfNull(left, nameof(left)); ArgumentNullException.ThrowIfNull(right, nameof(right)); @@ -97,28 +93,17 @@ namespace StgSharp.Mathematics.Numeric.Linear private sealed class SequentialPredictor : MatrixPredictor { - public override bool Predict( - Span node, - out int count - ) + public override bool Predict(Span node, out int count) { throw new NotImplementedException(); } - public override void Prefetch( - nuint origin, - ulong policy, - Span cache - ) + public override void Prefetch(nuint origin, ulong policy, Span cache) { throw new NotImplementedException(); } - public override void WriteBack( - nuint origin, - ulong policy, - ReadOnlySpan cache - ) + public override void WriteBack(nuint origin, ulong policy, ReadOnlySpan cache) { throw new NotImplementedException(); } diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Publish.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Publish.cs index 44937827..6e876e65 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Publish.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallel.Publish.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="MatrixParallel.Publish" // 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 @@ -28,7 +28,7 @@ using StgSharp.Collections; using StgSharp.HighPerformance.Memory; using StgSharp.Mathematics; -using StgSharp.Mathematics.Internal; +using StgSharp.Mathematics.Numeric.Runtime; using System; using System.Collections.Generic; using System.ComponentModel; @@ -48,10 +48,8 @@ namespace StgSharp.Mathematics.Numeric [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static unsafe MatrixParallelWrap ScheduleTask( - nuint computeHandle, - MatrixParallelTask* package - ) where TTask : IPrefetchPredict + internal static unsafe MatrixParallelWrap ScheduleTask(nuint computeHandle, MatrixParallelTask* package) + where TTask: IPrefetchPredict { /* int primCount = package->PrimCount; diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs index c2838d83..73ffabe4 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="MatrixParallelQueue" // 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 @@ -27,7 +27,7 @@ // ----------------------------------------------------------------------- using Microsoft.VisualBasic.FileIO; using StgSharp.HighPerformance.Memory; -using StgSharp.Mathematics.Internal; +using StgSharp.Mathematics.Numeric.Runtime; using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -42,8 +42,6 @@ namespace StgSharp.Mathematics.Numeric internal unsafe class MatrixParallelQueue { - // TODO 为各种不同的计算策略设计专门的预测器,以适配L4 - private static ConcurrentStack _recycle = []; private MatrixParallelTask* _package; private ulong _cursor; @@ -53,9 +51,14 @@ namespace StgSharp.Mathematics.Numeric public MatrixParallelTask* Package => _package; + public MatrixElementType ElementType { get; private set; } + + public MatrixIntrinsicHandle Handle { get; private set; } + public static MatrixParallelQueue Rent( - MatrixParallelTask* pack - ) + MatrixElementType elementType, + MatrixIntrinsicHandle handle, + MatrixParallelTask* pack) { if (!_recycle.TryPop(out MatrixParallelQueue? queue)) { queue = new MatrixParallelQueue(); @@ -63,23 +66,21 @@ namespace StgSharp.Mathematics.Numeric queue._package = pack; queue._maxCount = ((ulong)pack->CommonX) * ((ulong)pack->CommonY); queue._cursor = 0; + queue.ElementType = elementType; + queue.Handle = handle; return queue; } - public static void Return( - MatrixParallelQueue queue - ) + public static void Return(MatrixParallelQueue queue) { _recycle.Push(queue); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public (ulong count, ulong offset) Steal( - ulong request - ) + public bool Steal(MatrixParallelTask* task) { - ulong cursor = Interlocked.Add(ref _cursor, request) - request; - return (Math.Min(request, Math.Max(0, _maxCount - cursor)), cursor); + // TODO 使用提取的内容填充task + throw new NotImplementedException(); } } diff --git a/StgSharp.Mathematics/Internal/MatrixParallelTask.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelTask.cs similarity index 88% rename from StgSharp.Mathematics/Internal/MatrixParallelTask.cs rename to StgSharp.Mathematics/Numeric/Runtime/MatrixParallelTask.cs index abe9bcdd..c2399895 100644 --- a/StgSharp.Mathematics/Internal/MatrixParallelTask.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelTask.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="MatrixParallelTask" // 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 @@ -30,7 +30,7 @@ using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -namespace StgSharp.Mathematics.Internal +namespace StgSharp.Mathematics.Numeric.Runtime { [StructLayout(LayoutKind.Explicit)] internal unsafe struct MatrixParallelTask @@ -55,13 +55,9 @@ namespace StgSharp.Mathematics.Internal } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Copy( - MatrixParallelTask* source, - MatrixParallelTask* target - ) + public static void Copy(MatrixParallelTask* source, MatrixParallelTask* target) { - Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTask), - sizeof(MatrixParallelTask)); + Buffer.MemoryCopy(source, target, sizeof(MatrixParallelTask), sizeof(MatrixParallelTask)); } } diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelThread.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelThread.cs index a5b9ada9..a8c0b2dc 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelThread.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/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 @@ -26,7 +26,7 @@ // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- using StgSharp.Internal; -using StgSharp.Mathematics.Internal; +using StgSharp.Mathematics.Numeric.Runtime; using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -53,9 +53,7 @@ namespace StgSharp.Mathematics.Numeric private readonly ManualResetEventSlim _resetEvent; private readonly Thread _managedThread; - internal MatrixParallelThread( - int poolId - ) + internal MatrixParallelThread(int poolId) { _resetEvent = new ManualResetEventSlim(false); _managedThread = new Thread(MatrixParallelWorkLoad); @@ -97,13 +95,9 @@ namespace StgSharp.Mathematics.Numeric [LibraryImport(Native.LibName, EntryPoint = "sn_set_thread_affinity")] [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])] - internal static partial void SetThreadAffinity( - int coreId - ); + internal static partial void SetThreadAffinity(int coreId); - protected virtual void Dispose( - bool disposing - ) + protected virtual void Dispose(bool disposing) { if (!_disposed) { diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixPredictor.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixPredictor.cs index 743de23b..6b85d2ab 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixPredictor.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixPredictor.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="MatrixPredictor" // 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,26 +34,16 @@ using System.Threading.Tasks; namespace StgSharp.Mathematics.Numeric.Runtime { - public abstract class MatrixPredictor : IPrefetchPredict - where TSelf : MatrixPredictor + public abstract class MatrixPredictor : IPrefetchPredict where TSelf: MatrixPredictor { - public abstract bool Predict( - Span node, - out int count - ); + protected MatrixPredictor() { } - public abstract void Prefetch( - nuint origin, - ulong policy, - Span cache - ); + public abstract bool Predict(Span node, out int count); - public abstract void WriteBack( - nuint origin, - ulong policy, - ReadOnlySpan cache - ); + public abstract void Prefetch(nuint origin, ulong policy, Span cache); + + public abstract void WriteBack(nuint origin, ulong policy, ReadOnlySpan cache); } } diff --git a/StgSharp.UserInterface/Service/IService.cs b/StgSharp.UserInterface/Service/IService.cs new file mode 100644 index 00000000..5ee1ec47 --- /dev/null +++ b/StgSharp.UserInterface/Service/IService.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StgSharp.UnserInterface.Service +{ + internal interface IService + { + } +} diff --git a/StgSharp.Mathematics/Global.cs b/StgSharp.UserInterface/State/IState.cs similarity index 80% rename from StgSharp.Mathematics/Global.cs rename to StgSharp.UserInterface/State/IState.cs index 9292f53b..ae238bf8 100644 --- a/StgSharp.Mathematics/Global.cs +++ b/StgSharp.UserInterface/State/IState.cs @@ -1,9 +1,9 @@ //----------------------------------------------------------------------- // ----------------------------------------------------------------------- -// file="Global" +// file="IState" // 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 @@ -25,12 +25,13 @@ // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- -global using StgSharp.Internal; -global using StgSharp.Mathematics.Internal; -global using System; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; -global using System.Numerics; -global using System.Threading; -using System.Runtime.CompilerServices; - -[assembly: InternalsVisibleTo("StgSharp.Graphics")] +namespace StgSharp.UnserInterface.State +{ + internal class IState { } +} diff --git a/StgSharp.UserInterface/StgSharp.UnserInterface.csproj b/StgSharp.UserInterface/StgSharp.UnserInterface.csproj index 1ac5b9cc..b4b1d3ea 100644 --- a/StgSharp.UserInterface/StgSharp.UnserInterface.csproj +++ b/StgSharp.UserInterface/StgSharp.UnserInterface.csproj @@ -1,7 +1 @@ - - - - - - - \ No newline at end of file + \ No newline at end of file diff --git a/StgSharp.UserInterface/View/PresentorDescription.cs b/StgSharp.UserInterface/View/PresentorDescription.cs new file mode 100644 index 00000000..f4139202 --- /dev/null +++ b/StgSharp.UserInterface/View/PresentorDescription.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StgSharp.UnserInterface.View +{ + internal class PresentorDescription + { + } +}