I am writing some simple MATLAB mex interfaces that allow me to pass large amounts of data between Julia and MATLAB the two with as little copying as possible. I was hoping to gain some insight into how to debug code that uses the GC and some of the other functions in the julia.h file. I have read what is available on the Embedding Julia page on the main page but much is still unclear to me. Here is a simple test case
#include <julia.h>
#include <mex.h>
#ifdef _OS_LINUX_
#include <dlfcn.h>
#endif
int init(){
#ifdef _OS_LINUX_
if (!dlopen("/usr/local/julia/lib/libjulia.so", RTLD_LAZY | RTLD_GLOBAL))
{
mexErrMsgTxt(dlerror());
}
#endif
if(jl_is_initialized()) {
mexPrintf("---------------------------\n");
mexPrintf("Starting bytes in GC %d\n\n", jl_gc_total_bytes());
mexPrintf("---------------------------\n");
mexPrintf("Clean with jl_clear_malloc_data\n");
jl_clear_malloc_data();
mexPrintf("Total bytes in GC %d\n", jl_gc_total_bytes());
mexPrintf("Leftover in GC %d\n\n", jl_gc_diff_total_bytes());
mexPrintf("Clean with gc()\n");
jl_value_t *gc = jl_eval_string("gc()");
mexPrintf("Total bytes in GC %d\n", jl_gc_total_bytes());
mexPrintf("Leftover in GC %d\n\n", jl_gc_diff_total_bytes());
mexPrintf("Clean with jl_gc_collect\n");
jl_gc_collect(1);
mexPrintf("Total bytes in GC %d\n", jl_gc_total_bytes());
mexPrintf("Leftover in GC %d\n\n", jl_gc_diff_total_bytes());
mexPrintf("Clean with workspace\n");
jl_value_t *workspace = jl_eval_string("workspace()");
JL_GC_PUSH1(&workspace);
JL_GC_POP();
mexPrintf("Total bytes in GC %d\n", jl_gc_total_bytes());
mexPrintf("Leftover in GC %d\n\n", jl_gc_diff_total_bytes());
// jl_atexit_hook(0);
mexPrintf("---------------------------\n");
mexPrintf("Shutdown old Julia instance\n");
mexPrintf("---------------------------\n\n");
}else{
jl_init();
mexPrintf("---------------------------\n");
mexPrintf("Initiated a Julia instance\n");
mexPrintf("---------------------------\n\n");
}
return jl_is_initialized();
}
void jl_atexit_hook_0() {
jl_atexit_hook(0);
}
void mexFunction(int nl, mxArray *pl[], int nr, const mxArray *pr[]) {
mexPrintf("==========================\n");
mexPrintf("\tIN MEX\n");
mexPrintf("==========================\n");
if(init()){
mexPrintf("Run JL Command\n");
jl_value_t * ret = jl_eval_string("sqrt(2");
mexPrintf("Total bytes in GC %d\n", jl_gc_total_bytes());
mexPrintf("Leftover in GC %d\n\n", jl_gc_diff_total_bytes());
mexPrintf("PUSH\n");
JL_GC_PUSH1(&ret);
mexPrintf("Total bytes in GC %d\n", jl_gc_total_bytes());
mexPrintf("Left over bytes in GC %d\n\n", jl_gc_diff_total_bytes());
mexPrintf("POP\n");
JL_GC_POP();
mexPrintf("Total bytes in GC %d\n", jl_gc_total_bytes());
mexPrintf("Left over bytes in GC %d\n\n", jl_gc_diff_total_bytes());
}else{
mexErrMsgTxt("Julia not Initiated");
}
mexAtExit(jl_atexit_hook_0);
mexPrintf("Left over bytes in GC %d\n", jl_gc_diff_total_bytes());
mexPrintf("Total bytes in GC %d\n", jl_gc_total_bytes());
mexPrintf("Time in GC %d\n\n\n", jl_gc_total_hrtime());
return;
}
I need to use code similar to run Julia code several times from MATLAB, and I do not want to have close and start a new Julia instance in between calls because this decreases efficiency. I am not as knowledgeable as I need to be to understand if I am/am not allocate memory without deallocation with my methods. Any insight would be helpful.
BTW: I am running Ubuntu, MATLAB 2012b, C90 and Julia v0.6.3