Skip to content

Commit

Permalink
Apply default VS formatting
Browse files Browse the repository at this point in the history
Also insert namespace BenchmarksGame.
  • Loading branch information
JosephTremoulet committed Sep 15, 2017
1 parent 4d9e8b5 commit d0099ff
Show file tree
Hide file tree
Showing 18 changed files with 1,755 additions and 1,583 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,87 +19,94 @@ minor improvements by Alex Yakunin
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

public sealed class BinaryTrees
namespace BenchmarksGame
{
public const int MinDepth = 4;

public static void Main(string[] args)
public sealed class BinaryTrees
{
var n = args.Length == 0 ? 0 : int.Parse(args[0]);
var maxDepth = n < (MinDepth + 2) ? MinDepth + 2 : n;
var stretchDepth = maxDepth + 1;
public const int MinDepth = 4;

public static void Main(string[] args)
{
var n = args.Length == 0 ? 0 : int.Parse(args[0]);
var maxDepth = n < (MinDepth + 2) ? MinDepth + 2 : n;
var stretchDepth = maxDepth + 1;

var stretchDepthTask = Task.Run(() => TreeNode.CreateTree(stretchDepth).CountNodes());
var maxDepthTask = Task.Run(() => TreeNode.CreateTree(maxDepth).CountNodes());
var stretchDepthTask = Task.Run(() => TreeNode.CreateTree(stretchDepth).CountNodes());
var maxDepthTask = Task.Run(() => TreeNode.CreateTree(maxDepth).CountNodes());

var tasks = new Task<string>[(maxDepth - MinDepth) / 2 + 1];
for (int depth = MinDepth, ti = 0; depth <= maxDepth; depth += 2, ti++) {
var iterationCount = 1 << (maxDepth - depth + MinDepth);
var depthCopy = depth; // To make sure closure value doesn't change
tasks[ti] = Task.Run(() => {
var count = 0;
if (depthCopy >= 17) {
var tasks = new Task<string>[(maxDepth - MinDepth) / 2 + 1];
for (int depth = MinDepth, ti = 0; depth <= maxDepth; depth += 2, ti++)
{
var iterationCount = 1 << (maxDepth - depth + MinDepth);
var depthCopy = depth; // To make sure closure value doesn't change
tasks[ti] = Task.Run(() =>
{
var count = 0;
if (depthCopy >= 17)
{
// Parallelized computation for relatively large tasks
var miniTasks = new Task<int>[iterationCount];
for (var i = 0; i < iterationCount; i++)
miniTasks[i] = Task.Run(() => TreeNode.CreateTree(depthCopy).CountNodes());
Task.WaitAll(miniTasks);
for (var i = 0; i < iterationCount; i++)
count += miniTasks[i].Result;
}
else {
for (var i = 0; i < iterationCount; i++)
miniTasks[i] = Task.Run(() => TreeNode.CreateTree(depthCopy).CountNodes());
Task.WaitAll(miniTasks);
for (var i = 0; i < iterationCount; i++)
count += miniTasks[i].Result;
}
else
{
// Sequential computation for smaller tasks
for (var i = 0; i < iterationCount; i++)
count += TreeNode.CreateTree(depthCopy).CountNodes();
}
return $"{iterationCount}\t trees of depth {depthCopy}\t check: {count}";
});
}
Task.WaitAll(tasks);
count += TreeNode.CreateTree(depthCopy).CountNodes();
}
return $"{iterationCount}\t trees of depth {depthCopy}\t check: {count}";
});
}
Task.WaitAll(tasks);

Console.WriteLine("stretch tree of depth {0}\t check: {1}",
stretchDepth, stretchDepthTask.Result);
foreach (var task in tasks)
Console.WriteLine(task.Result);
Console.WriteLine("long lived tree of depth {0}\t check: {1}",
maxDepth, maxDepthTask.Result);
Console.WriteLine("stretch tree of depth {0}\t check: {1}",
stretchDepth, stretchDepthTask.Result);
foreach (var task in tasks)
Console.WriteLine(task.Result);
Console.WriteLine("long lived tree of depth {0}\t check: {1}",
maxDepth, maxDepthTask.Result);
}
}
}

public struct TreeNode
{
public sealed class NodeData
public struct TreeNode
{
public TreeNode Left, Right;

public NodeData(TreeNode left, TreeNode right)
public sealed class NodeData
{
Left = left;
Right = right;
public TreeNode Left, Right;

public NodeData(TreeNode left, TreeNode right)
{
Left = left;
Right = right;
}
}
}

public NodeData Data;
public NodeData Data;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TreeNode(TreeNode left, TreeNode right)
{
Data = new NodeData(left, right);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TreeNode(TreeNode left, TreeNode right)
{
Data = new NodeData(left, right);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TreeNode CreateTree(int depth)
{
return depth <= 0
? default(TreeNode)
: new TreeNode(CreateTree(depth - 1), CreateTree(depth - 1));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TreeNode CreateTree(int depth)
{
return depth <= 0
? default(TreeNode)
: new TreeNode(CreateTree(depth - 1), CreateTree(depth - 1));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int CountNodes()
{
if (ReferenceEquals(Data, null))
return 1;
return 1 + Data.Left.CountNodes() + Data.Right.CountNodes();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int CountNodes()
{
if (ReferenceEquals(Data, null))
return 1;
return 1 + Data.Left.CountNodes() + Data.Right.CountNodes();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,72 +15,81 @@ contributed by Marek Safar

using System;

class BinaryTrees
namespace BenchmarksGame
{
const int minDepth = 4;

public static void Main(String[] args)
{
int n = 0;
if (args.Length > 0) n = Int32.Parse(args[0]);

int maxDepth = Math.Max(minDepth + 2, n);
int stretchDepth = maxDepth + 1;

int check = (TreeNode.bottomUpTree(stretchDepth)).itemCheck();
Console.WriteLine("stretch tree of depth {0}\t check: {1}", stretchDepth, check);

TreeNode longLivedTree = TreeNode.bottomUpTree(maxDepth);

for (int depth=minDepth; depth<=maxDepth; depth+=2){
int iterations = 1 << (maxDepth - depth + minDepth);

check = 0;
for (int i=1; i<=iterations; i++)
{
check += (TreeNode.bottomUpTree(depth)).itemCheck();
}

Console.WriteLine("{0}\t trees of depth {1}\t check: {2}",
iterations, depth, check);
}

Console.WriteLine("long lived tree of depth {0}\t check: {1}",
maxDepth, longLivedTree.itemCheck());
}


struct TreeNode
{
class Next
{
public TreeNode left, right;
}

private Next next;

internal static TreeNode bottomUpTree(int depth){
if (depth>0){
return new TreeNode(
bottomUpTree(depth-1)
, bottomUpTree(depth-1)
);
}
else {
return new TreeNode();
}
}

TreeNode(TreeNode left, TreeNode right){
this.next = new Next ();
this.next.left = left;
this.next.right = right;
}

internal int itemCheck(){
// if necessary deallocate here
if (next==null) return 1;
else return 1 + next.left.itemCheck() + next.right.itemCheck();
}
}
class BinaryTrees
{
const int minDepth = 4;

public static void Main(String[] args)
{
int n = 0;
if (args.Length > 0) n = Int32.Parse(args[0]);

int maxDepth = Math.Max(minDepth + 2, n);
int stretchDepth = maxDepth + 1;

int check = (TreeNode.bottomUpTree(stretchDepth)).itemCheck();
Console.WriteLine("stretch tree of depth {0}\t check: {1}", stretchDepth, check);

TreeNode longLivedTree = TreeNode.bottomUpTree(maxDepth);

for (int depth = minDepth; depth <= maxDepth; depth += 2)
{
int iterations = 1 << (maxDepth - depth + minDepth);

check = 0;
for (int i = 1; i <= iterations; i++)
{
check += (TreeNode.bottomUpTree(depth)).itemCheck();
}

Console.WriteLine("{0}\t trees of depth {1}\t check: {2}",
iterations, depth, check);
}

Console.WriteLine("long lived tree of depth {0}\t check: {1}",
maxDepth, longLivedTree.itemCheck());
}


struct TreeNode
{
class Next
{
public TreeNode left, right;
}

private Next next;

internal static TreeNode bottomUpTree(int depth)
{
if (depth > 0)
{
return new TreeNode(
bottomUpTree(depth - 1)
, bottomUpTree(depth - 1)
);
}
else
{
return new TreeNode();
}
}

TreeNode(TreeNode left, TreeNode right)
{
this.next = new Next();
this.next.left = left;
this.next.right = right;
}

internal int itemCheck()
{
// if necessary deallocate here
if (next == null) return 1;
else return 1 + next.left.itemCheck() + next.right.itemCheck();
}
}
}
}
Loading

0 comments on commit d0099ff

Please sign in to comment.