LoginSignup
1
0

FreeBSDでstan

Posted at

cmdstan

cmdstanはstanに、stanはstanmathに、stanmathはTBBに依存していて、サブモジュールとしてツリーの中に持っています。

%ls stan/lib/stan_math/lib/tbb_2020.3

pkgのonetbbはバージョンが新しく、うまく行かなかったので、tbb_2020.3をコンパイルして別の場所にインストールすることにします。また、stanのソースをC++にコンパイルするstancはバイナリをダウンロードしていますが、FreeBSD用のバイナリは提供されていませんので、ソースからコンパイルします。

まずは、リポジトリから依存関係を含めてソースを取得します。

git clone --recursive https://github.com/stan-dev/cmdstan.git

FreeBSDを認識するようにmake/stancを編集します。

% git diff make/stanc
diff --git a/make/stanc b/make/stanc
index e860ac5..b0c3c1c 100644
--- a/make/stanc
+++ b/make/stanc
@@ -36,7 +36,8 @@ else ifeq ($(OS),Linux)
 	ARCH_TAG := -armhf
   endif
  endif
-
+else ifeq ($(OS),FreeBSD)
+  OS_TAG := FreeBSD
 else
   $(error  Cannot detect OS properly. $n This will impede automatically downloading the correct stanc. $n Please visit https://github.com/stan-dev/stanc3/releases and download a stanc binary for your OS and place it in ./bin/stanc. $n )
 endif

stancのソースとTBBのインストール先を指定します。

% cat make/local
CXX = c++
TBB_CXX_TYPE = clang
TBB_LIB = ${HOME}/.local/lib 
STANC3 = ${HOME}/src/stanc3

stanc3

既定ではstancはバイナリをダウンロードします。FreeBSD用はないので、Not Foundと書かれたファイルができてしまいます。getting_startedを参考にソースからコンパイルします。stanc3はOCamlで書かれていますので、OCamlのインストールも必要です。

cmdstanのmake/localに指定したパス(上の例では${HOME}/src/stanc3)に取得します。

git clone https://github.com/stan-dev/stanc3.git

OCamlのインストールには、GNU patch必要なので、インストールします。

# pkg install patch
``
[Install OCaml](https://www.ocaml.org/install)の通りにインストールします。

bash -c "sh <(curl -fsSL https://raw.githubusercontent.com/ocaml/opam/master/shell/install.sh)"
opam install ocaml-lsp-server odoc ocamlformat utop


新しいターミナルを立ち上げて、stancをビルドします。
```zsh
opam update; opam install --deps-only --with-test .

バイナリは_build/default/src/stanc/stanc.exe
にできます。

tbb_2020.3

tbbのディレクトリに移動して、ビルドします。pkgのonetbbとコンフリクトしないように、${HOME}/.localにライブラリとヘッダをインストールしました。

% cd stan/lib/stan_math/lib/tbb_2020.3
% gmake
% cp build/FreeBSD_intel64_clang_cc16.0.6_kernel14.0_release/libtbb.so ~/.local/lib
% cp build/FreeBSD_intel64_clang_cc16.0.6_kernel14.0_release/libtbbmalloc.so ~/.local/lib
% cd ~/.local/include
% ln -s ${OLDPWD}/include/tbb .

最後にcmdstanをインストールして、例をビルド、実行して動作確認をします。

gmake build
gmake examples/bernoulli/bernoulli

rstan

FreeBSDでは~/.R/Makevarsに2行追加する必要があります。

  • TBBのインターフェースが変更され、古いヘッダは削除されたので、プリプロセッサフラグで新しいものを使うことを指定します。
  • libtbb.soだけでなくlibtbbmalloc.soもリンクする必要があります。
    gistにもMakevarのサンプルを置きましたが、ここにも書いておきます。
CPPFLAGS+= -DTBB_INTERFACE_NEW=true
LDFLAGS+= -ltbb -ltbbmalloc

以上の変更をすれば、Rからinstall.packages("rstan")でコンパイルはうまくいきます。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0