Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 39 additions & 0 deletions c_vs_julia.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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...))
(jltime, ctime)
end


run(pipeline(`./revcomp $argcmd`, stdin = fasta_gen))
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
60 changes: 18 additions & 42 deletions cpp_src/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ for cmd in make_cmds
end
end

fasta_input = "fasta.txt"
fasta_gen = joinpath(@__DIR__, "..", "fasta", "fasta.jl")
run(pipeline(`$(Base.julia_cmd()) $fasta_gen 25000000` ;stdout = fasta_input))


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

Expand All @@ -31,60 +35,32 @@ map(benchmarks) do (bench, arg)
else
`./$bench $arg`
end
@elapsed read(cmd, String)
end

map(benchmarks) do (bench, arg)
cd(@__DIR__)
timings = map(benchmarks) do (bench, arg)
println(bench)
root = joinpath(dir, bench)
jl = joinpath(root, string(bench, "-fast.jl"))
if !isfile(jl)
jl = replace(jl, "-fast" => "")
end
@assert isfile(jl)
isfile("result.bin") && rm("result.bin")
args = [:stdout => "result.bin"]
argcmd = ``
cmd = if arg isa String
push!(args, :stdin => joinpath(dir, bench, arg))
@assert isfile(arg)
push!(args, :stdin => arg)
else
argcmd = `$arg`
end
jltime = withenv("JULIA_NUM_THREADS" => 16) do
@elapsed run(pipeline(`julia -O3 $jl $argcmd`; args...))
end
# jltime = withenv("JULIA_NUM_THREADS" => 16) do
# @elapsed run(pipeline(`julia -O3 $jl $argcmd`; args...))
# end
jltime = 0.0
ctime = @elapsed run(pipeline(`./$bench $argcmd`; args...))
(jltime, ctime)
end

function create_cmain(dir, bench, arg)
root = joinpath(dir, bench)
jl = joinpath(root, string(bench, "-fast.jl"))
if !isfile(jl)
jl = replace(jl, "-fast" => "")
end
src = read(jl, String)
str = split(read(jl, String), "\n", keepempty = false)
lastend = str[findlast(x-> occursin(x, "end"), str)]
start = lastend.offset + lastend.ncodeunits
main = src[(start + 1):end]
rest = src[1:start]
open(joinpath(root, "cmain.jl"), "w") do io
println(io, rest)
println(io, """
Base.@ccallable function julia_main(ARGS::Vector{String})::Cint
""")
for line in split(main, "\n", keepempty = false)
println(io, " ", line)
end
println(io, """
return 0
end
""")
if arg isa Integer
println(io, "julia_main([\"$arg\"])")
end
end
end
for (bench, arg) in benchmarks
create_cmain(dir, bench, arg)
end

run(pipeline(`./revcomp $argcmd`, stdin = fasta_gen))
23 changes: 8 additions & 15 deletions fasta/fasta-fast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the changes to this file be cherry-picked out of this branch as an improvement to the existing Julia program?

const line_width = 60

const alu = string(
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG",
"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA",
"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT",
"ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA",
"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG",
"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC",
"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA")
const alu = b"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGACCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAATACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCAGCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGGAGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCCAGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA"

const iub1 = b"acgtBDHKMNRSVWY"
const iub2 = [0.27, 0.12, 0.12, 0.27, 0.02,0.02, 0.02, 0.02, 0.02, 0.02,0.02, 0.02, 0.02, 0.02, 0.02]
Expand All @@ -31,18 +24,18 @@ function repeat_fasta(io, src, n)
col = 1
count = 1
c, state = iterate(I)
write(io, c % UInt8)
write(io, c)
while count < n
col += 1
c, state = iterate(I, state)
write(io, c % UInt8)
write(io, c)
if col == line_width
write(io, '\n')
write(io, '\n' % UInt8)
col = 0
end
count += 1
end
write(io, '\n')
write(io, '\n' % UInt8)
return
end

Expand Down Expand Up @@ -77,7 +70,7 @@ function random_fasta(io, symb, pr, n)
return
end

function perf_fasta(_n=25000000, io = stdout)
function perf_fasta(n=25000000, io = stdout)
write(io, ">ONE Homo sapiens alu\n")
repeat_fasta(io, alu, 2n)

Expand All @@ -87,5 +80,5 @@ function perf_fasta(_n=25000000, io = stdout)
random_fasta(io, homosapiens1, homosapiens2, 5n)
end

n = parse(Int,ARGS[1])
perf_fasta(n)
# n = parse(Int,ARGS[1])
@time perf_fasta(25000000, IOBuffer())
10 changes: 10 additions & 0 deletions jl_build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
binarytrees
fannkuchredux
fasta
knucleotide
mandelbrot
nbody
pidigits
regexredux
revcomp
spectralnorm
45 changes: 18 additions & 27 deletions knucleotide/cmain.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# The Computer Language Benchmarks Game
# Th# The Computer Language Benchmarks Game
# https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
#
# contributed by David Campbell
Expand All @@ -7,12 +7,8 @@
#
# Bit-twiddle optimizations added by Kristoffer Carlsson

using Distributed
using Printf

addprocs(4)

@everywhere begin
const NucleotideLUT = zeros(UInt8, 256)
NucleotideLUT['A'%UInt8] = 0
NucleotideLUT['C'%UInt8] = 1
Expand Down Expand Up @@ -114,57 +110,52 @@ function sorted_array(m)
sort!(kn)
end

do_work(str::String, i::Int) = sorted_array(count_data(str, KNucleotides{i,determine_inttype(i)}))
do_work(str::String, i::Int) = sorted_array(count_data(str, KNucleotides{i, determine_inttype(i)}))
do_work(str::String, i::String) = count_one(str, i)

end # @everywhere

function print_knucs(a::Array{KNuc, 1})
function print_knucs(io, a::Array{KNuc, 1})
sum = 0
for kn in a
sum += kn.count
end
for kn in a
@printf("%s %.3f\n", kn.name, 100.0kn.count/sum)
@printf(io, "%s %.3f\n", kn.name, 100.0kn.count/sum)
end
println()
println(io)
end

function perf_k_nucleotide(io = stdin)
function perf_k_nucleotide(io = stdin, output = stdout)
three = ">THREE "
while true
line = readline(io)
if startswith(line, three)
break
end
end
data = read(io, String)
str = filter(!isequal('\n'), data)

vs = [1, 2, "GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"]
str = sprint() do sio
foreach(line-> write(sio, line), eachline(io))
end
vs = (1, 2, "GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT")
results = Vector{Any}(undef, length(vs))
order = collect(enumerate(vs))

@sync for w in workers()
@async while !isempty(order)
v = pop!(order)
r = remotecall_fetch(do_work, w, str, v[2])
results[v[1]] = r
end
Threads.@threads for i in length(vs):-1:1
results[i] = do_work(str, vs[i])
end

for (v, result) in zip(vs, results)
if result isa Array
print_knucs(result)
print_knucs(output, result)
end
if result isa Int
@printf("%d\t%s\n", result, v)
@printf(output, "%d\t%s\n", result, v)
end
end
end
Base.@ccallable function julia_main(ARGS::Vector{String})::Cint

Base.@ccallable function julia_main(ARGS::Vector{String})::Cint
perf_k_nucleotide()
return 0
end

open(joinpath(@__DIR__, "knucleotide-input.txt")) do io
perf_k_nucleotide(io, IOBuffer())
end
17 changes: 3 additions & 14 deletions knucleotide/knucleotide-fast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
using Distributed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the changes to this file be cherry-picked out of this branch as an improvement to the existing Julia program?

using Printf

addprocs(4)

@everywhere begin
const NucleotideLUT = zeros(UInt8, 256)
NucleotideLUT['A'%UInt8] = 0
NucleotideLUT['C'%UInt8] = 1
Expand Down Expand Up @@ -117,8 +114,6 @@ end
do_work(str::String, i::Int) = sorted_array(count_data(str, KNucleotides{i,determine_inttype(i)}))
do_work(str::String, i::String) = count_one(str, i)

end # @everywhere

function print_knucs(a::Array{KNuc, 1})
sum = 0
for kn in a
Expand All @@ -141,16 +136,10 @@ function perf_k_nucleotide(io = stdin)
data = read(io, String)
str = filter(!isequal('\n'), data)

vs = [1, 2, "GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"]
vs = (1, 2, "GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT")
results = Vector{Any}(undef, length(vs))
order = collect(enumerate(vs))

@sync for w in workers()
@async while !isempty(order)
v = pop!(order)
r = remotecall_fetch(do_work, w, str, v[2])
results[v[1]] = r
end
for (i, v) in enumerate(vs)
results[i] = do_work(w, str, v[2])
end

for (v, result) in zip(vs, results)
Expand Down
7 changes: 4 additions & 3 deletions mandelbrot/cmain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ function mandelbrot(n = 200, io = stdout)
write(io, "P4\n$n $n\n")
write(io, rows)
end
Base.@ccallable function julia_main(ARGS::Vector{String})::Cint

mandelbrot(parse(Int, ARGS[1]), stdout)
Base.@ccallable function julia_main(ARGS::Vector{String})::Cint
n = parse(Int, ARGS[1])
mandelbrot(n, stdout)
return 0
end

mandelbrot(160, IOBuffer())
julia_main(["8"])
7 changes: 4 additions & 3 deletions nbody/cmain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,13 @@ function perf_nbody(N::Int=1000)
end
@printf("%.9f\n", energy(bodies))
end
Base.@ccallable function julia_main(ARGS::Vector{String})::Cint

end # module
end

Base.@ccallable function julia_main(ARGS::Vector{String})::Cint
n = parse(Int,ARGS[1])
NBody.perf_nbody(n)
return 0
end

julia_main(["50000000"])
julia_main(["50000"])
Loading