Skip to content

Commit

Permalink
Merge pull request #43 from Clonkk/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
Clonkk authored Apr 6, 2022
2 parents f1e21fd + 4bf388f commit 0c65227
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/ex06_tensor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion examples/ex09_embed_file.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
17 changes: 17 additions & 0 deletions examples/ex10_julia_threads.nim
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion nimjl.nimble
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
6 changes: 5 additions & 1 deletion nimjl/cores.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions nimjl/glucose.nim
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) =
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_embedressources.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import unittest
import nimjl

proc testEmbedRessources*() =
Julia.init:
Julia.init(1):
Embed:
file("embed.jl")
dir("assets/")
Expand Down
2 changes: 1 addition & 1 deletion tests/testfull.nim
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ when defined(checkMemLeak):
import std/os

when isMainModule:
Julia.init:
Julia.init(2):
Pkg: add("LinearAlgebra")

when defined(checkMemLeak):
Expand Down

0 comments on commit 0c65227

Please sign in to comment.