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

Prepare CUTEst.jl for single and quadruple precision #353

Merged
merged 1 commit into from
Aug 19, 2024
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
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

Comment on lines +13 to +18
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should at some point create a structure for this? That could be an issue with the tag good first issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a SIF demuxer in the latest version of CUTEst such that we don't need anymore to update the global variables.
The issue is that package was not updated since long time we need many modifications if we want to easily upgrade it.

export CUTEstModel, sifdecoder, build_libsif, set_mastsif

Expand Down
Loading
Loading