# Contexts Use a {class}`SolverContext ` to select the JCMsuite installation and runtime settings for `jcmsuite`. ```{admonition} When To Create A Context :class: note 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 {func}`create_context ` to create a context. The context uses the `JCMROOT` environment variable when `jcm_root` is not set. ```python 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 {class}`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 {class}`SolverContext `: ```python 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: ```python 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 {func}`set_default_context ` when the whole process must use a new context by default: ```python 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 {func}`get_current_context() ` to inspect the active scoped context. Use {func}`get_default_context() ` to get the process-wide default context. ## Inspect A Context Use {func}`jcmsuite.info(context=...) ` to inspect installation, system, interface, and license information for a context: ```python 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 {attr}`jcm_root ` and {attr}`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 {class}`RuntimeContextError `. Check `JCMROOT`, the explicit `jcm_root`, and the installation before you retry the solve. Contexts are also relevant to [Info](../core/info.md) and [Daemon Execution](daemon/index.md). See the [Use Solver Contexts tutorial](../tutorials/execution_runtime/contexts.md) for a runnable example.