diff --git a/StgSharp.Mathematics/Internal/IntrinsicContext.cs b/StgSharp.Mathematics/Internal/IntrinsicContext.cs index 6932eade..c7b30c15 100644 --- a/StgSharp.Mathematics/Internal/IntrinsicContext.cs +++ b/StgSharp.Mathematics/Internal/IntrinsicContext.cs @@ -2,8 +2,8 @@ // ----------------------------------------------------------------------- // file="IntrinsicContext" // 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 @@ -68,7 +68,9 @@ 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); @@ -76,6 +78,24 @@ namespace StgSharp.Mathematics.Internal this.Mask = mask; } + [Obsolete("Do not use this constructor. Use pre-defined ones from static fields.", true)] + public MatrixElementType() + { + throw new NotSupportedException("Do not use this constructor. Use pre-defined ones from static fields."); + } + + public override readonly bool Equals( + object? obj + ) + { + return (obj is MatrixElementType type) && (Mask == type.Mask); + } + + public override readonly int GetHashCode() + { + return Mask; + } + } [StructLayout(LayoutKind.Sequential)] @@ -105,14 +125,19 @@ 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]; } @@ -143,15 +168,20 @@ 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/MatrixCompute.cs b/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.cs index c13a2d3e..33d6c90a 100644 --- a/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.cs +++ b/StgSharp.Mathematics/Numeric/Linear/MatrixCompute.cs @@ -71,7 +71,7 @@ namespace StgSharp.Mathematics.Numeric op(task); return; } - MatrixParallelWrap taskWrap = MatrixParallel.ScheduleTask(task); + MatrixParallelWrap taskWrap = MatrixParallel.ScheduleTask(task); taskWrap.LaunchParallel(SleepMode.DeepSleep); taskWrap.Dispose(); } diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs index 33f1a639..c2cc5a93 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs @@ -33,6 +33,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices.JavaScript; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -42,12 +43,6 @@ namespace StgSharp.Mathematics.Numeric internal unsafe interface IMatrixParallelQueue where TSelf : IMatrixParallelQueue { - static abstract TSelf Create( - MatrixParallelTask* baseTask, - L4 buffer, - int numaCount - ); - void Return( MatrixParallelTask* task, in L4CacheLine left, @@ -64,32 +59,35 @@ namespace StgSharp.Mathematics.Numeric } - internal sealed unsafe class BufferQueueLeftRightAns : IMatrixParallelQueue + internal interface IBufferQueueLeftRightAns : IMatrixParallelQueue { } + + internal sealed unsafe class BufferQueueLeftRightAns_FullNuma : IMatrixParallelQueue { private MatrixParallelTask* _baseTask; - private bool _needNuma; private L4 _buffer; + private readonly nuint _byteSize; + private readonly nuint _count; private volatile nuint _cursor; - private readonly nuint Count; - private BufferQueueLeftRightAns() { } + private volatile nuint _mat1; + private volatile nuint _mat2; + private volatile nuint _mat3; - private BufferQueueLeftRightAns( - MatrixParallelTask* baseTask + private BufferQueueLeftRightAns_FullNuma() { } + + internal BufferQueueLeftRightAns_FullNuma( + MatrixParallelTask* baseTask ) { _baseTask = baseTask; - Count = ((nuint)_baseTask->CommonX) * ((nuint)_baseTask->CommonY); - } + _count = ((nuint)_baseTask->CommonX) * ((nuint)_baseTask->CommonY); + _byteSize = ((nuint)_baseTask->Mat1) + (((nuint)_baseTask->CommonX) * ((nuint)_baseTask->CommonY) * _baseTask->ElementType.Size); - public static unsafe BufferQueueLeftRightAns Create( - MatrixParallelTask* baseTask, - L4 buffer, - int numaCount - ) - { - return new BufferQueueLeftRightAns(baseTask); + + _mat1 = (nuint)_baseTask->Mat1; + _mat2 = (nuint)_baseTask->Mat2; + _mat3 = (nuint)_baseTask->Mat3; } public unsafe void Return( @@ -121,11 +119,11 @@ namespace StgSharp.Mathematics.Numeric } private class Predictor( - BufferQueueLeftRightAns host + BufferQueueLeftRightAns_FullNuma host ) : IL4Predict { - private readonly BufferQueueLeftRightAns _host; + private readonly BufferQueueLeftRightAns_FullNuma _host; public PredictResult Predict( Span node, @@ -136,16 +134,16 @@ namespace StgSharp.Mathematics.Numeric nuint mat1 = (nuint)_host._baseTask->Mat1; nuint mat2 = (nuint)_host._baseTask->Mat2; nuint mat3 = (nuint)_host._baseTask->Mat3; - nuint _cur = begin * _host._baseTask->ElementType.Size; + nuint _cur = 16 * begin * _host._baseTask->ElementType.Size; nuint mat1_cur = ((nuint)_host._baseTask->Mat1) + _cur; nuint mat2_cur = ((nuint)_host._baseTask->Mat2) + _cur; nuint mat3_cur = ((nuint)_host._baseTask->Mat3) + _cur; - nuint end = ((nuint)_host._baseTask->Mat1) + (((nuint)_host._baseTask->CommonX) * ((nuint)_host._baseTask->CommonY) * _host._baseTask->ElementType.Size); + nuint end = _host._byteSize; nuint mat1_end = ((nuint)_host._baseTask->Mat1) + end; nuint mat2_end = ((nuint)_host._baseTask->Mat2) + end; nuint mat3_end = ((nuint)_host._baseTask->Mat3) + end; - nuint size = 1024 * 16 * _host._baseTask->ElementType.Size; + nuint size = 1024 * 16 * ((nuint)_host._baseTask->ElementType.Size); for (int i = 0; i < node.Length; i += 3) { int cycle = i / 3; diff --git a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelTask.cs b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelTask.cs index d6a25f35..3fcfc9bc 100644 --- a/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelTask.cs +++ b/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelTask.cs @@ -52,20 +52,20 @@ namespace StgSharp.Mathematics.Numeric.Runtime [FieldOffset(8)] public MatrixKernel* Mat2; [FieldOffset(16)] public MatrixKernel* Mat3; /// - /// Being offset of first kernel in a L4 cache line. - /// - [FieldOffset(24)] public nuint CacheIndex; - /// /// Count of kernels to be calculated in a L4 cache line. /// [FieldOffset(28)] public int CacheLength; [FieldOffset(32)] public int CommonK; [FieldOffset(36)] public int CommonX; [FieldOffset(40)] public int CommonY; - [FieldOffset(44)] public int SpanLength; + [FieldOffset(44)] public int VectorLength; [FieldOffset(48)] public MatrixElementType ElementType; [FieldOffset(52)] public MatrixIntrinsicHandle ComputeHandle; - [FieldOffset(56)] public nint Span; + /// + /// Beginning offset of first kernel in a L4 cache line. + /// + [FieldOffset(24)] public nuint Offset; + [FieldOffset(56)] public nuint Vector; [FieldOffset(64)] public ScalarPacket Scalar; public MatrixParallelTask() diff --git a/StgSharp.Mathematics/Numeric/Runtime/QueueFactory.cs b/StgSharp.Mathematics/Numeric/Runtime/QueueFactory.cs new file mode 100644 index 00000000..a6431687 --- /dev/null +++ b/StgSharp.Mathematics/Numeric/Runtime/QueueFactory.cs @@ -0,0 +1,42 @@ +//----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +// file="QueueFactory" +// Project: StgSharp +// 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. +// +// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StgSharp.Mathematics.Numeric +{ + internal interface IMatrixQueueFactory + { + + IMatrixParallelQueue CreateSequential(); + + } +}