LoginSignup
3
0

More than 3 years have passed since last update.

パイ(RaspberryPi)の上でパイ(π)を計算してみる

Last updated at Posted at 2019-12-22

動機

先日、GDG DevFest Tokyo 2019に参加しました。
そのkeynoteのEmma Haruka Iwaoさんの円周率計算の世界記録の話で、y-cruncherという円周率計算のプログラムがあることを知りました。
y-cruncherは手元のPCでも動かせるとのことでしたので、自分でも試してみよう思いました。

なぜRaspberryPiか

さて、y-cruncherのページを見てみると、Macはサポートされていない。
WindowsPCも手元になく、唯一、ラズパイ3はUbuntu(Linux) 64bit対応!
よし動かしてみよう。しかもPiだし(πなだけに)。

結論から言うと、CPUアーキテクチャの違いにより、RapberryPiの上で、y-cruncherは動かせませんでした。(終了)
別のソフトを利用して計算するように方針転換したので、興味があれば下を見てください。。

準備

まずはUbuntuをRaspberryPiに入れます。手順は公式通り.

  1. os imageのダウンロード
  2. SDのフォーマット、imapeのコピー
  3. 起動

起動後のOSとシステム情報確認は以下の通りです。

# os確認
ubuntu@ubuntu:~$cat /etc/lsb-release
Ubuntu 19.10

# アーキテクチャ確認
ubuntu@ubuntu:~$ uname -a
Linux ubuntu 5.3.0-1014-raspi2 #16-Ubuntu SMP Tue Nov 26 11:18:23 UTC 2019 aarch64 aarch64 aarch64 GNU/Linux

問題発生

wgetして落としていざ実行。

./y-cruncher エイや! ...

cannot execute binary file: Exec format error

?!
そして y-cruncher のページを見てみると。。

All Systems: An x86 or x64 processor.

オワタ。Raspberry Pi 3 model BのCPUはARMでそもそも動かせませんでした。

別の方法を考えた

以下のようなサイトを見つけました。先行者がいた。ほとんど参考にさせていただきました。
https://nepia01.blogspot.com/2015/04/raspberry-pi.html
http://xn--w6q13e505b.jp/program/introduction.html

GMPライブラリというものがあり、多倍長演算ライブラリのようです。

これを用いて円周率の計算ができるようです。
https://gmplib.org/pi-with-gmp.html

当初の目的とは全然違いますが、これを使ってます!

# m4をインストール
ubuntu@ubuntu:~$ sudo apt install m4

# 作業ディレクトリの作成、移動
ubuntu@ubuntu:~$ mkdir pi
ubuntu@ubuntu:~$ cd pi

# dl
ubuntu@ubuntu:~/pi$ wget https://ftp.gnu.org/gnu/gmp/gmp-6.0.0a.tar.xz

# tar
ubuntu@ubuntu:~/pi$ tar xvf gmp-6.0.0.tar.xz

# 移動
ubuntu@ubuntu:~/pi$ cd gmp-6.0.0/

# gmp設定
ubuntu@ubuntu:~/pi/gmp-6.0.0$ ./configure
ubuntu@ubuntu:~/pi/gmp-6.0.0$ make -j4
ubuntu@ubuntu:~/pi/gmp-6.0.0$ make check
ubuntu@ubuntu:~/pi/gmp-6.0.0$ sudo make install

# 円周率プログラムのDL
ubuntu@ubuntu:~/pi$ wget gmplib.org/download/misc/gmp-chudnovsky.c

# コンパイル
ubuntu@ubuntu:~/pi$ gcc -Wall -o gmp-chudnovsky gmp-chudnovsky.c -Igmp-6.0.0 -Lgmp-6.0.0/.libs -lgmp -lm

動作確認

1000桁実行してみます。円周率も出力。

ubuntu@ubuntu:~/pi$ ./gmp-chudnovsky 1000 1
#terms=70, depth=8
sieve   time =  0.002
...................................................

bs      time =  0.002
   gcd  time =  0.000
div     time =  0.000
sqrt    time =  0.000
mul     time =  0.000
total   time =  0.004
   P size=1334 digits (1.334000)
   Q size=1327 digits (1.327000)
pi(0,70)=
0.314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019894e1

0.004秒で実行終了。はっや。

次に、1000万桁を実行してみます。


ubuntu@ubuntu:~/pi$ ./gmp-chudnovsky 10000000
#terms=705136, depth=21
sieve   time =  0.857
...................................................

bs      time = 54.182
   gcd  time =  0.000
div     time =  6.411
sqrt    time =  3.518
mul     time =  2.261
total   time = 67.263
   P size=14561191 digits (1.456119)
   Q size=14561184 digits (1.456118)

67.263秒かかりました。

1億桁計算してみると...

Out of memory: Killed process ...

流石に1GBのメモリに乗り切りませんでいた。

まとめ

ソフトウェアをただ動かしただけですが、Piでπを計算してみたということでお許しください。

円周率の導出方法や、数値計算する上での計算手法やアルゴリズムを勉強して、いつか別の記事で書いてみたいと思います。

その他

# ssh接続したい場合
$ ssh ubuntu@RaspberryPiのipアドレス

参考

3
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
3
0