xref: /libCEED/julia/LibCEED.jl/src/Ceed.jl (revision 6c6044755ff91cefef5da44fde896bd53fec652f)
144554ea0SWill Paznerstruct CeedError <: Exception
244554ea0SWill Pazner    fname::String
344554ea0SWill Pazner    lineno::Int
444554ea0SWill Pazner    func::String
544554ea0SWill Pazner    ecode::Int
644554ea0SWill Pazner    message::String
744554ea0SWill Paznerend
844554ea0SWill Pazner
944554ea0SWill Pazner# COV_EXCL_START
1044554ea0SWill Paznerfunction Base.showerror(io::IO, e::CeedError)
1144554ea0SWill Pazner    println(io, "libCEED error code ", e.ecode, " in ", e.func)
1244554ea0SWill Pazner    println(io, e.fname, ':', e.lineno, '\n')
1344554ea0SWill Pazner    println(io, e.message)
1444554ea0SWill Paznerend
1544554ea0SWill Pazner# COV_EXCL_STOP
1644554ea0SWill Pazner
1744554ea0SWill Paznerfunction handle_ceed_error(
1844554ea0SWill Pazner    ceed::C.Ceed,
1944554ea0SWill Pazner    c_fname::Cstring,
2044554ea0SWill Pazner    lineno::Cint,
2144554ea0SWill Pazner    c_func::Cstring,
2244554ea0SWill Pazner    ecode::Cint,
2344554ea0SWill Pazner    c_format::Cstring,
2444554ea0SWill Pazner    args::Ptr{Cvoid},
2544554ea0SWill Pazner)
2644554ea0SWill Pazner    c_message = ccall(
2744554ea0SWill Pazner        (:CeedErrorFormat, C.libceed),
2844554ea0SWill Pazner        Cstring,
2944554ea0SWill Pazner        (C.Ceed, Cstring, Ptr{Cvoid}),
3044554ea0SWill Pazner        ceed,
3144554ea0SWill Pazner        c_format,
3244554ea0SWill Pazner        args,
3344554ea0SWill Pazner    )
3444554ea0SWill Pazner    fname = unsafe_string(c_fname)
3544554ea0SWill Pazner    func = unsafe_string(c_func)
3644554ea0SWill Pazner    message = unsafe_string(c_message)
3744554ea0SWill Pazner    throw(CeedError(fname, lineno, func, ecode, message))
3844554ea0SWill Paznerend
3944554ea0SWill Pazner
4044554ea0SWill Paznermutable struct Ceed
4144554ea0SWill Pazner    ref::RefValue{C.Ceed}
4244554ea0SWill Paznerend
4344554ea0SWill Pazner
4444554ea0SWill Pazner"""
4544554ea0SWill Pazner    Ceed(spec="/cpu/self")
4644554ea0SWill Pazner
4744554ea0SWill PaznerWraps a libCEED `Ceed` object, created with the given resource specification string.
4844554ea0SWill Pazner"""
4944554ea0SWill Paznerfunction Ceed(spec::AbstractString="/cpu/self")
5044554ea0SWill Pazner    obj = Ceed(Ref{C.Ceed}())
5144554ea0SWill Pazner    C.CeedInit(spec, obj.ref)
5244554ea0SWill Pazner    ehandler = @cfunction(
5344554ea0SWill Pazner        handle_ceed_error,
5444554ea0SWill Pazner        Cint,
5544554ea0SWill Pazner        (C.Ceed, Cstring, Cint, Cstring, Cint, Cstring, Ptr{Cvoid})
5644554ea0SWill Pazner    )
5744554ea0SWill Pazner    C.CeedSetErrorHandler(obj.ref[], ehandler)
5844554ea0SWill Pazner    finalizer(obj) do x
5944554ea0SWill Pazner        # ccall(:jl_safe_printf, Cvoid, (Cstring, Cstring), "Finalizing %s.\n", repr(x))
6044554ea0SWill Pazner        destroy(x)
6144554ea0SWill Pazner    end
6244554ea0SWill Pazner    return obj
6344554ea0SWill Paznerend
6444554ea0SWill Paznerdestroy(c::Ceed) = C.CeedDestroy(c.ref) # COV_EXCL_LINE
6544554ea0SWill PaznerBase.getindex(c::Ceed) = c.ref[]
6644554ea0SWill Pazner
6744554ea0SWill PaznerBase.show(io::IO, ::MIME"text/plain", c::Ceed) = ceed_show(io, c, C.CeedView)
6844554ea0SWill Pazner
6944554ea0SWill Pazner"""
7044554ea0SWill Pazner    getresource(c::Ceed)
7144554ea0SWill Pazner
7244554ea0SWill PaznerReturns the resource string associated with the given [`Ceed`](@ref) object.
7344554ea0SWill Pazner"""
7444554ea0SWill Paznerfunction getresource(c::Ceed)
75*ec672a92SWill Pazner    res = Ref{Ptr{Cchar}}()
7644554ea0SWill Pazner    C.CeedGetResource(c[], res)
77*ec672a92SWill Pazner    unsafe_string(Cstring(res[]))
7844554ea0SWill Paznerend
7944554ea0SWill Pazner
8044554ea0SWill Pazner"""
8144554ea0SWill Pazner    isdeterministic(c::Ceed)
8244554ea0SWill Pazner
8344554ea0SWill PaznerReturns true if backend of the given [`Ceed`](@ref) object is deterministic, and false
8444554ea0SWill Paznerotherwise.
8544554ea0SWill Pazner"""
8644554ea0SWill Paznerfunction isdeterministic(c::Ceed)
8744554ea0SWill Pazner    isdet = Ref{Bool}()
8844554ea0SWill Pazner    C.CeedIsDeterministic(c[], isdet)
8944554ea0SWill Pazner    isdet[]
9044554ea0SWill Paznerend
9144554ea0SWill Pazner
9244554ea0SWill Pazner"""
9344554ea0SWill Pazner    get_preferred_memtype(c::Ceed)
9444554ea0SWill Pazner
9544554ea0SWill PaznerReturns the preferred [`MemType`](@ref) (either `MEM_HOST` or `MEM_DEVICE`) of the given
9644554ea0SWill Pazner[`Ceed`](@ref) object.
9744554ea0SWill Pazner"""
9844554ea0SWill Paznerfunction get_preferred_memtype(c::Ceed)
9944554ea0SWill Pazner    mtype = Ref{MemType}()
10044554ea0SWill Pazner    C.CeedGetPreferredMemType(c[], mtype)
10144554ea0SWill Pazner    mtype[]
10244554ea0SWill Paznerend
10344554ea0SWill Pazner
10444554ea0SWill Pazner"""
10544554ea0SWill Pazner    iscuda(c::Ceed)
10644554ea0SWill Pazner
10744554ea0SWill PaznerReturns true if the given [`Ceed`](@ref) object has resource `"/gpu/cuda/*"` and false
10844554ea0SWill Paznerotherwise.
10944554ea0SWill Pazner"""
11044554ea0SWill Paznerfunction iscuda(c::Ceed)
11144554ea0SWill Pazner    res_split = split(getresource(c), "/")
11244554ea0SWill Pazner    length(res_split) >= 3 && res_split[3] == "cuda"
11344554ea0SWill Paznerend
114