Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix juliacall multi-threading segmentation fault #102

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions examples/omeinsum_contractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
from typing import List, Set, Dict, Tuple
import tempfile
import warnings
import cotengra as ctg

# Prerequisites for running this example:
Expand All @@ -12,6 +13,10 @@
# Step 3: install juliacall via `pip install juliacall`, this example was tested with juliacall 0.9.9
# Step 4: install julia package `OMEinsum`, this example was tested with OMEinsum v0.7.2,
# see https://docs.julialang.org/en/v1/stdlib/Pkg/ for more details on julia's package manager
# Step 5: for julia multi-threading, set env variable PYTHON_JULIACALL_THREADS=<N|auto>.
# However, in order to use julia multi-threading in juliacall,
# we have to turn off julia GC at the risk of OOM.
# See see https://github.com/cjdoris/PythonCall.jl/issues/219 for more details.
from juliacall import Main as jl

jl.seval("using OMEinsum")
Expand Down Expand Up @@ -84,7 +89,21 @@ def __call__(
sc_weight=self.sc_weight,
rw_weight=self.rw_weight,
)

nthreads = jl.Threads.nthreads()
if nthreads > 1:
warnings.warn(
"Julia receives Threads.nthreads()={0}. "
"However, in order to use julia multi-threading in juliacall, "
"we have to turn off julia GC at the risk of OOM. "
"That means you may need a large memory machine. "
"Please see https://github.com/cjdoris/PythonCall.jl/issues/219 "
"for more details.".format(nthreads)
)
jl.GC.enable(False)
optcode = jl.OMEinsum.optimize_code(eincode, size_dict, algorithm)
if nthreads > 1:
jl.GC.enable(True)
# jl.println("time and space complexity computed by OMEinsum: ",
# jl.OMEinsum.timespace_complexity(optcode, size_dict))

Expand Down