Contexts

Use a SolverContext to select the JCMsuite installation and runtime settings for jcmsuite.

When To Create A Context

You do not need to create a context manually. Create one when your script must use different JCMsuite installations or settings.

Default Context

If you do not provide a context, jcmsuite creates a default context. The default context uses the JCMROOT environment variable to find the installation.

Create A Context

Call create_context to create a context. The context uses the JCMROOT environment variable when jcm_root is not set.

from jcmsuite.context import create_context

context = create_context(jcm_root="/opt/jcmsuite/current")

The context accepts these main settings:

  • jcm_root: JCMsuite installation root.

  • backend_mode: preferred way of using solve: "local", "daemon", or "auto".

  • num_threads: thread-count hint for the solver.

  • memory_limit_mb: memory-limit hint for the solver.

  • gpus: local GPU selection. Use "all" or "no". You can also give one non-negative GPU identifier or a non-empty sequence of GPU identifiers.

  • temp_dir_root: base directory for temporary solve working directories.

  • debug: enable debug logging.

  • logging_level and log_dir: configure logging.

The context checks the installation path when you create it. If jcm_root or JCMROOT specifies a path, the path must be a directory. Runtime operations can raise RuntimeContextError when the required JCMsuite binaries are not available.

Use A Context

Choose one of these methods.

Pass A Context

Pass context= when one operation must use a specific context. The context= argument accepts a SolverContext:

import jcmsuite
from jcmsuite.context import create_context

context = create_context(jcm_root="/opt/jcmsuite/special_version")
jcmsuite.info(context=context)
# ...
#              JCMROOT : /opt/jcmsuite/special_version
# ...

Use A Context Scope

Use a with block when several operations must use the same context. The context is active only inside the indented block:

import jcmsuite
from jcmsuite.context import create_context

context = create_context(jcm_root="/opt/jcmsuite/special_version")

with context:
    jcmsuite.info()
# ...
#              JCMROOT : /opt/jcmsuite/special_version
# ...

The context is active only inside the with block. After the block, the previous context is active again.

Nested scopes are supported. When an inner scope ends, the outer context is restored.

Set A Default Context

Use set_default_context when the whole process must use a new context by default:

import jcmsuite
from jcmsuite.context import create_context, set_default_context

context = create_context(jcm_root="/opt/jcmsuite/special_version")
set_default_context(context)

jcmsuite.info()
# ...
#              JCMROOT : /opt/jcmsuite/special_version
# ...

The default context is process-wide.

Context Precedence

The context is resolved in this order:

  1. An explicit context= argument.

  2. The nearest active with scope.

  3. The process-wide default context.

  4. A default context created when it is first needed.

Use get_current_context() to inspect the active scoped context. Use get_default_context() to get the process-wide default context.

Inspect A Context

Use jcmsuite.info(context=...) to inspect installation, system, interface, and license information for a context:

details = jcmsuite.info(context=context, print=False)
print(details.installation.jcm_root)
print(details.installation.version)
print(details.system.n_cores)
print(details.license.period)

The context also exposes jcm_root and system directly. Installation and license details become available after the context performs its startup probe.

If the installation path is invalid, or the required JCMsuite binaries are unavailable, context creation or startup can raise RuntimeContextError. Check JCMROOT, the explicit jcm_root, and the installation before you retry the solve.

Contexts are also relevant to Info and Daemon Execution. See the Use Solver Contexts tutorial for a runnable example.