Skip to content

Instantly share code, notes, and snippets.

@RyanGlScott
Created October 27, 2016 15:47
Show Gist options
  • Save RyanGlScott/6a707536d6279e339909e43274ad66fc to your computer and use it in GitHub Desktop.
Save RyanGlScott/6a707536d6279e339909e43274ad66fc to your computer and use it in GitHub Desktop.

hmatrix issues

To get started, you'll need GHC 8.0.1 (I've also reproduced these issues with GHC HEAD, so that should work too). You'll also need the HEAD version of hmatrix. To do:

  1. git clone https://github.com/albertoruiz/hmatrix. (I'm using commit b32b37472559f202995e894f9a627871737d4619 at the moment.)

  2. Make sure you have OpenBLAS installed. I did pacman -S mingw-w64-x86_64-openblas. Apparently there are other ways to install OpenBLAS, but I haven't tried them.

  3. Apply the following change:

    diff --git a/packages/base/hmatrix.cabal b/packages/base/hmatrix.cabal
    index 89e5423..a526851 100644
    --- a/packages/base/hmatrix.cabal
    +++ b/packages/base/hmatrix.cabal
    @@ -121,7 +121,7 @@ library
    
         if os(windows)
             if flag(openblas)
    -            extra-libraries:    libopenblas, libgcc_s_seh-1, libgfortran-3, libquadmath-0
    +            extra-libraries:    openblas, libgcc_s_seh-1
             else
                 extra-libraries:    blas lapack
    
    

    Neither libgfortran-3 nor libquadmath-0 seem to be necessary to link hmatrix, so I opted to remove them to spare myself the expense of installing gcc-fortran.

  4. cabal install hmatrix/packages/base/. (The hmatrix repo contains other libraries, but they should be irrelevant, I think.)

Issue 1: To lib or not to lib

One oddity I observed concerns the naming of something in extra-libraries. To see what I mean, try running runghc hmatrix/examples/inplace.hs. It should just work. Now go back to hmatrix/packages/base/hmatrix.cabal and change libopenblas to just openblas in the extra-libraries field in the if os(windows) block. Re-run cabal install hmatrix/packages/base/ and runghc hmatrix/examples/inplace.hs. Now it'll fail!

ghc.exe: C:/msys64/mingw64/lib/../lib/libopenblas.a: unknown symbol `lsame_'

ghc.exe: Could not on-demand load symbol 'dgesvd_'

ghc.exe: C:\Users\RyanGlScott\AppData\Roaming\cabal\x86_64-windows-ghc-8.0.1\hmatrix-0.18.0.0-ASQ7f3Yo6PP5MiGTxN80a6\HShmatrix-0.18.0.0-ASQ7f3Yo6PP5MiGTxN80a6.o: unknown symbol `dgesvd_'

inplace.hs: inplace.hs: unable to load package `hmatrix-0.18.0.0'

This issue only seems to be present in GHCi, as compiling inplace.hs works fine, regardless of whether libopenblas or openblas is linked against.

What could be going on here? Does the lib prefix really carry that much power?

Issue 2: GHCi segfaulting

Another concerning thing (which I'm hoping is reproducible on another machine) is that if you install hmatrix (with libopenblas, as noted above) and then run runghc hmatrix/examples/bool.hs, you might be greeted with:

Segmentation fault/access violation in generated code

Oh dear. And it works fine when compiled, to make things scarier. Also, if you apply this change:

diff --git a/packages/base/src/Internal/C/vector-aux.c b/packages/base/src/Internal/C/vector-aux.c
index 1cef27d..3fa14c8 100644
--- a/packages/base/src/Internal/C/vector-aux.c
+++ b/packages/base/src/Internal/C/vector-aux.c
@@ -1,5 +1,6 @@
 #include <complex.h>
 #include <inttypes.h>
+#include <stdio.h>

 typedef double complex TCD;
 typedef float  complex TCF;
@@ -1450,6 +1451,7 @@ int conjugateC(KCVEC(x),CVEC(t)) {
 #define STEP_IMP         \
     int k;               \
     for(k=0;k<xn;k++) {  \
+        printf("%d\n", k); \
         yp[k]=xp[k]>0;   \
     }                    \
     OK

re-install hmatrix and re-run runghc hmatrix/examples/bool.hs, then it works fine (albeit with more output).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment