wiki:Tools/OpenBLAS

OpenBLAS

This is an alternative to the classic BLAS and LAPACK libraries from netlib.

It turns out it is murderously hard to compile it with the right optimizations, such as -m64 -march=native and -fopenmp

The problem is that Ubuntu (and Debian) have a DYNAMIC_ARCH=1 version of the library, without -fopenmp

To build an optimized but compatible version we have to perform a few simple steps

  • make sure you are using gcc instead of clang due to different and incompatible OpenMP implementations
  • make sure $LD is set to ld instead of ld.lld from LLVM
  • make sure there is no libomp.so form Clang available in the linker's search path (just rename it for a time being)
  • make sure you have the same flags for gcc and gfortran, especially -m64 and -fopenmp (build script selects it by default anyway)
  • use f95

This is how to compile a dynamic Ubuntu-compatible version DYNAMIC_ARCH=1, so other programs will be dynamically linked with this new version.

#!/bin/bash

export CC=gcc
export CXX=g++
export LD=ld
 
export FC=gfortran
export F77=gfortran
export FFLAGS="-m64 -march=native -mtune=native -O3 -frecursive -fPIC -fopenmp"
export FC=f95
export FCFLAGS="-m64 -march=native -mtune=native -O3 -frecursive -fPIC -fopenmp"
export F95=f95
export F95FLAGS="-m64 -march=native -mtune=native -O3 -frecursive -fPIC -fopenmp"

export CFLAGS="-Wformat -Wformat-security -Werror=format-security \
    -fstack-protector-strong -fstack-protector-all --param ssp-buffer-size=4 \
    -fno-strict-aliasing -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 \
    -ffunction-sections -fdata-sections \
    -m64 -march=native -mtune=native -O3 \
    -fPIC \
    -fopenmp \
    -I/usr/local/include"
export LDFLAGS="-L/usr/local/lib \
    -fPIC \
    -fopenmp \
    -Wl,--gc-sections -Wl,--as-needed \
    -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack"
export CXXFLAGS="-Wformat -Wformat-security -Werror=format-security \
    -fstack-protector-strong -fstack-protector-all --param ssp-buffer-size=4 \
    -fno-strict-aliasing -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 \
    -std=c++1y \
    -fno-rtti -fno-exceptions \
    -ffunction-sections -fdata-sections \
    -m64 -march=native -mtune=native -O3 \
    -fPIC \
    -fopenmp \
    -I/usr/local/include"
export CXXLDFLAGS="-L/usr/local/lib \
    -fPIC \
    -fopenmp \
    -Wl,--gc-sections -Wl,--as-needed \
    -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack"

ulimit -s 10000
make CC=gcc FC=f95 DYNAMIC_ARCH=1 USE_OPENMP=1 PREFIX="/usr/local"
sudo make install PREFIX="/usr/local"

Here is how to compile an CPU-optimized version DYNAMIC_ARCH=0

#!/bin/bash

export CC=gcc
export CXX=g++
export LD=ld
 
export FC=gfortran
export FCFLAGS="-m64 -fopenmp -march=native -mtune=native -O2 -frecursive -fPIC"
export F77=gfortran
export FFLAGS="-m64 -fopenmp -march=native -mtune=native -O2 -frecursive -fPIC" 
export F95=f95
export F95FLAGS="-m64 -fopenmp -march=native -mtune=native -O2 -frecursive -fPIC"

export CFLAGS="-Wformat -Wformat-security -Werror=format-security \
    -fstack-protector-strong -fstack-protector-all --param ssp-buffer-size=4 \
    -fno-strict-aliasing -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 \
    -ffunction-sections -fdata-sections \
    -m64 -march=native -mtune=native -O3 \
    -fopenmp \
    -fPIC \
    -I/usr/local/include"
export LDFLAGS="\
    -m64 \
    -fopenmp \
    -fPIC \
    -Wl,--gc-sections -Wl,--as-needed \
    -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack"
export CXXFLAGS="-Wformat -Wformat-security -Werror=format-security \
    -fstack-protector-strong -fstack-protector-all --param ssp-buffer-size=4 \
    -fno-strict-aliasing -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 \
    -fno-rtti -fno-exceptions \
    -ffunction-sections -fdata-sections \
    -m64 -march=native -mtune=native -O3 \
    -fopenmp \
    -fPIC \
    -I/usr/local/include"
export CXXLDFLAGS=" \
    -m64 \
    -fopenmp \
    -fPIC \
    -Wl,--gc-sections -Wl,--as-needed \
    -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack"

ulimit -s 10000
make all CC=gcc FC=f95 PREFIX="/usr/local"
sudo make install PREFIX="/usr/local"

Using Cmake

mkdir build; cd build

cmake  \
    -DBUILD_SHARED_LIBS=On \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX="/usr/local" \
     ..

make -s -j3 all
Last modified 3 years ago Last modified on Apr 5, 2018, 7:33:28 AM
Note: See TracWiki for help on using the wiki.