Skip to content

Commit

Permalink
Use metaprogramming for generic functions in core_interface.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
amontoison committed Aug 19, 2024
1 parent feeadf8 commit 22d8487
Show file tree
Hide file tree
Showing 7 changed files with 2,138 additions and 1,478 deletions.
2 changes: 1 addition & 1 deletion docs/src/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ For each of those, we dropped the `cutest_`, so the functions `cutest_ufn` and
To use then you have to convert the types using `Cint` and `Cdouble`, and
pass arrays because of the underlying pointers in Fortran.
In practice, there isn't much improvement in calling these or `ccall`s, except
for the use of the internal `cutest_lib`.
for the use of the internal `cutest_lib_double`.

**Only use these functions if you really know what you're doing.**

Expand Down
38 changes: 37 additions & 1 deletion gen/wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function main()

args = get_default_args()
push!(args, "-I$include_dir")
push!(args, "-DREAL_128")

ctx = create_context(headers, args, options)
build!(ctx)
Expand Down Expand Up @@ -44,13 +45,48 @@ function main()
for (index, block) in enumerate(blocks)
if contains(block, "function")
fname = split(split(block, "function ")[2], "(")[1]
ptr = "ptr_$(fname) = Libdl.dlsym(cutest_lib, :$(fname))"
if contains(block, "_s(") || contains(block, "_s_(")
ptr = "ptr_$(fname) = Libdl.dlsym(cutest_lib_single, :$(fname))"
elseif contains(block, "_q(") || contains(block, "_q_(")
ptr = "ptr_$(fname) = Libdl.dlsym(cutest_lib_quadruple, :$(fname))"
else
ptr = "ptr_$(fname) = Libdl.dlsym(cutest_lib_double, :$(fname))"
end
block = replace(block, " @ccall libcutest.$fname" => " $ptr\n @ccall \$ptr_$(fname)")
end
code = code * block
(index < nblocks) && (code = code * "end\n")
end

# Add wrappers of "fortran_open" and "fortran_close" for single and quadruple precisison
blocks = split(code, "end\n")
nblocks = length(blocks)
code = ""
for (index, block) in enumerate(blocks)
code = code * block
(index < nblocks) && (code = code * "end\n")
for routine in ("fortran_open_", "fortran_close_")
# add the routines `fortran_open_s_` and `fortran_close_s_`
if contains(block, routine)
block_single = block
block_single = replace(block_single, "double" => "single")
block_single = replace(block_single, routine => "$(routine)s_")
block_single = replace(block_single, ":$(routine)s_" => ":$(routine)")
code = code * block_single
(index < nblocks) && (code = code * "end\n")
end
# Add the routines `fortran_open_q_` and `fortran_close_q_`
if contains(block, routine)
block_quadruple = block
block_quadruple = replace(block_quadruple, "double" => "quadruple")
block_quadruple = replace(block_quadruple, routine => "$(routine)q_")
block_quadruple = replace(block_quadruple, ":$(routine)q_" => ":$(routine)")
code = code * block_quadruple
(index < nblocks) && (code = code * "end\n")
end
end
end

write(path, code)
format_file(path, YASStyle())

Expand Down
12 changes: 6 additions & 6 deletions src/CUTEst.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#See JuliaSmoothOptimizers/NLPModels.jl/issues/113
__precompile__()

# Using CUTEst from Julia.

module CUTEst

using CUTEst_jll
Expand All @@ -14,8 +10,12 @@ using NLPModels
import Libdl.dlsym

# Only one problem can be interfaced at any given time.
global cutest_instances = 0
global cutest_lib = C_NULL
global cutest_instances_single = 0
global cutest_instances_double = 0
global cutest_instances_quadruple = 0
global cutest_lib_single = C_NULL
global cutest_lib_double = C_NULL
global cutest_lib_quadruple = C_NULL

export CUTEstModel, sifdecoder, build_libsif, set_mastsif

Expand Down
Loading

0 comments on commit 22d8487

Please sign in to comment.