mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-07-29 10:07:43 +00:00
92 lines
2.9 KiB
Plaintext
92 lines
2.9 KiB
Plaintext
|
|
#!meta
|
||
|
|
|
||
|
|
{"kernelInfo":{"defaultKernelName":"csharp","items":[{"name":"csharp","languageName":"csharp"},{"name":"fsharp","languageName":"F#","aliases":["f#","fs"]},{"name":"html","languageName":"HTML"},{"name":"http","languageName":"HTTP"},{"name":"javascript","languageName":"JavaScript","aliases":["js"]},{"name":"mermaid","languageName":"Mermaid"},{"name":"pwsh","languageName":"PowerShell","aliases":["powershell"]},{"name":"value"}]}}
|
||
|
|
|
||
|
|
#!csharp
|
||
|
|
|
||
|
|
// Reference the library
|
||
|
|
#r "..\..\bin\QuanTAlib.dll"
|
||
|
|
|
||
|
|
using System;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Numerics;
|
||
|
|
using QuanTAlib;
|
||
|
|
|
||
|
|
// 1. Check Hardware Support
|
||
|
|
Console.WriteLine($"SIMD Hardware Acceleration: {Vector.IsHardwareAccelerated}");
|
||
|
|
Console.WriteLine($"Vector<double> Count: {Vector<double>.Count}");
|
||
|
|
|
||
|
|
#!csharp
|
||
|
|
|
||
|
|
// 2. Basic Operations
|
||
|
|
// Demonstrate Sum, Min, Max, Average using SIMD extensions
|
||
|
|
|
||
|
|
// Define data locally in this cell
|
||
|
|
double[] data = new double[1000];
|
||
|
|
for (int i = 0; i < data.Length; i++) data[i] = i;
|
||
|
|
|
||
|
|
// We use explicit static method calls with .AsSpan() to ensure correct overload resolution
|
||
|
|
// and avoid creating top-level ReadOnlySpan variables (which causes CS8345).
|
||
|
|
|
||
|
|
double sum = SimdExtensions.SumSIMD(data.AsSpan());
|
||
|
|
double minVal = SimdExtensions.MinSIMD(data.AsSpan());
|
||
|
|
double maxVal = SimdExtensions.MaxSIMD(data.AsSpan());
|
||
|
|
double avg = SimdExtensions.AverageSIMD(data.AsSpan());
|
||
|
|
|
||
|
|
Console.WriteLine($"Sum: {sum}");
|
||
|
|
Console.WriteLine($"Min: {minVal}");
|
||
|
|
Console.WriteLine($"Max: {maxVal}");
|
||
|
|
Console.WriteLine($"Average: {avg}");
|
||
|
|
|
||
|
|
#!csharp
|
||
|
|
|
||
|
|
// 3. Advanced Statistics
|
||
|
|
|
||
|
|
double[] dataStats = new double[1000];
|
||
|
|
for (int i = 0; i < dataStats.Length; i++) dataStats[i] = i;
|
||
|
|
|
||
|
|
double variance = SimdExtensions.VarianceSIMD(dataStats.AsSpan());
|
||
|
|
double stdDev = SimdExtensions.StdDevSIMD(dataStats.AsSpan());
|
||
|
|
|
||
|
|
Console.WriteLine($"Variance: {variance:F4}");
|
||
|
|
Console.WriteLine($"Standard Deviation: {stdDev:F4}");
|
||
|
|
|
||
|
|
#!csharp
|
||
|
|
|
||
|
|
// 4. Combined Operations
|
||
|
|
|
||
|
|
double[] dataComb = new double[1000];
|
||
|
|
for (int i = 0; i < dataComb.Length; i++) dataComb[i] = i;
|
||
|
|
|
||
|
|
var (min, max) = SimdExtensions.MinMaxSIMD(dataComb.AsSpan());
|
||
|
|
Console.WriteLine($"Min: {min}, Max: {max}");
|
||
|
|
|
||
|
|
#!csharp
|
||
|
|
|
||
|
|
// 5. Performance Comparison (Simple Benchmark)
|
||
|
|
|
||
|
|
int size = 1_000_000;
|
||
|
|
double[] largeData = new double[size];
|
||
|
|
Random rnd = new Random(42);
|
||
|
|
for (int i = 0; i < size; i++) largeData[i] = rnd.NextDouble();
|
||
|
|
|
||
|
|
// Warmup
|
||
|
|
SimdExtensions.SumSIMD(largeData.AsSpan());
|
||
|
|
|
||
|
|
// Measure SIMD
|
||
|
|
long start = DateTime.UtcNow.Ticks;
|
||
|
|
double sumSimd = SimdExtensions.SumSIMD(largeData.AsSpan());
|
||
|
|
long end = DateTime.UtcNow.Ticks;
|
||
|
|
double simdTime = (end - start) / 10000.0; // ms
|
||
|
|
|
||
|
|
// Measure Scalar (LINQ Sum as proxy for scalar loop)
|
||
|
|
start = DateTime.UtcNow.Ticks;
|
||
|
|
double sumScalar = largeData.Sum();
|
||
|
|
end = DateTime.UtcNow.Ticks;
|
||
|
|
double scalarTime = (end - start) / 10000.0; // ms
|
||
|
|
|
||
|
|
Console.WriteLine($"Array Size: {size:N0}");
|
||
|
|
Console.WriteLine($"SIMD Time: {simdTime:F4} ms");
|
||
|
|
Console.WriteLine($"Scalar Time: {scalarTime:F4} ms");
|
||
|
|
Console.WriteLine($"Speedup: {scalarTime / simdTime:F2}x");
|