Skip to content

Commit

Permalink
Merge 33a22f7 into df15f33
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano committed Jul 8, 2018
2 parents df15f33 + 33a22f7 commit 71699a6
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ os:
- linux
- osx
julia:
- 0.6
- 0.7
- nightly
notifications:
email: false
Expand Down
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
julia 0.6
julia 0.7-beta
BinaryProvider 0.3.0
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.7/julia-0.7-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.7/julia-0.7-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"

Expand Down
44 changes: 22 additions & 22 deletions src/Cuba.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,19 @@ function generic_integrand!(ndim::Cint, x_::Ptr{Cdouble}, ncomp::Cint,
end

# Return pointer for "integrand", to be passed as "integrand" argument to Cuba functions.
integrand_ptr(integrand::T) where {T} = cfunction(generic_integrand!, Cint,
(Ref{Cint}, # ndim
Ptr{Cdouble}, # x
Ref{Cint}, # ncomp
Ptr{Cdouble}, # f
Ref{T})) # userdata
integrand_ptr_nvec(integrand::T) where {T} = cfunction(generic_integrand!, Cint,
(Ref{Cint}, # ndim
Ptr{Cdouble}, # x
Ref{Cint}, # ncomp
Ptr{Cdouble}, # f
Ref{T}, # userdata
Ref{Cint})) # nvec
integrand_ptr(integrand::T) where {T} = @cfunction(generic_integrand!, Cint,
(Ref{Cint}, # ndim
Ptr{Cdouble}, # x
Ref{Cint}, # ncomp
Ptr{Cdouble}, # f
Ref{T})) # userdata
integrand_ptr_nvec(integrand::T) where {T} = @cfunction(generic_integrand!, Cint,
(Ref{Cint}, # ndim
Ptr{Cdouble}, # x
Ref{Cint}, # ncomp
Ptr{Cdouble}, # f
Ref{T}, # userdata
Ref{Cint})) # nvec

abstract type Integrand{T} end

Expand Down Expand Up @@ -167,9 +167,9 @@ end
else
integrand = integrand_ptr_nvec(x.func)
end
integral = Vector{Cdouble}(x.ncomp)
error = Vector{Cdouble}(x.ncomp)
prob = Vector{Cdouble}(x.ncomp)
integral = Vector{Cdouble}(undef, x.ncomp)
error = Vector{Cdouble}(undef, x.ncomp)
prob = Vector{Cdouble}(undef, x.ncomp)
neval = Ref{Int64}(0)
fail = Ref{Cint}(0)
nregions = Ref{Cint}(0)
Expand All @@ -179,22 +179,22 @@ end

### Other functions, not exported
function cores(n::Integer, p::Integer)
ccall((:cubacores, libcuba), Ptr{Void}, (Cint, Cint), n, p)
ccall((:cubacores, libcuba), Ptr{Cvoid}, (Cint, Cint), n, p)
return 0
end

function accel(n::Integer, p::Integer)
ccall((:cubaaccel, libcuba), Ptr{Void}, (Cint, Cint), n, p)
ccall((:cubaaccel, libcuba), Ptr{Cvoid}, (Cint, Cint), n, p)
return 0
end

function init(f::Ptr{Void}, arg::Ptr{Void})
ccall((:cubainit, libcuba), Ptr{Void}, (Ptr{Void}, Ptr{Void}), f, arg)
function init(f::Ptr{Cvoid}, arg::Ptr{Cvoid})
ccall((:cubainit, libcuba), Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}), f, arg)
return 0
end

function exit(f::Ptr{Void}, arg::Ptr{Void})
ccall((:cubaexit, libcuba), Ptr{Void}, (Ptr{Void}, Ptr{Void}), f, arg)
function exit(f::Ptr{Cvoid}, arg::Ptr{Cvoid})
ccall((:cubaexit, libcuba), Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}), f, arg)
return 0
end

Expand Down
8 changes: 4 additions & 4 deletions src/cuhre.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ struct Cuhre{T} <: Integrand{T}
maxevals::Int64
key::Int
statefile::String
spin::Ptr{Void}
spin::Ptr{Cvoid}
end

@inline function dointegrate!(x::Cuhre{T}, integrand, integral,
error, prob, neval, fail, nregions) where {T}
ccall((:llCuhre, libcuba), Cdouble,
(Cint, # ndim
Cint, # ncomp
Ptr{Void}, # integrand
Ptr{Cvoid}, # integrand
Any, # userdata
Int64, # nvec
Cdouble, # reltol
Expand All @@ -50,7 +50,7 @@ end
Int64, # maxevals
Cint, # key
Ptr{Cchar}, # statefile
Ptr{Void}, # spin
Ptr{Cvoid}, # spin
Ptr{Cint}, # nregions
Ptr{Int64}, # neval
Ptr{Cint}, # fail
Expand Down Expand Up @@ -87,7 +87,7 @@ function cuhre(integrand::T, ndim::Integer=2, ncomp::Integer=1;
nvec::Integer=NVEC, reltol::Real=RELTOL, abstol::Real=ABSTOL,
flags::Integer=FLAGS, minevals::Real=MINEVALS,
maxevals::Real=MAXEVALS, key::Integer=KEY,
statefile::AbstractString=STATEFILE, spin::Ptr{Void}=SPIN) where {T}
statefile::AbstractString=STATEFILE, spin::Ptr{Cvoid}=SPIN) where {T}
# Cuhre requires "ndim" to be at least 2, even for an integral over a one
# dimensional domain. Instead, we don't prevent users from setting wrong
# "ndim" values like 0 or negative ones.
Expand Down
14 changes: 7 additions & 7 deletions src/divonne.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ struct Divonne{T} <: Integrand{T}
ldxgiven::Int
xgiven::Array{Cdouble, 2}
nextra::Int64
peakfinder::Ptr{Void}
peakfinder::Ptr{Cvoid}
statefile::String
spin::Ptr{Void}
spin::Ptr{Cvoid}
end

@inline function dointegrate!(x::Divonne{T}, integrand, integral,
error, prob, neval, fail, nregions) where {T}
ccall((:llDivonne, libcuba), Cdouble,
(Cint, # ndim
Cint, # ncomp
Ptr{Void}, # integrand
Ptr{Cvoid}, # integrand
Any, # userdata
Int64, # nvec
Cdouble, # reltol
Expand All @@ -72,9 +72,9 @@ end
Cint, # ldxgiven
Ptr{Cdouble}, # xgiven
Int64, # nextra
Ptr{Void}, # peakfinder
Ptr{Cvoid}, # peakfinder
Ptr{Cchar}, # statefile
Ptr{Void}, # spin
Ptr{Cvoid}, # spin
Ptr{Cint}, # nregions
Ptr{Int64}, # neval
Ptr{Cint}, # fail
Expand Down Expand Up @@ -134,9 +134,9 @@ function divonne(integrand::T, ndim::Integer=2, ncomp::Integer=1;
xgiven::Array{Cdouble,2}=zeros(Cdouble, ldxgiven,
ngiven),
nextra::Integer=NEXTRA,
peakfinder::Ptr{Void}=PEAKFINDER,
peakfinder::Ptr{Cvoid}=PEAKFINDER,
statefile::AbstractString=STATEFILE,
spin::Ptr{Void}=SPIN) where {T}
spin::Ptr{Cvoid}=SPIN) where {T}
# Divonne requires "ndim" to be at least 2, even for an integral over a one
# dimensional domain. Instead, we don't prevent users from setting wrong
# "ndim" values like 0 or negative ones.
Expand Down
8 changes: 4 additions & 4 deletions src/suave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ struct Suave{T} <: Integrand{T}
nmin::Int64
flatness::Cdouble
statefile::String
spin::Ptr{Void}
spin::Ptr{Cvoid}
end

@inline function dointegrate!(x::Suave{T}, integrand, integral,
error, prob, neval, fail, nregions) where {T}
ccall((:llSuave, libcuba), Cdouble,
(Cint, # ndim
Cint, # ncomp
Ptr{Void}, # integrand
Ptr{Cvoid}, # integrand
Any, # userdata
Int64, # nvec
Cdouble, # reltol
Expand All @@ -56,7 +56,7 @@ end
Int64, # nmin
Cdouble, # flatness
Ptr{Cchar}, # statefile
Ptr{Void}, # spin
Ptr{Cvoid}, # spin
Ptr{Cint}, # nregions
Ptr{Int64}, # neval
Ptr{Cint}, # fail
Expand Down Expand Up @@ -98,7 +98,7 @@ function suave(integrand::T, ndim::Integer=1, ncomp::Integer=1;
flags::Integer=FLAGS, seed::Integer=SEED,
minevals::Real=MINEVALS, maxevals::Real=MAXEVALS,
nnew::Integer=NNEW, nmin::Integer=NMIN, flatness::Real=FLATNESS,
statefile::AbstractString=STATEFILE, spin::Ptr{Void}=SPIN) where {T}
statefile::AbstractString=STATEFILE, spin::Ptr{Cvoid}=SPIN) where {T}
return dointegrate(Suave(integrand, ndim, ncomp, Int64(nvec), Cdouble(reltol),
Cdouble(abstol), flags, seed, trunc(Int64, minevals),
trunc(Int64, maxevals), Int64(nnew), Int64(nmin),
Expand Down
8 changes: 4 additions & 4 deletions src/vegas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ struct Vegas{T} <: Integrand{T}
nbatch::Int64
gridno::Int
statefile::String
spin::Ptr{Void}
spin::Ptr{Cvoid}
end

@inline function dointegrate!(x::Vegas{T}, integrand, integral,
error, prob, neval, fail, nregions) where {T}
ccall((:llVegas, libcuba), Cdouble,
(Cint, # ndim
Cint, # ncomp
Ptr{Void}, # integrand
Ptr{Cvoid}, # integrand
Any, # userdata
Int64, # nvec
Cdouble, # reltol
Expand All @@ -58,7 +58,7 @@ end
Int64, # nbatch
Cint, # gridno
Ptr{Cchar}, # statefile
Ptr{Void}, # spin
Ptr{Cvoid}, # spin
Ptr{Int64}, # neval
Ptr{Cint}, # fail
Ptr{Cdouble}, # integral
Expand Down Expand Up @@ -101,7 +101,7 @@ function vegas(integrand::T, ndim::Integer=1, ncomp::Integer=1;
minevals::Real=MINEVALS, maxevals::Real=MAXEVALS,
nstart::Integer=NSTART, nincrease::Integer=NINCREASE,
nbatch::Integer=NBATCH, gridno::Integer=GRIDNO,
statefile::AbstractString=STATEFILE, spin::Ptr{Void}=SPIN) where {T}
statefile::AbstractString=STATEFILE, spin::Ptr{Cvoid}=SPIN) where {T}
return dointegrate(Vegas(integrand, ndim, ncomp, Int64(nvec), Cdouble(reltol),
Cdouble(abstol), flags, seed, trunc(Int64, minevals),
trunc(Int64, maxevals), Int64(nstart),
Expand Down
13 changes: 7 additions & 6 deletions test/benchmark.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

### Code:

using Cuba
using Cuba, Printf

const ndim=3
const ncomp=11
Expand Down Expand Up @@ -58,29 +58,30 @@ function test(x::Vector{Float64}, f::Vector{Float64})
@inbounds f[11] = t11(x[1], x[2], x[3])
end

info("Performance of Cuba.jl:")
@info "Performance of Cuba.jl:"
for alg in (vegas, suave, divonne, cuhre)
# Run the integrator a first time to compile the function.
alg(test, ndim, ncomp, abstol=abstol,
reltol=reltol);
tic();
start_time = time_ns()
alg(test, ndim, ncomp, abstol=abstol,
reltol=reltol);
println(@sprintf("%10.6f seconds (%s)", toq(), ucfirst(string(alg)[6:end])))
end_time = time_ns()
println(@sprintf("%10.6f seconds (%s)", Int(end_time - start_time)/1e9, uppercasefirst(string(alg)[6:end])))
end

cd(dirname(@__FILE__)) do
if mtime("benchmark.c") > mtime("benchmark-c")
run(`gcc -O3 -I ../deps/usr/include -o benchmark-c benchmark.c ../deps/usr/lib/libcuba.a -lm`)
end
info("Performance of Cuba Library in C:")
@info "Performance of Cuba Library in C:"
run(`./benchmark-c`)

if success(`which gfortran`)
if mtime("benchmark.f") > mtime("benchmark-fortran")
run(`gfortran -O3 -fcheck=no-bounds -cpp -o benchmark-fortran benchmark.f ../deps/usr/lib/libcuba.a -lm`)
end
info("Performance of Cuba Library in Fortran:")
@info "Performance of Cuba Library in Fortran:"
run(`./benchmark-fortran`)
end
end
22 changes: 11 additions & 11 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
### Code:

using Cuba
using Base.Test
using Test
using Distributed

f1(x,y,z) = sin(x)*cos(y)*exp(z)
f2(x,y,z) = exp(-(x*x + y*y + z*z))
Expand All @@ -39,7 +40,6 @@ Cuba.accel(0,1000)

# Test results and make sure the estimation of error is exact.
# Analytic expressions: [(e-1)*(1-cos(1))*sin(1), (sqrt(pi)*erf(1)/2)^3, zeta(3)]
answer = [0.6646696797813771, 0.41653838588663805, 1.2020569031595951]
ncomp = 3
@testset "$alg" for (alg, abstol) in ((vegas, 1e-4), (suave, 1e-3),
(divonne, 1e-4), (cuhre, 1e-8))
Expand All @@ -51,20 +51,20 @@ ncomp = 3
result = @inferred alg(integrand1, 3, ncomp, abstol=abstol, reltol=1e-8,
flags=0, maxevals = 3e9)
end
for i = 1:ncomp
@test result[1][i] answer[i] atol = result[2][i]
for (i, answer) in enumerate([0.6646696797813771, 0.41653838588663805, 1.2020569031595951])
@test isapprox(result[1][i], answer, atol=result[2][i])
end
end

integrand2(x, f) = f[1], f[2] = reim(cis(x[1]))

@testset "ndim in Cuhre and Divonne" begin
result, rest = @inferred cuhre((x,f) -> f[] = cos(x[]))
@test result[] sin(1)
result, rest = divonne((x,f) -> f[] = cos(x[]), reltol=1e-8, abstol=1e-8)
@test isapprox(result[], sin(1), atol=1e-8)
@test_throws ArgumentError cuhre((x,f) -> f[] = cos(x[]), 1)
@test_throws ArgumentError divonne((x,f) -> f[] = cos(x[]), 1)
result, rest = @inferred cuhre((x,f) -> f[1] = cos(x[1]))
@test result[1] sin(1)
result, rest = divonne((x,f) -> f[1] = cos(x[1]), reltol=1e-8, abstol=1e-8)
@test isapprox(result[1], sin(1), atol=1e-8)
@test_throws ArgumentError cuhre((x,f) -> f[1] = cos(x[1]), 1)
@test_throws ArgumentError divonne((x,f) -> f[1] = cos(x[1]), 1)
end

@testset "Integral over infinite domain" begin
Expand Down Expand Up @@ -97,4 +97,4 @@ Cuba.exit(C_NULL, C_NULL)
Cuba.integrand_ptr(Cuba.generic_integrand!)

# Dummy call to show
show(DevNull, cuhre((x, f) -> f[1] = x[]))
show(devnull, cuhre((x, f) -> f[1] = x[1]))

0 comments on commit 71699a6

Please sign in to comment.