//このページが表示される方は、URLから「action=SOURCE&」を削除してみてください [[R備忘録 - 記事一覧]] !!!ATLASをRで使う(Linux編) *投稿者: みゅ *カテゴリ: なし *優先度: 普通 *状態: 完了 *日時: 2010年05月12日 14時09分44秒 //{{bugstate}} !!内容 *ATLASについては【Rで並列処理 - Atlasを使う[[BugTrack-R備忘録/19]]】でも触れているのですが、gentoo LinuxのPortageを使ってインストールを行っているので自分でも何がインストールされているのかわかりにくい.そこで、ソースからインストールしてみることにした. *今回は安定版の「atlas3.8.3.tar.gz」を使用 *ATLASのインストレーションガイド **【http://math-atlas.sourceforge.net/atlas_install/】 **あまりちゃんと読んでないので、間違いがあったら、[別館|http://phoenixx.sakura.ne.jp/mt/R/2010/05/atlasrlinux.html]で指摘いただくと助かります. *ここに書いてあるのはFedraCoreの6年くらい前のやつなので、細かいところはLinuxのディストリビューターによって違うでしょう. !!ATLASをインストールする前に !lapackのインストール. *ATLASのINSTALL.txtを見ると、full LAPACK libraryをビルドするには、先にlapackをビルドしなければならないそうで. If you want to build a full LAPACK library (i.e. all of the lapack library, including those lapack routines not natively provided by ATLAS), see: NOTE ON BUILDING A FULL LAPACK LIBRARY *NOTE ON BUILDING A FULL LAPACK LIBRARY If you want to build a full LAPACK library, you must obtain and build netlib's LAPACK library (ATLAS provides only a few lapack routines natively). For the routines ATLAS does not provide natively, you will therefore only have the Fortran77 interface (ATLAS's native lapack routines have both F77 and C interfaces). Install LAPACK first (minimally, doing a "make lib" after editing the make.inc for your system). IMPORTANT NOTE: if you wish to build a dynamic library, remember to specify -fPIC (assuming gfortran/g77) for both the OPTS and NOOPT macros of lapack's make.inc!! Once the library is built, you have to tell ATLAS to use it during the configure, which you do by adding the following flag to configure: --with-netlib-lapack= (eg. --with-netlib-lapack=/home/whaley/LAPACK3.0/lapack_LINUX.a). If you want to add lapack symbols to a previously built ATLAS, see ATLAS/doc/LibReadme.txt for further info. *lapackのインストールガイドは【http://www.netlib.org/lapack/lawn81/node12.html】.ここを読むと、lapackにはconfigureは特にない. *lapackのソースをダウンロード.解凍後、とりあえず以下のようにやってmake.incを作る cp make.inc.example make.inc *その後上の説明にあるように共有ライブラリで使用するためにOPTとNOOPTを設定する.以下のように. OPTS = -fPIC NOOPT = -fPIC *その後makeする make *このままmakeしても以下のようにエラーが出る. gfortran: ../../blas_LINUX.a: そのようなファイルやディレクトリはありません *先のインストレーションガイドを読むと以下のように書いてある. Next, if you will be using a locally available BLAS library, you will need to remove blaslib from the lib definition. And finally, if you do not wish to build all of the libraries individually and likewise run all of the testing and timing separately, you can modify the all definition to specify the amount of the installation process that you want performed. By default, the all definition is set to all: lapack_install lib lapack_testing blas_testing *だが元のMakefileを見るとblaslibが元から入っていない・・・ *ので以下の様に追加してあげる. all: blaslib lapack_install lib lapack_testing blas_testing *で、make.だけど、別々に分けてmakeしても良い. *blas_LINUX.aを作成. make blaslib *blasのテスト. make blas_testing *lapackビルド.lapack_LINUX.aとtmglib_LINUX.aを作成. make lib *上でlapack_LINUX.aが作成されてるのだけど、念のためlapackテスト. make lapack_testing !!ATLASインストール *BUILDING DYNAMIC/SHARED LIBRARIESを読むと commands to your configure command: -Fa alg -fPIC to force ATLAS to be built using position independent code (required for a *ということなので. *コンフィギュア **スレッドの数を指定するには「-t」オプションで ../ATLAS/configure -b 32 -Fa alg -fPIC -t 4 --with-netlib-lapack=../lapack-3.2.1/lapack_LINUX.a *make ************************************************** mkdir my_build_dir ; cd my_build_dir /path/to/ATLAS/configure [flags] make ! tune and compile library make check ! perform sanity tests make ptcheck ! checks of threaded code for multiprocessor systems make time ! provide performance summary as % of clock rate make install ! Copy library and include files to other directories ************************************************** **make installはroot権限で !.so(動的共有ライブラリ)を作成する *インストレーションマニュアルに作り方が書いてあった. *http://math-atlas.sourceforge.net/atlas_install/atlas_install.html#SECTION00043000000000000000 After your build is complete, you can cd to your OBJdir/lib directory, and ask ATLAS to build the .so you want. If you want all libraries, including the FORTRAN77 routines, the target choices are: shared : create shared versions of ATLAS's sequential libs ptshared : create shared versions of ATLAS's threaded libs *以下をそれぞれ実行 cd lib make shared make ptshared *こんな感じでライブラリが作られる $ ls lib Make.inc libatlas.a libcblas.a libf77blas.a liblapack.a libptcblas.a libptf77blas.a libtstatlas.a Makefile libatlas.so libcblas.so libf77blas.so liblapack.so libptcblas.so libptf77blas.so *Rで使うときのために、すべてのBLAS・LAPACKのシンボルが含まれる動的共有ファイルを作成してみる. ld -melf_i386 -shared -soname liblapack.so -o liblapack.so --whole-archive liblapack.a libptcblas.a libptf77blas.a libatlas.a *Rにて > dyn.load("/home/admin/src/ATLAS3.8.3/lib/liblapack.so") > is.loaded("dgels") [1] TRUE > is.loaded("dgelsd") [1] TRUE > is.loaded("daxpy_") [1] TRUE > is.loaded("dsyev") [1] TRUE > is.loaded("dsymm") [1] TRUE !リンク *doc/LibReadme.txtにその方法が記述. ********************************* LINKING ************************************* When linking, remember that order is important. So, if you want uniprocessor libs, your link line would contain IN THIS ORDER: -LLIBDIR -llapack -lcblas -lf77blas -latlas And if you want to utilize an SMP version, it would be: -LLIBDIR -llapack -lptcblas -lptf77blas -latlas NOTE: On Apple's OS X, the above won't work, since system directories are searched before the -L directories. OS X includes ATLAS internally, so the above link line will always get you OS X's libs instead of the ones that you have built. The easiest solution is to explicitly link to each library using the full path, rather than using the -L/-l combo. !!Rインストール !(ATLAS化)blasを使う ./configure --with-blas='-L/usr/local/atlas/lib -lptf77blas -latlas -lpthread' --enable-R-shlib !(ATLAS化)blas+lapack ./configure --with-blas='-L/usr/local/atlas/lib -lptf77blas -latlas -lpthread' --with-lapack='-L/usr/local/atlas/lib -llapack -lptcblas -L/usr/lib/gcc/i386-redhat-linux/4.0.0 -lgfortranpreview' --enable-R-shlib *OSをCentOS5.4にしたら「-L/usr/lib/gcc/i386-redhat-linux/4.0.0 -lgfortranpreview」はなくてもコンフィグは通るようになった. *コンフィグの結果 R is now configured for i686-pc-linux-gnu Source directory: . Installation directory: /usr/local C compiler: gcc -std=gnu99 -g -O2 Fortran 77 compiler: g77 -g -O2 C++ compiler: g++ -g -O2 Fortran 90/95 compiler: gfortran -g -O2 Obj-C compiler: -g -O2 Interfaces supported: X11, tcltk External libraries: readline, BLAS(ATLAS), LAPACK(generic) Additional capabilities: PNG, JPEG, TIFF, iconv, MBCS, NLS Options enabled: shared R library, R profiling, Java *この場合はliRblas.soもlibRlapack.soも作成されない. !simple ./configure --enable-R-shlib *コンフィグの結果 R is now configured for i686-pc-linux-gnu Source directory: . Installation directory: /usr/local C compiler: gcc -std=gnu99 -g -O2 Fortran 77 compiler: g77 -g -O2 C++ compiler: g++ -g -O2 Fortran 90/95 compiler: gfortran -g -O2 Obj-C compiler: -g -O2 Interfaces supported: X11, tcltk External libraries: readline Additional capabilities: PNG, JPEG, TIFF, iconv, MBCS, NLS Options enabled: shared R library, shared BLAS, R profiling, Java Recommended packages: yes *デフォルトでは「shared BLAS」を作成するようだ. *make.すでにRがインストールされているなどでR_HOME環境変数が設定されている場合にはクリアしてmakeしたほうが良いようだ. R_HOME= make *この場合はRに付属しているFORTRANのBLASコードを使ってBLASのライブラリを構成する.従ってもちろんATLASではありません. make[4]: Entering directory `/home/admin/src/R-2.9.2_simple/src/extra/blas' g77 -fpic -g -O2 -c blas.f -o blas.o g77 -fpic -g -O2 -c cmplxblas.f -o cmplxblas.o gcc -std=gnu99 -shared -L/usr/local/lib -o libRblas.so blas.o cmplxblas.o -lg2c -lm # xerbla.o make[4]: Leaving directory `/home/admin/src/R-2.9.2_simple/src/extra/blas' *「libR.so」を作成. gcc -std=gnu99 -shared -L/usr/local/lib -o libR.so CConverters.o CommandLineArgs.o Rdynload.o Renviron.o RNG.o apply.o apse.o arithmetic.o array.o attrib.o base.o bind.o builtin.o character.o coerce.o colors.o complex.o connections.o context.o cov.o cum.o dcf.o datetime.o debug.o deparse.o deriv.o devices.o dotcode.o dounzip.o dstruct.o duplicate.o engine.o envir.o errors.o eval.o format.o fourier.o gevents.o gram.o gram-ex.o gramRd.o graphics.o identical.o inspect.o inlined.o internet.o iosupport.o 【lapack.o】 list.o localecharset.o logic.o main.o mapply.o match.o memory.o model.o names.o objects.o optim.o optimize.o options.o par.o paste.o pcre.o platform.o plot.o plot3d.o plotmath.o print.o printarray.o printvector.o printutils.o qsort.o random.o regex.o registration.o relop.o rlocale.o saveload.o scan.o seq.o serialize.o size.o sort.o source.o split.o sprintf.o startup.o subassign.o subscript.o subset.o summary.o sysutils.o unique.o util.o version.o vfonts.o xxxpr.o ../unix/Rembedded.o ../unix/libunix.a ../appl/libappl.a ../nmath/libnmath.a ../extra/zlib/libz.a ../extra/bzip2/libbz2.a ../extrapcre/libpcre.a -L../../lib -l【Rblas】 -lgfortran -lm -lreadline -ldl -lm **上記「lapack.o(lapack.c)」はRで使用される関数へのlapackのインターフェースを定義するものであって、LAPACK本体ではない模様. *この「libRblas.so」をR.binの作成に使用. gcc -std=gnu99 -Wl,--export-dynamic -L/usr/local/lib -o R.bin Rmain.o -L../../lib -lR -lRblas *LAPACKのライブラリは以下のように作成される.double版のみのようだ. make[4]: ディレクトリ `/home/admin/src/R-2.9.2/src/modules/lapack' に入ります gfortran -fpic -g -O2 -ffloat-store -c dlamch.f -o dlamch.o gfortran -fpic -g -O2 -c dlapack0.f -o dlapack0.o gfortran -fpic -g -O2 -c dlapack1.f -o dlapack1.o gfortran -fpic -g -O2 -c dlapack2.f -o dlapack2.o gfortran -fpic -g -O2 -c dlapack3.f -o dlapack3.o gfortran -fpic -g -O2 -c dlapack4.f -o dlapack4.o gfortran -fpic -g -O2 -c cmplx.f -o cmplx.o gcc -std=gnu99 -shared -L/usr/local/lib -o libRlapack.so dlamch.o dlapack0.o dlapack1.o dlapack2.o dlapack3.o dlapack4.o cmplx.o -L../../../lib -lRblas **この動的共有ライブラリは、Rの中でLAPACK関数が呼び出される時に、loadされる.「lapack.c」で定義されている. static void La_Init(void) { int res = R_moduleCdynload("lapack", 1, 1); initialized = -1; if(!res) return; if(!ptr->svd) error(_("lapack routines cannot be accessed in module")); initialized = 1; #ifdef Win32 /* gfortran initialization sets these to _O_BINARY */ setmode(1,_O_TEXT); /* stdout */ setmode(2,_O_TEXT); /* stderr */ #endif return; } !システムにインストールされているBLASを使う *「--with-blas」オプションはRに入っているBLASではなく、ユーザーがRと関係なくインストールしたBLASを使うように指定する. ./configure --with-blas='-LBLASLIBDIR -lblas -LGFORTRANLIBDIR -lgfortranpreview' --enable-R-shlib *コンフィグの結果 R is now configured for i686-pc-linux-gnu Source directory: . Installation directory: /usr/local C compiler: gcc -std=gnu99 -g -O2 Fortran 77 compiler: g77 -g -O2 C++ compiler: g++ -g -O2 Fortran 90/95 compiler: gfortran -g -O2 Obj-C compiler: -g -O2 Interfaces supported: X11, tcltk External libraries: readline, BLAS(generic) Additional capabilities: PNG, JPEG, TIFF, iconv, MBCS, NLS Options enabled: shared R library, R profiling, Java *「libR.so」の作成に【'-LBLASLIBDIR -lblas -LGFORTRANLIBDIR -lgfortranpreview'】が使われている. **libRblas.soは作成されない. gcc -std=gnu99 -shared -L/usr/local/lib -o libR.so CConverters.o CommandLineArgs.o Rdynload.o Renviron.o RNG.o apply.o apse.o ・・・ ../unix/Rembedded.o ../unix/libunix.a ../appl/libappl.a ../nmath/libnmath.a ../extra/zlib/libz.a ../extra/bzip2/libbz2.a ../extra/pcre/libpcre.a -L/home/admin/src/lapack-3.2.1 -lblas -L/usr/lib/gcc/i386-redhat-linux/4.0.0 -lgfortranpreview -lg2c -lm -lreadline -lncurses -ldl -lm *そして、「R.bin」の作成に「libR.so」が使用されている. gcc -std=gnu99 -I../../src/extra/zlib -I../../src/extra/bzip2 -I../../src/extra/pcre -I. -I../../src/include -I../../src/include -I/usr/local/include -DHAVE_CONFIG_H -fpic -g -O2 -c Rmain.c -o Rmain.o gcc -std=gnu99 -Wl,--export-dynamic -L/usr/local/lib -o R.bin Rmain.o -L../../lib -lR *「libRlapack.so」も同様に作成されている. *どうもこういうことらしい. ** --with-blas オプションをつけると、Rのソース外のどこかにBLASがあるということだから、libRblas.soを作成しない.このオプションをつけると他のアプリでもBLASを使うだろうから(この動的共有ライブラリを作成しないというオプションを別途つけない限り)libRblas.soを作成する? ** --with-lapack オプションも同様. !システムにインストールされているLAPACKを使う ./configure --with-blas='-L/home/admin/src/lapack-3.2.1 -lblas -L/usr/lib/gcc/i386-redhat-lin ux/4.0.0 -lgfortranpreview' --with-lapack='-L/home/admin/src/lapack-3.2.1 -llapack' --enable-R-shlib *コンフィグの結果 R is now configured for i686-pc-linux-gnu Source directory: . Installation directory: /usr/local C compiler: gcc -std=gnu99 -g -O2 Fortran 77 compiler: g77 -g -O2 C++ compiler: g++ -g -O2 Fortran 90/95 compiler: gfortran -g -O2 Obj-C compiler: -g -O2 Interfaces supported: X11, tcltk External libraries: readline, BLAS(generic), LAPACK(generic) Additional capabilities: PNG, JPEG, TIFF, iconv, MBCS, NLS Options enabled: shared R library, R profiling, Java *なるほど、Rで呼ばれたときにloadする、「lapack.so」の作成にLAPACKを使用しているということか・・・ make[4]: ディレクトリ `/home/admin/src/R-2.9.2/src/modules/lapack' に入ります gcc -std=gnu99 -I. -I../../../src/include -I../../../src/include -I/usr/local/include -DHAVE_CONFIG_H -fpic -g -O2 -c Lapack.c -o Lapack.o gcc -std=gnu99 -shared -L/usr/local/lib -o lapack.so Lapack.o -L../../../lib -lR -llapack -lblas -lgfortran -lm !!ようやく *なにをやっているのか理解できた・・・ !!コメント //{{comment}}