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

How to add functions? #48

Open
JeffreySarnoff opened this issue Jun 14, 2020 · 5 comments
Open

How to add functions? #48

JeffreySarnoff opened this issue Jun 14, 2020 · 5 comments

Comments

@JeffreySarnoff
Copy link
Member

I'm not sure what you are doing with the local version of @ccall.
I'd like to add some functions via PR. I know how to do it this way:

const LibQM = dlopen(find_library(Quadmath.libquadmath))

Base.rem(x::Float128,y::Float128) = 
   ccall(dlsym(LibQM, :fmodq), Float128, (Float128, Float128), x, y)

How is this best done in keeping with the structure of the package?

@simonbyrne
Copy link
Member

I think it should be

Float128(@ccall(libquadmath.fmodq(x::Cfloat128, y::Cfloat128)::Cfloat128))

@JeffreySarnoff
Copy link
Member Author

Same question for complex functions libquadmath supports.

quadmath.h: __complex128 clogq (__complex128 x)
is used through Julia in one of these two ways:

  • log(z::Complex128)::Complex128
  • log(z::Complex{Float128})::Complex{Float128}.

--- from quadmath.jl ---

# we use this slightly cumbersome definition to ensure
# that the value is 128-bit aligned
# and passed on the xmm registers,
# matching the x86_64 ABI for __float128.
const Cfloat128 = NTuple{2,VecElement{Float64}}

struct Float128 <: AbstractFloat
    data::Cfloat128
end
convert(::Type{Float128}, x::Number) = Float128(x)

const ComplexF128 = Complex{Float128}

Base.cconvert(::Type{Cfloat128}, x::Float128) = x.data
Base.cconvert(::Type{Ref{Cfloat128}}, x::Float128) =
  Ref{Cfloat128}(x.data)

I think defining Ccomplex128 would be more robust and performant when using Complex elementary functions from libquadmath. Which def is best?

  • NTuple{2, Cfloat128}
  • NTuple{4, VecElement{Float64}})
    With constructors or converters or, probably, both:
    convert{::Type{Ccomplex128}, x::ComplexF128) and
    convert{::Type{ComplexF128}, x::Ccomplex128).

@amontoison
Copy link
Contributor

amontoison commented Aug 20, 2024

Hi @JeffreySarnoff @simonbyrne
Do you know how to adapt the cconvert / unsafe_convert such we can pass a reference / vector of Float128 from Julia to the following wrappers?

# f(x)
function cutest_ufn_q_(status, n, x, f)
  ptr_cutest_ufn_q_ = Libdl.dlsym(cutest_lib_quadruple, :cutest_ufn_q_)
  @ccall $ptr_cutest_ufn_q_(status::Ptr{Cint}, n::Ptr{Cint}, x::Ptr{Cfloat128},
                            f::Ptr{Cfloat128})::Cvoid
end

# gradient(x)
function cutest_ugr_q_(status, n, x, g)
  ptr_cutest_ugr_q_ = Libdl.dlsym(cutest_lib_quadruple, :cutest_ugr_q_)
  @ccall $ptr_cutest_ugr_q_(status::Ptr{Cint}, n::Ptr{Cint}, x::Ptr{Cfloat128},
                            g::Ptr{Cfloat128})::Cvoid
end

function cutest_cint_uofg_q_(status, n, x, f, g, grad)
  ptr_cutest_cint_uofg_q_ = Libdl.dlsym(cutest_lib_quadruple, :cutest_cint_uofg_q_)
  @ccall $ptr_cutest_cint_uofg_q_(status::Ptr{Cint}, n::Ptr{Cint}, x::Ptr{Cfloat128}, f::Ptr{Cfloat128},
                                  g::Ptr{Cfloat128}, grad::Ptr{Bool})::Cvoid
end

I'm working on a new version of CUTEst.jl that supports quadruple precision.
We recently added the support of __float128 in the Fortran library.

@JeffreySarnoff
Copy link
Member Author

I do not how to do that.

@RalphAS
Copy link
Collaborator

RalphAS commented Aug 21, 2024

Have you tried using the generic Array -> Ptr method of unsafe_convert (or a replica for whatever you use for f, g, etc.)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants