Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
65 changes: 65 additions & 0 deletions binarytrees/cmain.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# The Computer Language Benchmarks Game
# https://salsa.debian.org/benchmarksgame-team/benchmarksgame/

# contributed by Simon Danisch
# based on the [C++ implementation](https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/binarytrees-gpp-9.html)

using Printf
struct Node
left::UInt32
right::UInt32
end
function alloc!(pool, left, right)
push!(pool, Node(left, right))
return length(pool)
end
function make(pool, d)
d == 0 && return 0
alloc!(pool, make(pool, d - 1), make(pool, d - 1))
end
check(pool, t::Node) = 1 + check(pool, t.left) + check(pool, t.right)
function check(pool, node::Integer)
node == 0 && return 1
@inbounds return check(pool, pool[node])
end
function threads_inner(pool, d, min_depth, max_depth)
niter = 1 << (max_depth - d + min_depth)
c = 0
for j = 1:niter
c += check(pool, make(pool, d))
empty!(pool)
end
return (niter, d, c)
end

function loop_depths(io, d, min_depth, max_depth, ::Val{N}) where N
threadstore = ntuple(x-> NTuple{3, Int}[], N)
Threads.@threads for d in min_depth:2:max_depth
pool = Node[]
push!(threadstore[Threads.threadid()], threads_inner(pool, d, min_depth, max_depth))
end
for results in threadstore, result in results
@printf(io, "%i\t trees of depth %i\t check: %i\n", result...)
end
end
function perf_binary_trees(io, N::Int=10)
min_depth = 4
max_depth = N
stretch_depth = max_depth + 1
pool = Node[]
# create and check stretch tree
c = check(pool, make(pool, stretch_depth))
@printf(io, "stretch tree of depth %i\t check: %i\n", stretch_depth, c)

long_lived_tree = make(pool, max_depth)

loop_depths(io, min_depth, min_depth, max_depth, Val(Threads.nthreads()))
@printf(io, "long lived tree of depth %i\t check: %i\n", max_depth, check(pool, long_lived_tree))
end
Base.@ccallable function julia_main(ARGS::Vector{String})::Cint

perf_binary_trees(stdout, parse(Int, ARGS[1]))
return 0
end

julia_main(["21"])
36 changes: 36 additions & 0 deletions c_vs_julia.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
cd(@__DIR__)
fasta_input = "fasta.txt"
fasta_gen = joinpath("fasta", "fasta.jl")
run(pipeline(`$(Base.julia_cmd()) $fasta_gen 25000000` ;stdout = fasta_input))

benchmarks = [
("binarytrees", 21),
("fannkuchredux", 12),
("fasta", 25000000),
("knucleotide", fasta_input),
("mandelbrot", 16000),
("nbody", 50000000),
("pidigits", 10000),
("regexredux", fasta_input),
("revcomp", fasta_input),
("spectralnorm", 5500),
]

timings = map(benchmarks) do (bench, arg)
println(bench)
isfile("result.bin") && rm("result.bin")
args = [:stdout => "result.bin"]
argcmd = ``
cmd = if arg isa String
@assert isfile(arg)
push!(args, :stdin => arg)
else
argcmd = `$arg`
end
jltime = withenv("JULIA_NUM_THREADS" => 16) do
exe = joinpath("jl_build", bench, "cmain")
@elapsed run(pipeline(`$() $argcmd`; args...))
end
ctime = @elapsed run(pipeline(`./$bench $argcmd`; args...))
(julia = jltime, cpp = ctime)
end
11 changes: 11 additions & 0 deletions cpp_src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
binarytrees
fannkuchredux
fasta
knucleotide
mandelbrot
nbody
pidigits
regexredux
revcomp
spectralnorm
*.o
139 changes: 139 additions & 0 deletions cpp_src/binarytrees.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/* The Computer Language Benchmarks Game
* https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
*
* contributed by Jon Harrop
* modified by Alex Mizrahi
* modified by Andreas Schäfer
* very minor omp tweak by The Anh Tran
* modified to use apr_pools by Dave Compton
* *reset*
*/

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <apr_pools.h>


const size_t LINE_SIZE = 64;

class Apr
{
public:
Apr()
{
apr_initialize();
}

~Apr()
{
apr_terminate();
}
};

struct Node
{
Node *l, *r;

int check() const
{
if (l)
return l->check() + 1 + r->check();
else return 1;
}
};

class NodePool
{
public:
NodePool()
{
apr_pool_create_unmanaged(&pool);
}

~NodePool()
{
apr_pool_destroy(pool);
}

Node* alloc()
{
return (Node *)apr_palloc(pool, sizeof(Node));
}

void clear()
{
apr_pool_clear(pool);
}

private:
apr_pool_t* pool;
};

Node *make(int d, NodePool &store)
{
Node* root = store.alloc();

if(d>0){
root->l=make(d-1, store);
root->r=make(d-1, store);
}else{
root->l=root->r=0;
}

return root;
}

int main(int argc, char *argv[])
{
Apr apr;
int min_depth = 4;
int max_depth = std::max(min_depth+2,
(argc == 2 ? atoi(argv[1]) : 10));
int stretch_depth = max_depth+1;

// Alloc then dealloc stretchdepth tree
{
NodePool store;
Node *c = make(stretch_depth, store);
std::cout << "stretch tree of depth " << stretch_depth << "\t "
<< "check: " << c->check() << std::endl;
}

NodePool long_lived_store;
Node *long_lived_tree = make(max_depth, long_lived_store);

// buffer to store output of each thread
char *outputstr = (char*)malloc(LINE_SIZE * (max_depth +1) * sizeof(char));

#pragma omp parallel for
for (int d = min_depth; d <= max_depth; d += 2)
{
int iterations = 1 << (max_depth - d + min_depth);
int c = 0;

// Create a memory pool for this thread to use.
NodePool store;

for (int i = 1; i <= iterations; ++i)
{
Node *a = make(d, store);
c += a->check();
store.clear();
}

// each thread write to separate location
sprintf(outputstr + LINE_SIZE * d, "%d\t trees of depth %d\t check: %d\n",
iterations, d, c);
}

// print all results
for (int d = min_depth; d <= max_depth; d += 2)
printf("%s", outputstr + (d * LINE_SIZE) );
free(outputstr);

std::cout << "long lived tree of depth " << max_depth << "\t "
<< "check: " << (long_lived_tree->check()) << "\n";

return 0;
}
Loading