Last active
August 29, 2015 14:10
-
-
Save twadleigh/387f7aeb9707226d7d18 to your computer and use it in GitHub Desktop.
Julia as a matlab mex function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <julia.h> | |
| #include <mex.h> | |
| void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { | |
| jl_function_t *mex_main; | |
| jl_value_t *array_type; | |
| jl_array_t *mxout; | |
| jl_array_t *mxin; | |
| mex_main = jl_get_function(jl_current_module, "mex_main"); | |
| array_type = jl_apply_array_type(jl_voidpointer_type, 1); | |
| mxout = jl_ptr_to_array_1d(array_type, plhs, nlhs < 1 ? 1 : nlhs, 0); | |
| mxin = jl_ptr_to_array_1d(array_type, prhs, nrhs, 0); | |
| jl_call2(mex_main, (jl_value_t*)mxout, (jl_value_t*)mxin); | |
| if (jl_exception_occurred()) { | |
| mexErrMsgTxt(jl_typeof_str(jl_exception_occurred())); | |
| } | |
| } | |
| unsigned char g_init = 0; | |
| void init_mexjulia() { | |
| if(g_init == 0) { | |
| jl_init("C:/Julia-0.4.0-dev/bin"); | |
| JL_SET_STACK_BASE; | |
| jl_load("mexjulia.jl"); | |
| g_init = 1; | |
| } | |
| } | |
| /* TODO: On linux, need to make sure that "-init init_mexjulia" is added to | |
| the ld command line, so that it is called on library load. | |
| */ | |
| #ifdef _WIN32 | |
| #include <windows.h> | |
| BOOL WINAPI DllMain(HINSTANCE h, DWORD r, LPVOID v) { | |
| init_mexjulia(); | |
| return TRUE; | |
| } | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| mex_err_msg(msg) = ccall((:mexErrMsgTxt,"libmex"), Void, (Ptr{UInt8},), utf8(msg)) | |
| create_string(str) = ccall((:mxCreateString,"libmx"), Ptr{Void}, (Ptr{UInt8},), utf8(str)) | |
| function mex_main(mxout::Vector{Ptr{Void}}, mxin::Vector{Ptr{Void}}) | |
| try | |
| mxout[1] = create_string("Hello, from julia!") | |
| catch e | |
| mex_err_msg(@sprintf("Unhandled exception in mex_main: %s", string(e))) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CC = x86_64-w64-mingw32-gcc | |
| MEX_EXT = mexw64 | |
| JULIA_INC_DIR = "C:/Julia-0.4.0-dev/include/julia" | |
| JULIA_BIN_DIR = "C:/Julia-0.4.0-dev/bin" | |
| MATLAB_INC_DIR = "C:/Program Files/MATLAB/R2012a/extern/include" | |
| MATLAB_BIN_DIR = "C:/Program Files/MATLAB/R2012a/bin/win64" | |
| CCOPTS = -I$(JULIA_INC_DIR) -I$(MATLAB_INC_DIR) | |
| CCOPTS += -DMATLAB_MEX_FILE | |
| LOPTS = -L$(JULIA_BIN_DIR) -L$(MATLAB_BIN_DIR) | |
| LOPTS += -shared -ljulia -lmex | |
| mexjulia.$(MEX_EXT) : mexjulia.o | |
| $(CC) $(LOPTS) -o $@ $< | |
| mexjulia.o : mexjulia.c | |
| $(CC) $(CCOPTS) -c -o $@ $< | |
| .PHONY : clean | |
| clean : | |
| -rm mexjulia.$(MEX_EXT) mexjulia.o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment