Skip to content
CristianAndrade94 edited this page Nov 13, 2022 · 14 revisions

The installation steps here work for the newest GHC, and I hope others can improve this installation tutorial, esp. for windows os.

Linux

Debian/Ubuntu

sudo apt-get install libgsl0-dev liblapack-dev libatlas-base-dev
stack install hmatrix-tests (OR cabal install hmatrix-tests)

In the shell, the above commands whill install hmatrix and hmatrix-gsl.

Fedora

sudo dnf install blas-devel
sudo dnf install lapack-devel
sudo dnf install gsl-devel
cabal install hmatrix-tests

In the shell, after running the above command, hmatrix and hmatrix-gsl are installed.

Windows

Install with OpenBLAS

On windows OS, according to the original installation help, OpenBLAS is recommended. So the first step is to install openblas. In the installation help, it tells to download the compiled openblas from http://www.openblas.net/. However, if you install hmatrix according to that tutorial, hmatrix can be installed, but an error telling that the libopenblas.dll can't be accessed will be shown when you implement some code in GHCi. So you need to compile and install openblas from the source manully. As for compiling openblas, please refer to the OpenBLAS wiki. Since there are more information on the OpenBLAS wiki, I'll give the simple steps that work for compiling openblas and installing hmatrix.

  1. Install MinGW (GCC) compiler suite, either either 32-bit (http://www.mingw.org/) or 64-bit (http://mingw-w64.sourceforge.net/), and install msys with MinGW. Note: there are some problems if you use mingw-w64 through msys2 to compile openblas. For details, please see issue 1503. So make sure you install MinGW and msys from http://mingw-w64.sourceforge.net/, which is important.
  2. Download the openblas source either by git clone or by zip-file.
  3. Open msys terminal, in which cd to the openblas folder, and compile the souce by make command
cd ~/OpenBLAS
make
  1. Install the library to a certain location.
make PREFIX=/your/installation/path install
  1. Copy
  • the compiled libopenblas.dll ---> C:\Users{yourname}\AppData\Local\Programs\stack\x86_64-windows\msys2-20161025\mingw64\bin,
  • files in include ---> C:\Users{yourname}\AppData\Local\Programs\stack\x86_64-windows\msys2-20161025\mingw64\include,
  • files in lib ---> C:\Users{yourname}\AppData\Local\Programs\stack\x86_64-windows\msys2-20161025\mingw64\lib,
  • or any locations that are in the search path.
  1. Install hmatrix.
stack install hmatrix --flag hmatrix:openblas

Or you can use cabal to install it.

After that, if the installation succeeds, it will work in GHCi without any errors.

Install with normal Blas and Lapack

On windows, when you install hmatrix with stack install hmatrix --flag hmatrix:openblas with the openblas as a backend, hmatrix is always installed as a global-project, not into a specific stack snapshot. In order to install hmatrix into a stack snapshot, you need first to install blas and lapack packages. So the first step is to install blas and lapack. Although there are some prebuilt libraries and .dll here for blas and lapack, I have never installed hmatrix successfully with those prebuilt libraries. So I decided to compile those two libraries from sources manually. The following compiling steps are from Lapack for Windows:

  1. Download the lapack.tgz from the netlib website and unzip.
  2. Download CMAKE and install it on your machine. Make sure you are using CMAKE 2.8.13 or above.
  3. Download MinGW 32 bits or MinGW-w64 and install it on your machine. Please note: you should install MinGW and msys from http://mingw-w64.sourceforge.net/ as suggested in the Install with OpenBLAS.
  4. Put the GNU runtime directory (that is your mingw-w64 ) in your PATH, for me I added C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin (MinGW 64 bits) in my PATH (right click on your computer icon, go to properties, advanced system settings, Environment Variables, look for the PATH variable and put 'C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin;' in front of its current value).
  5. Open CMAKE
    • Point to your lapack-3.8.0 folder as the source code folder in the Where is the source code
    • Point to a new folder where you want the build to be (not the same is better) in the Where to build the binaries
    • Click configure, check the install path if you want to have the libraries and includes in a particular location
    • Choose MinGW Makefiles.
    • Click "Specify native compilers" and indicate the path to the Mingw compilers.
      • For Win32, on my machine, the Fortran Compiler is "C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gfortran.exe", the C compiler is "C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc.exe", and C++ compiler is "C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe".
      • For x64, on my machine, the Fortran compiler is "C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/x86_64-w64-mingw32-gfortran.exe", the C compiler is "C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/x86_64-w64-mingw32-gcc.exe", and the C++ copiler is "C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/x86_64-w64-mingw32-g++.exe"
    • Set the 'BUILD_SHARED_LIBS' option to ON
    • Set the 'CMAKE_GNUtoMS' option to ON.
    • If you want to build the LAPACKE library, set the 'LAPACKE' option to ON.
    • Click again configure until everything becomes white
    • Click generate, that will create the mingw build
    • Close CMAKE
  6. Open a cmd prompt or powershell
  7. Go to your build folder using the cd command, that is, the folder you have set in Where to build the binaries
  8. Type mingw32-make and wait for the compilation to finish
  9. Type mingw32-make test if you want to run LAPACK testings to make sure everything is ok. (However, I met the No tests were found!!!, and I don't know how to figure it)
  10. Your libs are in the lib folder, the dlls are in the bin folder, and the header files are in the include folder. The resulting build will provide both GNU-format and MS-format import libraries for the DLLs.
  11. Copy the files in the lib, include, and bin folders into the corresponding folders in the stack's msys2/mingw64 directories.

At last, you can install hmatrix into the stack snapshot by running:

stack install hmatrix

NOTE: When you run mingw32-make in the step 8 to compile the lapack, there may be an error, telling that:

``` [ 3%] Built target blas [ 3%] Linking Fortran shared library ..\bin\liblapack.dll C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(string_intrinsics.o):(.text$_gfortran_concat_string+0x0): multiple definition of `_gfortran_concat_string' ../lib/libblas.dll.a(d000008.o):(.text+0x0): first defined here C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(string_intrinsics.o):(.text$_gfortran_string_len_trim+0x0): multiple definition of `_gfortran_string_len_trim' ../lib/libblas.dll.a(d000038.o):(.text+0x0): first defined here C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(transfer.o):(.text$_gfortran_transfer_integer_write+0x0): multiple definition of `_gfortran_transfer_integer_write' ../lib/libblas.dll.a(d000058.o):(.text+0x0): first defined here C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(transfer.o):(.text$_gfortran_transfer_character_write+0x0): multiple definition of `_gfortran_transfer_character_write' ../lib/libblas.dll.a(d000053.o):(.text+0x0): first defined here C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(transfer.o):(.text$_gfortran_st_write+0x0): multiple definition of `_gfortran_st_write' ../lib/libblas.dll.a(d000032.o):(.text+0x0): first defined here C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/libgfortran.a(transfer.o):(.text$_gfortran_st_write_done+0x0): multiple definition of `_gfortran_st_write_done' ../lib/libblas.dll.a(d000033.o):(.text+0x0): first defined here collect2.exe: error: ld returned 1 exit status mingw32-make[2]: *** [SRC\CMakeFiles\lapack.dir\build.make:27686: bin/liblapack.dll] Error 1 mingw32-make[1]: *** [CMakeFiles\Makefile2:162: SRC/CMakeFiles/lapack.dir/all] Error 2 mingw32-make: *** [Makefile:162: all] Error 2 ```

This problem may be caused by overloading of symbols according to this blog. All you need to do is to edit the next file created by Cmake in the build folder:

{Your build folder}\SRC\CMakeFiles\lapack.dir\link.txt

And add the following sentence as an option of x86_64-w64-mingw32-gfortran.exe

-Wl,--allow-multiple-definition

The corresponding line of link.txt is edited as follows:

C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\gfortran.exe  -O2 -DNDEBUG -O2  -shared -o ..\bin\liblapack.dll -Wl,--allow-multiple-definition -Wl,--out-implib,..\lib\liblapack.dll.a -Wl,--major-image-version,3,--minor-image-version,8 -Wl,--whole-archive CMakeFiles\lapack.dir/objects.a -Wl,--no-whole-archive @CMakeFiles\lapack.dir\linklibs.rsp

Now if you run mingw32-make, lapack.dll should be created. Good luck!

Clone this wiki locally