(Optional) Generating a Complete LAPACK
Procedure
KML_LAPACK depends on the open source Netlib LAPACK to provide complete LAPACK interface functions. You need to obtain the open source Netlib LAPACK v3.9.1 source package after installing KML_LAPACK.
- Download the Netlib LAPACK v3.9.1 source package. Save the file in a path that can be accessed by the compiler, for example, /data/lapack-3.9.1.tar.gz.
- Assume that libklapack.a is in the /usr/local/kml/lib/libklapack.a directory. Run the following script to compile the original Netlib LAPACK library and generate the adaptation file liblapack_adapt.a in the lapack_adapt subdirectory of the current directory.
netlib=/data/lapack-3.9.1.tar.gz klapack=/usr/local/kml/lib/libklapack.a mkdir lapack_adapt cd lapack_adapt # build netlib lapack mkdir netlib cd netlib tar zxvf $netlib mkdir build cd build cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON ../lapack-3.9.1 make -j cd ../.. cp netlib/build/lib/liblapack.a liblapack_adapt.a # get symbols defined both in klapack and netlib lapack nm -g liblapack_adapt.a | grep 'T ' | grep -oP '\K\w+(?=_$)' | sort | uniq > netlib.sym nm -g $klapack | grep 'T ' | grep -oP '\K\w+(?=_$)' | sort | uniq > klapack.sym comm -12 klapack.sym netlib.sym > comm.sym # update symbols name of liblapack_adapt.a while read sym; do \ if ! nm liblapack_adapt.a | grep -qe " T ${sym}_\$"; then \ continue; \ fi; \ ar x liblapack_adapt.a $sym.f.o; \ mv $sym.f.o ${sym}_netlib.f.o; \ objcopy --redefine-sym ${sym}_=${sym}_netlib_ ${sym}_netlib.f.o; \ ar d liblapack_adapt.a ${sym}.f.o; \ ar ru liblapack_adapt.a ${sym}_netlib.f.o; \ rm ${sym}_netlib.f.o; \ done < comm.sym - (Optional) Generate a single KML_LAPACK dynamic library with complete functions.
In some application scenarios, only a single LAPACK dynamic link library can be linked. In this case, you can use the following method to combine libklapack_xxx.a and liblapack_adapt.a generated previously into a single libklapack_full.so.
Assume that KML_LAPACK is /usr/local/kml/lib/libklapack.a and /usr/local/kml/lib/libkservice.a, then the adapted Netlib LAPACK library is liblapack_adapt.a in the current directory.gcc -o libklapack_full.so -shared -fPIC -Wl,--whole-archive /usr/local/kml/lib/libklapack.a liblapack_adapt.a -Wl,--no-whole-archive /usr/local/kml/lib/libkservice.a -fopenmp -lpthread -lgfortran -lm
After the execution is complete, the libklapack_full.so file is generated in the current directory. You can link the libklapack_full.so file to obtain all interface functions of LAPACK-3.9.1.
The KML_BLAS library and system libraries such as libgfortran need to be linked separately.
Verifying the Installation
Assume that the environment variable KML_LAPACK_ROOT is the directory of libklapack.*, then ADAPT_ROOT is the directory of the Netlib LAPACK library after adaptation. User applications can use dynamic link libraries (DLLs) or static link libraries (SLLs). When compiling the application source code, you need to link to the KML_LAPACK, Netlib LAPACK, Netlib BLAS, and GFortran libraries and enable OpenMP support.
- Scenario 1 with KML_BLAS (high performance)Assume that the libraries (libkblas.a and libkblas.so) constructed by KML_BLAS are located in KML_BLAS_ROOT. To achieve optimal performance, use the KML_BLAS single-thread version. When KML_BLAS is used, liblapack_adapt.a obtained in the previous section needs to adapt to Netlib LAPACK, but libblas.a in Netlib LAPACK is not required.
KML_LAPACK uses multiple threads to call KML_BLAS. Therefore, KML_BLAS of the nolocking version cannot be used. You are advised to use the omp version, that is, /usr/local/kml/lib/kblas/omp/libkblas.so.
- Using the dynamic link library
In this scenario, the Netlib LAPACK has only the static library liblapack_adapt.a. Therefore, the library is always statically linked. The libblas.a file of the Netlib LAPACK does not need to be linked.
gcc app.c -o app -fopenmp -I $KML_LAPACK_ROOT/include/kml-0.3.0 -L /usr/local/kml/lib -lklapack -L $ADAPT_ROOT -l:liblapack_adapt.a -L $KML_BLAS_ROOT -lkblas -lgfortran -lm -lkservice
The dynamic link libraries required by KML_LAPACK must be found, or /usr/local/kml/lib and $KML_BLAS_ROOT must be added to LD_LIBRARY_PATH:
export LD_LIBRARY_PATH=/usr/local/kml/lib:$KML_BLAS_ROOT:$LD_LIBRARY_PATH
- Using the static link library
In this scenario, libblas.a of Netlib LAPACK does not need to be linked.
gcc app.c -o app -fopenmp -I $KML_LAPACK_ROOT/include/kml-0.3.0 -L /usr/local/kml/lib -l:libklapack.a -L $ADAPT_ROOT -l:liblapack_adapt.a -L $KML_BLAS_ROOT -l:libkblas.a -l:libkservice.a -l:libgfortran.a -lm
- Using the dynamic link library
- Scenario 2 without KML_BLAS (depending on BLAS in Netlib LAPACK, low performance)
- Using the dynamic link library
gcc app.c -o app -fopenmp -I $KML_LAPACK_ROOT/include/kml-0.3.0 -L /usr/local/kml/lib -lklapack -L $ADAPT_ROOT -l:liblapack_adapt.a -lblas -lgfortran -lm -lkservice
The dynamic link libraries required by KML_LAPACK must be found, or /usr/local/kml/lib and $ADAPT_ROOT must be added to LD_LIBRARY_PATH:
export LD_LIBRARY_PATH=/usr/local/kml/lib:$ADAPT_ROOT:$LD_LIBRARY_PATH
- Using the static link library
gcc app.c -o app -fopenmp -I $KML_LAPACK_ROOT/include/kml-0.3.0 -L /usr/local/kml/lib -l:libklapack.a -L $ADAPT_ROOT -l:liblapack_adapt.a -l:libblas.a -l:libkservice.a -l:libgfortran.a -lm
- Using the dynamic link library