はじめに
最近Project Eularで問題を解いてるんですが、brute force実装をする意外に方法がない時があります。しかしOCamlではマルチコアを有効に使う並列計算処理を書くのがちょっと手間らしいので、OCamlMPIを使うことにしました。
環境
Ubuntu 10.04 がベース。
gcc | 4.4.3 (Ubuntu 4.4.3-4ubuntu5) |
---|---|
make | 3.81 (x86_64-pc-linux-gnu) |
ocaml | 3.11.2 |
omake | 0.9.8.5 (release 3) |
MPI | MPICH 1.2.7-9.1ubuntu1 |
インストール
MPICHのインストール
$ sudo apt-get install mpich-bin libmpich1.0-dev
Makefileの設定
まずターゲットディレクトリを作成する。Makefileにあるように ocamlc -where が指定する場所に作成する。
$ mkdir `ocamlc -where`/ocamlmpi
ここをDESTDIRに指定する。
DESTDIR=`$(OCAMLC) -where`/ocamlmpi MPIINCDIR=/usr/include/mpi MPILIBDIR=/usr/lib
ビルド
普通にmake allとmake installするだけ。
$ make all gcc -I`ocamlc -where` -I/usr/include/mpi -O -g -Wall -c -o init.o init.c gcc -I`ocamlc -where` -I/usr/include/mpi -O -g -Wall -c -o comm.o comm.c gcc -I`ocamlc -where` -I/usr/include/mpi -O -g -Wall -c -o msgs.o msgs.c msgs.c: In function ‘caml_mpi_send’: msgs.c:40: warning: implicit declaration of function ‘caml_enter_blocking_section’ msgs.c:42: warning: implicit declaration of function ‘caml_leave_blocking_section’ gcc -I`ocamlc -where` -I/usr/include/mpi -O -g -Wall -c -o collcomm.o collcomm.c collcomm.c: In function ‘caml_mpi_scatter_float’: collcomm.c:123: warning: unused variable ‘len’ collcomm.c: In function ‘caml_mpi_scatter_floatarray’: collcomm.c:145: warning: unused variable ‘srclen’ collcomm.c: In function ‘caml_mpi_gather_float’: collcomm.c:197: warning: unused variable ‘reslen’ collcomm.c: In function ‘caml_mpi_allgather_float’: collcomm.c:245: warning: unused variable ‘reslen’ gcc -I`ocamlc -where` -I/usr/include/mpi -O -g -Wall -c -o groups.o groups.c gcc -I`ocamlc -where` -I/usr/include/mpi -O -g -Wall -c -o utils.o utils.c rm -f libcamlmpi.a ar rc libcamlmpi.a init.o comm.o msgs.o collcomm.o groups.o utils.o ocamlc -c mpi.mli ocamlc -c mpi.ml File "mpi.ml", line 329, characters 6-12: Warning Y: unused variable myself. ocamlc -a -o mpi.cma -custom mpi.cmo -cclib -lcamlmpi -ccopt -L/usr/lib -cclib -lmpi ocamlopt -c mpi.ml File "mpi.ml", line 329, characters 6-12: Warning Y: unused variable myself. ocamlopt -a -o mpi.cmxa mpi.cmx -cclib -lcamlmpi -ccopt -L/usr/lib -cclib -lmpi $ sudo make install cp mpi.mli mpi.cmi mpi.cma mpi.cmxa mpi.a libcamlmpi.a `ocamlc -where`/ocamlmpi
テスト
READMEにあるようにmake testしてみる。
$ make test ocamlc -o testmpi unix.cma mpi.cma test.ml -ccopt -L. File "test.ml", line 144, characters 6-9: Warning Y: unused variable res. mpirun -np 5 ./testmpi ... 0: my wtime is 18.141 1: my wtime is 12.418 2: my wtime is 8.895 4: my wtime is 4.047 3: my wtime is 6.531
テストが成功したので、これでOK。とりあえずできたので、今度はMac OS Xでやる。これがまたはまる予感。