Update CoreClr, PgoData to preview1-26004-01, master-20171204-0047, respectively...
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / Benchstones / BenchF / InvMt / InvMt.cs
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.
4 //
5 // Solution of linear algebraic equations and matrix inversion.
6
7 using Microsoft.Xunit.Performance;
8 using System;
9 using System.Runtime.CompilerServices;
10 using Xunit;
11
12 [assembly: OptimizeForBenchmarks]
13
14 namespace Benchstone.BenchF
15 {
16 public static class InvMt
17 {
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 80;
22 #endif
23
24     private const int MatSize = Iterations;
25
26     private static T[][] AllocArray<T>(int n1, int n2)
27     {
28         T[][] a = new T[n1][];
29         for (int i = 0; i < n1; ++i)
30         {
31             a[i] = new T[n2];
32         }
33         return a;
34     }
35
36     [MethodImpl(MethodImplOptions.NoInlining)]
37     private static bool Bench()
38     {
39         double[][] t = AllocArray<double>(MatSize + 1, (MatSize + 1) * 2);
40
41         double det, detinv, ber, p;
42         int n, i, j;
43
44         n = MatSize;
45         for (i = 1; i <= n; i++)
46         {
47             for (j = 1; j <= n; j++)
48             {
49                 if (i == j)
50                 {
51                     t[i][j] = 2.0001;
52                     t[i][n + 1 + j] = 1.0;
53                 }
54                 else
55                 {
56                     t[i][j] = 1.0001;
57                     t[i][n + 1 + j] = 0.0;
58                 }
59             }
60             t[i][n + 1] = System.Math.Sqrt((float)i);
61         }
62
63         Inner(t, out det, ref n);
64
65         for (i = 1; i <= n; i++)
66         {
67             for (j = 1; j <= n; j++)
68             {
69                 p = t[i][j];
70                 t[i][j] = t[i][n + 1 + j];
71                 t[i][n + 1 + j] = p;
72             }
73         }
74
75         Inner(t, out detinv, ref n);
76
77         ber = 0.0;
78         for (i = 1; i <= n; i++)
79         {
80             ber = ber + System.Math.Abs(System.Math.Sqrt((double)i) - t[i][n + 1]);
81         }
82
83         return true;
84     }
85
86     private static void Inner(double[][] t, out double det, ref int n)
87     {
88         double tik, tkk;
89
90         det = 1.0;
91         for (int k = 1; k <= n; k++)
92         {
93             tkk = t[k][k];
94             det = det * tkk;
95
96             for (int j = 1; j <= (2 * n + 1); j++)
97             {
98                 t[k][j] = t[k][j] / tkk;
99             }
100
101             for (int i = 1; i <= n; i++)
102             {
103                 if (i != k)
104                 {
105                     tik = t[i][k];
106                     for (int j = 1; j <= (2 * n + 1); j++)
107                     {
108                         t[i][j] = t[i][j] - t[k][j] * tik;
109                     }
110                 }
111             }
112         }
113     }
114
115     [Benchmark]
116     public static void Test()
117     {
118         foreach (var iteration in Benchmark.Iterations)
119         {
120             using (iteration.StartMeasurement())
121             {
122                 Bench();
123             }
124         }
125     }
126
127     private static bool TestBase()
128     {
129         bool result = Bench();
130         return result;
131     }
132
133     public static int Main()
134     {
135         bool result = TestBase();
136         return (result ? 100 : -1);
137     }
138 }
139 }