xref: /libCEED/julia/LibCEED.jl/src/Ceed.jl (revision 44554ea01e90fce366fc2a203c44be15754a38d6)
1*44554ea0SWill Paznerstruct CeedError <: Exception
2*44554ea0SWill Pazner    fname::String
3*44554ea0SWill Pazner    lineno::Int
4*44554ea0SWill Pazner    func::String
5*44554ea0SWill Pazner    ecode::Int
6*44554ea0SWill Pazner    message::String
7*44554ea0SWill Paznerend
8*44554ea0SWill Pazner
9*44554ea0SWill Pazner# COV_EXCL_START
10*44554ea0SWill Paznerfunction Base.showerror(io::IO, e::CeedError)
11*44554ea0SWill Pazner    println(io, "libCEED error code ", e.ecode, " in ", e.func)
12*44554ea0SWill Pazner    println(io, e.fname, ':', e.lineno, '\n')
13*44554ea0SWill Pazner    println(io, e.message)
14*44554ea0SWill Paznerend
15*44554ea0SWill Pazner# COV_EXCL_STOP
16*44554ea0SWill Pazner
17*44554ea0SWill Paznerfunction handle_ceed_error(
18*44554ea0SWill Pazner    ceed::C.Ceed,
19*44554ea0SWill Pazner    c_fname::Cstring,
20*44554ea0SWill Pazner    lineno::Cint,
21*44554ea0SWill Pazner    c_func::Cstring,
22*44554ea0SWill Pazner    ecode::Cint,
23*44554ea0SWill Pazner    c_format::Cstring,
24*44554ea0SWill Pazner    args::Ptr{Cvoid},
25*44554ea0SWill Pazner)
26*44554ea0SWill Pazner    c_message = ccall(
27*44554ea0SWill Pazner        (:CeedErrorFormat, C.libceed),
28*44554ea0SWill Pazner        Cstring,
29*44554ea0SWill Pazner        (C.Ceed, Cstring, Ptr{Cvoid}),
30*44554ea0SWill Pazner        ceed,
31*44554ea0SWill Pazner        c_format,
32*44554ea0SWill Pazner        args,
33*44554ea0SWill Pazner    )
34*44554ea0SWill Pazner    fname = unsafe_string(c_fname)
35*44554ea0SWill Pazner    func = unsafe_string(c_func)
36*44554ea0SWill Pazner    message = unsafe_string(c_message)
37*44554ea0SWill Pazner    throw(CeedError(fname, lineno, func, ecode, message))
38*44554ea0SWill Paznerend
39*44554ea0SWill Pazner
40*44554ea0SWill Paznermutable struct Ceed
41*44554ea0SWill Pazner    ref::RefValue{C.Ceed}
42*44554ea0SWill Paznerend
43*44554ea0SWill Pazner
44*44554ea0SWill Pazner"""
45*44554ea0SWill Pazner    Ceed(spec="/cpu/self")
46*44554ea0SWill Pazner
47*44554ea0SWill PaznerWraps a libCEED `Ceed` object, created with the given resource specification string.
48*44554ea0SWill Pazner"""
49*44554ea0SWill Paznerfunction Ceed(spec::AbstractString="/cpu/self")
50*44554ea0SWill Pazner    obj = Ceed(Ref{C.Ceed}())
51*44554ea0SWill Pazner    C.CeedInit(spec, obj.ref)
52*44554ea0SWill Pazner    ehandler = @cfunction(
53*44554ea0SWill Pazner        handle_ceed_error,
54*44554ea0SWill Pazner        Cint,
55*44554ea0SWill Pazner        (C.Ceed, Cstring, Cint, Cstring, Cint, Cstring, Ptr{Cvoid})
56*44554ea0SWill Pazner    )
57*44554ea0SWill Pazner    C.CeedSetErrorHandler(obj.ref[], ehandler)
58*44554ea0SWill Pazner    finalizer(obj) do x
59*44554ea0SWill Pazner        # ccall(:jl_safe_printf, Cvoid, (Cstring, Cstring), "Finalizing %s.\n", repr(x))
60*44554ea0SWill Pazner        destroy(x)
61*44554ea0SWill Pazner    end
62*44554ea0SWill Pazner    return obj
63*44554ea0SWill Paznerend
64*44554ea0SWill Paznerdestroy(c::Ceed) = C.CeedDestroy(c.ref) # COV_EXCL_LINE
65*44554ea0SWill PaznerBase.getindex(c::Ceed) = c.ref[]
66*44554ea0SWill Pazner
67*44554ea0SWill PaznerBase.show(io::IO, ::MIME"text/plain", c::Ceed) = ceed_show(io, c, C.CeedView)
68*44554ea0SWill Pazner
69*44554ea0SWill Pazner"""
70*44554ea0SWill Pazner    getresource(c::Ceed)
71*44554ea0SWill Pazner
72*44554ea0SWill PaznerReturns the resource string associated with the given [`Ceed`](@ref) object.
73*44554ea0SWill Pazner"""
74*44554ea0SWill Paznerfunction getresource(c::Ceed)
75*44554ea0SWill Pazner    res = Ref{Cstring}()
76*44554ea0SWill Pazner    C.CeedGetResource(c[], res)
77*44554ea0SWill Pazner    unsafe_string(res[])
78*44554ea0SWill Paznerend
79*44554ea0SWill Pazner
80*44554ea0SWill Pazner"""
81*44554ea0SWill Pazner    isdeterministic(c::Ceed)
82*44554ea0SWill Pazner
83*44554ea0SWill PaznerReturns true if backend of the given [`Ceed`](@ref) object is deterministic, and false
84*44554ea0SWill Paznerotherwise.
85*44554ea0SWill Pazner"""
86*44554ea0SWill Paznerfunction isdeterministic(c::Ceed)
87*44554ea0SWill Pazner    isdet = Ref{Bool}()
88*44554ea0SWill Pazner    C.CeedIsDeterministic(c[], isdet)
89*44554ea0SWill Pazner    isdet[]
90*44554ea0SWill Paznerend
91*44554ea0SWill Pazner
92*44554ea0SWill Pazner"""
93*44554ea0SWill Pazner    get_preferred_memtype(c::Ceed)
94*44554ea0SWill Pazner
95*44554ea0SWill PaznerReturns the preferred [`MemType`](@ref) (either `MEM_HOST` or `MEM_DEVICE`) of the given
96*44554ea0SWill Pazner[`Ceed`](@ref) object.
97*44554ea0SWill Pazner"""
98*44554ea0SWill Paznerfunction get_preferred_memtype(c::Ceed)
99*44554ea0SWill Pazner    mtype = Ref{MemType}()
100*44554ea0SWill Pazner    C.CeedGetPreferredMemType(c[], mtype)
101*44554ea0SWill Pazner    mtype[]
102*44554ea0SWill Paznerend
103*44554ea0SWill Pazner
104*44554ea0SWill Pazner"""
105*44554ea0SWill Pazner    iscuda(c::Ceed)
106*44554ea0SWill Pazner
107*44554ea0SWill PaznerReturns true if the given [`Ceed`](@ref) object has resource `"/gpu/cuda/*"` and false
108*44554ea0SWill Paznerotherwise.
109*44554ea0SWill Pazner"""
110*44554ea0SWill Paznerfunction iscuda(c::Ceed)
111*44554ea0SWill Pazner    res_split = split(getresource(c), "/")
112*44554ea0SWill Pazner    length(res_split) >= 3 && res_split[3] == "cuda"
113*44554ea0SWill Paznerend
114