Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hack to make threaded flint working #604

Merged
merged 1 commit into from
Jul 12, 2019
Merged
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
6 changes: 6 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,9 @@ Nemo depends on various C libraries which are installed using binaries by defaul
Building from source can be enabled by setting the environment variable `NEMO_SOURCE_BUILD=1`
and then doing `Pkg.build("Nemo")` or `Pkg.add("Nemo")` depending on whether Nemo
was already installed.

## Experimental threading support for flint

Enabling a threaded version of flint can be done by setting the evironment
variable `NEMO_THREADED=1`. To set the actual number of threads, use
`Nemo.flint_set_num_threads($numberofthreads)`.
43 changes: 30 additions & 13 deletions src/Nemo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ else
const libantic = joinpath(pkgdir, "deps", "usr", "lib", "libantic")
end

const __isthreaded = "NEMO_THREADED" in keys(ENV) && ENV["NEMO_THREADED"] == "1"

function flint_abort()
error("Problem in the Flint-Subsystem")
end
Expand Down Expand Up @@ -212,6 +214,14 @@ function trace_memory(b::Bool)
end

function __init__()

# In case libgmp picks up the wrong libgmp later on, we "unset" the jl_*
# functions from the julia :libgmp.
if __isthreaded
ccall((:__gmp_set_memory_functions, :libgmp), Nothing,
(Int, Int, Int), 0, 0, 0)
end

if "HOSTNAME" in keys(ENV) && ENV["HOSTNAME"] == "juliabox"
push!(Libdl.DL_LOAD_PATH, "/usr/local/lib")
elseif Sys.islinux()
Expand All @@ -227,18 +237,20 @@ function __init__()
end

if !Sys.iswindows()
ccall((:__gmp_set_memory_functions, libgmp), Nothing,
(Ptr{Nothing},Ptr{Nothing},Ptr{Nothing}),
cglobal(:jl_gc_counted_malloc),
cglobal(:jl_gc_counted_realloc_with_old_size),
cglobal(:jl_gc_counted_free))

ccall((:__flint_set_memory_functions, libflint), Nothing,
(Ptr{Nothing},Ptr{Nothing},Ptr{Nothing},Ptr{Nothing}),
cglobal(:jl_malloc),
cglobal(:jl_calloc),
cglobal(:jl_realloc),
cglobal(:jl_free))
if !__isthreaded
ccall((:__gmp_set_memory_functions, libgmp), Nothing,
(Ptr{Nothing},Ptr{Nothing},Ptr{Nothing}),
cglobal(:jl_gc_counted_malloc),
cglobal(:jl_gc_counted_realloc_with_old_size),
cglobal(:jl_gc_counted_free))

ccall((:__flint_set_memory_functions, libflint), Nothing,
(Ptr{Nothing},Ptr{Nothing},Ptr{Nothing},Ptr{Nothing}),
cglobal(:jl_malloc),
cglobal(:jl_calloc),
cglobal(:jl_realloc),
cglobal(:jl_free))
end
end

ccall((:flint_set_abort, libflint), Nothing,
Expand All @@ -254,10 +266,15 @@ function __init__()

global _get_Special_of_nf = t[1]
global _set_Special_of_nf = t[2]

end

function flint_set_num_threads(a::Int)
ccall((:flint_set_num_threads, libflint), Nothing, (Int,), a)
if !__isthreaded
error("To use threaded flint, julia has to be started with NEMO_THREADED=1")
else
ccall((:flint_set_num_threads, libflint), Nothing, (Int,), a)
end
end

function flint_cleanup()
Expand Down