Skip to content

Instantly share code, notes, and snippets.

@standarddeviant
Last active January 18, 2018 04:21
Show Gist options
  • Save standarddeviant/870c23a874d49e11356c5f6417bcba7b to your computer and use it in GitHub Desktop.
Save standarddeviant/870c23a874d49e11356c5f6417bcba7b to your computer and use it in GitHub Desktop.
Usable julia embedding example (julia 0.6.2)
# 0.6.2 in the home directory
#export JULIA_DIR="$HOME/julia-d386e40c17/"
#export JULIA_HOME="$JULIA_DIR/bin"
echo "JULIA_DIR = $JULIA_DIR"
echo "JULIA_HOME = $JULIA_HOME"
#
gcc -o wizbang -fPIC -I$JULIA_DIR/include/julia \
-std=gnu99 \
-L$JULIA_DIR/lib -L$JULIA_DIR/lib/julia \
wizbang.c \
-l:libjulia.so.0.6.2 \
-l:libstdc++.so.6 -l:libLLVM-3.9.so \
-DJULIA_ENABLE_THREADING=1 \
-lpthread \
LD_LIBRARY_PATH="$JULIA_DIR/lib" ./wizbang
START jl_init()... END
sqrt(2.0) = 1.4142135623730951
the size of input is (256, 2)
the size of output is (256, 5)
wiz!
#include <julia.h>
/* define some plausible constants to make a convincing example */
#define INCHANS (2)
#define OUTCHANS (5)
#define NFRAMES (256)
int main(int argc, char *argv[])
{
/* required: setup the Julia context */
printf("START jl_init()...");
jl_init();
printf(" END\n");
/* run Julia commands */
jl_eval_string("@show sqrt(2.0)");
/* Create 2D array of float64 type for input/output */
jl_value_t *array_type = jl_apply_array_type(
(jl_value_t*)jl_float64_type, 2);
jl_array_t *input_array = jl_alloc_array_2d(array_type, NFRAMES, INCHANS);
jl_array_t *output_array = jl_alloc_array_2d(array_type, NFRAMES, OUTCHANS);
JL_GC_PUSH3(&array_type, &input_array, &output_array);
jl_eval_string("include(\"wizbang.jl\")");
jl_function_t *wizbang_func = jl_get_function(jl_main_module, "wizbang");
jl_call2(wizbang_func, (jl_value_t*)input_array, (jl_value_t*)output_array);
/* let Julia GC know it can safely GC arrin and arrout */
JL_GC_POP();
/* strongly recommended: notify Julia that the
program is about to terminate. this allows
Julia time to cleanup pending write requests
and run all finalizers
*/
jl_atexit_hook(0);
return 0;
}
function wizbang(inp, outp)
println("the size of input is $(size(inp))")
println("the size of output is $(size(outp))")
println("wiz!")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment