mmottl / gsl-ocaml

OCaml bindings to the GSL (GNU Scientific Library).
Other
36 stars 10 forks source link

blas used on os x #14

Closed nilsbecker closed 7 years ago

nilsbecker commented 7 years ago

i have installed gsl via homebrew on os x. when i do gsl-config --libs i get -L/usr/local/Cellar/gsl/2.2.1/lib -lgsl -lgslcblas which seems to indicate that a brew-provided blas was linked. this is not what the README says, where it is claimed that an optimized blas is linked automatically on os x. i checked also via the Activity Monitor for open files of an ocaml toplevel where gsl was #required. here also, a libgslcblas from homebrew was shown. i could provide the list of open files if desired.

what do i need to do to link to apple's blas? should this be changed to happen automatically even for brew installed gsl?

mmottl commented 7 years ago

If I remember correctly, the GSL-provided BLAS/LAPACK libraries will be overridden by the OCaml GSL-bindings. gsl-ocaml links with the Accelerate framework by default. You can call otool -L on your executables to see whether they link with the Accelerate framework.

nilsbecker commented 7 years ago

ok, i get:

hmtree.native:
    /System/Library/Frameworks/Accelerate.framework/Versions/A/Accelerate (compatibility version 1.0.0, current version 4.0.0)
    /usr/local/opt/gsl/lib/libgsl.19.dylib (compatibility version 22.0.0, current version 22.0.0)
    /usr/local/opt/gsl/lib/libgslcblas.0.dylib (compatibility version 1.0.0, current version 1.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)

which i find confusing since it links with accelerate but also shows the gsl-provided cblas if i'm not mistaken. in that program, also lacaml was used if that matters. maybe lacaml actually links accelerate and gsl-ocaml the cblas?

mmottl commented 7 years ago

Lacaml should only link with the Accelerate framework. I guess on Mac OS X it might be necessary to supply gsl-config --libs-without-cblas within the gsl-ocaml myocamlbuild.ml file. If you want, you can try it out by editing that file and replacing the --libs flag with the mentioned one. Maybe it makes a difference or not. You may need to run an example to find out.

nilsbecker commented 7 years ago

ok thanks, will try tomorrow.

nilsbecker commented 7 years ago

ok, i tried: i now have this in my local opam-pinned copy of gsl-ocaml:

          (*let ic = Unix.open_process_in "gsl-config --cflags --libs" in*)
          let ic = Unix.open_process_in "gsl-config --cflags --libs-without-cblas" in

i reinstalled the pinned version with opam. now the same program as before, with otool -L gives

hmtree.native:
    /System/Library/Frameworks/Accelerate.framework/Versions/A/Accelerate (compatibility version 1.0.0, current version 4.0.0)
    /usr/local/opt/gsl/lib/libgsl.19.dylib (compatibility version 22.0.0, current version 22.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)

so the cblas is gone. i have not tried if this program runs yet.

i have tried a toplevel with gsl loaded. calls to Gsl_blas (gemm) work, although in the open files for that process, nothing regarding accelerate was shown. but also the gsl-provided cblas was not shown anymore, so it seems to work to not link that.

so i think your suggestion works. i would think this should then be fixed in the opam package?

mmottl commented 7 years ago

The more important question is, does the change actually improve performance. If the Accelerate BLAS overrides the GSL-provided one anyway, the change won't be necessary. Maybe you can try out matrix multiplication as a test, which should likely show a significant performance difference.

nilsbecker commented 7 years ago

hmm it may be that cblas is linked but not used. i tried again with a minimal program which just uses gsl as its only package, and with the original opam-installed gsl. indeed cblas is linked, but so is accelerate, which is here not a byproduct of using lacaml. probably performance testing would be the only way to decide.

nilsbecker commented 7 years ago

ok. it does seem to matter. i made a program that LU-inverts a 400x400 matrix 100 times. with the standard opam installation of gsl-ocaml this takes 5.9s to run. after pinning gsl-ocaml with the myocamlbuild change above, the same (recompiled) program does not link cblas anymore and runs in 3.0s

(in each case i reran a few times to be sure)

nilsbecker commented 7 years ago

for reference:

let a = Gsl.Matrix.create 400 400 in
let () = Gsl.Matrix.set_id a in
let () = Gsl.Matrix.set a 123 223 123. in
for i = 1 to 100 do
  ignore @@ Gsl.Linalg.invert_LU (`M a);
done;
mmottl commented 7 years ago

Ok, thanks for testing this. I've made a new release that will avoid linking with the "slow" library on Mac OS X, the reasonable assumption being that users will always want to use the Accelerate Framework on this platform.