Separate large perf benchmarks into their own legs (#15231)
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / Benchstones / BenchI / MulMatrix / MulMatrix.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
6 using Microsoft.Xunit.Performance;
7 using System;
8 using System.Runtime.CompilerServices;
9 using Xunit;
10
11 [assembly: OptimizeForBenchmarks]
12
13 namespace Benchstone.BenchI
14 {
15 public static class MulMatrix
16 {
17
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 100;
22 #endif
23
24     const int Size = 75;
25     static volatile object VolatileObject;
26
27     static void Escape(object obj) {
28         VolatileObject = obj;
29     }
30
31     static T[][] AllocArray<T>(int n1, int n2) {
32         T[][] a = new T[n1][];
33         for (int i = 0; i < n1; ++i) {
34             a[i] = new T[n2];
35         }
36         return a;
37     }
38
39     static void Inner(int[][] a, int[][] b, int[][] c) {
40
41         int i, j, k, l;
42
43         // setup
44         for (j = 0; j < Size; j++) {
45             for (i = 0; i < Size; i++) {
46                 a[i][j] = i;
47                 b[i][j] = 2 * j;
48                 c[i][j] = a[i][j] + b[i][j];
49             }
50         }
51
52         // jkl
53         for (j = 0; j < Size; j++) {
54             for (k = 0; k < Size; k++) {
55                 for (l = 0; l < Size; l++) {
56                     c[j][k] += a[j][l] * b[l][k];
57                 }
58             }
59         }
60
61         // jlk
62         for (j = 0; j < Size; j++) {
63             for (l = 0; l < Size; l++) {
64                 for (k = 0; k < Size; k++) {
65                     c[j][k] += a[j][l] * b[l][k];
66                 }
67             }
68         }
69
70         // kjl
71         for (k = 0; k < Size; k++) {
72             for (j = 0; j < Size; j++) {
73                 for (l = 0; l < Size; l++) {
74                     c[j][k] += a[j][l] * b[l][k];
75                 }
76             }
77         }
78
79         // klj
80         for (k = 0; k < Size; k++) {
81             for (l = 0; l < Size; l++) {
82                 for (j = 0; j < Size; j++) {
83                     c[j][k] += a[j][l] * b[l][k];
84                 }
85             }
86         }
87
88         // ljk
89         for (l = 0; l < Size; l++) {
90             for (j = 0; j < Size; j++) {
91                 for (k = 0; k < Size; k++) {
92                     c[j][k] += a[j][l] * b[l][k];
93                 }
94             }
95         }
96
97         // lkj
98         for (l = 0; l < Size; l++) {
99             for (k = 0; k < Size; k++) {
100                 for (j = 0; j < Size; j++) {
101                     c[j][k] += a[j][l] * b[l][k];
102                 }
103             }
104         }
105
106         return;
107     }
108
109     [MethodImpl(MethodImplOptions.NoInlining)]
110     static bool Bench() {
111         int[][] a = AllocArray<int>(Size, Size);
112         int[][] b = AllocArray<int>(Size, Size);
113         int[][] c = AllocArray<int>(Size, Size);
114
115         for (int i = 0; i < Iterations; ++i) {
116             Inner(a, b, c);
117         }
118
119         Escape(c);
120         return true;
121     }
122
123     [Benchmark]
124     public static void Test() {
125         foreach (var iteration in Benchmark.Iterations) {
126             using (iteration.StartMeasurement()) {
127                 Bench();
128             }
129         }
130     }
131
132     static bool TestBase() {
133         bool result = Bench();
134         return result;
135     }
136
137     public static int Main() {
138         bool result = TestBase();
139         return (result ? 100 : -1);
140     }
141 }
142 }