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

Move memory management into Julia domain using MATFrostMemory concept. #56

Merged
merged 1 commit into from
Aug 30, 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
55 changes: 19 additions & 36 deletions src/juliacall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,19 @@ module _JuliaCall
end

struct MATFrostOutput
value::MATFrostArray
data::Any
value::Any
exception::Bool
end

struct MATFrostOutputUnwrap
exception::Bool
value::MATFrostArray
end
end

# module MATFrostPackage
# end
const MATFROSTMEMORY = Dict{MATFrostArray, Any}()

function juliacall(mfa::MATFrostArray)

try
fns = [Symbol(unsafe_string(unsafe_load(mfa.fieldnames, i))) for i in 1:mfa.nfields]

if !(:package in fns && :func in fns && :args in fns)
s = _ConvertToMATLAB.convert("Not working2" * string(fns))
return MATFrostOutput(s.matfrostarray, s, false)
throw(MATFrostException("matfrostjulia:incorrectInputSignature", "Missing either field: 'package', 'func' or 'args'."))
end

package_i = findfirst(fn -> fn == :package, fns)
Expand Down Expand Up @@ -88,41 +80,32 @@ module _JuliaCall
# The main Julia call
vo = func(args...)

vom = _ConvertToMATLAB.convert(vo)

return MATFrostOutput(vom.matfrostarray, vom, false)
vom = _ConvertToMATLAB.convert(MATFrostOutput(vo, false))
MATFROSTMEMORY[vom.matfrostarray] = vom
return vom.matfrostarray
catch e
if isa(e, MATFrostException)
mfe = _ConvertToMATLAB.convert(e)
return MATFrostOutput(mfe.matfrostarray, mfe, true)
mfe = _ConvertToMATLAB.convert(MATFrostOutput(e, true))
MATFROSTMEMORY[mfe.matfrostarray] = mfe
return mfe.matfrostarray
else
s = _ConvertToMATLAB.convert(MATFrostException("matfrostjulia:crashed", sprint(showerror, e, catch_backtrace())))
return MATFrostOutput(s.matfrostarray, s, true)
mfe = _ConvertToMATLAB.convert(MATfrostOutput(MATFrostException("matfrostjulia:crashed", sprint(showerror, e, catch_backtrace())), true))
MATFROSTMEMORY[mfe.matfrostarray] = mfe
return mfe.matfrostarray
end
end

end
end

function juliacall_test(mfa::MATFrostArray)
s = _ConvertToMATLAB.convert("Is this working on Linux?")
juliacall_c() = @cfunction(juliacall, MATFrostArray, (MATFrostArray,))

MATFrostOutput(s.matfrostarray, s, false)
function freematfrostmemory(mfa::MATFrostArray)
delete!(MATFROSTMEMORY, mfa)
return nothing
end

juliacall_c() = @cfunction(juliacall, Any, (MATFrostArray,))

freematfrostmemory_c() = @cfunction(freematfrostmemory, Cvoid, (MATFrostArray,))

unwrap(val::MATFrostOutput) = MATFrostOutputUnwrap(val.exception, val.value)

unwrap_c() = @cfunction(unwrap, MATFrostOutputUnwrap, (Any,))



test2 = ["SDF", "SDFS", "FD"]

mfa = MATFrostArray(0, 0, 0, 0, 0, 0)

test1 = juliacall(mfa)


end
21 changes: 9 additions & 12 deletions src/matfrostjuliacall/matfrostjuliacall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,27 +121,24 @@ class MexFunction : public matlab::mex::Function {

auto mfa = MATFrost::ConvertToJulia::convert(inputs[0]);

auto juliacall = (jl_value_t* (*)(MATFrostArray)) jl_unbox_voidpointer(jl_eval_string("MATFrost._JuliaCall.juliacall_c()"));
auto unwrap = (MATFrostOutputUnwrap (*)(jl_value_t*)) jl_unbox_voidpointer(jl_eval_string("MATFrost._JuliaCall.unwrap_c()"));
auto juliacall = (MATFrostArray (*)(MATFrostArray)) jl_unbox_voidpointer(jl_eval_string("MATFrost._JuliaCall.juliacall_c()"));
auto freematfrostmemory = (void (*)(MATFrostArray)) jl_unbox_voidpointer(jl_eval_string("MATFrost._JuliaCall.freematfrostmemory_c()"));

jl_value_t* jlo = juliacall(mfa->matfrostarray);
MATFrostArray jlo = juliacall(mfa->matfrostarray);

JL_GC_PUSH1(jlo);
const matlab::data::StructArray mato = MATFrost::ConvertToMATLAB::convert(jlo);

const MATFrostOutputUnwrap mfao = unwrap(jlo);
freematfrostmemory(jlo);

const matlab::data::Array mato = MATFrost::ConvertToMATLAB::convert(mfao.value);

JL_GC_POP();

if (mfao.exception) {
const matlab::data::StructArray matso = mato;
const matlab::data::TypedArray<bool> exception_b = mato[0]["exception"];
if (exception_b[0]) {
const matlab::data::StructArray matso = mato[0]["value"];
const matlab::data::StringArray exception_id = matso[0]["id"];
const matlab::data::StringArray exception_message = matso[0]["message"];
throw matlab::engine::MATLABException(exception_id[0], exception_message[0]);
}

outputs[0] = mato;
outputs[0] = mato[0]["value"];

}
catch(const matlab::engine::MATLABException& ex)
Expand Down