Skip to content

Commit

Permalink
Add C# multi-dimension array versions of the Benchstones tests (#60192)
Browse files Browse the repository at this point in the history
* Add C# multi-dimension array versions of the Benchstones tests

The Benchstones tests using multi-dimension arrays currently are
implemented using C# "jagged" arrays. Add versions of the tests
that use true C# multi-dimensional arrays. The code is exactly
the same, except for the array creation and indexing.

This will allow us to compare code generated for each, as well as
to verify functionality. I intend to add versions of these to the
microbenchmarks in the https://github.com/dotnet/performance repo as well.

* Fix MDPuzzle build
  • Loading branch information
BruceForstall authored Oct 12, 2021
1 parent debe671 commit df42364
Show file tree
Hide file tree
Showing 28 changed files with 2,619 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public static class LogicArray
public const int Iterations = 3000;
#endif

const int ArraySize = 50;

struct Workarea
{
public int X;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//

using Microsoft.Xunit.Performance;
using System;
using System.Runtime.CompilerServices;
using Xunit;

[assembly: OptimizeForBenchmarks]

namespace Benchstone.MDBenchF
{
public static class MDInProd
{
#if DEBUG
public const int Iterations = 1;
#else
public const int Iterations = 70;
#endif

private const int RowSize = 10 * Iterations;

private static int s_seed;

[MethodImpl(MethodImplOptions.NoInlining)]
private static bool Bench()
{
double[,] rma = new double[RowSize, RowSize];
double[,] rmb = new double[RowSize, RowSize];
double[,] rmr = new double[RowSize, RowSize];

double sum;

Inner(rma, rmb, rmr);

for (int i = 1; i < RowSize; i++)
{
for (int j = 1; j < RowSize; j++)
{
sum = 0;
for (int k = 1; k < RowSize; k++)
{
sum = sum + rma[i,k] * rmb[k,j];
}
if (rmr[i,j] != sum)
{
return false;
}
}
}

return true;
}

private static void InitRand()
{
s_seed = 7774755;
}

private static int Rand()
{
s_seed = (s_seed * 77 + 13218009) % 3687091;
return s_seed;
}

private static void InitMatrix(double[,] m)
{
for (int i = 1; i < RowSize; i++)
{
for (int j = 1; j < RowSize; j++)
{
m[i,j] = (Rand() % 120 - 60) / 3;
}
}
}

private static void InnerProduct(out double result, double[,] a, double[,] b, int row, int col)
{
result = 0.0;
for (int i = 1; i < RowSize; i++)
{
result = result + a[row,i] * b[i,col];
}
}

private static void Inner(double[,] rma, double[,] rmb, double[,] rmr)
{
InitRand();
InitMatrix(rma);
InitMatrix(rmb);
for (int i = 1; i < RowSize; i++)
{
for (int j = 1; j < RowSize; j++)
{
InnerProduct(out rmr[i,j], rma, rmb, i, j);
}
}
}

[Benchmark]
public static void Test()
{
foreach (var iteration in Benchmark.Iterations)
{
using (iteration.StartMeasurement())
{
Bench();
}
}
}

private static bool TestBase()
{
bool result = Bench();
return result;
}

public static int Main()
{
bool result = TestBase();
return (result ? 100 : -1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
<PropertyGroup>
<ProjectAssetsFile>$(JitPackagesConfigFileDirectory)benchmark\obj\project.assets.json</ProjectAssetsFile>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//
// Solution of linear algebraic equations and matrix inversion.

using Microsoft.Xunit.Performance;
using System;
using System.Runtime.CompilerServices;
using Xunit;

[assembly: OptimizeForBenchmarks]

namespace Benchstone.MDBenchF
{
public static class MDInvMt
{
#if DEBUG
public const int Iterations = 1;
#else
public const int Iterations = 80;
#endif

private const int MatSize = Iterations;

[MethodImpl(MethodImplOptions.NoInlining)]
private static bool Bench()
{
double[,] t = new double[MatSize + 1, (MatSize + 1) * 2];

double det, detinv, ber, p;
int n, i, j;

n = MatSize;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
if (i == j)
{
t[i,j] = 2.0001;
t[i,n + 1 + j] = 1.0;
}
else
{
t[i,j] = 1.0001;
t[i,n + 1 + j] = 0.0;
}
}
t[i,n + 1] = System.Math.Sqrt((float)i);
}

Inner(t, out det, ref n);

for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
p = t[i,j];
t[i,j] = t[i,n + 1 + j];
t[i,n + 1 + j] = p;
}
}

Inner(t, out detinv, ref n);

ber = 0.0;
for (i = 1; i <= n; i++)
{
ber = ber + System.Math.Abs(System.Math.Sqrt((double)i) - t[i,n + 1]);
}

return true;
}

private static void Inner(double[,] t, out double det, ref int n)
{
double tik, tkk;

det = 1.0;
for (int k = 1; k <= n; k++)
{
tkk = t[k,k];
det = det * tkk;

for (int j = 1; j <= (2 * n + 1); j++)
{
t[k,j] = t[k,j] / tkk;
}

for (int i = 1; i <= n; i++)
{
if (i != k)
{
tik = t[i,k];
for (int j = 1; j <= (2 * n + 1); j++)
{
t[i,j] = t[i,j] - t[k,j] * tik;
}
}
}
}
}

[Benchmark]
public static void Test()
{
foreach (var iteration in Benchmark.Iterations)
{
using (iteration.StartMeasurement())
{
Bench();
}
}
}

private static bool TestBase()
{
bool result = Bench();
return result;
}

public static int Main()
{
bool result = TestBase();
return (result ? 100 : -1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
<PropertyGroup>
<ProjectAssetsFile>$(JitPackagesConfigFileDirectory)benchmark\obj\project.assets.json</ProjectAssetsFile>
</PropertyGroup>
</Project>
Loading

0 comments on commit df42364

Please sign in to comment.