# Build VTK with Python Bindings and CUDA, TBB, & MPI on Windows

**URL:** https://discourse.vtk.org/t/build-vtk-with-python-bindings-and-cuda-tbb-mpi-on-windows/6468
**Category:** Support
**Created:** [August 25, 2021, 11:22pm UTC](https://discourse.vtk.org/t/build-vtk-with-python-bindings-and-cuda-tbb-mpi-on-windows/6468 "2021-08-25T23:22:43Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![adamgranthendry](https://discourse.vtk.org/user_avatar/discourse.vtk.org/adamgranthendry/32/3853_2.png) [@adamgranthendry](https://discourse.vtk.org/u/adamgranthendry)
#### Post date: [August 25, 2021, 11:22pm UTC](https://discourse.vtk.org/t/build-vtk-with-python-bindings-and-cuda-tbb-mpi-on-windows/6468/1 "2021-08-25T23:22:43Z")

</div>

### Problem

I cannot load `VTK` into `python` on `Windows` with support for `CUDA`, `MPI`, and `TBB`. Volume rendering is unreasonably slow for production with the default PyPI distribution (hours or more) and I need to leverage my hardware to speed up computations.

I have successfully built `VTK 9.3.0` for `Python 3.8.10-x64` with `CMake` and `Visual Studio` and have `bin`, `lib`, `include`, and `share` folders and have added these to `PATH`, but Python does not see `vtk`:

```auto
Python 3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import vtk
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'vtk'
>>>

```

I attempted to place the generated `Lib/site-packages`, `bin`, `lib`, and `include` folders in appropriate places in `C:\Program Files\Python38`, but I still get a `DLL` load error:

```auto
> py -3.8
Python 3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import vtk
Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\site-packages\vtkmodules\ __init__.py", line 13, in <module>
    from . import vtkCommonCore
ImportError: DLL load failed while importing vtkCommonCore: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Python38\lib\site-packages\vtk.py", line 30, in <module>
    all_m = importlib.import_module('vtkmodules.all')
  File "C:\Program Files\Python38\lib\importlib\ __init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Program Files\Python38\lib\site-packages\vtkmodules\ __init__.py", line 15, in <module>
    import _vtkmodules_static
ModuleNotFoundError: No module named '_vtkmodules_static'
>>>

```

### Setup

* * *

**OS:** Windows 10 Enterprise, x64-bit, Build 1909  
**CPU:** 2x Intel(R) Xeon(R) Gold 6248R  
**Disk:** 2TB NVMe M.2 SSD  
**RAM:** 192 GB DDR4  
**Compute GPUs:** 2x NVIDIA Quadro RTX8000 in TCC mode with NVLink  
**Display GPU:** 1x NVIDIA Quadro RTX4000  
**Visual Studio:** [Visual Studio 16 2019 Community][1]  
**Windows SDK:** [10.0.19041.0][2], targeting 10.0.18363  
**Python:** [3.8.10 x64-bit][3]  
**CMake:** [3.21.1][4] (_I use `CMake-GUI`_)  
**vcpkg:** [2021-08-12][5]  
**VTK Source:** [vtk-9.0.3-cp38-cp38-win\_amd64][6] (download from VTK website; not GitHub master)  
**VTKPythonPackage:** [GitHub Link][7] (_…not sure if I need this…_)  
**CUDA Toolkit:** [11.4][8]  
**TBB:** [2021.3.0][9] (via Intel(R) oneAPI Base Toolkit)  
**MPI:** [2021.3.0][9] (via Intel(R) oneAPI HPC Toolkit)

* * *

**NOTE:** _If desired, Embree, OSPRay, and OpenVKL are available in the Intel(R) oneAPI Rendering Toolkit_

### Preparation

* * *

_ **NOTE:** _ _There are **A LOT** of programs to be downloaded, installed, and configured, and they are important for proper building. In an attempt to shorten this post to an MRE (Minimal Reproducable Example), please go straight to the `Steps to Reproduce` section._

_ **NOTE:** _ _When setting Environment Variables, you may get an error that a PATH length greater than 2047 is not permitted. To overcome this hurdle, it is simplest to edit the path directly in the Windows Registry. Open `regedit` as an Admin, navigate to_

```auto
Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

```

_and set the `Path` variable there._

_ **TIP:** _ _To edit `PATH`, I copy and paste from the Registry into Word, then selected and replace `;`'s with paragraph endings `^p` by selecting `Ctrl+H` and then using the `Replace` tool as follows:_

[![Find and Replace][10]][10]

_When finished, I simply reverse the changes by replacing `^p`’s with `;`s. This is simpler for me than struggling with regular expressions._

* * *

#### 1. Install Visual Studio

1. Download the [Visual Studio Installer][1]

2. Run the installer, choose `Visual Studio Community 2019`, and select `Desktop development with C++`. Ensure the following are selected in `Individual Components`

- `Windows 10 SDK`
- `C++ ATL for latest...`
- `C++ MFC for latest...`
- `C++ CMake tools for Windows`

_ **NOTE:** _ (For me on `Windows 10` with `Visual Studio 2019`, these are `v142`)

1. Ensure the path to the corresponding `bin`, `lib`, and `include` folders are in the `PATH` of `System Environment Variables`. These can be either:

```auto
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\lib
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include

```

and/or

```auto
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64\
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\lib\x64\
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include

```

_ **NOTE:** _ (I recommend the latter at a minimum. )

1. Ensure the following additional `System Environment Variables` have been set by the installer:

```auto
VS140COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\
VS160COMCOMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\
VS2019INSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
VSSDK140INSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio 14.0\VSSDK\

```

_ **NOTE:** _ _Even though the installer downloads these to `Program Files (x86)`, they are in fact the `x64-bit` programs (I know…Windows is weird…)_

#### 2. Install CUDA

1. Download and run `cuda_11.4.1_471.41_win10.exe` ([link][11])

_ **TIP:** _ _The local installer will take longer to download, but your installs will run faster. I recommend saving the installer for the future anyway in case your CUDA Toolkit becomes corrupted or you need to install on another machine._

1. Ensure the following are added to `PATH` in `System Environment Variables`

```auto
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.4\libnvvp

```

_ **ASIDE:** _ _You **MUST** use version 11.4 for `VTK 9`. However, as an aside, when using 10.2 in CMake, if CMake cannot find CUDA, this is probably because the CUDA Toolkit 10.2 installer for some reason does not copy the `Visual Studio MSBuild Extensions` over to your Visual Studio folder. Copy the files at_

```auto
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\extras\visual_studio_integration\MSBuildExtensions\

```

_to_

```auto
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\VC\VCTargets\BuildCustomizations

```

_ **NOTE:** _ _If you want to run your GPU in TCC mode ( **WARNING:** This disables graphics output), and/or if you have two GPUs connected via NVLink and want to activate NVLink, run the following command in command prompt:_

```auto
> nvidia-smi -i 0 -dm 1
> nvidia-smi -i 1 -dm 1

```

_Assuming you have two GPUs with ID’s 0 and 1 you want to run in TCC mode. (To check, simply run `nvidia-smi` and see which GPUs you want to activate in TCC mode with the `-dm` switch). The [PugetSystems Instructions][12] have change with CUDA 11. NVIDIA’s newer drivers, which ship with the NVIDIA Control Panel, no longer have the `Configure SLI, Surround, PhysX` under `3D Settings`, particularly if using a Quadro card rather than a GeForce card. Instead, NVLink will be activated once TCC mode is set, which disables graphics output. The program they have provided only works for CUDA 10, so you will get an error with it when using CUDA 11 even though it is enabled._

_ **NOTE:** _ _If you get an error “Intel Graphics Driver for Windows not found”, ignore it. You only need the drivers for the CPU._

_ **NOTE:** _ _Don’t add more than the `/lib/` and `/libnvvp/` folders to path. CMake might not be able to find CUDA if you do. If you have to add other folders (e.g. `CUPTI`), I recommend the `lib/` and `libnvvp/` folder appear **FIRST** in PATH._

#### 3. Install `CMake`

1. Download and install [CMake][4] (…that was easy)

#### 4. Install `vcpkg`

1. Download and configure `vcpkg`:

```bash
cd C:\
git clone https://github.com/microsoft/vcpkg
cd vcpkg
.\bootstrap-vcpkg.bat
vcpkg integrate install

```

1. Set the default triplet in the `System Environment Variables` to `x64-windows`:

```auto
VCPKG_DEFAULT_TRIPLET=x64-windows

```

1. Install ports using `vcpkg install`. Press `Configure` in CMake and every time it cannot find a dependency you want installed, search for the dependency with `vcpkg search` and install it with `vcpkg install` (to remove a package, use `vcpkg remove`).\_

_ **NOTE:** _ _ **BE AWARE** that depending on what you install, these dependencies take **A LOT** of space and take **A LONG TIME** to download._

#### 5. Install `Intel oneAPI Toolkits`

_ **NOTE:** _ _These include `TBB`, `MPI`, `OSPRay`, and the Intel Fortran compiler `ifort.exe`, which you need._

1. Intel(R) oneAPI Base Toolkit
2. Intel(R) oneAPI HPC Toolkit
3. Intel(R) oneAPI Rendering Toolkit (_for Embree, OSPRay, and OpenVKL, optionally_)

and add the corresponding `lib`, `bin`, and `include` folders to `PATH`.

#### 6. Install `Intel SPMD Compiler`

1. [Intel(R) Implicit SPMD Program Compiler][13]

#### 7. Additional Dependencies with Installers

_ **NOTE:** _ _You may not need or want need these. I am adding them here for completeness. If CMake complains about not finding these, I recommend installing them._

1. [Nullsoft Scriptable Install System][14]
2. [WiX Toolset][15]
3. [MySQL Community (GPL) Installer for Windows][16]
4. [MySQL ODBC Connector][17]
5. [Strawberry Perl][18]

#### 8. Dependencies that are Unsuccessful on Windows

_ **NOTE:** _ _I have tried installing binaries for these and adding them to `PATH`, but they just don’t seem to work on Windows.._

_ **NOTE:** _ _For each of these, you can also try downloading corresponding python 3.8 zip source file (on `GitHub, under `Releases`, and on `conda forge`, under `Files`) to a folder on `C:` and then add the subfolders `bin`, `lib`, `include` (and `share` if available) to the `PATH` `System Environment Variable`._

1. [OpenTurns 1.17][19]
2. [GDAL 3.3.1][20]

### Steps to Reproduce

1. I set my `PATH` first. `VTK` will make `bin`, `include`, `lib`, and `share` folders, so I make sure these are at the front of `PATH`. Here is what I have (_ **HINT:** _ Refer to setting `PATH` in `regedit` to avoid the Windows error of PATH length greater than 2047 characters in the Preparation section above)

2. Open CMake as an Administrator

3. Set the Source folder to `C:\VTK\src` and Build folder to `C:\VTK\build`

4. Press `Configure` and select `Visual Studio 16 2019` as the generator with `x64` as the optional platform:  
[![CMake01][21]][21]

**NOTE:** _I do not know if libraries for VTK and CUDA should be built for 32-bit. I am using 64-bit as my best guess._

1. After Configuration completes, you will see many variables, which is understandable overwhelming and it is unclear what the differences are between `DEFAULT`, `YES`, `NO`, `WANT`, and `DON'T WANT`. Unfortunately, the answer is hidden tucked away in VTK’s CMake Module API Doxygen docs

[VTK CMake Module API][22]

[![Enable Status Values][23]][23]

For now, I leave everything default and edit the following CMake variables:

_ **TIP:** _ _Check the `Grouped` and `Advanced` check boxes next to the `Add Entry` button to see all variables grouped by corresponding project/module:_

```auto
CMAKE_CXX_MP_FLAG Checked
CMAKE_INSTALL_PREFIX C:/VTK
VTK_GROUP_ENABLE_Imaging WANT
VTK_GROUP_ENABLE_MPI WANT
VTK_GROUP_ENABLE_Qt WANT
VTK_PYTHON_VERSION 3
VTK_SMP_IMPLEMENTATION_TYPE TBB
VTK_USE_CUDA Checked
VTK_USE_MPI Checked
VTK_WRAP_PYTHON Checked
VTK_ENABLE_KITS Checked

```

and delete

```auto
Python2_EXECUTABLE

```

under `Ungrouped Entries`

1. Rerun Configure

2. Change the following CMake Variables:

```auto
Python3_EXECUTABLE C:/Program Files/Python38/python.exe
Python3_INCLUDE_DIR C:/Program Files/Python38/include
Python3_LIBRARY C:/Program Files/Python38/libs/python38.lib

```

1. Rerun Configure

##### Output from 3rd Configure:

```auto
Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.18363.
Found Python3: C:/Program Files/Python38/python.exe (found suitable version "3.8.10", minimum required is "3.2") found components: Interpreter Development.Module Development.Embed 
Configuring done

```

1. My CMake variables now look like this:

_ **ASIDE:** _ _Would be nice if the CMake GUI had an option to output variables to a text file, especially for debugging…_

_ **ASIDE:** _ _Would be nice if CMake GUI could tell you what had actually been activiated/used since everything shows up with `DEFAULT` value and we don’t want to change that…_

_ **ASIDE:** _ _Would be nice if there was a high level overview somewhere of what all of these modules entail so users can understand what they want/need…_

[![CMake Variables 01][24]][24]

[![CMake Variables 02][25]][25]

[![CMake Variables 03][26]][26]

[![CMake Varables 04][27]][27]

[![CMake Variables 05][28]][28]

[![CMake Variables 06][29]][29]

[![CMake Variables 07][30]][30]

1. Click `Generate`

##### Output from Generate

```auto
Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.18363.
Configuring done
Generating done

```

1. Open `Visual Studio 2019` as an Administrator

2. Open the Solution file `VTK.sln` in `C:\VTK\build`

3. Change the build type from `Debug` to `Release` for speed:

[![Release Mode][31]][31]

1. Right-click on `ALL_BUILD` and select `Build`. In the end, the output is

```auto
========== Build: 398 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

```

1. Right-click on `INSTALL` and select `Build`. In the end, the output is:

```auto
========== Build: 116 succeeded, 0 failed, 283 up-to-date, 0 skipped ==========

```

### Issue

Please see problem at top of post.
