1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 // Solution of linear algebraic equations and matrix inversion.
7 using Microsoft.Xunit.Performance;
9 using System.Runtime.CompilerServices;
12 [assembly: OptimizeForBenchmarks]
14 namespace Benchstone.BenchF
16 public static class InvMt
19 public const int Iterations = 1;
21 public const int Iterations = 80;
24 private const int MatSize = Iterations;
26 private static T[][] AllocArray<T>(int n1, int n2)
28 T[][] a = new T[n1][];
29 for (int i = 0; i < n1; ++i)
36 [MethodImpl(MethodImplOptions.NoInlining)]
37 private static bool Bench()
39 double[][] t = AllocArray<double>(MatSize + 1, (MatSize + 1) * 2);
41 double det, detinv, ber, p;
45 for (i = 1; i <= n; i++)
47 for (j = 1; j <= n; j++)
52 t[i][n + 1 + j] = 1.0;
57 t[i][n + 1 + j] = 0.0;
60 t[i][n + 1] = System.Math.Sqrt((float)i);
63 Inner(t, out det, ref n);
65 for (i = 1; i <= n; i++)
67 for (j = 1; j <= n; j++)
70 t[i][j] = t[i][n + 1 + j];
75 Inner(t, out detinv, ref n);
78 for (i = 1; i <= n; i++)
80 ber = ber + System.Math.Abs(System.Math.Sqrt((double)i) - t[i][n + 1]);
86 private static void Inner(double[][] t, out double det, ref int n)
91 for (int k = 1; k <= n; k++)
96 for (int j = 1; j <= (2 * n + 1); j++)
98 t[k][j] = t[k][j] / tkk;
101 for (int i = 1; i <= n; i++)
106 for (int j = 1; j <= (2 * n + 1); j++)
108 t[i][j] = t[i][j] - t[k][j] * tik;
116 public static void Test()
118 foreach (var iteration in Benchmark.Iterations)
120 using (iteration.StartMeasurement())
127 private static bool TestBase()
129 bool result = Bench();
133 public static int Main()
135 bool result = TestBase();
136 return (result ? 100 : -1);