Skip to content
Open
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
7 changes: 6 additions & 1 deletion src/engine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,13 @@ function put_variable(session::MSession, name::Symbol, v::MxArray)
string(name),
v,
)

# The size limit for `put_variable` is 2GB according to the MATLAB documentation, but seems to be a bit higher in practice.
# https://www.mathworks.com/help/matlab/apiref/engputvariable.html
ret != 0 && throw(
MEngineError("failed to put variable $(name) into MATLAB session (err = $ret)"),
MEngineError(
"failed to put variable $(name) into MATLAB session (err = $ret). Ensure the that the variable name does not conflict with internal MATLAB names and that the size of the variable is below the MATLAB limit of 2GB.",
),
)
return nothing
end
Expand Down
16 changes: 16 additions & 0 deletions src/mxarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ mxarray(x::T) where {T<:MxComplexNum} = mxarray([x])
# mxArray use Julia array's memory

function mxarray(a::Array{T}) where {T<:MxRealNum}
# Check the array size
if sizeof(a) > 2 * 1024^3
@warn(
"Input array size exceeds the limit of 2 GB and is too large to be used with the MATLAB engine.",
maxlog = 1
)
end

mx = mxarray(T, size(a))
ccall(
:memcpy,
Expand All @@ -269,6 +277,14 @@ function mxarray(a::Array{T}) where {T<:MxRealNum}
end

function mxarray(a::Array{T}) where {T<:MxComplexNum}
# Check the array size
if sizeof(a) > 2 * 1024^3
@warn(
"Input array size exceeds the limit of 2 GB and is too large to be used with the MATLAB engine.",
maxlog = 1
)
end

mx = mxarray(T, size(a))
na = length(a)
rdat = unsafe_wrap(Array, real_ptr(mx), na)
Expand Down
Loading