Created
September 10, 2017 11:47
-
-
Save halirutan/9e5ffff3e3519472d6e8a0bb283564bb to your computer and use it in GitHub Desktop.
Simple way to call a Julia function throught LibraryLink
This file contains 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 "WolframLibrary.h" | |
#include "julia.h" | |
DLLEXPORT mint WolframLibrary_getVersion() { | |
return WolframLibraryVersion; | |
} | |
DLLEXPORT int WolframLibrary_initialize(WolframLibraryData libData) { | |
return 0; | |
} | |
DLLEXPORT void WolframLibrary_uninitialize(WolframLibraryData libData) {} | |
EXTERN_C DLLEXPORT int juliaInit(WolframLibraryData libData, mint argc, MArgument *args, MArgument res) { | |
jl_init(); | |
return LIBRARY_NO_ERROR; | |
} | |
EXTERN_C DLLEXPORT int juliaExit(WolframLibraryData libData, mint argc, MArgument *args, MArgument res) { | |
int ret = 0; | |
jl_atexit_hook(ret); | |
return ret; | |
} | |
EXTERN_C DLLEXPORT int juliaFunc(WolframLibraryData libData, mint argc, MArgument *args, MArgument res) { | |
mreal in = MArgument_getReal(args[0]); | |
jl_eval_string("my_func(x) = 2*x"); | |
jl_function_t *func = jl_get_function(jl_current_module, "my_func"); | |
jl_value_t* arg = jl_box_float64(in); | |
double ret = jl_unbox_float64(jl_call1(func, arg)); | |
MArgument_setReal(res, ret); | |
return LIBRARY_NO_ERROR; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment