Files
stgsharp/StgSharp.Mathematics/Numeric/Runtime/MatrixParallelQueue.cs
T

197 lines
7.1 KiB
C#

//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// 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.Text;
using System.Threading;
using System.Threading.Tasks;
namespace StgSharp.Mathematics.Numeric
{
internal unsafe interface IMatrixParallelQueue<TSelf> where TSelf : IMatrixParallelQueue<TSelf>
{
static abstract TSelf Create(
MatrixParallelTask* baseTask,
L4 buffer,
int numaCount
);
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 sealed unsafe class BufferQueueLeftRightAns : IMatrixParallelQueue<BufferQueueLeftRightAns>
{
private MatrixParallelTask* _baseTask;
private bool _needNuma;
private L4 _buffer;
private volatile nuint _cursor;
private readonly nuint Count;
private BufferQueueLeftRightAns() { }
private BufferQueueLeftRightAns(
MatrixParallelTask* baseTask
)
{
_baseTask = baseTask;
Count = ((nuint)_baseTask->CommonX) * ((nuint)_baseTask->CommonY);
}
public static unsafe BufferQueueLeftRightAns Create(
MatrixParallelTask* baseTask,
L4 buffer,
int numaCount
)
{
return new BufferQueueLeftRightAns(baseTask);
}
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 host
) : IL4Predict
{
private readonly BufferQueueLeftRightAns _host;
public PredictResult Predict(
Span<CacheLineDescription> 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 = 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 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;
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<byte> 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<byte>(bPtr, cache.Length).CopyTo(cache);
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteBack(
nuint origin,
uint policy,
ReadOnlySpan<byte> cache
)
{
MatrixCachePolicy pEnum = (MatrixCachePolicy)policy;
if ((pEnum & (MatrixCachePolicy.Ans | MatrixCachePolicy.Sequential)) != 0)
{
byte* bPtr = (byte*)origin;
cache.CopyTo(new Span<byte>(bPtr, cache.Length));
}
}
}
}
}