Running MPI Calculations
Goal
Execute MPI-enabled Qrunch scripts on HPC clusters using containerized images with mpirun or Slurm job schedulers.
Prerequisites
Qrunch MPI container image (see Building an Image)
MPI-enabled Python script (see Using the MPI cuTensorNet Calculator)
Access to HPC cluster with: - MPI installation (OpenMPI or compatible) - Container runtime (Apptainer/Singularity or Enroot) - NVIDIA GPUs with CUDA drivers
Input files (molecule configuration, license file)
Overview
There are many different ways of submitting batch jobs. In the following, we discuss two common approaches for running MPI calculations with Qrunch containers:
Direct execution with mpirun + Apptainer: Launch MPI processes directly on allocated nodes
Batch submission with Slurm + Enroot: Submit jobs to the scheduler for queued execution
Both approaches launch multiple containerized MPI processes that run your Qrunch script with GPU access and MPI communication. Choose based on your cluster’s configuration and scheduler.
Running with mpirun and Apptainer
This approach uses mpirun to launch multiple Apptainer containers directly.
Basic Example
mpirun -n 4 \
apptainer exec --nv \
--bind /path/to/workspace:/workspace \
qrunch-mpi.sif \
uv run --python 3.12 /workspace/app/your_script.py
Key flags:
mpirun -n 4Launch 4 MPI processes (1 root + 3 workers)
apptainer execExecute a command inside the container
--nvEnable NVIDIA GPU support (passes through GPUs from host)
--bind /host/path:/container/pathMount host directory into container at specified path
Advanced Options
Add environment variables and resource limits for production runs:
mpirun -n 4 \
apptainer exec --nv \
--bind /path/to/workspace:/workspace \
--env QRUNCH_NUM_THREADS=10 \
--env QRUNCH_NO_GUARD=1 \
qrunch-mpi.sif \
uv run --python 3.12 /workspace/app/your_script.py
--env QRUNCH_NUM_THREADS=10Set CPU threads per MPI process (match to allocated cores per task)
--env QRUNCH_NO_GUARD=1Disable verbose logging guards for cleaner output
Note
If you encounter MPI compatibility issues, you can try binding the host’s OpenMPI into the container, though this may not always resolve the problem:
--bind /path/to/openmpi-5.0.10:/opt/openmpi-5.0.10 \
--env LD_LIBRARY_PATH=/opt/openmpi-5.0.10/lib:/opt/conda/envs/cuquantum/lib:/usr/local/cuda/lib64:/.singularity.d/libs
Multi-Node Example
For multi-node execution, specify hosts:
mpirun -n 8 -host node1:4,node2:4 \
apptainer exec --nv \
--bind /shared/workspace:/workspace \
qrunch-mpi.sif \
uv run --python 3.12 /workspace/app/your_script.py
This runs 4 processes on node1 and 4 processes on node2.
Common Environment Variables
Pass environment variables to the container with --env:
apptainer exec --nv \
--bind /path/to/workspace:/workspace \
--env QRUNCH_NUM_THREADS=10 \
--env QRUNCH_LOG_LEVEL=INFO \
qrunch-mpi.sif \
uv run --python 3.12 /workspace/app/your_script.py
QRUNCH_NUM_THREADSCPU threads per MPI process (set to match allocated cores per task)
QRUNCH_LOG_LEVELLogging verbosity:
DEBUG,INFO,WARNING, orERRORQRUNCH_NO_GUARDSet to
1to disable verbose logging guards
Running with Slurm and Enroot
On Slurm-managed HPC clusters, batch jobs are commonly submitted with sbatch, though other submission methods may be used depending on your cluster configuration. The exact setup depends on your cluster, so consult your cluster’s documentation for specific requirements. This example demonstrates a typical approach using sbatch.
The batch script uses srun to launch containerized MPI tasks. This approach integrates with the cluster scheduler for resource management and job queuing.
Basic Slurm Script
Create a batch script (e.g., run_calculation.sbatch):
#!/usr/bin/env bash
#SBATCH --job-name=qrunch_calculation
#SBATCH --nodes=2
#SBATCH --ntasks-per-node=4 # MPI tasks per node
#SBATCH --gpus-per-node=4 # GPUs per node
#SBATCH --cpus-per-task=10 # CPU cores per task
#SBATCH --time=00:10:00 # Wall time limit (HH:MM:SS)
#SBATCH --output=logs/%x-%j.out
#SBATCH --error=logs/%x-%j.err
set -euo pipefail
mkdir -p logs # Required for --output and --error paths
# Set threading to match CPUs per task
export SRUN_CPUS_PER_TASK=${SLURM_CPUS_PER_TASK:?SLURM_CPUS_PER_TASK is not set}
export QRUNCH_NUM_THREADS=${SRUN_CPUS_PER_TASK}
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/opt/conda/envs/cuquantum/lib:/usr/local/cuda/lib64"
export QRUNCH_NO_GUARD=1
export QRUNCH_LOG_LEVEL=INFO
srun \
--container-image=/path/to/qrunch-mpi.sqsh \
--container-mounts=/shared/workspace:/workspace \
--container-env=SRUN_CPUS_PER_TASK,QRUNCH_NUM_THREADS,LD_LIBRARY_PATH,QRUNCH_NO_GUARD,QRUNCH_LOG_LEVEL \
uv run --python 3.12 /workspace/app/your_script.py
Submit with:
sbatch run_calculation.sbatch
Environment Variables:
SRUN_CPUS_PER_TASKCopies Slurm’s CPU allocation for use inside the container. The
:?syntax ensures the script fails if the variable is not set.QRUNCH_NUM_THREADSControl threading for Qrunch first and third-party libraries. Should match
SLURM_CPUS_PER_TASKfor optimal CPU utilization.LD_LIBRARY_PATHLibrary search path including cuQuantum and CUDA libraries. Required for GPU-accelerated tensor operations.
QRUNCH_NO_GUARDSet to
1to disable verbose logging guards for cleaner output.QRUNCH_LOG_LEVELLogging verbosity:
DEBUG,INFO,WARNING, orERROR.
These variables are passed to the container via --container-env (comma-separated list).
Resource Allocation
Key Slurm parameters for MPI calculations:
--nodes=NNumber of compute nodes to allocate
--ntasks-per-node=TMPI processes per node. Typically matches GPUs per node (one MPI rank per GPU).
--gpus-per-node=GGPUs per node. Should match
--ntasks-per-nodefor one GPU per MPI process.--cpus-per-task=CCPU cores per MPI process. Set
QRUNCH_NUM_THREADSto this value in your script.
Example: 2 nodes × 4 GPUs/node × 10 cores/GPU = 8 total MPI processes
#SBATCH --nodes=2
#SBATCH --ntasks-per-node=4
#SBATCH --gpus-per-node=4
#SBATCH --cpus-per-task=10
Total resources: 8 MPI ranks, 8 GPUs, 80 CPU cores
srun Container Options
--container-image=PATHPath to container image (
.sqshfor Enroot,.sifmay also work depending on cluster configuration)--container-mounts=HOST:CONTAINERMount host filesystem into container (e.g.,
/scratch/data:/workspace)--container-env=VAR1,VAR2,...Comma-separated list of environment variables to pass into the container
Directory Structure
Organize your workspace for easy mounting:
workspace/
├── app/
│ └── your_script.py
├── input/
│ ├── molecule.xyz
│ └── license.txt
├── output/
└── data/
Mount the entire workspace into the container:
--bind /path/to/workspace:/workspace # for Apptainer
--container-mounts=/path/to/workspace:/workspace # for srun
Your script accesses files at:
molecule_path = Path("/workspace/input/molecule.xyz")
license_path = Path("/workspace/input/license.txt")
output_dir = Path("/workspace/output")
Performance Tuning
Thread Configuration
In your Slurm script, set threading environment variables to match SLURM_CPUS_PER_TASK:
export SRUN_CPUS_PER_TASK=${SLURM_CPUS_PER_TASK}
export QRUNCH_NUM_THREADS=${SRUN_CPUS_PER_TASK}
In your Python script, set the calculator’s threads parameter to match:
import os
OptimizerOptions(
samples=32, # More samples = better contraction paths
threads=os.environ["QRUNCH_NUM_THREADS"], # Or set explicitly to something else
slicing=SlicerOptions(
min_slices=size, # Match number of MPI ranks
slice_factor=2,
),
)
Troubleshooting
Container not found
The container path must be accessible from compute nodes. Use shared filesystem paths (e.g., /scratch, /shared), not login-node-local paths like /home/username.
MPI version mismatch errors
The Qrunch MPI image uses OpenMPI 5.0.8. If your cluster uses a different MPI version or implementation (MPICH, Intel MPI), you may encounter initialization errors. Contact Qrunch support for assistance with MPI compatibility.
GPUs not detected inside container
Apptainer: Ensure you use the
--nvflagSlurm: Verify
--gpus-per-nodein your batch script matches your allocationTest GPU visibility:
srun --container-image=/path/to/qrunch-mpi.sqsh nvidia-smi
Worker processes hang after calculation completes
The root rank must call contraction_calculator.shutdown() in a finally block. Without this, workers remain blocked in serve_forever(). See Using the MPI cuTensorNet Calculator for the correct pattern.
Permission denied errors on mounted directories
Create workspace directories with correct permissions before launching:
mkdir -p /scratch/workspace/{app,input,output,data}
chmod -R 755 /scratch/workspace
See Also
Building an Image - Build Qrunch MPI container images (Docker/Apptainer/Enroot)
Using the MPI cuTensorNet Calculator - Write MPI-enabled Qrunch scripts with proper root-worker architecture
Your HPC cluster’s user guide for cluster-specific Slurm and container runtime configuration