//----------------------------------------------------------------------- // ----------------------------------------------------------------------- // file="MatrixParallelQueue" // 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 Microsoft.VisualBasic.FileIO; using StgSharp.HighPerformance.Memory; using StgSharp.Mathematics.Numeric.Runtime; using System; 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; namespace StgSharp.Mathematics.Numeric { internal unsafe interface IMatrixParallelQueue where TSelf : IMatrixParallelQueue { void Return( MatrixParallelTask* task, in L4CacheLine left, in L4CacheLine right, in L4CacheLine ans ); bool Steal( MatrixParallelTask* task, out L4CacheLine left, out L4CacheLine right, out L4CacheLine ans ); } internal interface IBufferQueueLeftRightAns : IMatrixParallelQueue { } internal sealed unsafe class BufferQueueLeftRightAns_FullNuma : IMatrixParallelQueue { private MatrixParallelTask* _baseTask; private L4 _buffer; private readonly nuint _byteSize; private readonly nuint _count; private volatile nuint _cursor; private volatile nuint _mat1; private volatile nuint _mat2; private volatile nuint _mat3; private BufferQueueLeftRightAns_FullNuma() { } internal BufferQueueLeftRightAns_FullNuma( MatrixParallelTask* baseTask ) { _baseTask = baseTask; _count = ((nuint)_baseTask->CommonX) * ((nuint)_baseTask->CommonY); _byteSize = ((nuint)_baseTask->Mat1) + (((nuint)_baseTask->CommonX) * ((nuint)_baseTask->CommonY) * _baseTask->ElementType.Size); _mat1 = (nuint)_baseTask->Mat1; _mat2 = (nuint)_baseTask->Mat2; _mat3 = (nuint)_baseTask->Mat3; } public unsafe void Return( MatrixParallelTask* task, in L4CacheLine left, in L4CacheLine right, in L4CacheLine ans ) { if (left.Address != 0) { left.Dispose(); } if (right.Address != 0) { right.Dispose(); } if (ans.Address != 0) { ans.Dispose(); } } public unsafe bool Steal( MatrixParallelTask* task, out L4CacheLine left, out L4CacheLine right, out L4CacheLine ans ) { throw new NotImplementedException(); } private class Predictor( BufferQueueLeftRightAns_FullNuma host ) : IL4Predict { private readonly BufferQueueLeftRightAns_FullNuma _host; public PredictResult Predict( Span node, out int count ) { nuint begin = _host._cursor; nuint mat1 = (nuint)_host._baseTask->Mat1; nuint mat2 = (nuint)_host._baseTask->Mat2; nuint mat3 = (nuint)_host._baseTask->Mat3; 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 = _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 * ((nuint)_host._baseTask->ElementType.Size); for (int i = 0; i < node.Length; i += 3) { int cycle = i / 3; ref CacheLineDescription prediction = ref node[i]; prediction.Address = mat1_cur + size; } count = 1; return PredictResult.Success; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Prefetch( nuint origin, uint policy, Span cache ) { MatrixCachePolicy pEnum = (MatrixCachePolicy)policy; if ((pEnum & (MatrixCachePolicy.Ans | MatrixCachePolicy.Sequential)) != 0) { if ((pEnum & (MatrixCachePolicy.Left | MatrixCachePolicy.Right)) != 0) { byte* bPtr = (byte*)origin; new Span(bPtr, cache.Length).CopyTo(cache); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WriteBack( nuint origin, uint policy, ReadOnlySpan cache ) { MatrixCachePolicy pEnum = (MatrixCachePolicy)policy; if ((pEnum & (MatrixCachePolicy.Ans | MatrixCachePolicy.Sequential)) != 0) { byte* bPtr = (byte*)origin; cache.CopyTo(new Span(bPtr, cache.Length)); } } } } }