hlsf mat enumerator ok, going on test
This commit is contained in:
@@ -31,7 +31,7 @@
|
||||
using StgSharp.HighPerformance;
|
||||
using System;
|
||||
using System.Numerics;
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
using hlsfAllocator = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator;
|
||||
using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
|
||||
|
||||
@@ -65,8 +65,22 @@ namespace StgSharp.Mathematics
|
||||
|
||||
protected int KernelRowCount { get; init; }
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Matrix<T> FromHlsf(int column, int row, hlsfAllocator allocator)
|
||||
{
|
||||
return new HlsfMatrix<T>(column, row, allocator);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public abstract MatrixSegmentEnumeration GetColumnEnumeration();
|
||||
|
||||
public abstract ref MatrixKernel GetMatrixKernel(int column, int row);
|
||||
|
||||
public abstract MatrixSegmentEnumeration GetRowEnumeration();
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public abstract MatrixSegmentEnumeration GetSequentialEnumeration();
|
||||
|
||||
[Obsolete("This method does not contain boarder check, so be careful on size or use indexer instead.", false)]
|
||||
public abstract ref T UnsafeIndex(int column, int row);
|
||||
|
||||
@@ -81,102 +95,109 @@ namespace StgSharp.Mathematics
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal unsafe class GCMatrix<T> : Matrix<T> where T: unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
private readonly MatrixKernel[,] buffer;
|
||||
|
||||
public GCMatrix(int column, int row)
|
||||
public readonly unsafe ref struct MatrixSegmentEnumeration(
|
||||
MatrixKernel* source,
|
||||
int size,
|
||||
int stride,
|
||||
int secondaryStride)
|
||||
{
|
||||
KernelColumnCount = (column + 3) / 4;
|
||||
KernelRowCount = (row + 3) / t_count;
|
||||
buffer = new MatrixKernel[KernelColumnCount, KernelRowCount];
|
||||
}
|
||||
|
||||
public override ref T this[int column, int row]
|
||||
{
|
||||
get
|
||||
private readonly MatrixKernel* _source = source;
|
||||
private readonly int _secondaryStride = secondaryStride;
|
||||
private readonly int _size = size;
|
||||
private readonly int _stride = stride;
|
||||
|
||||
public Enumerator GetEnumerator()
|
||||
{
|
||||
AssertIndex(column, row);
|
||||
int kernelColumn = column / 4, c = column % 4;
|
||||
int kernelRow = row / t_count, r = row % t_count;
|
||||
ref MatrixKernel kernel = ref buffer[kernelColumn, kernelRow];
|
||||
return ref kernel.UnsafeIndex<T>(c, r);
|
||||
return new Enumerator(_source, _size, _stride, _size, _secondaryStride);
|
||||
}
|
||||
}
|
||||
|
||||
public override Matrix<T> this[ReadOnlySpan<MatrixIndex> column, ReadOnlySpan<MatrixIndex> row]
|
||||
{
|
||||
get => throw new NotImplementedException();
|
||||
set => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override unsafe ref MatrixKernel GetMatrixKernel(int column, int row)
|
||||
{
|
||||
AssertIndex(column, row);
|
||||
int kernelColumn = column / 4;
|
||||
int kernelRow = row / t_count;
|
||||
return ref buffer[kernelColumn, kernelRow];
|
||||
}
|
||||
|
||||
public override ref T UnsafeIndex(int column, int row)
|
||||
{
|
||||
int kernelColumn = column / 4, c = column % 4;
|
||||
int kernelRow = row / t_count, r = row % t_count;
|
||||
ref MatrixKernel kernel = ref buffer[kernelColumn, kernelRow];
|
||||
return ref kernel.UnsafeIndex<T>(c, r);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal unsafe class HlsfMatrix<T> : Matrix<T> where T: unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
private readonly MatrixKernel* k_buffer;
|
||||
private readonly hlsfHandle handle;
|
||||
|
||||
internal HlsfMatrix(int column, int row, hlsfAllocator allocator)
|
||||
{
|
||||
KernelColumnCount = (column + 3) / 4;
|
||||
KernelRowCount = (row + 3) / t_count;
|
||||
this.handle = allocator.Alloc((uint)(KernelColumnCount * KernelRowCount * sizeof(MatrixKernel)));
|
||||
k_buffer = (MatrixKernel*)handle.Pointer;
|
||||
}
|
||||
|
||||
public override ref T this[int column, int row]
|
||||
{
|
||||
get
|
||||
public ref struct Enumerator(MatrixKernel* source, int end, int stride, int size, int secondaryStride)
|
||||
{
|
||||
AssertIndex(column, row);
|
||||
int kernelColumn = column / 4, c = column % 4;
|
||||
int kernelRow = row / t_count, r = row % t_count;
|
||||
ref MatrixKernel kernel = ref k_buffer[(kernelColumn * kernelColumn) + kernelRow];
|
||||
return ref kernel.UnsafeIndex<T>(c, r);
|
||||
|
||||
private MatrixKernel* _current = source - stride;
|
||||
private readonly MatrixKernel* _end = source + end;
|
||||
private readonly MatrixKernel* _source = source;
|
||||
private readonly int _secondaryStride = secondaryStride;
|
||||
private readonly int _size = size;
|
||||
private readonly int _stride = stride;
|
||||
|
||||
public readonly MatrixSegment Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new(_current, _size, _secondaryStride);
|
||||
}
|
||||
|
||||
public readonly void Dispose() { }
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
_current += _stride;
|
||||
return _current < _end;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_current = _source - _stride;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override Matrix<T> this[ReadOnlySpan<MatrixIndex> column, ReadOnlySpan<MatrixIndex> row]
|
||||
public readonly unsafe ref struct MatrixSegment(MatrixKernel* source, int size, int stride)
|
||||
{
|
||||
get => throw new NotImplementedException();
|
||||
set => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override unsafe ref MatrixKernel GetMatrixKernel(int column, int row)
|
||||
{
|
||||
AssertIndex(column, row);
|
||||
int kernelColumn = column / 4, c = column % 4;
|
||||
int kernelRow = row / t_count, r = row % t_count;
|
||||
return ref k_buffer[(kernelColumn * kernelColumn) + kernelRow];
|
||||
}
|
||||
private readonly MatrixKernel* _source = source;
|
||||
private readonly int _size = size;
|
||||
private readonly int _stride = stride;
|
||||
|
||||
public ref MatrixKernel this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (index < 0 || index >= _size) {
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
}
|
||||
return ref _source[index * _stride];
|
||||
}
|
||||
}
|
||||
|
||||
public Enumerator GetEnumerator()
|
||||
{
|
||||
return new Enumerator(_source, _size, _stride);
|
||||
}
|
||||
|
||||
public ref struct Enumerator(MatrixKernel* source, int end, int stride)
|
||||
{
|
||||
|
||||
private readonly MatrixKernel* _end = source + end;
|
||||
private readonly MatrixKernel* _source = source;
|
||||
private readonly int _stride = stride;
|
||||
|
||||
public MatrixKernel* Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private set;
|
||||
} = source - stride;
|
||||
|
||||
public void Dispose() { }
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
Current += _stride;
|
||||
return Current < _end;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
Current = _source - _stride;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override ref T UnsafeIndex(int column, int row)
|
||||
{
|
||||
int kernelColumn = column / 4, c = column % 4;
|
||||
int kernelRow = row / t_count, r = row % t_count;
|
||||
ref MatrixKernel kernel = ref k_buffer[(kernelColumn * kernelColumn) + kernelRow];
|
||||
return ref kernel.UnsafeIndex<T>(c, r);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user