juliamatlab / mexjulia

embedding Julia in the MATLAB process.
MIT License
52 stars 14 forks source link

Jl.mex example not working #23

Open abmnmj opened 7 years ago

abmnmj commented 7 years ago

Running the Jl.mex example causes an error.

The two first examples in the Readme work:

>> disp(Jl.eval('2+2'))
          4

>> disp(Jl.call('factorial', 10))
          3628800

The file for the third example is in the Matlab path

>> type('double_it.jl')

function double_it(outs::Vector{Ptr{Void}}, ins::Vector{Ptr{Void}})
  try
    mex_return(outs, [ 2*v for v in mex_args(ins) ]...)
  catch e
    mex_showerror(e)
  end
end

and is included

Jl.include('double_it.jl')

Make an input array:

>> a = rand(5,5)

a =

    0.2760    0.4984    0.7513    0.9593    0.8407
    0.6797    0.9597    0.2551    0.5472    0.2543
    0.6551    0.3404    0.5060    0.1386    0.8143
    0.1626    0.5853    0.6991    0.1493    0.2435
    0.1190    0.2238    0.8909    0.2575    0.9293

And call double_it.jl with a:

>> Jl.mex('double_it', a)
Error using cell
Size inputs must be scalar.

Error in Jl.mex (line 8)
        varargout = cell(nout, 1);

Looking at the following lines in Jl.m it appears that the first argument of mex should be the number of output arguments (if nout can be interpreted as "number of outputs")

    % call a MEX-like Julia function
    function varargout = mex(nout, varargin)
        varargout = cell(nout, 1);

but nout contains the file name

K>> disp(nout)
double_it 
twadleigh commented 7 years ago

Jl.mex does now require an extra integer argument. I think you may be looking at README.md for master. The README.md for the rebranding branch includes an example of calling Jl.mex with the extra integer argument.

I hope to merge rebranding branch into master soon (this week?) to avoid this sort of confusion.

Thanks for testing mexjulia out. Please do keep opening issues as you find them.

abmnmj commented 7 years ago

Yes, you're right -- I was using the example in the trunk version of README.md. Sorry for the noise!

Unfortunately I now get an error that crashes MATLAB when running the double_it example. I get consistent crashes if I run each command in README.md (except Jl.include('my_own_julia_code.jl')) in order, one-at-a-time, when running Jl.mex(1, 'double_it', a). I've tried multiple times, also using a pristine installation of mexjulia, and it crashes MATLAB every time.

"Interestingly", I once tried running the double_it.m script (instead of running the commands line-by-line) which did work, and after that I could run Jl.mex(1, 'double_it', a) a few times more. Unfortunately I cannot repeat this (and not for lack of trying...). :smile:

If I instead run the double_it.m script immediately after starting MATLAB, it will run without problems multiple times. If I then call Jl.eval('warn("Oh, no!")'); I get (with MATLAB responsive):

>> Jl.eval('warn("Oh, no!")');
Error using mexjulia
OutOfMemoryError()
 in yieldto(::Task, ::ANY) at .\event.jl:136
 in wait() at .\event.jl:169
 in stream_wait(::Task) at .\stream.jl:44
 in uv_write(::Base.PipeEndpoint, ::Ptr{UInt8}, ::UInt64) at .\stream.jl:820
 in unsafe_write(::Base.PipeEndpoint, ::Ptr{UInt8}, ::UInt64) at .\stream.jl:830
 in write(::Base.PipeEndpoint, ::Array{UInt8,1}) at .\io.jl:175
 in print at .\strings\io.jl:70 [inlined]
 in with_output_color(::Function, ::Symbol, ::Base.PipeEndpoint, ::String, ::Vararg{String,N}) at .\util.jl:302
 in print_with_color(::Symbol, ::Base.PipeEndpoint, ::String, ::Vararg{String,N}) at .\util.jl:306
 in #warn#936(::String, ::Bool, ::Void, ::Void, ::Void, ::Int64, ::Function, ::Base.PipeEndpoint, ::String,::Vararg{String,N}) at .\util.jl:340
 in warn(::Base.PipeEndpoint, ::String) at .\util.jl:332
 in #warn#937(::Array{Any,1}, ::Function, ::String, ::Vararg{String,N}) at .\util.jl:350
 in warn(::String) at .\util.jl:350
 in collect(::Base.Generator{Array{MATLAB.MxArray,1},Mex.##5#6}) at .\array.jl:307
 in jl_eval(::Array{MATLAB.MxArray,1}) at F:\software\Julia\mexjulia\branches\rebranding\jl\Mex.jl:72
 in jl_mex(::Array{Ptr{Void},1}, ::Array{Ptr{Void},1}) at F:\software\Julia\mexjulia\branches\rebranding\jl\Mex.jl:54

Error in Jl.mex (line 10)
        [varargout{:}] = mexjulia('jl_mex', varargin{:});

Error in Jl.eval (line 16)
        [varargout{:}] = Jl.mex(nargin, 'Mex.jl_eval', varargin{:});

After this I can still run all the example commands in README.md without any crash. However, after a few more executions of Jl.eval('warn("Oh, no!")'); it stops giving any output.

Then Jl.eval('println("Hello, world!")'); acts up instead:

>> Jl.eval('println("Hello, world!")');
Error using mexjulia
schedule: Task not runnable
 in enq_work(::Task) at .\event.jl:77
 in uv_writecb_task(::Ptr{Void}, ::Int32) at .\stream.jl:873
 in process_events(::Bool) at .\libuv.jl:82
 in wait() at .\event.jl:147
 in stream_wait(::Task) at .\stream.jl:44
 in uv_write(::Base.PipeEndpoint, ::Ptr{UInt8}, ::UInt64) at .\stream.jl:820
 in unsafe_write(::Base.PipeEndpoint, ::Ptr{UInt8}, ::UInt64) at .\stream.jl:830
 in unsafe_write(::Base.PipeEndpoint, ::Base.RefValue{UInt8}, ::Int64) at .\io.jl:155
 in write(::Base.PipeEndpoint, ::Char) at .\io.jl:194
 in print(::Base.PipeEndpoint, ::Char) at .\char.jl:45
 in print(::Base.PipeEndpoint, ::String, ::Char, ::Vararg{Char,N}) at .\strings\io.jl:19
 in println(::String) at .\coreio.jl:5
 in collect(::Base.Generator{Array{MATLAB.MxArray,1},Mex.##5#6}) at .\array.jl:307
 in jl_eval(::Array{MATLAB.MxArray,1}) at F:\software\Julia\mexjulia\branches\rebranding\jl\Mex.jl:72
 in jl_mex(::Array{Ptr{Void},1}, ::Array{Ptr{Void},1}) at F:\software\Julia\mexjulia\branches\rebranding\jl\Mex.jl:54

Error in Jl.mex (line 10)
        [varargout{:}] = mexjulia('jl_mex', varargin{:});

Error in Jl.eval (line 16)
        [varargout{:}] = Jl.mex(nargin, 'Mex.jl_eval', varargin{:}); 

Finally, this makes MATLAB unresponsive:

>> Jl.eval('warn("Oh, no!")');

and I have to kill it in the Task Manager.

twadleigh commented 7 years ago

I wonder if the issue that you report here is related to #28.