arrange of proj structure
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
# Matrix Coordinate & Naming Conventions (StgSharp)
|
||||
|
||||
This document defines the coordinate terminology and naming rules used across `Matrix<T>`, `MatrixKernel<T>`, and packed/triangular storage layouts.
|
||||
Goal: eliminate ambiguity around `column`/`row` names (e.g., “index **of** a column” vs “index **along** a column”).
|
||||
|
||||
---
|
||||
|
||||
## 1) The key rule: encode prepositions into identifiers (`Of` vs `In`)
|
||||
|
||||
### `...OfX` — “which X” (object identity / ordinal)
|
||||
Use **`Of`** when the value identifies **which object** (which column, which row, which kernel, etc.).
|
||||
|
||||
Examples:
|
||||
- `columnOfElement`: the element is located in **which column**
|
||||
- `rowOfKernel`: the kernel block is located in **which row**
|
||||
|
||||
### `...InX` / `...WithinX` — “inside X” (local coordinate)
|
||||
Use **`In`** (or `Within`) when the value is a **local coordinate inside** another object.
|
||||
|
||||
Examples:
|
||||
- `columnInKernel`: local column coordinate **inside** a kernel block
|
||||
- `rowInKernel`: local row coordinate **inside** a kernel block
|
||||
|
||||
> Quick mental model: **`Of` = identity**, **`In` = inside**.
|
||||
|
||||
---
|
||||
|
||||
## 2) Coordinate levels: `Element`, `KernelGrid`, `InKernel`
|
||||
|
||||
StgSharp commonly uses three coordinate levels:
|
||||
|
||||
1. **Element-level**: final matrix element coordinates (`Matrix<T>[col,row]`)
|
||||
2. **Kernel-grid level**: coordinates of a kernel block in the kernel grid
|
||||
3. **In-kernel level**: local element coordinates within a kernel (e.g., 4×4)
|
||||
|
||||
Recommended names:
|
||||
|
||||
| Level | Column name | Row name | Meaning |
|
||||
|---|---|---|---|
|
||||
| Element | `columnOfElement` | `rowOfElement` | element located in which column/row (0-based) |
|
||||
| Kernel grid | `columnOfKernel` | `rowOfKernel` | kernel block located in which kernel-column/kernel-row (0-based) |
|
||||
| In-kernel | `columnInKernel` | `rowInKernel` | local coordinate inside the kernel (0..KernelSize-1) |
|
||||
|
||||
---
|
||||
|
||||
## 3) Index base: 0-based (mandatory)
|
||||
|
||||
Unless explicitly documented otherwise, all coordinates are **0-based**:
|
||||
|
||||
- `columnOfElement ∈ [0, ColumnLength-1]`
|
||||
- `rowOfElement ∈ [0, RowLength-1]`
|
||||
- `columnOfKernel ∈ [0, KernelColumnLength-1]`
|
||||
- `rowOfKernel ∈ [0, KernelRowLength-1]`
|
||||
- `columnInKernel ∈ [0, KernelSize-1]`
|
||||
- `rowInKernel ∈ [0, KernelSize-1]`
|
||||
|
||||
---
|
||||
|
||||
## 4) Mapping element coordinates to kernel coordinates (KernelSize = 4 example)
|
||||
|
||||
For a fixed kernel size `KernelSize = 4`:
|
||||
|
||||
```csharp
|
||||
int columnOfKernel = columnOfElement / 4;
|
||||
int rowOfKernel = rowOfElement / 4;
|
||||
|
||||
int columnInKernel = columnOfElement % 4;
|
||||
int rowInKernel = rowOfElement % 4;
|
||||
```
|
||||
|
||||
If `columnOfElement` and `rowOfElement` are guaranteed non-negative, bit operations can be used:
|
||||
|
||||
```csharp
|
||||
int columnOfKernel = columnOfElement >> 2;
|
||||
int rowOfKernel = rowOfElement >> 2;
|
||||
|
||||
int columnInKernel = columnOfElement & 3;
|
||||
int rowInKernel = rowOfElement & 3;
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5) Packed storage: distinguish `Index` from `Offset`
|
||||
|
||||
Packed layouts do **not** store “empty blocks”; stored blocks are placed contiguously in a packed buffer.
|
||||
|
||||
### Use the correct terms
|
||||
- **`packedKernelIndex`**: “the N-th kernel
|
||||
@@ -0,0 +1,38 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Cordination"
|
||||
// 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.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public enum CoordinatePlain
|
||||
{
|
||||
|
||||
XY,
|
||||
YZ,
|
||||
XZ,
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="IUnmanagedVector"
|
||||
// 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.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public interface IUnmanagedVector<TSelf> where TSelf: unmanaged, IUnmanagedVector<TSelf>
|
||||
{
|
||||
|
||||
public static abstract TSelf Zero { get; }
|
||||
|
||||
public static abstract TSelf One { get; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Matrix.Create"
|
||||
// Project: StgSharp
|
||||
// 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
|
||||
// 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 StgSharp.HighPerformance;
|
||||
using StgSharp.HighPerformance.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public unsafe partial class Matrix<T>
|
||||
{
|
||||
|
||||
private const int KernelSideLength = 4;
|
||||
|
||||
public static Matrix<T> Create(
|
||||
int columnLength,
|
||||
int rowLength,
|
||||
MatrixLayout layout,
|
||||
HybridLayerSegregatedFitAllocator allocator)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(allocator);
|
||||
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(columnLength);
|
||||
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(rowLength);
|
||||
|
||||
int kernelColumns = DivideRoundUp(columnLength, KernelSideLength);
|
||||
int kernelRows = DivideRoundUp(rowLength, KernelSideLength);
|
||||
|
||||
if (layout is MatrixLayout.UpperTriangle or MatrixLayout.LowerTriangle)
|
||||
{
|
||||
if (kernelColumns != kernelRows) {
|
||||
throw new ArgumentException("Triangular layouts require square matrices.");
|
||||
}
|
||||
}
|
||||
|
||||
long kernelCount = layout switch
|
||||
{
|
||||
MatrixLayout.DenseRectangle => (long)kernelColumns * kernelRows,
|
||||
MatrixLayout.UpperTriangle => TriangularKernelCount(kernelColumns),
|
||||
MatrixLayout.LowerTriangle => TriangularKernelCount(kernelRows),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(layout))
|
||||
};
|
||||
|
||||
long requiredBytes = kernelCount * MatrixKernel<T>.Size;
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(requiredBytes, nameof(requiredBytes));
|
||||
|
||||
HybridLayerSegregatedFitAllocationHandle handle = allocator.Alloc((uint)requiredBytes);
|
||||
MatrixStorageHLSF<T> storage = new MatrixStorageHLSF<T>(allocator, handle);
|
||||
|
||||
Matrix<T> matrix = new Matrix<T>(MatrixKernelBoarder.Create(layout), new MatrixSize(columnLength, rowLength,
|
||||
kernelColumns,
|
||||
kernelRows), storage);
|
||||
|
||||
return matrix;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int DivideRoundUp(int value, int divisor)
|
||||
{
|
||||
return (value + (divisor - 1)) / divisor;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static long TriangularKernelCount(int order)
|
||||
{
|
||||
long n = order;
|
||||
return (n * (n + 1L)) / 2L;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Matrix"
|
||||
// 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 StgSharp.HighPerformance;
|
||||
using StgSharp.Mathematics.Numeric;
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
// using hlsfHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public unsafe partial class Matrix<T> where T : unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
private protected static readonly int t_count = 16 / sizeof(T);
|
||||
|
||||
private protected static readonly int t_size = sizeof(T);
|
||||
private readonly MatrixKernelBoarder _boarder;
|
||||
private readonly MatrixSize _size;
|
||||
private readonly MatrixStorage<T> _buffer;
|
||||
|
||||
private protected Matrix(
|
||||
MatrixKernelBoarder boarder,
|
||||
MatrixSize size,
|
||||
MatrixStorage<T> buffer
|
||||
|
||||
)
|
||||
{
|
||||
_boarder = boarder;
|
||||
_size = size;
|
||||
_buffer = buffer;
|
||||
}
|
||||
|
||||
public T this[
|
||||
int x,
|
||||
int y
|
||||
]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (y < 0 || y > _size.ElementColumnLength || x < 0 || x > _size.ElementRowLength) {
|
||||
throw new ArgumentOutOfRangeException($"Index [{x},{y}] is out of matrix index range.");
|
||||
}
|
||||
long offset = GetElementOffset(x, y);
|
||||
if (offset < 0) {
|
||||
return T.Zero;
|
||||
}
|
||||
return ((T*)Buffer)[offset];
|
||||
}
|
||||
set
|
||||
{
|
||||
if (y < 0 || y > _size.ElementColumnLength || x < 0 || x > _size.ElementRowLength) {
|
||||
throw new ArgumentOutOfRangeException($"Index [{x},{y}] is out of matrix index range.");
|
||||
}
|
||||
long offset = GetElementOffset(x, y);
|
||||
if (offset < 0) {
|
||||
return;
|
||||
}
|
||||
((T*)Buffer)[offset] = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Matlab style indexer to generate a new matrix
|
||||
/// </summary>
|
||||
/// <param name="column">
|
||||
///
|
||||
/// </param>
|
||||
/// <param name="row">
|
||||
///
|
||||
/// </param>
|
||||
/// <returns>
|
||||
///
|
||||
/// </returns>
|
||||
/// <exception cref="NotImplementedException">
|
||||
///
|
||||
/// </exception>
|
||||
public unsafe Matrix<T> this[
|
||||
ReadOnlySpan<MatrixIndex> column,
|
||||
ReadOnlySpan<MatrixIndex> row
|
||||
]
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
set { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Length of the matrix in columns.
|
||||
/// </summary>
|
||||
public int ColumnLength => _size.ElementColumnLength;
|
||||
|
||||
/// <summary>
|
||||
/// Length of the matrix in rows.
|
||||
/// </summary>
|
||||
public int RowLength => _size.ElementRowLength;
|
||||
|
||||
public MatrixLayout Layout { get; init; }
|
||||
|
||||
internal MatrixKernel<T>* Buffer
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => (MatrixKernel<T>*)_buffer.BufferPointer;
|
||||
}
|
||||
|
||||
protected internal int KernelColumnLength => _size.KernelColumnLength;
|
||||
|
||||
protected internal int KernelRowLength => _size.KernelRowLength;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe ref MatrixKernel<T> GetMatrixKernel(
|
||||
int x,
|
||||
int y
|
||||
)
|
||||
{
|
||||
return ref _boarder.IsInBuffer(x, y, in _size) ?
|
||||
ref _buffer[_boarder.GetIndexOffset(x, y, in _size)] :
|
||||
ref Unsafe.NullRef<MatrixKernel<T>>();
|
||||
}
|
||||
|
||||
internal long GetElementOffset(
|
||||
int x,
|
||||
int y
|
||||
)
|
||||
{
|
||||
int
|
||||
k_c = y / 4,
|
||||
c = y % 4,
|
||||
k_r = x / 4,
|
||||
r = x % 4;
|
||||
if (!_boarder.IsInBuffer(k_r, k_c, in _size)) {
|
||||
return -1;
|
||||
}
|
||||
long kernelOffset = _boarder.GetIndexOffset(k_r, k_c, in _size);
|
||||
|
||||
return kernelOffset * 16 + 4 * r + c;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal unsafe ref T GetElementUnsafe(
|
||||
int x,
|
||||
int y
|
||||
)
|
||||
{
|
||||
// Caller guarantees: indices in range, layout is dense square.
|
||||
int k_c = y / 4;
|
||||
int k_r = x / 4;
|
||||
int c = y % 4;
|
||||
int r = x % 4;
|
||||
|
||||
ref MatrixKernel<T> kernel = ref _buffer[_boarder.GetIndexOffset(k_r, k_c, in _size)];
|
||||
return ref kernel[c, r];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal unsafe MatrixKernel<T>* GetKernelAddressUnsafe(
|
||||
int x,
|
||||
int y
|
||||
)
|
||||
{
|
||||
return Buffer + _boarder.GetIndexOffset(x, y, in _size);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal unsafe ref MatrixKernel<T> GetKernelUnsafe(
|
||||
int x,
|
||||
int y
|
||||
)
|
||||
{
|
||||
// Caller guarantees: indices in range, layout is dense square.
|
||||
int k_c = y / 4;
|
||||
int k_r = x / 4;
|
||||
int c = y % 4;
|
||||
int r = x % 4;
|
||||
return ref _buffer[_boarder.GetIndexOffset(k_c, k_r, in _size)];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixIndex"
|
||||
// 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.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct MatrixIndex
|
||||
{
|
||||
|
||||
public readonly int Begin;
|
||||
public readonly int End;
|
||||
|
||||
private MatrixIndex(int begin, int end, bool safe = false)
|
||||
{
|
||||
Begin = begin;
|
||||
End = end;
|
||||
}
|
||||
|
||||
internal MatrixIndex(int begin, int end)
|
||||
{
|
||||
if (begin > end) {
|
||||
throw new ArgumentException("Begin index cannot be greater than end index.");
|
||||
}
|
||||
|
||||
Begin = begin;
|
||||
End = end;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static implicit operator MatrixIndex((int, int) range)
|
||||
{
|
||||
return new(range.Item1, range.Item2, false);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static implicit operator MatrixIndex(int index)
|
||||
{
|
||||
return new(index, -1, false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixMetadata"
|
||||
// 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.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public enum MatrixLayout
|
||||
{
|
||||
|
||||
DenseRectangle,
|
||||
UpperTriangle,
|
||||
LowerTriangle,
|
||||
|
||||
}
|
||||
|
||||
public abstract class MatrixKernelBoarder
|
||||
{
|
||||
|
||||
private static readonly MatrixKernelBoarder DenseSquareInstance = new DenseSquareKernelBoarder();
|
||||
private static readonly MatrixKernelBoarder LowerTriangleInstance = new LowerTriangleKernelBoarder();
|
||||
private static readonly MatrixKernelBoarder UpperTriangleInstance = new UpperTriangleKernelBoarder();
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static MatrixKernelBoarder Create(
|
||||
MatrixLayout layout
|
||||
)
|
||||
{
|
||||
return layout switch
|
||||
{
|
||||
MatrixLayout.DenseRectangle => DenseSquareInstance,
|
||||
MatrixLayout.UpperTriangle => UpperTriangleInstance,
|
||||
MatrixLayout.LowerTriangle => LowerTriangleInstance,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(layout)),
|
||||
};
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public abstract long GetIndexOffset(
|
||||
int xKernel,
|
||||
int yKernel,
|
||||
in MatrixSize size
|
||||
);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public abstract bool IsInBuffer(
|
||||
int xKernel,
|
||||
int yKernel,
|
||||
in MatrixSize enumerator
|
||||
);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected static long GetTriangularOrder(
|
||||
in MatrixSize enumerator
|
||||
)
|
||||
{
|
||||
return Math.Min(enumerator.KernelColumnLength, enumerator.KernelRowLength);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected static bool IsWithinKernelDomain(
|
||||
in MatrixSize enumerator,
|
||||
int xKernel,
|
||||
int yKernel
|
||||
)
|
||||
{
|
||||
return xKernel >= 0 &&
|
||||
yKernel >= 0 &&
|
||||
xKernel < enumerator.KernelColumnLength &&
|
||||
yKernel < enumerator.KernelRowLength;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected static bool IsWithinTriangularDomain(
|
||||
in MatrixSize enumerator,
|
||||
int xKernel,
|
||||
int yKernel
|
||||
)
|
||||
{
|
||||
long order = GetTriangularOrder(enumerator);
|
||||
return xKernel >= 0 && yKernel >= 0 && xKernel < order && yKernel < order;
|
||||
}
|
||||
|
||||
private sealed class DenseSquareKernelBoarder : MatrixKernelBoarder
|
||||
{
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override long GetIndexOffset(
|
||||
int xKernel,
|
||||
int yKernel,
|
||||
in MatrixSize enumerator
|
||||
)
|
||||
{
|
||||
long x = xKernel;
|
||||
long y = yKernel;
|
||||
long col = enumerator.KernelColumnLength;
|
||||
|
||||
// Console.Write($"Attempt to get offset at ({x},{y}), offset is {x * col + y} ");
|
||||
return x * col + y;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override bool IsInBuffer(
|
||||
int xKernel,
|
||||
int yKernel,
|
||||
in MatrixSize enumerator
|
||||
)
|
||||
{
|
||||
return IsWithinKernelDomain(enumerator, xKernel, yKernel);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private sealed class LowerTriangleKernelBoarder : MatrixKernelBoarder
|
||||
{
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override long GetIndexOffset(
|
||||
int xKernel,
|
||||
int yKernel,
|
||||
in MatrixSize enumerator
|
||||
)
|
||||
{
|
||||
long order = GetTriangularOrder(enumerator);
|
||||
long x = xKernel;
|
||||
long y = yKernel;
|
||||
|
||||
long prefixColumns = x * order - (x * (x - 1L)) / 2L;
|
||||
return prefixColumns + (y - x);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override bool IsInBuffer(
|
||||
int xKernel,
|
||||
int yKernel,
|
||||
in MatrixSize enumerator
|
||||
)
|
||||
{
|
||||
return IsWithinTriangularDomain(enumerator, xKernel, yKernel) && yKernel >= xKernel;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private sealed unsafe class UpperTriangleKernelBoarder : MatrixKernelBoarder
|
||||
{
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override long GetIndexOffset(
|
||||
int xKernel,
|
||||
int yKernel,
|
||||
in MatrixSize enumerator
|
||||
)
|
||||
{
|
||||
long x = xKernel;
|
||||
long y = yKernel;
|
||||
|
||||
long prefixColumns = (x * (x + 1L)) / 2L;
|
||||
return prefixColumns + y;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override bool IsInBuffer(
|
||||
int xKernel,
|
||||
int yKernel,
|
||||
in MatrixSize enumerator
|
||||
)
|
||||
{
|
||||
return IsWithinTriangularDomain(enumerator, xKernel, yKernel) && yKernel <= xKernel;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
public struct MatrixSize
|
||||
{
|
||||
|
||||
[FieldOffset(0)] public readonly int ElementColumnLength;
|
||||
[FieldOffset(4)] public readonly int ElementRowLength;
|
||||
|
||||
[FieldOffset(8)] public readonly int KernelColumnLength;
|
||||
[FieldOffset(12)] public readonly int KernelRowLength;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public MatrixSize(
|
||||
int elementColumnLength,
|
||||
int elementRowLength,
|
||||
int kernelColumnLength,
|
||||
int kernelRowLength
|
||||
)
|
||||
{
|
||||
ElementColumnLength = elementColumnLength;
|
||||
ElementRowLength = elementRowLength;
|
||||
KernelColumnLength = kernelColumnLength;
|
||||
KernelRowLength = kernelRowLength;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
public struct MatrixKernelEnumerator
|
||||
{
|
||||
|
||||
[FieldOffset(0)] public int CurrentColumn;
|
||||
[FieldOffset(4)] public int CurrentRow;
|
||||
|
||||
[FieldOffset(8)] public readonly int KernelColumnLength;
|
||||
[FieldOffset(12)] public readonly int KernelRowLength;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public MatrixKernelEnumerator(
|
||||
int kernelColumnLength,
|
||||
int kernelRowLength,
|
||||
int currentColumn = 0,
|
||||
int currentRow = 0
|
||||
)
|
||||
{
|
||||
CurrentColumn = currentColumn;
|
||||
CurrentRow = currentRow;
|
||||
KernelColumnLength = kernelColumnLength;
|
||||
KernelRowLength = kernelRowLength;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="MatrixStorage"
|
||||
// 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 StgSharp.HighPerformance;
|
||||
using StgSharp.HighPerformance.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using HLSFAllocator = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocator;
|
||||
using HLSFHandle = global::StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
|
||||
|
||||
namespace StgSharp.Mathematics.Numeric
|
||||
{
|
||||
public abstract class MatrixStorage<T> : IDisposable where T : unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
internal unsafe ref MatrixKernel<T> this[long index]
|
||||
{
|
||||
get { return ref Unsafe.AsRef<MatrixKernel<T>>(BufferPointer + MatrixKernel<T>.Size * index); }
|
||||
}
|
||||
|
||||
protected internal abstract unsafe T* BufferPointer { get; }
|
||||
|
||||
public abstract void Dispose();
|
||||
|
||||
}
|
||||
|
||||
internal class MatrixStorageHLSF<T>(HLSFAllocator allocator, HLSFHandle handle) : MatrixStorage<T>
|
||||
where T : unmanaged, INumber<T>
|
||||
{
|
||||
|
||||
internal HLSFAllocator _allocator = allocator;
|
||||
internal HLSFHandle _handle = handle;
|
||||
|
||||
protected internal override unsafe T* BufferPointer => (T*)_handle.Pointer;
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
allocator.Free(_handle);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user