Skip to content

Commit

Permalink
Merge pull request #640 from sys27/feature/matrix-optimization
Browse files Browse the repository at this point in the history
Matrix Optimizations
  • Loading branch information
sys27 committed Jul 2, 2023
2 parents c32663a + 2fa45b5 commit b6e34bf
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@

namespace xFunc.Benchmark.Benchmarks;

public class MulMatrixBenchmark
public class MatrixBenchmark
{
private readonly Random random = new Random();

private Mul mul;
private Matrix matrix1;
private Matrix matrix2;

[Params(2, 10, 100)]
public int Size;

[GlobalSetup]
public void Setup()
{
var left = CreateMatrix();
var right = CreateMatrix();

mul = new Mul(left, right);
matrix1 = CreateMatrix();
matrix2 = CreateMatrix();
}

private Matrix CreateMatrix()
Expand All @@ -30,7 +27,7 @@ private Matrix CreateMatrix()
{
var vector = ImmutableArray.CreateBuilder<IExpression>(Size);
for (var j = 0; j < Size; j++)
vector.Add(new Number(random.Next()));
vector.Add(new Number(Random.Shared.Next()));

vectors.Add(new Vector(vector.ToImmutableArray()));
}
Expand All @@ -39,5 +36,18 @@ private Matrix CreateMatrix()
}

[Benchmark]
public object MulMatrix() => mul.Execute();
public object AddMatrix()
=> new Add(matrix1, matrix2).Execute();

[Benchmark]
public object SubMatrix()
=> new Sub(matrix1, matrix2).Execute();

[Benchmark]
public object MulMatrix()
=> new Mul(matrix1, matrix2).Execute();

[Benchmark]
public object MulMatrixByNumber()
=> new Mul(matrix1, Number.Two).Execute();
}
37 changes: 0 additions & 37 deletions xFunc.Benchmark/Benchmarks/MulVectorBenchmark.cs

This file was deleted.

4 changes: 1 addition & 3 deletions xFunc.Benchmark/Benchmarks/TransposeBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ namespace xFunc.Benchmark.Benchmarks;

public class TransposeBenchmark
{
private readonly Random random = new Random();

private Transpose transpose;

[ParamsSource(nameof(GetSizes))]
Expand All @@ -35,7 +33,7 @@ private Matrix CreateMatrix()
{
var vector = ImmutableArray.CreateBuilder<IExpression>(Size.columns);
for (var j = 0; j < Size.columns; j++)
vector.Add(new Number(random.Next()));
vector.Add(new Number(Random.Shared.Next()));

vectors.Add(new Vector(vector.ToImmutableArray()));
}
Expand Down
47 changes: 47 additions & 0 deletions xFunc.Benchmark/Benchmarks/VectorBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Immutable;

namespace xFunc.Benchmark.Benchmarks;

public class VectorBenchmark
{
private Vector vector1;
private Vector vector2;

[Params(2, 10, 100)]
public int Size;

[GlobalSetup]
public void Setup()
{
vector1 = CreateVector();
vector2 = CreateVector();
}

private Vector CreateVector()
{
var vector = ImmutableArray.CreateBuilder<IExpression>(Size);
for (var j = 0; j < Size; j++)
vector.Add(new Number(Random.Shared.Next()));

return new Vector(vector.ToImmutableArray());
}

[Benchmark]
public object AddVectors()
=> new Add(vector1, vector2).Execute();

[Benchmark]
public object SubVectors()
=> new Sub(vector1, vector2).Execute();

[Benchmark]
public object MulVectors()
=> new Mul(vector1, vector2).Execute();

[Benchmark]
public object MulVectorByNumber()
=> new Mul(vector1, Number.Two).Execute();
}
9 changes: 5 additions & 4 deletions xFunc.Maths/Expressions/Matrices/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Immutable;
using System.Runtime.CompilerServices;

namespace xFunc.Maths.Expressions.Matrices;

Expand Down Expand Up @@ -82,12 +83,12 @@ public override bool Equals(object? obj)
/// <inheritdoc />
public object Execute(ExpressionParameters? parameters)
{
var args = ImmutableArray.CreateBuilder<Vector>(Rows);
var args = new Vector[Rows];

foreach (var vector in Vectors)
args.Add((Vector)vector.Execute(parameters));
for (var i = 0; i < Vectors.Length; i++)
args[i] = (Vector)Vectors[i].Execute(parameters);

return new Matrix(args.ToImmutableArray());
return new Matrix(Unsafe.As<Vector[], ImmutableArray<Vector>>(ref args));
}

/// <inheritdoc />
Expand Down

0 comments on commit b6e34bf

Please sign in to comment.