Skip to content

Commit

Permalink
Add benchmarks
Browse files Browse the repository at this point in the history
- Add benchmark for multiplying matrices by scalars in parallel
- Update version
  • Loading branch information
HamletTanyavong committed Oct 18, 2023
1 parent dd796c1 commit 83033b9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Mathematics.NET/Mathematics.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Nullable>enable</Nullable>
<Platforms>x64</Platforms>
<Title>Mathematics.NET</Title>
<Version>0.1.0-alpha.1</Version>
<Version>0.1.0-alpha.2</Version>
<Authors>Hamlet Tanyavong</Authors>
<Description>Mathematics.NET is a C# class library that provides tools for solving mathematical problems.</Description>
<PackageTags>mathematics; math; maths</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// <copyright file="MatrixMultiplyByScalarBenchmarks.cs" company="Mathematics.NET">
// Mathematics.NET
// https://github.com/HamletTanyavong/Mathematics.NET
//
// MIT License
//
// Copyright (c) 2023 Hamlet Tanyavong
//
// 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.
// </copyright>

using CommunityToolkit.HighPerformance;
using CommunityToolkit.HighPerformance.Helpers;
using Mathematics.NET.Core.ParallelActions;

namespace Mathematics.NET.Benchmarks.LinearAlgebra;

[MemoryDiagnoser]
[RankColumn]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
public class MatrixMultiplyByScalarBenchmarks
{
public int Rows { get; set; }
public int Cols { get; set; }
public required ComplexNumber[,] MatrixOne { get; set; }
public required ComplexNumber[,] MatrixTwo { get; set; }

[GlobalSetup]
public void GlobalSetup()
{
Rows = 100;
Cols = 100;

MatrixOne = new ComplexNumber[Rows, Cols];

for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Cols; j++)
{
MatrixOne[i, j] = new(Random.Shared.NextDouble(), Random.Shared.NextDouble());
MatrixTwo[i, j] = new(Random.Shared.NextDouble(), Random.Shared.NextDouble());
}
}
}

[Benchmark(Baseline = true)]
public void MultiplyByScalarNaive()
{
var matrixAsSpan = MatrixOne.AsMemory2D().Span;
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Cols; j++)
{
matrixAsSpan[i, j] *= Real.Pi;
}
}
}

[Benchmark]
public void MultiplyByScalarParallel()
{
Memory2D<ComplexNumber> matrixAsMemory = MatrixTwo;
ParallelHelper.ForEach(matrixAsMemory, new MultiplyByScalarAction<ComplexNumber>(Real.Pi));
}
}
1 change: 1 addition & 0 deletions tests/Mathematics.NET.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
{
typeof(ComplexDivisionBenchmarks),
typeof(ComplexTrigonometryBenchmarks),
typeof(MatrixMultiplyByScalarBenchmarks),
typeof(NormBenchmarks),
typeof(RealvsDouble),
typeof(SystemComplexAbsVsComplexAbsBenchmarks)
Expand Down

0 comments on commit 83033b9

Please sign in to comment.