Commit 10788aab authored by igouy-guest's avatar igouy-guest

*** empty log message ***


SKIPPED:
	website/websites/u64q/code/fannkuchredux.6.csharpcore.code
	website/websites/u64q/code/fannkuchredux.6.csharpcore.log
	website/websites/u64q/data/data.csv
	website/websites/u64q/data/ndata.csv
	website/websites/u64q/data/u64q_bulkdata.csv.bz2
parent bf1c201c
......@@ -8,16 +8,15 @@
using System;
using System.Threading;
using System.Runtime.CompilerServices;
public static class FannkuchRedux
{
static int[] chkSums, maxFlips;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void firstPermutation(int n, int[] fact, int[] p, int[] pp, int[] count, int idx)
static int taskCount;
static int[] fact, chkSums, maxFlips;
static void firstPermutation(int[] p, int[] pp, int[] count, int idx)
{
for (int i=0; i<n; ++i) p[i] = i;
for (int i=n-1; i>0; --i)
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;
......@@ -30,7 +29,6 @@ public static class FannkuchRedux
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int nextPermutation(int[] p, int[] count)
{
int first = p[1];
......@@ -49,71 +47,68 @@ public static class FannkuchRedux
return first;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void countFlips(int n, int[] p, int[] pp, int first, int sign, ref int chksum, ref int maxflips)
static int countFlips(int first, int[] p, int[] pp)
{
if (first==0) return;
if (p[first]==0)
{
chksum += sign;
return;
}
for(int i=0; i<n; i++) pp[i] = p[i];
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--)
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)
{
chksum += sign * flips;
if(flips>maxflips) maxflips = flips;
return;
}
if (pp[tp]==0) return flips;
pp[first] = first;
first = tp;
flips++;
}
}
static void run(int n, int[] fact, int taskId, int taskSize)
static void run(int n, int taskSize)
{
int[] p = new int[n], pp = new int[n], count = new int[n];
int maxflips=1, chksum=0;
firstPermutation(n, fact, p, pp, count, taskId*taskSize);
countFlips(n, p, pp, p[0], 1, ref chksum, ref maxflips);
while (--taskSize>0)
int taskId = 0, chksum = 0, maxflips = 0;
while((taskId = Interlocked.Decrement(ref taskCount)) >= 0)
{
countFlips(n, p, pp, nextPermutation(p, count), 1-(taskSize%2)*2, ref chksum, ref maxflips);
firstPermutation(p, pp, count, taskId*taskSize);
var flips = countFlips(p[0], p, pp);
chksum += flips;
if(flips>maxflips) maxflips = flips;
for(int i=1; i<taskSize; i++)
{
flips = countFlips(nextPermutation(p, count), p, pp);
chksum += (1-(i%2)*2) * flips;
if(flips>maxflips) maxflips = flips;
}
}
chkSums[taskId] = chksum;
maxFlips[taskId] = maxflips;
chkSums[-taskId-1] = chksum;
maxFlips[-taskId-1] = maxflips;
}
public static void Main(string[] args)
{
int n = args.Length > 0 ? int.Parse(args[0]) : 7;
var fact = new int[n+1];
fact = new int[n+1];
fact[0] = 1;
var factn = 1;
for (int i=1; i<fact.Length; i++) { fact[i] = factn *= i; }
int nTasks = Environment.ProcessorCount;
chkSums = new int[nTasks];
maxFlips = new int[nTasks];
int taskSize = factn/nTasks;
var threads = new Thread[nTasks];
for(int i=1; i<nTasks; i++)
taskCount = n>9 ? factn/(7*6*5*4*3*2) : Environment.ProcessorCount;
int taskSize = factn/taskCount;
int nThreads = Environment.ProcessorCount;
chkSums = new int[nThreads];
maxFlips = new int[nThreads];
var threads = new Thread[nThreads];
for(int i=1; i<nThreads; i++)
{
int j = i;
(threads[j] = new Thread(() => run(n, fact, j, taskSize))).Start();
(threads[i] = new Thread(() => run(n, taskSize))).Start();
}
run(n, fact, 0, taskSize);
run(n, taskSize);
int chksum=chkSums[0], maxflips=maxFlips[0];
for(int i=1; i<threads.Length; i++)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment