Skip to content

Commit 0c65227

Browse files
authored
Merge pull request #43 from Clonkk/devel
Devel
2 parents f1e21fd + 4bf388f commit 0c65227

8 files changed

+33
-9
lines changed

examples/ex06_tensor.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import arraymancer
22
import nimjl
33

44
proc main() =
5-
Julia.init:
5+
Julia.init(1):
66
Pkg:
77
add "LinearAlgebra"
88
# Initialize Julia VM. This should be done once in the lifetime of your program.

examples/ex09_embed_file.nim

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ proc main_2() =
2727

2828
proc main_1() =
2929
# Idiomatic way to embed Julia ressources and call them during after VM Init
30-
Julia.init:
30+
# The int argument is the number of threads used by the Julia VM
31+
Julia.init(2):
3132
# Install package at init
3233
Pkg:
3334
add("LinearAlgebra")

examples/ex10_julia_threads.nim

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import nimjl
2+
import std/os
3+
4+
proc main() =
5+
# This is the other syntax with dependencies
6+
# It is strictly equivalent to
7+
# Julia.init(4)
8+
# Calling Julia.init() is equivalent to calling Julia.init(1)
9+
Julia.init(4):
10+
Pkg: add("LinearAlgebra")
11+
12+
let Threads = jlGetModule("Threads")
13+
echo Threads.nthreads()
14+
Julia.exit()
15+
16+
when isMainModule:
17+
main()

nimjl.nimble

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Nimjl
22
# Licensed and distributed under MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
3-
version = "0.7.1"
3+
version = "0.7.2"
44
author = "Regis Caillaud"
55
description = "Nim Julia bridge"
66
license = "MIT"

nimjl/cores.nim

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ proc jlSym*(symname: string): JlSym =
99

1010
proc jlExceptionHandler*() =
1111
let excpt: JlValue = jl_exception_occurred()
12-
## Convert a Julia exception to Nim exception
12+
## Convert a Julia exception to Nim exception
1313
if not isNil(excpt):
1414
let msg = $(jl_exception_message())
1515
raise newException(JlError, msg)
@@ -92,6 +92,10 @@ proc jlVmInit*() =
9292
return
9393
# raise newException(JlError, "jl_init() must be called once per process")
9494

95+
proc jlVmInit*(nthreads: int) =
96+
putEnv("JULIA_NUM_THREADS", $nthreads)
97+
jlVmInit()
98+
9599
# Not exported for now because I don't know how it works
96100
proc jlVmInit(pathToImage: string) {.used.} =
97101
## Same as jlVmInit but with a pre-compiler image

nimjl/glucose.nim

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# This file is named glucose because it gives you sugar ;)
22
# It contains most syntactic sugar to ease using Julia inside Nim
3+
import std/os
34
import ./types
45
import ./cores
56
import ./functions
@@ -9,10 +10,10 @@ import ./private/jlcores
910

1011
type Julia* = object
1112

12-
proc init*(jl: type Julia) =
13-
jlVmInit()
13+
proc init*(jl: type Julia, nthreads: int = 1) =
14+
jlVmInit(nthreads)
1415

15-
template init*(jl: type Julia, body: untyped) =
16+
template init*(jl: type Julia, nthreads: int, body: untyped) =
1617
## Init Julia VM
1718
var packages: seq[string]
1819
template Pkg(innerbody: untyped) =
@@ -42,6 +43,7 @@ template init*(jl: type Julia, body: untyped) =
4243

4344
# Don't do anything if Julia is already initialized
4445
if not jlVmIsInit():
46+
putEnv("JULIA_NUM_THREADS", $nthreads)
4547
jl_init()
4648
# Module installation
4749
Julia.useModule("Pkg")

tests/test_embedressources.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import unittest
22
import nimjl
33

44
proc testEmbedRessources*() =
5-
Julia.init:
5+
Julia.init(1):
66
Embed:
77
file("embed.jl")
88
dir("assets/")

tests/testfull.nim

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ when defined(checkMemLeak):
199199
import std/os
200200

201201
when isMainModule:
202-
Julia.init:
202+
Julia.init(2):
203203
Pkg: add("LinearAlgebra")
204204

205205
when defined(checkMemLeak):

0 commit comments

Comments
 (0)