Nim-Julia bridge
Prerequisite
- Install Julia version 1.5.3 or above. Version 1.6.6 or above is recommended.
juliaexecutable must be in your path.- By-default,
nimjlwill use Julia'sSys.BINDIRparent directory as the installation folder of Julia. - If you want
nimjlto use a specific Julia installation, set the environment variableJULIA_PATHto the root of the installation folder.
- By-default,
- Run
nimble installornimble develop
After this steps, $JULIA_PATH/include should points to Julia header and $JULIA_PATH/lib should point to libjulia.so
You can also install Julia locally by running nimble install julia, in that case it will install Julia in the third_party folder
Ressources
How to embed Julia w/ C :
-
https://docs.julialang.org/en/v1/manual/embedding/index.html#Working-with-Arrays-1
-
https://github.com/JuliaLang/julia/tree/master/test/embedding
-
Read the Scinim getting-started chapter on Nimjl
-
legacy/folder contains previous experiment and examples of wrapping in C. -
tests/testfull.nimis thet test suite -
examples/contains several examples
Next steps
Julia is mostly oriented towards numerical computing so Arrays are THE most important data structure to support
In-progress
Mostly quality-of-life improvements, especially when handling arrays.
- Improve Julia Arrays interop. from Nim.
- Supports complex Arrays
- map / apply / reduce /fold
Backlog
- Support Julia chaining syntax
- Add support for Enum types
Limitations
-
Value conversion Nim -> Julia are done by copy.
- Arrays are an exception to this rule and can be created from buffer / are accessible using a buffer.
-
Value conversion Julia -> Nim s always done by copy
- When using Arrays you can access the buffer as
ptr UncheckedArrayof the Julia Arrays withrawData(). - Using
to(seq[T])orto(Tensor[T])perform acopyMemofjlArray.rawData()in your seq/Tensor
- When using Arrays you can access the buffer as
-
Julia allocated arrays only goes up to 3 dimensions (but Arrays can be allocated in Nim)
-
Linux / WSL supports only
- Windows dynamic library linking is different than Linux.
- If you need Windows support, consider opening an issue or a PR :).
- Otherwise, just use WSL
Examples
Here is the basic example:
import nimjl
Julia.init() # Initialize Julia VM. Subsequent call will be ignored
var myval = 4.0'f64
# Call Julia function "sqrt" and convert the result to a float
var res = Julia.sqrt(myval).to(float64)
echo res # 2.0
Setting up Julia dependency
It is now possible to embed Julia files inside a Nim compiled binary to easily distribute Julia code. To make distribution possible, an API to call Pkg.add("...") has also been added with version number easy to specify.
import nimjl
Julia.init:
Pkg:
add(name="Polynomials", version="3.0.0")
add(name="LinearAlgebra")
add("DSP")
add(name="Wavelets", version="0.9.4")
Embed:
# embed all files with '*.jl' extension in folder ``JuliaToolBox/``
dir("JuliaToolBox/")
# embed all files with '*.jl' extension in the folder of he source file (at compilation) i.e. ``getProjectPath()``
thisDir()
# embed specific file; path should be relative to ``getProjectPath()``
file("localfile.jl")See examples/ex09_embed_file.nim for a concrete example
Take a look at tests/ or examples/ folder for typical examples.
License
This project is released under MIT License.