-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
377 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Mathematics.NET/Core/ParallelActions/MultiplyByScalarAction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// <copyright file="MultiplyByScalarAction.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 System.Runtime.CompilerServices; | ||
using CommunityToolkit.HighPerformance.Helpers; | ||
|
||
namespace Mathematics.NET.Core.ParallelActions; | ||
|
||
/// <summary>An action for multiplying items by a scalar</summary> | ||
/// <typeparam name="T">A type that implements <see cref="IComplex{T}"/></typeparam> | ||
public readonly struct MultiplyByScalarAction<T> : IRefAction<T> | ||
where T : IComplex<T> | ||
{ | ||
private readonly T _factor; | ||
|
||
public MultiplyByScalarAction(T factor) | ||
{ | ||
_factor = factor; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Invoke(ref T item) => item *= _factor; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
tests/Mathematics.NET.Benchmarks/LinearAlgebra/MatrixMultiplyByScalarBenchmarks.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// <copyright file="Assert.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; | ||
|
||
namespace Mathematics.NET.Tests; | ||
|
||
public sealed class Assert<T> | ||
where T : IComplex<T> | ||
{ | ||
public static void AreApproximatelyEqual(T expected, T actual, Real delta) | ||
{ | ||
if (T.IsNaN(expected) && T.IsNaN(actual) || T.IsInfinity(expected) && T.IsInfinity(actual)) | ||
{ | ||
return; | ||
} | ||
|
||
if (!Precision.AreApproximatelyEqual(expected, actual, delta)) | ||
{ | ||
Assert.Fail($$""" | ||
Actual value does not fall within the specifed margin of error, {{delta}}: | ||
Expected: {{expected}} | ||
Actual: {{actual}} | ||
"""); | ||
} | ||
} | ||
|
||
public static void ElementsAreApproximatelyEqual(Span2D<T> expected, Span2D<T> actual, Real delta) | ||
{ | ||
if (expected.Height != actual.Height || expected.Width != actual.Width) | ||
{ | ||
Assert.Fail($"Dimensions of the actual matrix, [{actual.Height}, {actual.Width}], does not match the dimensions of the expected matrix, [{expected.Height}, {expected.Width}]"); | ||
} | ||
|
||
for (int i = 0; i < expected.Height; i++) | ||
{ | ||
for (int j = 0; j < expected.Width; j++) | ||
{ | ||
if (T.IsNaN(expected[i, j]) && T.IsNaN(actual[i, j]) || T.IsInfinity(expected[i, j]) && T.IsInfinity(actual[i, j])) | ||
{ | ||
continue; | ||
} | ||
|
||
if (!Precision.AreApproximatelyEqual(expected[i, j], actual[i, j], delta)) | ||
{ | ||
Assert.Fail($$""" | ||
Actual value at row {{i}} and column {{j}} does not fall within the specifed margin of error, {{delta}}: | ||
Expected: {{expected[i, j]}} | ||
Actual: {{actual[i, j]}} | ||
"""); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.