find solution to combinition of predictor and queue

This commit is contained in:
Nitload
2026-03-20 00:15:58 +08:00
parent dcc2bb699a
commit 48c308ae3c
14 changed files with 116 additions and 159 deletions
+10 -4
View File
@@ -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")]
[assembly: InternalsVisibleTo("StgSharp.Graphics")]
[assembly: InternalsVisibleTo("StgSharpDebug")]
@@ -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<IInstancing> InstanceList
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => instanceList;
}
public List<IInstancing> InstanceList => instanceList;
public List<Vec4> CoordAndRotationList
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => coordList;
}
public List<Vec4> CoordAndRotationList => coordList;
public List<float> ScalingList
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => scalingList;
}
public List<float> ScalingList => scalingList;
public Span<Vec4> CoordAndRotationSpan
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => CollectionsMarshal.AsSpan(coordList);
}
public Span<Vec4> CoordAndRotationSpan => CollectionsMarshal.AsSpan(coordList);
public Span<float> ScalingSpan
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => CollectionsMarshal.AsSpan(scalingList);
}
public Span<float> ScalingSpan => CollectionsMarshal.AsSpan(scalingList);
public int CreateInstanceID()
{
@@ -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]<MatrixParallelTask*, void>)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]<MatrixKernel*, MatrixKernel*, MatrixKernel*, ulong, void>)ptr)(left,
right,
ans,
scalar);
((delegate* unmanaged[Cdecl]<MatrixKernel*, MatrixKernel*, MatrixKernel*, ulong, void>)ptr)(left, right,
ans, scalar);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly nint Get(
MatrixIntrinsicHandle index
)
public readonly nint Get(MatrixIntrinsicHandle index)
{
return this[(int)index];
}
@@ -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
{
@@ -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]<StgSharp.Mathematics.Internal.MatrixParallelTask*, void>;
using unsafe MkProc = delegate* unmanaged[Cdecl]<StgSharp.Mathematics.Numeric.Runtime.MatrixParallelTask*, void>;
namespace StgSharp.Mathematics.Numeric.Linear
{
public static unsafe partial class MatrixCompute
{
public static void Add(
Matrix<float> left,
Matrix<float> right,
Matrix<float> ans
)
public static void Add(Matrix<float> left, Matrix<float> right, Matrix<float> 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<SequentialPredictor>
{
public override bool Predict(
Span<CacheLinePrediction> node,
out int count
)
public override bool Predict(Span<CacheLinePrediction> node, out int count)
{
throw new NotImplementedException();
}
public override void Prefetch(
nuint origin,
ulong policy,
Span<byte> cache
)
public override void Prefetch(nuint origin, ulong policy, Span<byte> cache)
{
throw new NotImplementedException();
}
public override void WriteBack(
nuint origin,
ulong policy,
ReadOnlySpan<byte> cache
)
public override void WriteBack(nuint origin, ulong policy, ReadOnlySpan<byte> cache)
{
throw new NotImplementedException();
}
@@ -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<TTask>(
nuint computeHandle,
MatrixParallelTask* package
) where TTask : IPrefetchPredict
internal static unsafe MatrixParallelWrap ScheduleTask<TTask>(nuint computeHandle, MatrixParallelTask* package)
where TTask: IPrefetchPredict
{
/*
int primCount = package->PrimCount;
@@ -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<MatrixParallelQueue> _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();
}
}
@@ -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));
}
}
@@ -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)
{
@@ -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<TSelf> : IPrefetchPredict
where TSelf : MatrixPredictor<TSelf>
public abstract class MatrixPredictor<TSelf> : IPrefetchPredict where TSelf: MatrixPredictor<TSelf>
{
public abstract bool Predict(
Span<CacheLinePrediction> node,
out int count
);
protected MatrixPredictor() { }
public abstract void Prefetch(
nuint origin,
ulong policy,
Span<byte> cache
);
public abstract bool Predict(Span<CacheLinePrediction> node, out int count);
public abstract void WriteBack(
nuint origin,
ulong policy,
ReadOnlySpan<byte> cache
);
public abstract void Prefetch(nuint origin, ulong policy, Span<byte> cache);
public abstract void WriteBack(nuint origin, ulong policy, ReadOnlySpan<byte> cache);
}
}
@@ -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
{
}
}
@@ -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 { }
}
@@ -1,7 +1 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<Folder Include="View\" />
<Folder Include="State\" />
<Folder Include="Service\" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk" />
@@ -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
{
}
}