3

This example code previously appeared on a blog posting by Aditya, with one minor adjustments. I tried to run it on Debian 10 (buster), but it failed with the following error.

texlua julia.lua
ERROR: could not load library "/usr/lib/x86_64-linux-gnu/../bin/../lib/x86_64-linux-gnu/julia/sys.so"
/usr/lib/x86_64-linux-gnu/../bin/../lib/x86_64-linux-gnu/julia/sys.so: cannot open shared object file: No such file or directory

I tried this with both the default Julia version 1.0.3+dfsg-4, as well as the current unstable version 1.1.1+dfsg-1. In both cases this produced the error shown above. I'm also using a backport of TeX Live 2019 from Debian unstable (Debian 10/buster just has the prerelease), with LuaTeX 1.10.0.

Two different people report this code working for them on Arch Linux. One of them is Aditya.

To quote him from chat:

On my system, the library that should be loaded is libjulia.so and is located at /usr/lib/libjulia.so

On my Debian system, julia/sys.so is installed, but I'm not sure why it's not being found.

dlocate julia/sys.so
libjulia1: /usr/lib/x86_64-linux-gnu/julia/sys.so

The code follows.

local ffi = require("ffi")
local JULIA = ffi.load("julia", true)

ffi.cdef [[
void jl_init__threading(void);
typedef struct _jl_value_t jl_value_t;
jl_value_t *jl_eval_string(const char*);
]]

JULIA.jl_init__threading()

code = [[
x = [1 2 3]'
A = [1 0 1; 0 1 1; 1 1 0]

y = x'*A*x

print(y[1])
]]

JULIA.jl_eval_string(code)
3

To function, Julia needs the system runtime sys.jl. The Julia interpreter ships with a precompiled version of this runtime, which is dumped into sys.so. I think that the resolution of the path to sys.so can somehow be influenced by setting environment variables, but I was unable to find out how. Another alternative is to point the jl_init function to the path of the system image by using jl_init_with_image.

local ffi = require("ffi")
local JULIA = ffi.load("julia", true)

ffi.cdef [[
void jl_init_with_image__threading(const char *julia_bindir,
                                   const char *image_relative_path);
typedef struct _jl_value_t jl_value_t;
jl_value_t *jl_eval_string(const char*);
]]

JULIA.jl_init_with_image__threading("/usr/lib/x86_64-linux-gnu/julia/", "sys.so")

code = [[
x = [1 2 3]'
A = [1 0 1; 0 1 1; 1 1 0]

y = x'*A*x

print(y[1])
]]

JULIA.jl_eval_string(code)

I saved this file as /tmp/julia/test.lua and ran this in a Docker container using the following commands:

user@host:~$ sudo docker run -v /usr/local/texlive/2019/:/usr/local/texlive/2019/:ro -v /tmp/julia/:/tmp/julia/ -it --rm debian:buster
root@9903c6e0ca52:/# export PATH=/usr/local/texlive/2019/bin/x86_64-linux/:$PATH
root@9903c6e0ca52:/# apt-get update
[...]
root@9903c6e0ca52:/# apt-get install --no-install-recommends julia libjulia-dev
[...]
root@9903c6e0ca52:/# texlua /tmp/julia/test.lua
23root@9903c6e0ca52:/#

The 23 before root@9903c6e0ca52 on the last line is the output from Julia. This is the same as I get from an interactive Julia session:

julia> x = [1 2 3]'
3×1 LinearAlgebra.Adjoint{Int64,Array{Int64,2}}:
 1
 2
 3

julia> A = [1 0 1; 0 1 1; 1 1 0]
3×3 Array{Int64,2}:
 1  0  1
 0  1  1
 1  1  0

julia> y = x'*A*x
1×1 Array{Int64,2}:
 23

julia> print(y[1])
23
  • Hi, Henri. This version runs without error, but also does not output anything. Suggestions/thoughts? I'm also unclear about what is happening here. Is Lua calling a C library belonging to Julia? And if so, how is it possible to send code to the Julia interpreter/REPL from Lua? – Faheem Mitha Aug 15 at 6:16
  • @FaheemMitha It works for me in a Debian Buster Docker container. I have added the commands I run to the answer. – Henri Menke Aug 15 at 21:28
  • Sorry, it does indeed work for me. I think I missed the 23 at the beginning of the prompt. Silly me. Adding \n at the end works - i.e. print(y[1])print("\n"). Thank you very much. – Faheem Mitha Aug 16 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.