From 4980c25029e393183b3d62c29d9f5bbb5b68c105 Mon Sep 17 00:00:00 2001 From: Benjamin K Date: Fri, 21 Feb 2025 11:08:38 +0100 Subject: [PATCH] add warnings for large arrays add warnings for large arrays --- src/engine.jl | 7 ++++++- src/mxarray.jl | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/engine.jl b/src/engine.jl index f61f868..00ebcc8 100644 --- a/src/engine.jl +++ b/src/engine.jl @@ -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 diff --git a/src/mxarray.jl b/src/mxarray.jl index ec1113c..c919b9c 100644 --- a/src/mxarray.jl +++ b/src/mxarray.jl @@ -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, @@ -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)