Refactor and optimize TBar, TBarSeries, and TSeries notebooks; remove obsolete code

- Enhanced Alma class by simplifying the CalculateWeightedSum method and removing unnecessary comments.
- Removed SIMD-related methods from Conv class, replacing them with optimized DotProduct calls.
- Updated Sma and Wma classes to use source.ContainsNonFinite() for non-finite value checks, improving readability and performance.
This commit is contained in:
Miha Kralj
2025-12-10 15:03:58 -05:00
parent b26d5d7751
commit 47884bddab
9 changed files with 371 additions and 668 deletions
+29 -75
View File
@@ -155,90 +155,44 @@ public sealed class Alma : ITValuePublisher
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double CalculateWeightedSum()
{
// If buffer is not full, we only use the most recent 'count' weights?
// Standard ALMA usually waits for full period, or re-normalizes weights.
// Here we'll re-normalize based on how many items we have.
// But to match standard behavior, we usually just run on what we have.
// However, the weights are designed for a specific period.
// Using a partial window with full-period weights might be weird.
// Let's stick to the standard: use the weights corresponding to the filled positions.
// Since RingBuffer adds new items at 'head', and we want to apply weights
// such that weights[period-1] applies to the newest item, etc.
// RingBuffer: [Oldest ... Newest]
// Weights: [0 ... period-1]
// We want: Sum(Buffer[i] * Weights[i]) / Sum(Weights)
// BUT: If buffer is not full, say count=5, period=10.
// We have 5 items. Should we use weights[0..4] or weights[5..9]?
// Usually, moving averages grow.
// Let's assume we use the last 'count' weights, normalized.
ReadOnlySpan<double> bufferSpan = _buffer.GetSpan();
int count = bufferSpan.Length;
// If not full, we need to handle it carefully.
// For simplicity and performance, let's just iterate.
// Optimization: If full, use SIMD.
int count = _buffer.Count;
if (count == 0) return 0;
if (count < _period)
{
double sum = 0;
double wSum = 0;
// Map weights to buffer:
// Buffer[0] (oldest) -> Weights[period - count] ??
// Actually, standard is: Weights are fixed.
// Let's align newest with newest.
// Buffer[count-1] (newest) <-> Weights[period-1]
// Buffer[0] (oldest) <-> Weights[period-count]
// Partial buffer: align newest with newest
// Buffer[0] (oldest) -> Weights[period - count]
ReadOnlySpan<double> bufferSpan = _buffer.GetSpan();
int weightOffset = _period - count;
// Use DotProduct for partial sum
double sum = bufferSpan.DotProduct(_weights.AsSpan(weightOffset, count));
// Calculate weightSum for this subset
double wSum = 0;
for (int i = 0; i < count; i++)
{
double w = _weights[weightOffset + i];
sum += bufferSpan[i] * w;
wSum += w;
wSum += _weights[weightOffset + i];
}
return wSum > 0 ? sum / wSum : 0;
}
// Full buffer
return CalculateWeightedSumSimd(bufferSpan);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private double CalculateWeightedSumSimd(ReadOnlySpan<double> buffer)
{
double sum = 0;
int i = 0;
int len = _period;
if (Avx2.IsSupported && len >= Vector256<double>.Count)
{
var vSum = Vector256<double>.Zero;
ref double bufRef = ref MemoryMarshal.GetReference(buffer);
ref double wRef = ref MemoryMarshal.GetReference(_weights.AsSpan());
for (; i <= len - Vector256<double>.Count; i += Vector256<double>.Count)
{
var vBuf = Vector256.LoadUnsafe(ref Unsafe.Add(ref bufRef, i));
var vW = Vector256.LoadUnsafe(ref Unsafe.Add(ref wRef, i));
vSum = Avx.Add(vSum, Avx.Multiply(vBuf, vW));
}
// Horizontal sum
vSum = Avx.Add(vSum, Avx2.Permute4x64(vSum.AsUInt64(), 0b_01_00_11_10).AsDouble()); // skipcq: CS-R1131
vSum = Avx.Add(vSum, Avx2.Permute4x64(vSum.AsUInt64(), 0b_00_00_00_01).AsDouble()); // skipcq: CS-R1131
sum = vSum.GetElement(0);
}
// Scalar fallback
for (; i < len; i++)
{
sum += buffer[i] * _weights[i];
}
return sum / _weightSum;
// Full buffer: use precomputed _weightSum and SIMD DotProduct
// We use InternalBuffer and StartIndex to avoid allocation and handle wrapping
ReadOnlySpan<double> internalBuf = _buffer.InternalBuffer;
int head = _buffer.StartIndex;
// Part 1: Oldest to End of Buffer -> InternalBuffer[Head ... Cap-1]
// Matches Weights[0 ... Cap-Head-1]
int part1Len = _period - head;
double sum1 = internalBuf.Slice(head, part1Len).DotProduct(_weights.AsSpan(0, part1Len));
// Part 2: Start of Buffer to Newest -> InternalBuffer[0 ... Head-1]
// Matches Weights[Cap-Head ... Cap-1]
double sum2 = internalBuf.Slice(0, head).DotProduct(_weights.AsSpan(part1Len));
return (sum1 + sum2) / _weightSum;
}
public static TSeries Calculate(TSeries source, int period, double offset = 0.85, double sigma = 6.0)
+6 -326
View File
@@ -1,9 +1,6 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;
namespace QuanTAlib;
@@ -103,14 +100,14 @@ public sealed class Conv : ITValuePublisher
if (count < _period)
{
result = DotProduct(internalBuf.Slice(0, count), kernelSpan);
result = internalBuf.Slice(0, count).DotProduct(kernelSpan);
}
else
{
// Full: data is split at _head (which points to oldest)
int part1Len = _period - _head;
result = DotProduct(internalBuf.Slice(_head, part1Len), kernelSpan.Slice(0, part1Len))
+ DotProduct(internalBuf.Slice(0, _head), kernelSpan.Slice(part1Len));
result = internalBuf.Slice(_head, part1Len).DotProduct(kernelSpan.Slice(0, part1Len))
+ internalBuf.Slice(0, _head).DotProduct(kernelSpan.Slice(part1Len));
}
}
@@ -180,323 +177,6 @@ public sealed class Conv : ITValuePublisher
return new TSeries(t, v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static double DotProduct(ReadOnlySpan<double> a, ReadOnlySpan<double> b)
{
if (a.Length != b.Length || a.Length == 0) return 0;
int len = a.Length;
// Fast path for very small kernels (avoid SIMD overhead)
if (len <= 3)
{
ref double aRef = ref MemoryMarshal.GetReference(a);
ref double bRef = ref MemoryMarshal.GetReference(b);
double sum = aRef * bRef;
if (len > 1) sum += Unsafe.Add(ref aRef, 1) * Unsafe.Add(ref bRef, 1);
if (len > 2) sum += Unsafe.Add(ref aRef, 2) * Unsafe.Add(ref bRef, 2);
return sum;
}
if (Avx512F.IsSupported)
return DotProductAvx512(a, b);
if (Avx2.IsSupported)
return DotProductAvx2(a, b);
if (Sse2.IsSupported)
return DotProductSse2(a, b);
if (AdvSimd.Arm64.IsSupported)
return DotProductNeon(a, b);
double s = 0;
ref double ar = ref MemoryMarshal.GetReference(a);
ref double br = ref MemoryMarshal.GetReference(b);
int i = 0;
// Unroll scalar loop
for (; i <= len - 4; i += 4)
{
s += Unsafe.Add(ref ar, i) * Unsafe.Add(ref br, i);
s += Unsafe.Add(ref ar, i + 1) * Unsafe.Add(ref br, i + 1);
s += Unsafe.Add(ref ar, i + 2) * Unsafe.Add(ref br, i + 2);
s += Unsafe.Add(ref ar, i + 3) * Unsafe.Add(ref br, i + 3);
}
for (; i < len; i++)
{
s += Unsafe.Add(ref ar, i) * Unsafe.Add(ref br, i);
}
return s;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static double DotProductAvx512(ReadOnlySpan<double> a, ReadOnlySpan<double> b)
{
int len = a.Length;
int i = 0;
Vector512<double> vSum = Vector512<double>.Zero;
Vector512<double> vSum2 = Vector512<double>.Zero;
Vector512<double> vSum3 = Vector512<double>.Zero;
Vector512<double> vSum4 = Vector512<double>.Zero;
ref double aRef = ref MemoryMarshal.GetReference(a);
ref double bRef = ref MemoryMarshal.GetReference(b);
// Unroll loop: Process 32 doubles (4 vectors) at a time
if (len >= 32)
{
for (; i <= len - 32; i += 32)
{
var va1 = Vector512.LoadUnsafe(ref Unsafe.Add(ref aRef, i));
var vb1 = Vector512.LoadUnsafe(ref Unsafe.Add(ref bRef, i));
var va2 = Vector512.LoadUnsafe(ref Unsafe.Add(ref aRef, i + 8));
var vb2 = Vector512.LoadUnsafe(ref Unsafe.Add(ref bRef, i + 8));
var va3 = Vector512.LoadUnsafe(ref Unsafe.Add(ref aRef, i + 16));
var vb3 = Vector512.LoadUnsafe(ref Unsafe.Add(ref bRef, i + 16));
var va4 = Vector512.LoadUnsafe(ref Unsafe.Add(ref aRef, i + 24));
var vb4 = Vector512.LoadUnsafe(ref Unsafe.Add(ref bRef, i + 24));
vSum = Avx512F.FusedMultiplyAdd(va1, vb1, vSum);
vSum2 = Avx512F.FusedMultiplyAdd(va2, vb2, vSum2);
vSum3 = Avx512F.FusedMultiplyAdd(va3, vb3, vSum3);
vSum4 = Avx512F.FusedMultiplyAdd(va4, vb4, vSum4);
}
}
// Process remaining vectors (8 doubles at a time)
for (; i <= len - 8; i += 8)
{
var va = Vector512.LoadUnsafe(ref Unsafe.Add(ref aRef, i));
var vb = Vector512.LoadUnsafe(ref Unsafe.Add(ref bRef, i));
vSum = Avx512F.FusedMultiplyAdd(va, vb, vSum);
}
// Combine accumulators
vSum = Avx512F.Add(vSum, vSum2);
vSum3 = Avx512F.Add(vSum3, vSum4);
vSum = Avx512F.Add(vSum, vSum3);
// Horizontal sum - reduce to Vector256, then Vector128
Vector256<double> v256 = Avx512F.Add(vSum.GetLower(), vSum.GetUpper());
Vector128<double> lower = v256.GetLower();
Vector128<double> upper = v256.GetUpper();
Vector128<double> combined = Sse2.Add(lower, upper);
double sum = combined.GetElement(0) + combined.GetElement(1);
// Scalar remainder
for (; i < len; i++)
{
sum += Unsafe.Add(ref aRef, i) * Unsafe.Add(ref bRef, i);
}
return sum;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static double DotProductAvx2(ReadOnlySpan<double> a, ReadOnlySpan<double> b)
{
int len = a.Length;
int i = 0;
Vector256<double> vSum = Vector256<double>.Zero;
Vector256<double> vSum2 = Vector256<double>.Zero;
Vector256<double> vSum3 = Vector256<double>.Zero;
Vector256<double> vSum4 = Vector256<double>.Zero;
ref double aRef = ref MemoryMarshal.GetReference(a);
ref double bRef = ref MemoryMarshal.GetReference(b);
// Unroll loop: Process 16 doubles (4 vectors) at a time
if (len >= 16)
{
for (; i <= len - 16; i += 16)
{
var va1 = Vector256.LoadUnsafe(ref Unsafe.Add(ref aRef, i));
var vb1 = Vector256.LoadUnsafe(ref Unsafe.Add(ref bRef, i));
var va2 = Vector256.LoadUnsafe(ref Unsafe.Add(ref aRef, i + 4));
var vb2 = Vector256.LoadUnsafe(ref Unsafe.Add(ref bRef, i + 4));
var va3 = Vector256.LoadUnsafe(ref Unsafe.Add(ref aRef, i + 8));
var vb3 = Vector256.LoadUnsafe(ref Unsafe.Add(ref bRef, i + 8));
var va4 = Vector256.LoadUnsafe(ref Unsafe.Add(ref aRef, i + 12));
var vb4 = Vector256.LoadUnsafe(ref Unsafe.Add(ref bRef, i + 12));
if (Fma.IsSupported)
{
vSum = Fma.MultiplyAdd(va1, vb1, vSum);
vSum2 = Fma.MultiplyAdd(va2, vb2, vSum2);
vSum3 = Fma.MultiplyAdd(va3, vb3, vSum3);
vSum4 = Fma.MultiplyAdd(va4, vb4, vSum4);
}
else
{
vSum = Avx.Add(vSum, Avx.Multiply(va1, vb1));
vSum2 = Avx.Add(vSum2, Avx.Multiply(va2, vb2));
vSum3 = Avx.Add(vSum3, Avx.Multiply(va3, vb3));
vSum4 = Avx.Add(vSum4, Avx.Multiply(va4, vb4));
}
}
}
// Process remaining vectors (4 doubles at a time)
for (; i <= len - 4; i += 4)
{
var va = Vector256.LoadUnsafe(ref Unsafe.Add(ref aRef, i));
var vb = Vector256.LoadUnsafe(ref Unsafe.Add(ref bRef, i));
vSum = Fma.IsSupported
? Fma.MultiplyAdd(va, vb, vSum)
: Avx.Add(vSum, Avx.Multiply(va, vb));
}
// Combine accumulators
vSum = Avx.Add(vSum, vSum2);
vSum3 = Avx.Add(vSum3, vSum4);
vSum = Avx.Add(vSum, vSum3);
// Horizontal sum
Vector128<double> lower = vSum.GetLower();
Vector128<double> upper = vSum.GetUpper();
Vector128<double> combined = Sse2.Add(lower, upper);
double sum = combined.GetElement(0) + combined.GetElement(1);
// Process remaining elements (scalar)
for (; i < len; i++)
{
sum += Unsafe.Add(ref aRef, i) * Unsafe.Add(ref bRef, i);
}
return sum;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static double DotProductNeon(ReadOnlySpan<double> a, ReadOnlySpan<double> b)
{
int len = a.Length;
int i = 0;
Vector128<double> vSum = Vector128<double>.Zero;
Vector128<double> vSum2 = Vector128<double>.Zero;
Vector128<double> vSum3 = Vector128<double>.Zero;
Vector128<double> vSum4 = Vector128<double>.Zero;
ref double aRef = ref MemoryMarshal.GetReference(a);
ref double bRef = ref MemoryMarshal.GetReference(b);
// Unroll loop: Process 8 doubles (4 vectors) at a time
if (len >= 8)
{
for (; i <= len - 8; i += 8)
{
var va1 = Vector128.LoadUnsafe(ref Unsafe.Add(ref aRef, i));
var vb1 = Vector128.LoadUnsafe(ref Unsafe.Add(ref bRef, i));
var va2 = Vector128.LoadUnsafe(ref Unsafe.Add(ref aRef, i + 2));
var vb2 = Vector128.LoadUnsafe(ref Unsafe.Add(ref bRef, i + 2));
var va3 = Vector128.LoadUnsafe(ref Unsafe.Add(ref aRef, i + 4));
var vb3 = Vector128.LoadUnsafe(ref Unsafe.Add(ref bRef, i + 4));
var va4 = Vector128.LoadUnsafe(ref Unsafe.Add(ref aRef, i + 6));
var vb4 = Vector128.LoadUnsafe(ref Unsafe.Add(ref bRef, i + 6));
// NEON has FMA on ARM64
// Since we are inside DotProductNeon which is guarded by AdvSimd.Arm64.IsSupported,
// we can assume Arm64 support.
vSum = AdvSimd.Arm64.FusedMultiplyAdd(vSum, va1, vb1);
vSum2 = AdvSimd.Arm64.FusedMultiplyAdd(vSum2, va2, vb2);
vSum3 = AdvSimd.Arm64.FusedMultiplyAdd(vSum3, va3, vb3);
vSum4 = AdvSimd.Arm64.FusedMultiplyAdd(vSum4, va4, vb4);
}
}
// Process remaining vectors (2 doubles at a time)
for (; i <= len - 2; i += 2)
{
var va = Vector128.LoadUnsafe(ref Unsafe.Add(ref aRef, i));
var vb = Vector128.LoadUnsafe(ref Unsafe.Add(ref bRef, i));
vSum = AdvSimd.Arm64.FusedMultiplyAdd(vSum, va, vb);
}
// Combine accumulators
vSum = AdvSimd.Arm64.Add(vSum, vSum2);
vSum3 = AdvSimd.Arm64.Add(vSum3, vSum4);
vSum = AdvSimd.Arm64.Add(vSum, vSum3);
// Horizontal sum (NEON has pairwise add)
double sum = AdvSimd.Arm64.AddPairwiseScalar(vSum).ToScalar();
// Scalar remainder (0-1 elements)
for (; i < len; i++)
{
sum += Unsafe.Add(ref aRef, i) * Unsafe.Add(ref bRef, i);
}
return sum;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static double DotProductSse2(ReadOnlySpan<double> a, ReadOnlySpan<double> b)
{
int len = a.Length;
int i = 0;
Vector128<double> vSum = Vector128<double>.Zero;
Vector128<double> vSum2 = Vector128<double>.Zero;
ref double aRef = ref MemoryMarshal.GetReference(a);
ref double bRef = ref MemoryMarshal.GetReference(b);
// Process 4 doubles at a time using 2 accumulators
for (; i <= len - 4; i += 4)
{
var va1 = Vector128.LoadUnsafe(ref Unsafe.Add(ref aRef, i));
var vb1 = Vector128.LoadUnsafe(ref Unsafe.Add(ref bRef, i));
var va2 = Vector128.LoadUnsafe(ref Unsafe.Add(ref aRef, i + 2));
var vb2 = Vector128.LoadUnsafe(ref Unsafe.Add(ref bRef, i + 2));
if (Fma.IsSupported)
{
vSum = Fma.MultiplyAdd(va1, vb1, vSum);
vSum2 = Fma.MultiplyAdd(va2, vb2, vSum2);
}
else
{
vSum = Sse2.Add(vSum, Sse2.Multiply(va1, vb1));
vSum2 = Sse2.Add(vSum2, Sse2.Multiply(va2, vb2));
}
}
// Process remaining 2 doubles if available
if (i <= len - 2)
{
var va = Vector128.LoadUnsafe(ref Unsafe.Add(ref aRef, i));
var vb = Vector128.LoadUnsafe(ref Unsafe.Add(ref bRef, i));
vSum = Fma.IsSupported
? Fma.MultiplyAdd(va, vb, vSum)
: Sse2.Add(vSum, Sse2.Multiply(va, vb));
i += 2;
}
vSum = Sse2.Add(vSum, vSum2);
double sum = vSum.GetElement(0) + vSum.GetElement(1);
// Scalar remainder (0-1 elements)
for (; i < len; i++)
{
sum += Unsafe.Add(ref aRef, i) * Unsafe.Add(ref bRef, i);
}
return sum;
}
public static TSeries Calculate(TSeries source, double[] kernel)
{
var conv = new Conv(kernel);
@@ -548,14 +228,14 @@ public sealed class Conv : ITValuePublisher
{
int kernelOffset = period - count;
// Window is [0..count-1]
sum = DotProduct(window.Slice(0, count), kernelSpan.Slice(kernelOffset));
sum = window.Slice(0, count).DotProduct(kernelSpan.Slice(kernelOffset));
}
else
{
// Full buffer - branchless version
int part1Len = period - windowIdx;
sum = DotProduct(window.Slice(windowIdx, part1Len), kernelSpan.Slice(0, part1Len))
+ DotProduct(window.Slice(0, windowIdx), kernelSpan.Slice(part1Len));
sum = window.Slice(windowIdx, part1Len).DotProduct(kernelSpan.Slice(0, part1Len))
+ window.Slice(0, windowIdx).DotProduct(kernelSpan.Slice(part1Len));
}
output[i] = sum;
+1 -15
View File
@@ -225,7 +225,7 @@ public sealed class Sma : ITValuePublisher
// Try SIMD path for large, clean datasets
// Requirements: AVX2 support, large enough dataset, no NaN values
const int SimdThreshold = 256;
if (Avx2.IsSupported && len >= SimdThreshold && !HasNonFiniteValues(source))
if (Avx2.IsSupported && len >= SimdThreshold && !source.ContainsNonFinite())
{
CalculateSimdCore(source, output, period);
return;
@@ -367,20 +367,6 @@ public sealed class Sma : ITValuePublisher
}
}
/// <summary>
/// Checks if span contains any non-finite values (NaN or Infinity).
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool HasNonFiniteValues(ReadOnlySpan<double> span)
{
for (int idx = 0; idx < span.Length; idx++)
{
if (!double.IsFinite(span[idx]))
return true;
}
return false;
}
/// <summary>
/// Resets the SMA state.
/// </summary>
+1 -12
View File
@@ -213,7 +213,7 @@ public sealed class Wma : ITValuePublisher
if (len == 0) return;
const int SimdThreshold = 256;
if (Avx2.IsSupported && len >= SimdThreshold && !HasNonFiniteValues(source))
if (Avx2.IsSupported && len >= SimdThreshold && !source.ContainsNonFinite())
{
CalculateSimdCore(source, output, period);
return;
@@ -477,17 +477,6 @@ public sealed class Wma : ITValuePublisher
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool HasNonFiniteValues(ReadOnlySpan<double> span)
{
for (int idx = 0; idx < span.Length; idx++)
{
if (!double.IsFinite(span[idx]))
return true;
}
return false;
}
public void Reset()
{
_buffer.Clear();