Skip to content

Commit

Permalink
Add general multi-dimensional array test (#60749)
Browse files Browse the repository at this point in the history
This is similar to MDArray2, but uses the GetLowerBound/GetUpperBound
APIs, and has a test case that uses non-zero lower bounds.
It's in the benchmarks folder as it can be used as a benchmark
to compare against MDArray2 behavior.
  • Loading branch information
BruceForstall authored Oct 22, 2021
1 parent 4b0be56 commit ce4dd5f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//

using System.Runtime.CompilerServices;

namespace Benchstone.MDBenchI
{
public static class MDGeneralArray
{
#if DEBUG
public const int Iterations = 1;
#else
public const int Iterations = 5000;
#endif

static void Initialize(int[,,] s) {
for (int i = s.GetLowerBound(0); i <= s.GetUpperBound(0); i++) {
for (int j = s.GetLowerBound(1); j <= s.GetUpperBound(1); j++) {
for (int k = s.GetLowerBound(2); k <= s.GetUpperBound(2); k++) {
s[i,j,k] = (2 * i) - (3 * j) + (5 * k);
}
}
}
}

static bool VerifyCopy(int[,,] s, int[,,] d) {
for (int i = s.GetLowerBound(0); i <= s.GetUpperBound(0); i++) {
for (int j = s.GetLowerBound(1); j <= s.GetUpperBound(1); j++) {
for (int k = s.GetLowerBound(2); k <= s.GetUpperBound(2); k++) {
if (s[i,j,k] != d[i,j,k]) {
return false;
}
}
}
}

return true;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static bool Bench(int loop, int[,,] s, int[,,] d) {

Initialize(s);

for (; loop != 0; loop--) {
for (int i = s.GetLowerBound(0); i <= s.GetUpperBound(0); i++) {
for (int j = s.GetLowerBound(1); j <= s.GetUpperBound(1); j++) {
for (int k = s.GetLowerBound(2); k <= s.GetUpperBound(2); k++) {
d[i,j,k] = s[i,j,k];
}
}
}
}

bool result = VerifyCopy(s, d);

return result;
}

public static bool Test() {
int[,,] s = new int[10, 10, 10];
int[,,] d = new int[10, 10, 10];
return Bench(Iterations, s, d);
}

public static bool Test2() {
int[] lengths = new int[3] { 10, 10, 10 };
int[] lowerBounds = new int[3] { -5, 0, 5 };
int[,,] s = (int[,,])System.Array.CreateInstance(typeof(int), lengths, lowerBounds);
int[,,] d = (int[,,])System.Array.CreateInstance(typeof(int), lengths, lowerBounds);
return Bench(Iterations, s, d);
}

public static int Main() {
bool result = Test() && Test2();
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>

0 comments on commit ce4dd5f

Please sign in to comment.