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 using Microsoft.Xunit.Performance;
7 using System.Runtime.CompilerServices;
10 [assembly: OptimizeForBenchmarks]
12 namespace Benchstone.BenchF
14 public static class MatInv4
17 public const int Iterations = 1;
19 public const int Iterations = 60;
22 private static float s_det;
33 [MethodImpl(MethodImplOptions.NoInlining)]
34 private static bool Bench()
36 X a = new X(Iterations * Iterations);
37 float[] b = new float[Iterations * Iterations];
38 float[] c = new float[Iterations * Iterations];
39 float[] d = new float[Iterations * Iterations];
40 float[] l1 = new float[Iterations];
41 float[] l2 = new float[Iterations];
47 for (i = 0; i < n; ++i)
49 for (k = 0; k < n; ++k)
53 a.A[i * n + k] = 40.0F;
57 a.A[i * n + k] = 0.0F;
62 for (i = 0; i < n; ++i)
64 for (k = i; k < nsq; k += n)
70 /*** second(&t1); ***/
72 MinV1(b, ref n, out s_det, l1, l2);
79 /*** second(&tx); ***/
81 MProd(b, a.A, c, ref n);
82 for (k = 1; k <= nsq; ++k)
84 b[k - 1] = a.A[k - 1];
87 /*** second(&tx); ***/
89 MinV2(b, ref n, out s_det, l1, l2);
96 /*** second(&ty); ***/
98 MProd(b, a.A, d, ref n);
101 /*** second(&t2); ***/
112 private static void MinV1(float[] a, ref int n, out float d, float[] l, float[] m)
115 int i, j, k, ij, ik, ji, jk, nk, ki, kj, kk, iz, jp, jq, jr;
121 for (k = 1; k <= n; ++k)
128 for (j = k; j <= n; ++j)
130 // j <= n, so iz <= n^2 - n
132 for (i = k; i <= n; ++i)
134 // iz <= n^2 - n and i <= n, so ij <= n^2
136 if (System.Math.Abs(biga) >= System.Math.Abs(a[ij - 1]))
140 // accessing up to n^2 - 1
156 for (i = 1; i <= n; ++i)
158 // i <= n, ki <= n + n + ... + n (n times) i.e. k <= n * n (when ki = 0 initially)
160 // Accessing upto n^2 -1
162 // ji <= n^2 - n + n (for ki = 0 initially when k = n and 0 < j <= n)
163 // Therefore ji <= n^2
165 a[ki - 1] = a[ji - 1];
175 // 0 <= jp <= n^2 - n
177 for (j = 1; j <= n; ++j)
179 // 0 < nk <= n * (n-1)
184 // ji <= n^2 - n + n or ji <= n^2 (since 0 < j <= n)
187 a[jk - 1] = a[ji - 1];
199 for (i = 1; i <= n; ++i)
205 // 0 < nk <= n * (n-1)
208 a[ik - 1] = a[ik - 1] / (-biga);
211 for (i = 1; i <= n; ++i)
217 // 0 < nk <= n * (n-1)
223 for (j = 1; j <= n; ++j)
225 // i <= n, ij <= n + n + ... + n (n times) or ij <= n * n
231 // if i=1, kj = (1 + (n-1) * n) - 1 + n ==> ij = n^2
232 // if i=n, kj = (n * n) - n + n ==> ij = n ^2
235 a[ij - 1] = hold * a[kj - 1] + a[ij - 1];
239 for (j = 1; j <= n; ++j)
241 // k <= n, kj <= n + n + ... + n (n times) or kj <= n * n
247 // Accessing upto n^2 - 1
248 a[kj - 1] = a[kj - 1] / biga;
251 a[kk - 1] = 1.0F / biga;
266 // 0 <= jq <= n^2 - n
267 // 0 <= jr <= n^2 - n
270 for (j = 1; j <= n; ++j)
279 a[jk - 1] = -a[ji - 1];
288 // 0 <= jr <= n^2 - n
290 for (i = 1; i <= n; ++i)
292 // ki <= n + n + ... + n (n times) or ki <= n * n
295 // if i=1, ji = (1 + (n-1) * n) - 1 + n ==> ij = n^2
296 // if i=n, ji = (n * n) - n + n ==> ij = n ^2
297 // Therefore ji <= n^2
299 a[ki - 1] = -a[ji - 1];
305 private static void MinV2(float[] a, ref int n, out float d, float[] l, float[] m)
311 for (k = 1; k <= n; ++k)
315 biga = a[(k - 1) * n + (k - 1)];
316 for (j = k; j <= n; ++j)
318 for (i = k; i <= n; ++i)
320 // Accessing upto n^2 - n + n - 1 ==> n^2 - 1
321 if (System.Math.Abs(biga) >= System.Math.Abs(a[(i - 1) * n + (j - 1)]))
325 biga = a[(i - 1) * n + (j - 1)];
335 for (i = 1; i <= n; ++i)
337 // Accessing upto n^2 - n + n - 1 ==> n^2 - 1
338 hold = -a[(k - 1) * n + (i - 1)];
339 a[(k - 1) * n + (i - 1)] = a[(j - 1) * n + (i - 1)];
340 a[(j - 1) * n + (i - 1)] = hold;
348 for (j = 1; j <= n; ++j)
350 // Accessing upto n^2 - n + n - 1 ==> n^2 - 1
351 hold = -a[(j - 1) * n + (k - 1)];
352 a[(j - 1) * n + (k - 1)] = a[(j - 1) * n + (i - 1)];
353 a[(j - 1) * n + (i - 1)] = hold;
364 for (i = 1; i <= n; ++i)
368 // Accessing upto n^2 - n + n - 1 ==> n^2 - 1
369 a[(i - 1) * n + (k - 1)] = a[(i - 1) * n + (k - 1)] / (-biga);
372 for (i = 1; i <= n; ++i)
378 for (j = 1; j <= n; ++j)
382 // Accessing upto n^2 - n + n - 1 ==> n^2 - 1
383 a[(i - 1) * n + (j - 1)] = a[(i - 1) * n + (k - 1)] * a[(k - 1) * n + (j - 1)] + a[(i - 1) * n + (j - 1)];
387 for (j = 1; j < n; ++j)
391 // Accessing upto n^2 - n + n - 1 ==> n^2 - 1
392 a[(k - 1) * n + (j - 1)] = a[(k - 1) * n + (j - 1)] / biga;
396 a[(k - 1) * n + (k - 1)] = 1.0F / biga;
410 for (j = 1; j <= n; ++j)
412 // Accessing upto n^2 - n + n - 1 ==> n^2 - 1
413 hold = a[(j - 1) * n + (k - 1)];
414 a[(j - 1) * n + (k - 1)] = -a[(j - 1) * n + (i - 1)];
415 a[(j - 1) * n + (i - 1)] = hold;
423 for (i = 1; i <= n; ++i)
425 // Accessing upto n^2 - n + n - 1 ==> n^2 - 1
426 hold = a[(k - 1) * n + (i - 1)];
427 a[(k - 1) * n + (i - 1)] = -a[(j - 1) * n + (i - 1)];
428 a[(j - 1) * n + (i - 1)] = hold;
433 private static void MProd(float[] a, float[] b, float[] c, ref int n)
437 for (i = 1; i <= n; ++i)
439 for (j = 1; j <= n; ++j)
441 // Accessing upto n^2 - n + n - 1 ==> n^2 - 1
442 c[(i - 1) * n + (j - 1)] = 0.0F;
443 for (k = 1; k <= n; ++k)
445 c[(i - 1) * n + (j - 1)] = c[(i - 1) * n + (j - 1)] + a[(i - 1) * n + (k - 1)] * b[(k - 1) * n + (j - 1)];
452 private static void CompM(float[] a, float[] b, ref int n)
457 //(starting compare.)
458 for (i = 1; i <= n; ++i)
460 for (j = 1; j <= n; ++j)
467 sum = sum + System.Math.Abs(System.Math.Abs(a[(i - 1) * n + (j - 1)]) - x);
474 public static void Test()
476 foreach (var iteration in Benchmark.Iterations)
478 using (iteration.StartMeasurement())
485 private static bool TestBase()
487 bool result = Bench();
491 public static int Main()
493 bool result = TestBase();
494 return (result ? 100 : -1);