Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| technical:recipes:mpi4py-in-virtualenv [2025-01-31 15:14] – [VALET Package Definition] anita | technical:recipes:mpi4py-in-virtualenv [2025-01-31 15:29] (current) – [VALET Package Definition] anita | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Python Virtual Environments with mpi4py ====== | ||
| + | |||
| + | Most conda channels include copies of the mpi4py module to satisfy dependencies of MPI-parallelized packages. | ||
| + | |||
| + | ===== MPI and Conda Variants ===== | ||
| + | |||
| + | In this example we will build the virtual environment on Farber using the '' | ||
| + | |||
| + | <code bash> | ||
| + | $ vpkg_require openmpi/ | ||
| + | Adding dependency `ucx/1.9.0` to your environment | ||
| + | Adding package `openmpi/ | ||
| + | Adding package `anaconda/ | ||
| + | </ | ||
| + | |||
| + | <WRAP center round info 60%> | ||
| + | Due to recent announcements regarding Anaconda, and Intel dropping their distribution channel, any documentation referring to Intel' | ||
| + | |||
| + | Please use '' | ||
| + | </ | ||
| + | |||
| + | ===== Create a Directory Hierarchy ===== | ||
| + | |||
| + | We will be creating a Python virtual environment containing Numpy and Scipy libraries into which mpi4py will be added. | ||
| + | |||
| + | <code base> | ||
| + | $ mkdir -p ${HOME}/ | ||
| + | </ | ||
| + | |||
| + | Two things to note: | ||
| + | * As written the directory hierarchy is created in the user's home directory; '' | ||
| + | * The current date is used as a version identifier; using the format '' | ||
| + | The directory structure will lend '' | ||
| + | |||
| + | ===== Farber ===== | ||
| + | |||
| + | ==== Create the Virtual Environment ==== | ||
| + | |||
| + | The virtual environment is first populated with all packages that **do not** require mpi4py. | ||
| + | |||
| + | <WRAP center round important 60%> | ||
| + | The two channel options are present to ensure only the default Anaconda channels are consulted -- otherwise the command could still pick packages from the Intel channel, for example, which would still have the binary compatibility issues! | ||
| + | </ | ||
| + | |||
| + | <code bash> | ||
| + | $ conda create --prefix ${HOME}/ | ||
| + | Solving environment: | ||
| + | : | ||
| + | Proceed ([y]/n)? y | ||
| + | : | ||
| + | Preparing transaction: | ||
| + | Verifying transaction: | ||
| + | Executing transaction: | ||
| + | # | ||
| + | # To activate this environment, | ||
| + | # > source activate / | ||
| + | # | ||
| + | # To deactivate an active environment, | ||
| + | # > source deactivate | ||
| + | # | ||
| + | </ | ||
| + | |||
| + | Before building and installing mpi4py the environment needs to be activated: | ||
| + | |||
| + | <code bash> | ||
| + | $ source activate / | ||
| + | (/ | ||
| + | </ | ||
| + | |||
| + | ==== Building mpi4py ==== | ||
| + | |||
| + | With the new virtual environment activated, we can now build mpi4py against the local Open MPI library we added to the shell environment. | ||
| + | |||
| + | <code base> | ||
| + | (/ | ||
| + | Collecting mpi4py | ||
| + | Using cached mpi4py-3.0.3.tar.gz (1.4 MB) | ||
| + | Skipping wheel build for mpi4py, due to binaries being disabled for it. | ||
| + | Installing collected packages: mpi4py | ||
| + | Running setup.py install for mpi4py ... done | ||
| + | Successfully installed mpi4py-3.0.3 | ||
| + | </ | ||
| + | |||
| + | The '' | ||
| + | |||
| + | <code bash> | ||
| + | (/ | ||
| + | mpi4py | ||
| + | </ | ||
| + | |||
| + | Additional packages that require mpi4py can now be installed into the environment. | ||
| + | |||
| + | ==== VALET Package Definition ==== | ||
| + | |||
| + | The new virtual environment can easily be added to your login shell and job runtime environments using VALET. | ||
| + | |||
| + | <code bash> | ||
| + | $ mkdir -p ${HOME}/ | ||
| + | $ echo ${HOME}/ | ||
| + | / | ||
| + | </ | ||
| + | |||
| + | Take note of the path echoed, then create a new file named '' | ||
| + | |||
| + | <code json> | ||
| + | { " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | { " | ||
| + | " | ||
| + | } | ||
| + | ], | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Please note: | ||
| + | - The '' | ||
| + | - We do not need to tell VALET the full path to each version; the version identifier **is** the subdirectory or '' | ||
| + | - If you choose a different version of Open MPI or Anaconda, alter the '' | ||
| + | - New versions of this project are appended to the '' | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | }, | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | === Using the Virtual Environment === | ||
| + | |||
| + | The versions of the virtual environment declared in the VALET package are listed using the '' | ||
| + | |||
| + | <code bash> | ||
| + | $ vpkg_versions my-sci-app | ||
| + | Available versions in package (* = default version): | ||
| + | |||
| + | [/ | ||
| + | my-sci-app | ||
| + | * 20201102 | ||
| + | </ | ||
| + | |||
| + | Activating the virtual environment is accomplished using the '' | ||
| + | |||
| + | <code bash> | ||
| + | $ vpkg_require my-sci-app/ | ||
| + | Adding dependency `ucx/1.9.0` to your environment | ||
| + | Adding dependency `openmpi/ | ||
| + | Adding dependency `anaconda/ | ||
| + | Adding package `my-sci-app/ | ||
| + | (/ | ||
| + | ~/ | ||
| + | (/ | ||
| + | mpi4py | ||
| + | $ which mpirun | ||
| + | / | ||
| + | </ | ||
| + | |||
| + | ===== Caviness ===== | ||
| + | |||
| + | The steps for completing this work on Caviness are similar to those presented for Farber and of course following the first part to [[technical: | ||
| + | |||
| + | <code bash> | ||
| + | $ vpkg_require openmpi/ | ||
| + | Adding dependency `libfabric/ | ||
| + | Adding dependency `binutils/ | ||
| + | Adding dependency `gcc/ | ||
| + | Adding package `openmpi/ | ||
| + | Adding package `anaconda/ | ||
| + | </ | ||
| + | |||
| + | ==== Create the Virtual Environment ==== | ||
| + | |||
| + | The virtual environment is first populated with all packages that **do not** require mpi4py. | ||
| + | |||
| + | <code bash> | ||
| + | $ conda create --prefix ${HOME}/ | ||
| + | Collecting package metadata (current_repodata.json): | ||
| + | Solving environment: | ||
| + | : | ||
| + | Proceed ([y]/n)? y | ||
| + | : | ||
| + | # | ||
| + | # To activate this environment, | ||
| + | # | ||
| + | # $ conda activate / | ||
| + | # | ||
| + | # To deactivate an active environment, | ||
| + | # | ||
| + | # $ conda deactivate | ||
| + | |||
| + | </ | ||
| + | |||
| + | Before building and installing mpi4py the environment needs to be activated: | ||
| + | |||
| + | <code bash> | ||
| + | $ conda activate / | ||
| + | (/ | ||
| + | </ | ||
| + | |||
| + | ==== Building mpi4py ==== | ||
| + | |||
| + | With the new virtual environment activated, we can now build mpi4py against the local Open MPI library we added to the shell environment. Due to Anaconda trying to use a version of '' | ||
| + | |||
| + | <code base> | ||
| + | (/ | ||
| + | (/ | ||
| + | Collecting mpi4py | ||
| + | Using cached mpi4py-4.0.1.tar.gz (466 kB) | ||
| + | Skipping wheel build for mpi4py, due to binaries being disabled for it. | ||
| + | Installing collected packages: mpi4py | ||
| + | Running setup.py install for mpi4py ... done | ||
| + | Successfully installed mpi4py-4.0.1 | ||
| + | </ | ||
| + | |||
| + | The '' | ||
| + | |||
| + | <code bash> | ||
| + | (/ | ||
| + | mpi4py | ||
| + | </ | ||
| + | |||
| + | Additional packages that require mpi4py can now be installed into the environment. | ||
| + | |||
| + | ==== VALET Package Definition ==== | ||
| + | |||
| + | The new virtual environment can easily be added to your login shell and job runtime environments using VALET. | ||
| + | |||
| + | <code bash> | ||
| + | $ mkdir -p ${HOME}/ | ||
| + | $ echo ${HOME}/ | ||
| + | / | ||
| + | </ | ||
| + | |||
| + | Take note of the path echoed, then create a new file named '' | ||
| + | |||
| + | <code yaml> | ||
| + | my-sci-app: | ||
| + | prefix: / | ||
| + | description: | ||
| + | flags: | ||
| + | - no-standard-paths | ||
| + | actions: | ||
| + | - action: source | ||
| + | script: | ||
| + | sh: anaconda-activate.sh | ||
| + | order: failure-first | ||
| + | success: 0 | ||
| + | versions: | ||
| + | " | ||
| + | description: | ||
| + | dependencies: | ||
| + | - openmpi/ | ||
| + | - anaconda/ | ||
| + | </ | ||
| + | |||
| + | === Using the Virtual Environment === | ||
| + | |||
| + | The versions of the virtual environment declared in the VALET package are listed using the '' | ||
| + | |||
| + | <code bash> | ||
| + | $ vpkg_versions my-sci-app | ||
| + | |||
| + | Available versions in package (* = default version): | ||
| + | |||
| + | [/ | ||
| + | my-sci-app | ||
| + | * 20201102 | ||
| + | </ | ||
| + | |||
| + | Activating the virtual environment is accomplished using the '' | ||
| + | |||
| + | <code bash> | ||
| + | $ vpkg_require my-sci-app/ | ||
| + | Adding dependency `libfabric/ | ||
| + | Adding dependency `binutils/ | ||
| + | Adding dependency `gcc/ | ||
| + | Adding package `openmpi/ | ||
| + | Adding package `anaconda/ | ||
| + | Adding package `my-sci-app/ | ||
| + | (/ | ||
| + | ~/ | ||
| + | (/ | ||
| + | mpi4py | ||
| + | $ which mpirun | ||
| + | / | ||
| + | </ | ||
| + | |||
| + | ===== DARWIN ===== | ||
| + | |||
| + | The steps for completing this work on DARWIN are similar to those presented for Caviness and of course following the first part to [[technical: | ||
| + | |||
| + | <code bash> | ||
| + | $ vpkg_require openmpi/ | ||
| + | Adding dependency `gcc/ | ||
| + | Adding dependency `ucx/ | ||
| + | Adding package `openmpi/ | ||
| + | Adding package `anaconda/ | ||
| + | </ | ||
| + | |||
| + | ==== Create the Virtual Environment ==== | ||
| + | |||
| + | The virtual environment is first populated with all packages that **do not** require mpi4py. | ||
| + | |||
| + | <code bash> | ||
| + | $ conda create --prefix ${HOME}/ | ||
| + | Collecting package metadata (current_repodata.json): | ||
| + | Solving environment: | ||
| + | : | ||
| + | Proceed ([y]/n)? y | ||
| + | : | ||
| + | # | ||
| + | # To activate this environment, | ||
| + | # | ||
| + | # $ conda activate / | ||
| + | # | ||
| + | # To deactivate an active environment, | ||
| + | # | ||
| + | # $ conda deactivate | ||
| + | |||
| + | </ | ||
| + | |||
| + | Before building and installing mpi4py the environment needs to be activated: | ||
| + | |||
| + | <code bash> | ||
| + | $ conda activate / | ||
| + | (/ | ||
| + | </ | ||
| + | |||
| + | ==== Building mpi4py ==== | ||
| + | |||
| + | With the new virtual environment activated, we can now build mpi4py against the local Open MPI library we added to the shell environment. | ||
| + | |||
| + | <code base> | ||
| + | (/ | ||
| + | (/ | ||
| + | $ pip install --no-binary :all: --compile mpi4py | ||
| + | Collecting mpi4py | ||
| + | Downloading mpi4py-4.0.1.tar.gz (466 kB) | ||
| + | Installing build dependencies ... done | ||
| + | Getting requirements to build wheel ... done | ||
| + | Installing backend dependencies ... done | ||
| + | Preparing metadata (pyproject.toml) ... done | ||
| + | Building wheels for collected packages: mpi4py | ||
| + | Building wheel for mpi4py (pyproject.toml) ... done | ||
| + | Created wheel for mpi4py: filename=mpi4py-4.0.1-cp313-cp313-linux_x86_64.whl size=997834 sha256=b09b4fe26c8aa940bdcbdb512960fb73edb9ed9ed698b9455db3e1f3d5b078a5 | ||
| + | Stored in directory: / | ||
| + | Successfully built mpi4py | ||
| + | Installing collected packages: mpi4py | ||
| + | Successfully installed mpi4py-4.0.1 | ||
| + | </ | ||
| + | |||
| + | The '' | ||
| + | |||
| + | <code bash> | ||
| + | (/ | ||
| + | mpi4py | ||
| + | </ | ||
| + | |||
| + | Additional packages that require mpi4py can now be installed into the environment. | ||
| + | |||
| + | ==== VALET Package Definition ==== | ||
| + | |||
| + | The new virtual environment can easily be added to your login shell and job runtime environments using VALET. | ||
| + | |||
| + | <code bash> | ||
| + | $ mkdir -p ${HOME}/ | ||
| + | $ echo ${HOME}/ | ||
| + | / | ||
| + | </ | ||
| + | |||
| + | Take note of the path echoed, then create a new file named '' | ||
| + | |||
| + | <code yaml> | ||
| + | my-sci-app: | ||
| + | prefix: / | ||
| + | description: | ||
| + | flags: | ||
| + | - no-standard-paths | ||
| + | actions: | ||
| + | - action: source | ||
| + | script: | ||
| + | sh: anaconda-activate.sh | ||
| + | order: failure-first | ||
| + | success: 0 | ||
| + | versions: | ||
| + | " | ||
| + | description: | ||
| + | dependencies: | ||
| + | - openmpi/ | ||
| + | - anaconda/ | ||
| + | </ | ||
| + | |||
| + | === Using the Virtual Environment === | ||
| + | |||
| + | The versions of the virtual environment declared in the VALET package are listed using the '' | ||
| + | |||
| + | <code bash> | ||
| + | $ vpkg_versions my-sci-app | ||
| + | |||
| + | Available versions in package (* = default version): | ||
| + | |||
| + | [/ | ||
| + | my-sci-app | ||
| + | * 20250125 | ||
| + | </ | ||
| + | |||
| + | Activating the virtual environment is accomplished using the '' | ||
| + | |||
| + | <code bash> | ||
| + | $ vpkg_require my-sci-app/ | ||
| + | Adding dependency `gcc/ | ||
| + | Adding dependency `ucx/ | ||
| + | Adding dependency `openmpi/ | ||
| + | Adding dependency `anaconda/ | ||
| + | Adding package `my-sci-app/ | ||
| + | (/ | ||
| + | ~/ | ||
| + | (/ | ||
| + | / | ||
| + | </ | ||