LAPACK 3.9.0をGCCでインストール

 気が付いたらLAPACK 3.6.0のインストール方法を書いてから大分経ってたので,Ubuntu環境にTAR ballからインスト―する方法をメモ書きしておく。前提として

  • GCC (gcc, gfortran)が使える
  • make (GNU make)が使える
  • /usr/local/libにライブラリファイル(*.a)を,/usr/local/includeにインクルードファイル(*.h)をインストールできる権限(sudo)がある

を仮定している。 $ 太字がコマンドである。ついでに拙著「LAPACK/BLAS入門」のサンプルプログラム実行方法も書いておいた。

1. LAPACK 3.9.0のTAR ball(v3.9.0.tar.gz)をダウンロード

$ wget https://github.com/Reference-LAPACK/lapack/archive/v3.9.0.tar.gz
–2020-03-25 18:38:15– https://github.com/Reference-LAPACK/lapack/archive/v3.9.0.tar.gz
Resolving github.com… 52.192.72.89
Connecting to github.com|52.192.72.89|:443… connected.
HTTP request sent, awaiting response… 302 Found
Location: https://codeload.github.com/Reference-LAPACK/lapack/tar.gz/v3.9.0 [following]
–2020-03-25 18:38:16– https://codeload.github.com/Reference-LAPACK/lapack/tar.gz/v3.9.0
Resolving codeload.github.com… 52.193.111.178
Connecting to codeload.github.com|52.193.111.178|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: unspecified [application/x-gzip]
Saving to: “v3.9.0.tar.gz”
[ <=> ] 7,534,567 4.84M/s in 1.5s
2020-03-25 18:38:18 (4.84 MB/s) – “v3.9.0.tar.gz” saved [7534567]
$ ls -l v3.9.0.tar.gz
-rw-rw-r– 1 tkouya tkouya 7534567 Mar 25 18:38 v3.9.0.tar.gz
$ tar zxvf v3.9.0.tar.gz
lapack-3.9.0/
lapack-3.9.0/.appveyor.yml
(略)
lapack-3.9.0/meson.build
lapack-3.9.0/meson_options.txt
$

2. “lapack-3.9.0″ディレクトリに移動してMakefile用のincludeファイル(make.inc)をgfortran用に設定

$ cd lapack-3.9.0
$ ls
BLAS CTestCustom.cmake.in lapack.pc.in meson.build
CBLAS DOCS lapack_testing.py meson_options.txt
CMAKE INSTALL LICENSE README.md
CMakeLists.txt lapack_build.cmake Makefile SRC
CTestConfig.cmake LAPACKE make.inc.example TESTING
$ ls -l INSTALL
total 228
-rw-rw-r– 1 tkouya tkouya 483 Nov 21 16:57 CMakeLists.txt
-rw-rw-r– 1 tkouya tkouya 5355 Nov 21 16:57 dlamch.f
(略)
-rw-rw-r– 1 tkouya tkouya 2140 Nov 21 16:57 slamchtst.f
-rw-rw-r– 1 tkouya tkouya 21748 Nov 21 16:57 tstiee.f
$ cp INSTALL/make.inc.gfortran ./make.inc

3. Reference BLASを生成

$ cd BLAS
$ ls
blas.pc.in CMakeLists.txt Makefile SRC TESTING
$ make
make -C SRC
(略)

4. CBLASを生成

$ cd ../
$ cd CBLAS
$ ls
cblas.pc.in CMakeLists.txt include README testing
cmake examples Makefile src
$ make
cp include/cblas_mangling_with_flags.h.in include/cblas_mangling.h
make -C src

5. librefblas.a, libcblas.aが生成されていることを確認

$ cd ../
$ ls -l
-rw-rw-r– 1 tkouya tkouya 384424 Mar 25 18:44 libcblas.a <– cblas
-rw-rw-r– 1 tkouya tkouya 618312 Mar 25 18:43 librefblas.a <– reference BLAS

6. LAPACKを生成

$ make
make -C INSTALL run
make[1]: Entering directory `/home/tkouya/pool/lapack/lapack-3.9.0/INSTALL’
gfortran -O2 -frecursive -c -o lsame.o lsame.f
(略)
–> LAPACK TESTING SUMMARY <–
Processing LAPACK Testing output found in the TESTING directory
SUMMARY nb test run numerical error other error
================ =========== ================= ================
REAL 1308195 0 (0.000%) 0 (0.000%)
DOUBLE PRECISION 1309007 0 (0.000%) 0 (0.000%)
COMPLEX 760590 1 (0.000%) 0 (0.000%)
COMPLEX16 768086 1 (0.000%) 1 (0.000%)

–> ALL PRECISIONS 4145878 2 (0.000%) 1 (0.000%)

7. LAPACKEを生成

$ cd LAPACKE
$ make
(略)
make[1]: Leaving directory `/home/tkouya/pool/lapack/lapack-3.9.0/LAPACKE/utils’

8. ファイル確認

$ cd ../
$ ls -l
-rw-rw-r– 1 tkouya tkouya 384424 Mar 25 18:44 libcblas.a
-rw-rw-r– 1 tkouya tkouya 12336044 Mar 25 18:51 liblapack.a
-rw-rw-r– 1 tkouya tkouya 6898692 Mar 25 19:11 liblapacke.a
-rw-rw-r– 1 tkouya tkouya 618312 Mar 25 18:43 librefblas.a
-rw-rw-r– 1 tkouya tkouya 630092 Mar 25 18:51 libtmglib.a

9. /usr/local/libにライブラリ(*.a)ファイルを,/usr/local/includeにヘッダファイル(*.h)をコピー

$ sudo cp *.a /usr/local/lib
$ sudo cp CBLAS/include/*.h /usr/local/include
$ sudo cp LAPACKE/include/*.h /usr/local/include
$ ls -l /usr/local/lib
-rw-r–r– 1 root root 384424 Mar 25 19:15 libcblas.a
-rw-r–r– 1 root root 12336044 Mar 25 19:15 liblapack.a
-rw-r–r– 1 root root 6898692 Mar 25 19:15 liblapacke.a
-rw-r–r– 1 root root 618312 Mar 25 19:15 librefblas.a
-rw-r–r– 1 root root 630092 Mar 25 19:15 libtmglib.a
$ ls -l /usr/local/include
-rw-r–r– 1 root root 21243 Mar 25 20:42 cblas_f77.h
-rw-r–r– 1 root root 29918 Mar 25 20:42 cblas.h
-rw-r–r– 1 root root 444 Mar 25 20:42 cblas_mangling.h
-rw-r–r– 1 root root 8051 Mar 25 20:42 cblas_test.h
-rw-r–r– 1 root root 4236 Mar 25 19:15 lapacke_config.h
-rw-r–r– 1 root root 834528 Mar 25 19:15 lapacke.h
-rw-r–r– 1 root root 474 Mar 25 19:15 lapacke_mangling.h
-rw-r–r– 1 root root 33161 Mar 25 19:15 lapacke_utils.h
-rw-r–r– 1 root root 455216 Mar 25 19:15 lapack.h

10. 「LAPACK/BLAS入門」サンプルプログラムをインストール

$ git clone https://github.com/tkouya/lapack_blas_tutorial
$ cd lapack_blas_tutorial

Makefile.unix → 下記のように書き換え

#*************************************************#
# LAPACK/BLAS Tutorial                            #
# Makefile for Linux gcc or icc environment       #
# Last Update: 2016-12-02 (Fri) T.Kouya           #
#*************************************************#
# ------------------------ start of configuration --------------------------
# Intel C compiler
#include lapack_icc.inc ←コメント化

# GNU Compiler Collection
include lapack_gcc.inc ← コメント外して有効化

(略)
# ------------------------ end of configuration --------------------------
all: first matvec_mul linear_eq blas row_column_major eig dgecon openmp pthread ← "gpu"を外す
#all: first matvec_mul linear_eq blas row_column_major gpu eig dgecon openmp pthread spblas_mkl

lapack_gcc.inc → 下記のように書き換え

#*************************************************#
# LAPACK/BLAS Tutorial                            #
# Configuration file for GNU compiler collection  #
# Last Update: 2016-12-02 (Fri) T.Kouya           #
#*************************************************#
CC=gcc
FC=gfortran
CPP=g++

INC = -I/usr/local/include
#LIB = -L/usr/local/lib/gcc  -lgfortran -lm
LIB = -L/usr/local/lib  -lgfortran -lm

CBLAS_INC = $(INC)
CBLAS_LIB = $(LIB) -lcblas -lrefblas -lgfortran

LAPACKE_INC = -I/usr/local/include/lapacke $(CBLAS_INC)
#LAPACKE_LIB = -L/usr/local/lib/gcc -llapacke -llapack $(CBLAS_LIB)
LAPACKE_LIB = -L/usr/local/lib -llapacke -llapack $(CBLAS_LIB)

#IMKL_INC=-I/opt/intel/mkl/include
#IMKL_LIB=-L/opt/intel/mkl/lib/intel64 -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -lpthread -L/opt/intel/lib/intel64 -lifcore
#IMKL_LIB=-L/opt/intel/mkl/lib/intel64 -lmkl_intel_lp64 -lmkl_intel_thread -lmkl_core -L/opt/intel/lib/intel64 -lifcore -liomp5

OPENMP = -fopenmp
OPENMP_LIB = -lgomp

Makeしてコンパイル→実行ファイルの一つをお試し

$ make -f Makefile.unix
$./lapack_dgeev ←行列の固有値・固有ベクトルを計算
Dim = 3 ←次元数を入力
Eigenvalues =
0: ( 1.82216, 0)
1: ( 0.103726, 0.366921)
2: ( 0.103726, -0.366921)

(0.619085, 0) (0.634996, 0) (0.634996, 0)
(0.641249, 0) (-0.399111, -0.289681) (-0.399111, 0.289681)
(0.453358, 0) (-0.396179, 0.443416) (-0.396179, -0.443416)
||A * right_ev – lambda * right_ev||_F / ||A||_F = 4.35690729441612009e-16
||A^T * left_ev – lambda * left_ev||_F / ||A||_F = 3.79955740713784858e-16