Skip to content

Commit 043c88a

Browse files
committed
adds examples
1 parent 702092a commit 043c88a

8 files changed

Lines changed: 97 additions & 37 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
internal class CalculateVectorized : IExample
2+
{
3+
public void Run(TextWriter logger)
4+
{
5+
var x = Enumerable.Range(0, 256).Select(i => (float)i).ToArray();
6+
var y = Enumerable.Range(0, 256).Select(i => (float)i).ToArray();
7+
var z = Enumerable.Range(0, 256).Select(i => (float)i).ToArray();
8+
var ans = new float[256];
9+
Vectorization.SIMD.Calculate<float, Formula>(x, y, z, ans);
10+
logger.WriteLine(string.Join(", ", ans));
11+
}
12+
}
13+
14+
file readonly struct Formula : IVectorFormula3<float>
15+
{
16+
public float Calculate(float x1, float x2, float x3) =>
17+
x1 * x1 + x2 * x2 + x3 * x3;
18+
19+
public Vector<float> Calculate(Vector<float> x1, Vector<float> x2, Vector<float> x3) =>
20+
x1 * x1 + x2 * x2 + x3 * x3;
21+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
internal class CalculateVectorizedInterceptor : IExample
2+
{
3+
public void Run(TextWriter logger)
4+
{
5+
int[] x = [.. Enumerable.Range(0, 1000).Select(i => (int)i)];
6+
var y = new int[x.Length];
7+
var z = new int[x.Length];
8+
var vectorization = new InterceptVectorization(logger);
9+
10+
vectorization.Calculate(
11+
static x => x * x,
12+
x.AsSpan(),
13+
y.AsSpan()
14+
);
15+
16+
vectorization.Calculate(
17+
static (x, y) => ScalarOp.Add(x, y),
18+
x.AsSpan(),
19+
y.AsSpan(),
20+
z.AsSpan()
21+
);
22+
23+
logger.WriteLine(string.Join(", ", z.Take(10)));
24+
}
25+
}
26+
27+
file class InterceptVectorization(TextWriter logger) : SimdVectorization
28+
{
29+
protected override void CalculateCore<T, TFormula>(TFormula formula, ReadOnlySpan<T> x1, Span<T> ans)
30+
{
31+
logger.WriteLine("Intercepterd by CalculateCore`2(formula, x1, ans)");
32+
base.CalculateCore(formula, x1, ans);
33+
}
34+
35+
protected override void CalculateCore<T, TFormula>(TFormula formula, ReadOnlySpan<T> x1, ReadOnlySpan<T> x2, Span<T> ans)
36+
{
37+
logger.WriteLine("Intercepterd by CalculateCore`2(formula, x1, x2, ans)");
38+
base.CalculateCore(formula, x1, x2, ans);
39+
}
40+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
internal class GeneralizeMathFunc : IExample
2+
{
3+
public void Run(TextWriter logger)
4+
{
5+
var x = new Vector<double>([0.0, 1.0, 2.0, 3.0]);
6+
var sin_x_pi = VectorOp.Sin(VectorOp.Multiply(x, VectorOp.Const<double>.PI));
7+
logger.WriteLine(sin_x_pi);
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
global using System;
2+
global using System.Collections.Generic;
3+
global using System.IO;
4+
global using System.Numerics;
5+
global using System.Linq;
6+
global using SmartVectorDotNet;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
internal interface IExample
2+
{
3+
void Run(TextWriter logger);
4+
}
Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,6 @@
11
// See https://aka.ms/new-console-template for more information
2-
using SmartVectorDotNet;
32

4-
int[] x = [.. Enumerable.Range(0, 1000).Select(i => (int)i)];
5-
var y = new int[x.Length];
6-
var z = new int[x.Length];
7-
var vectorization = new InterceptVectorization(Console.Out);
8-
9-
vectorization.Calculate(
10-
static x => x * x,
11-
x.AsSpan(),
12-
y.AsSpan()
13-
);
14-
15-
vectorization.Calculate(
16-
static (x, y) => ScalarOp.Add(x, y),
17-
x.AsSpan(),
18-
y.AsSpan(),
19-
z.AsSpan()
20-
);
21-
22-
Console.WriteLine(string.Join(", ", z.Take(10)));
23-
24-
25-
class InterceptVectorization(TextWriter logger) : SimdVectorization
26-
{
27-
protected override void CalculateCore<T, TFormula>(TFormula formula, ReadOnlySpan<T> x1, Span<T> ans)
28-
{
29-
logger.WriteLine("Intercepterd by CalculateCore`2(formula, x1, ans)");
30-
base.CalculateCore(formula, x1, ans);
31-
}
32-
33-
protected override void CalculateCore<T, TFormula>(TFormula formula, ReadOnlySpan<T> x1, ReadOnlySpan<T> x2, Span<T> ans)
34-
{
35-
logger.WriteLine("Intercepterd by CalculateCore`2(formula, x1, x2, ans)");
36-
base.CalculateCore(formula, x1, x2, ans);
37-
}
38-
}
3+
new GeneralizeMathFunc().Run(Console.Out);
4+
new VoctorizeSimply().Run(Console.Out);
5+
new CalculateVectorized().Run(Console.Out);
6+
new CalculateVectorizedInterceptor().Run(Console.Out);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
internal class VoctorizeSimply : IExample
2+
{
3+
public void Run(TextWriter logger)
4+
{
5+
var x = Enumerable.Range(0, 256).Select(i => (double)i).ToArray();
6+
var tmp = new double[x.Length];
7+
var ans = new double[x.Length];
8+
Vectorization.SIMD.Multiply<double>(x, Math.PI, tmp);
9+
Vectorization.SIMD.Sin<double>(tmp, ans);
10+
logger.WriteLine(string.Join(", ", ans));
11+
}
12+
}

src/SmartVectorDotNet.Generators/VectorizationCalculatorGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public static void Calculate(
179179
}
180180
}
181181
""");
182-
context.AddSource(source.Location.Data + ".g.cs", sb.Build());
182+
context.AddSource(source.Location.Data.Replace('/', '-').Replace('=', '.') + ".g.cs", sb.Build());
183183
}
184184
}
185185

0 commit comments

Comments
 (0)