Building ITensor using MSYS2 and MinGW-x64 toolchain on Windows

In this guide I explain how to build ITensor library on Windows. Unfortunately, the current version of ITensor (2.1.1) cannot be built by the lastest version of Microsoft Visual Studio C++ compiler (2017), because the compiler is not fully C++11 compliant (namely, its lack of support for Expression SFINAE). We therefore need to use either Cygwin or MinGW compilers on Windows to build ITensor instead. Here I explain how to build ITensor using MinGW-w64 toolchain.

Install Toolchain

MSYS2 provides a convinent environment for the MinGW-w64 toolchain. Go to www.msys2.org and download x86_64 version. Follow instructions on the MSYS2 webpage. After the updates, restart MSYS2 MinGW 64-bit and install the MinGW-w64 toolchain, git, msys-make and vim:

$ pacman -S mingw-w64-x86_64-toolchain git make vim

Install OpenBLAS

ITensor uses BLAS and LAPACK routines. OpenBLAS is an open source implementation of the BLAS, bundled with the reference LAPACK implementation from netlib by default. Binary packages of OpenBLAS for Windows can normally be downloaded from www.openblas.net. However, the binary package for the most recent version v0.2.20 is not available for some reason. Binary package of a previous version v0.2.19 is available. But it uses an older version of gfortran, so I recommend building OpenBLAS yourself.

The compiler flags -Wa,-mbig-obj -O2 are a workaround for Windows’s limitation on binary files. Because of the -O2 flag, even the debug build will be optimized. This will unfortunately interfere with a debugger if you are using one, making debugging difficult. ITENSOR_MAKE_DYLIB=1 is for building dynamic library files libitensor.dll and libitensor-g.dll. Currently, the sample configuration included in ITensor does not support building of .dll files. Therefore, you need to add the following lines to options.mk:

Because of the “static” flags, the resulting binary ex.exe has all the dependent libraries (itensor, openblas, pthread, …) statically linked to it. This will increase the size of the binary file and also the build time. To dynamically link the libraries instead, you can remove the flags -static-libgcc, -static-libstdc++, and -Wl,-Bstatic:

⋮LIBS=-litensor\-lopenblas\-lpthread\-lgfortran\-lquadmath⋮

The ex.exe built by this new Makefile is smaller than before. When you try to run it, however, it will complain about the missing DLL files:

The simplest way to solve this problem is to copy all the dependent DLL files to the same directory as the executable. The DLL files for ITensor and OpenBLAS can be found in the bin subdirectory of ~/.local/pkg/ITensor and ~/.local/pkg/OpenBLAS-v0.2.20-Win64-int32 (You just installed them there). Other DLL files can be found in /mingw64/bin:

You might be thinking, “Why isn’t it printing the data? The norms seem correct…” This is because of how static variables defined in header are handled differently in Linux (and Mac OS X) and in Windows; the current version of ITensor is not compatible with how Windows handles it. I won’t go into detail, but this breaks the PrintData macro. Hopefully this will be fixed soon. If you want, you can print individual elements of the tensors and check whether the rest of the code works properly.