08665a8950ae786570afbf2e4717e637d7ab9d49
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchF / InProd / InProd.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.BenchF
14 {
15 public static class InProd
16 {
17 #if DEBUG
18     public const int Iterations = 1;
19 #else
20     public const int Iterations = 70;
21 #endif
22
23     private const int RowSize = 10 * Iterations;
24
25     private static int s_seed;
26
27     private static T[][] AllocArray<T>(int n1, int n2)
28     {
29         T[][] a = new T[n1][];
30         for (int i = 0; i < n1; ++i)
31         {
32             a[i] = new T[n2];
33         }
34         return a;
35     }
36
37     [MethodImpl(MethodImplOptions.NoInlining)]
38     private static bool Bench()
39     {
40         double[][] rma = AllocArray<double>(RowSize, RowSize);
41         double[][] rmb = AllocArray<double>(RowSize, RowSize);
42         double[][] rmr = AllocArray<double>(RowSize, RowSize);
43
44         double sum;
45
46         Inner(rma, rmb, rmr);
47
48         for (int i = 1; i < RowSize; i++)
49         {
50             for (int j = 1; j < RowSize; j++)
51             {
52                 sum = 0;
53                 for (int k = 1; k < RowSize; k++)
54                 {
55                     sum = sum + rma[i][k] * rmb[k][j];
56                 }
57                 if (rmr[i][j] != sum)
58                 {
59                     return false;
60                 }
61             }
62         }
63
64         return true;
65     }
66
67     private static void InitRand()
68     {
69         s_seed = 7774755;
70     }
71
72     private static int Rand()
73     {
74         s_seed = (s_seed * 77 + 13218009) % 3687091;
75         return s_seed;
76     }
77
78     private static void InitMatrix(double[][] m)
79     {
80         for (int i = 1; i < RowSize; i++)
81         {
82             for (int j = 1; j < RowSize; j++)
83             {
84                 m[i][j] = (Rand() % 120 - 60) / 3;
85             }
86         }
87     }
88
89     private static void InnerProduct(out double result, double[][] a, double[][] b, int row, int col)
90     {
91         result = 0.0;
92         for (int i = 1; i < RowSize; i++)
93         {
94             result = result + a[row][i] * b[i][col];
95         }
96     }
97
98     private static void Inner(double[][] rma, double[][] rmb, double[][] rmr)
99     {
100         InitRand();
101         InitMatrix(rma);
102         InitMatrix(rmb);
103         for (int i = 1; i < RowSize; i++)
104         {
105             for (int j = 1; j < RowSize; j++)
106             {
107                 InnerProduct(out rmr[i][j], rma, rmb, i, j);
108             }
109         }
110     }
111
112     [Benchmark]
113     public static void Test()
114     {
115         foreach (var iteration in Benchmark.Iterations)
116         {
117             using (iteration.StartMeasurement())
118             {
119                 Bench();
120             }
121         }
122     }
123
124     private static bool TestBase()
125     {
126         bool result = Bench();
127         return result;
128     }
129
130     public static int Main()
131     {
132         bool result = TestBase();
133         return (result ? 100 : -1);
134     }
135 }
136 }