diff --git a/examples/ex06_tensor.nim b/examples/ex06_tensor.nim index 2397c3e..fdca7fc 100644 --- a/examples/ex06_tensor.nim +++ b/examples/ex06_tensor.nim @@ -2,7 +2,7 @@ import arraymancer import nimjl proc main() = - Julia.init: + Julia.init(1): Pkg: add "LinearAlgebra" # Initialize Julia VM. This should be done once in the lifetime of your program. diff --git a/examples/ex09_embed_file.nim b/examples/ex09_embed_file.nim index d306a3b..afa3c4e 100644 --- a/examples/ex09_embed_file.nim +++ b/examples/ex09_embed_file.nim @@ -27,7 +27,8 @@ proc main_2() = proc main_1() = # Idiomatic way to embed Julia ressources and call them during after VM Init - Julia.init: + # The int argument is the number of threads used by the Julia VM + Julia.init(2): # Install package at init Pkg: add("LinearAlgebra") diff --git a/examples/ex10_julia_threads.nim b/examples/ex10_julia_threads.nim new file mode 100644 index 0000000..304bbe6 --- /dev/null +++ b/examples/ex10_julia_threads.nim @@ -0,0 +1,17 @@ +import nimjl +import std/os + +proc main() = + # This is the other syntax with dependencies + # It is strictly equivalent to + # Julia.init(4) + # Calling Julia.init() is equivalent to calling Julia.init(1) + Julia.init(4): + Pkg: add("LinearAlgebra") + + let Threads = jlGetModule("Threads") + echo Threads.nthreads() + Julia.exit() + +when isMainModule: + main() diff --git a/nimjl.nimble b/nimjl.nimble index 9b6e3c4..bc0dd39 100644 --- a/nimjl.nimble +++ b/nimjl.nimble @@ -1,6 +1,6 @@ # Nimjl # Licensed and distributed under MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT). -version = "0.7.1" +version = "0.7.2" author = "Regis Caillaud" description = "Nim Julia bridge" license = "MIT" diff --git a/nimjl/cores.nim b/nimjl/cores.nim index 0e271e5..fbc2bd5 100644 --- a/nimjl/cores.nim +++ b/nimjl/cores.nim @@ -9,7 +9,7 @@ proc jlSym*(symname: string): JlSym = proc jlExceptionHandler*() = let excpt: JlValue = jl_exception_occurred() - ## Convert a Julia exception to Nim exception + ## Convert a Julia exception to Nim exception if not isNil(excpt): let msg = $(jl_exception_message()) raise newException(JlError, msg) @@ -92,6 +92,10 @@ proc jlVmInit*() = return # raise newException(JlError, "jl_init() must be called once per process") +proc jlVmInit*(nthreads: int) = + putEnv("JULIA_NUM_THREADS", $nthreads) + jlVmInit() + # Not exported for now because I don't know how it works proc jlVmInit(pathToImage: string) {.used.} = ## Same as jlVmInit but with a pre-compiler image diff --git a/nimjl/glucose.nim b/nimjl/glucose.nim index ed09239..03adbf1 100644 --- a/nimjl/glucose.nim +++ b/nimjl/glucose.nim @@ -1,5 +1,6 @@ # This file is named glucose because it gives you sugar ;) # It contains most syntactic sugar to ease using Julia inside Nim +import std/os import ./types import ./cores import ./functions @@ -9,10 +10,10 @@ import ./private/jlcores type Julia* = object -proc init*(jl: type Julia) = - jlVmInit() +proc init*(jl: type Julia, nthreads: int = 1) = + jlVmInit(nthreads) -template init*(jl: type Julia, body: untyped) = +template init*(jl: type Julia, nthreads: int, body: untyped) = ## Init Julia VM var packages: seq[string] template Pkg(innerbody: untyped) = @@ -42,6 +43,7 @@ template init*(jl: type Julia, body: untyped) = # Don't do anything if Julia is already initialized if not jlVmIsInit(): + putEnv("JULIA_NUM_THREADS", $nthreads) jl_init() # Module installation Julia.useModule("Pkg") diff --git a/tests/test_embedressources.nim b/tests/test_embedressources.nim index 54aa472..e8bdec7 100644 --- a/tests/test_embedressources.nim +++ b/tests/test_embedressources.nim @@ -2,7 +2,7 @@ import unittest import nimjl proc testEmbedRessources*() = - Julia.init: + Julia.init(1): Embed: file("embed.jl") dir("assets/") diff --git a/tests/testfull.nim b/tests/testfull.nim index 70e5d3d..a1fbf52 100644 --- a/tests/testfull.nim +++ b/tests/testfull.nim @@ -199,7 +199,7 @@ when defined(checkMemLeak): import std/os when isMainModule: - Julia.init: + Julia.init(2): Pkg: add("LinearAlgebra") when defined(checkMemLeak):