Skip to content

Commit ce4dd5f

Browse files
Add general multi-dimensional array test (dotnet#60749)
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.
1 parent 4b0be56 commit ce4dd5f

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 System.Runtime.CompilerServices;
7+
8+
namespace Benchstone.MDBenchI
9+
{
10+
public static class MDGeneralArray
11+
{
12+
#if DEBUG
13+
public const int Iterations = 1;
14+
#else
15+
public const int Iterations = 5000;
16+
#endif
17+
18+
static void Initialize(int[,,] s) {
19+
for (int i = s.GetLowerBound(0); i <= s.GetUpperBound(0); i++) {
20+
for (int j = s.GetLowerBound(1); j <= s.GetUpperBound(1); j++) {
21+
for (int k = s.GetLowerBound(2); k <= s.GetUpperBound(2); k++) {
22+
s[i,j,k] = (2 * i) - (3 * j) + (5 * k);
23+
}
24+
}
25+
}
26+
}
27+
28+
static bool VerifyCopy(int[,,] s, int[,,] d) {
29+
for (int i = s.GetLowerBound(0); i <= s.GetUpperBound(0); i++) {
30+
for (int j = s.GetLowerBound(1); j <= s.GetUpperBound(1); j++) {
31+
for (int k = s.GetLowerBound(2); k <= s.GetUpperBound(2); k++) {
32+
if (s[i,j,k] != d[i,j,k]) {
33+
return false;
34+
}
35+
}
36+
}
37+
}
38+
39+
return true;
40+
}
41+
42+
[MethodImpl(MethodImplOptions.NoInlining)]
43+
static bool Bench(int loop, int[,,] s, int[,,] d) {
44+
45+
Initialize(s);
46+
47+
for (; loop != 0; loop--) {
48+
for (int i = s.GetLowerBound(0); i <= s.GetUpperBound(0); i++) {
49+
for (int j = s.GetLowerBound(1); j <= s.GetUpperBound(1); j++) {
50+
for (int k = s.GetLowerBound(2); k <= s.GetUpperBound(2); k++) {
51+
d[i,j,k] = s[i,j,k];
52+
}
53+
}
54+
}
55+
}
56+
57+
bool result = VerifyCopy(s, d);
58+
59+
return result;
60+
}
61+
62+
public static bool Test() {
63+
int[,,] s = new int[10, 10, 10];
64+
int[,,] d = new int[10, 10, 10];
65+
return Bench(Iterations, s, d);
66+
}
67+
68+
public static bool Test2() {
69+
int[] lengths = new int[3] { 10, 10, 10 };
70+
int[] lowerBounds = new int[3] { -5, 0, 5 };
71+
int[,,] s = (int[,,])System.Array.CreateInstance(typeof(int), lengths, lowerBounds);
72+
int[,,] d = (int[,,])System.Array.CreateInstance(typeof(int), lengths, lowerBounds);
73+
return Bench(Iterations, s, d);
74+
}
75+
76+
public static int Main() {
77+
bool result = Test() && Test2();
78+
return (result ? 100 : -1);
79+
}
80+
}
81+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
</PropertyGroup>
5+
<PropertyGroup>
6+
<DebugType>pdbonly</DebugType>
7+
<Optimize>true</Optimize>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<Compile Include="$(MSBuildProjectName).cs" />
11+
</ItemGroup>
12+
<PropertyGroup>
13+
<ProjectAssetsFile>$(JitPackagesConfigFileDirectory)benchmark\obj\project.assets.json</ProjectAssetsFile>
14+
</PropertyGroup>
15+
</Project>

0 commit comments

Comments
 (0)