forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfannkuch-redux-5.cs
More file actions
154 lines (139 loc) · 5.18 KB
/
fannkuch-redux-5.cs
File metadata and controls
154 lines (139 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// Adapted from fannkuch-redux C# .NET Core #5 program
// http://benchmarksgame.alioth.debian.org/u64q/program.php?test=fannkuchredux&lang=csharpcore&id=5
// aka (as of 2017-09-01) rev 1.6 of https://alioth.debian.org/scm/viewvc.php/benchmarksgame/bench/fannkuchredux/fannkuchredux.csharp-5.csharp?root=benchmarksgame&view=log
// Best-scoring C# .NET Core version as of 2017-09-01
/* The Computer Language Benchmarks Game
http://benchmarksgame.alioth.debian.org/
contributed by Isaac Gouy, transliterated from Oleg Mazurov's Java program
concurrency fix and minor improvements by Peperud
parallel and small optimisations by Anthony Lloyd
*/
using System;
using System.Threading;
using System.Runtime.CompilerServices;
using Xunit;
namespace BenchmarksGame
{
public class FannkuchRedux_5
{
static int[] fact, chkSums, maxFlips;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void firstPermutation(int[] p, int[] pp, int[] count, int idx)
{
for (int i = 0; i < p.Length; ++i) p[i] = i;
for (int i = count.Length - 1; i > 0; --i)
{
int d = idx / fact[i];
count[i] = d;
if (d > 0)
{
idx = idx % fact[i];
for (int j = i; j >= 0; --j) pp[j] = p[j];
for (int j = 0; j <= i; ++j) p[j] = pp[(j + d) % (i + 1)];
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int nextPermutation(int[] p, int[] count)
{
int first = p[1];
p[1] = p[0];
p[0] = first;
int i = 1;
while (++count[i] > i)
{
count[i++] = 0;
int next = p[1];
p[0] = next;
for (int j = 1; j < i;) p[j] = p[++j];
p[i] = first;
first = next;
}
return first;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int countFlips(int first, int[] p, int[] pp)
{
if (first == 0) return 0;
if (p[first] == 0) return 1;
for (int i = 0; i < pp.Length; i++) pp[i] = p[i];
int flips = 2;
while (true)
{
for (int lo = 1, hi = first - 1; lo < hi; lo++, hi--)
{
int t = pp[lo];
pp[lo] = pp[hi];
pp[hi] = t;
}
int tp = pp[first];
if (pp[tp] == 0) return flips;
pp[first] = first;
first = tp;
flips++;
}
}
static void run(int n, int taskId, int taskSize)
{
int[] p = new int[n], pp = new int[n], count = new int[n];
firstPermutation(p, pp, count, taskId * taskSize);
int chksum = countFlips(p[0], p, pp);
int maxflips = chksum;
while (--taskSize > 0)
{
var flips = countFlips(nextPermutation(p, count), p, pp);
chksum += (1 - (taskSize % 2) * 2) * flips;
if (flips > maxflips) maxflips = flips;
}
chkSums[taskId] = chksum;
maxFlips[taskId] = maxflips;
}
[Fact]
public static int TestEntryPoint()
{
return Test(null);
}
[MethodImpl(MethodImplOptions.NoInlining)]
static int Test(int? arg)
{
int n = arg ?? 7;
int sum = Bench(n, true);
int expected = 16;
// Return 100 on success, anything else on failure.
return sum - expected + 100;
}
static int Bench(int n, bool verbose)
{
fact = new int[n + 1];
fact[0] = 1;
var factn = 1;
for (int i = 1; i < fact.Length; i++) { fact[i] = factn *= i; }
// For n == 7 and nTasks > 8, the algorithm returns chkSum != 228
// Hence, we restrict the processor count to 8 to get consistency on
// all the hardwares.
// See https://github.com/dotnet/runtime/issues/67157
int nTasks = Math.Min(Environment.ProcessorCount, 8);
chkSums = new int[nTasks];
maxFlips = new int[nTasks];
int taskSize = factn / nTasks;
var threads = new Thread[nTasks];
for (int i = 1; i < nTasks; i++)
{
int j = i;
(threads[j] = new Thread(() => run(n, j, taskSize))).Start();
}
run(n, 0, taskSize);
int chksum = chkSums[0], maxflips = maxFlips[0];
for (int i = 1; i < threads.Length; i++)
{
threads[i].Join();
chksum += chkSums[i];
if (maxFlips[i] > maxflips) maxflips = maxFlips[i];
}
if (verbose) Console.Out.WriteLineAsync(chksum + "\nPfannkuchen(" + n + ") = " + maxflips);
return maxflips;
}
}
}