tsukuba.r #7 5/9

21
en.newikipedia.org (@niam) en.newikipedia.org の紹介とRで最大の行列を作る話 東京大学 中川研D1 江原 , @niam Tsukuba.R #7 2010/5/09 1

Upload: yo-ehara

Post on 30-Jun-2015

1.997 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tsukuba.R #7 5/9

en.newikipedia.org (@niam)

en.newikipedia.orgの紹介とRで最大の行列を作る話

東京大学 中川研D1江原 遥, @niam

Tsukuba.R #72010/5/09

1

Page 2: Tsukuba.R #7 5/9

en.newikipedia.org (@niam) 自己紹介

en.newikipedia.org 紹介http://en.newikipedia.org/wiki/Japan

スマート辞書付きWikipediaです.• ブラウザをユーザだと思って,ユーザの英語力に合わせて,ページを

ロードしたときに分からなさそうな単語を予測し,最初から訳を付けておくというのがスマート辞書です.

• 最初は,全ユーザの平均的な英語力が想定されます.

• 訳がついていない単語をクリックすると訳がつけられます.また,その単語を本当は知らなかったということなので英語力を調整して,次のページをロードした時の予測に役立てます.

• 逆に訳が付いている単語をクリックすると,訳が引っ込むと同時に,その単語は本当は知っていたということなので,英語力を調整して,次のページをロードした時の予測に役立てます.

2

Page 3: Tsukuba.R #7 5/9

en.newikipedia.org (@niam) Wikipediaにneをつけるだけ

en.wikipedia.org/wiki/Japan

en.newikipedia.org/wiki/Japan

3

Page 4: Tsukuba.R #7 5/9

en.newikipedia.org (@niam)

ところで.

4

Page 5: Tsukuba.R #7 5/9

en.newikipedia.org (@niam)

このシステム,Rと何の関係が?

5

Page 6: Tsukuba.R #7 5/9

en.newikipedia.org (@niam)

関係ないです

2005年頃は,Rでfor文をバリバリ書いてたんですが,最近は,pythonやってます.

6

applyを知らなかったあの頃

Page 7: Tsukuba.R #7 5/9

en.newikipedia.org (@niam)

さすがにまずいので…

7

Page 8: Tsukuba.R #7 5/9

en.newikipedia.org (@niam)

8

ほぅ…

Page 9: Tsukuba.R #7 5/9

en.newikipedia.org (@niam)

9

なんと.

Page 10: Tsukuba.R #7 5/9

en.newikipedia.org (@niam)

実際にやってみた

10

研究室の大容量メモリマシンで

Page 11: Tsukuba.R #7 5/9

en.newikipedia.org (@niam) topコマンド

Mem: 148,441,292k total, 12,141,880k used, 136,299,412k free

11

OK,空いてる1300 x 1800000 ≒1.3K x 1.8M = 2.34 G1要素に40バイト使っても,93.6GB<136GBなので,さすがに大丈夫だろう.

Page 12: Tsukuba.R #7 5/9

en.newikipedia.org (@niam) よし!

12

> system.time(i.u.mat<-matrix(0,1300,1800000))

以下にエラー matrix(0, 1300, 1800000)指定された要素数が多すぎます

Timing stopped at:0 0 0

なんでだーーーーーー??メモリは足りているはずなのに.

Page 13: Tsukuba.R #7 5/9

en.newikipedia.org (@niam) help(“Memory-limits”)There are also limits on individual objects. On all builds of R,

the maximum length (number of elements) of a vector is 2^31 - 1 ~2*10^9, as lengths are stored as signed integers. In addition,the storage space cannot exceed the address limit, and if you tryto exceed that limit, the error message begins ‘cannot allocatevector of length’. The number of characters in a characterstring is in theory only limited by the address space.

13

Page 14: Tsukuba.R #7 5/9

en.newikipedia.org (@niam) help(“Memory-limits”)There are also limits on individual objects. On all builds of R,

the maximum length (number of elements) of a vector is 2^31 - 1 ~2*10^9, as lengths are stored as signed integers. In addition,

the storage space cannot exceed the address limit, and if you tryto exceed that limit, the error message begins ‘cannot allocatevector of length’. The number of characters in a characterstring is in theory only limited by the address space.

14

理由:添字が32bitの符号付きintだから.符号付きなのは,Rでは負の添字を使って,v[-50]で50番目の要素を除いたベクトルを作る機能があるから.

Page 15: Tsukuba.R #7 5/9

en.newikipedia.org (@niam) じゃぁ,これはできる?

> system.time(i.u.mat<-matrix(0,1,2^31-1))

15

ユーザ システム 経過7.613 3.882 11.495

できたー!!

psでのプロセスのメモリ使用量(RSS)33,587,916 kbytes

要素数は2G個なので,1要素あたり10バイト以上もメモリを食う計算.最初,3GBだと思って2個Rを立ち上げたら,79GBぐらい使ってて慌ててkillしました.

2^31-1≒2G< 2.34 G≒ 1.3K x 1.8M だから,さっきの行列は要素数の制限に引っかかって出来なかった.

Page 16: Tsukuba.R #7 5/9

en.newikipedia.org (@niam) 厳密に2^31-1なようです.> system.time(i.u.mat<-matrix(0,2,2^31-1))以下にエラー matrix(0, 2, 2^31 - 1) : 指定された要素数が多すぎます

Timing stopped at: 0 0 0

> system.time(i.u.mat<-matrix(0,1,2^31))以下にエラー matrix(0, 1, 2^31) :

'ncol' の値が不正です(大きすぎるか NA です)追加情報: 警告メッセージ:In matrix(0, 1, 2^31) : 強制変換により NA が生成されまし

たTiming stopped at: 0 0 0

16

Page 17: Tsukuba.R #7 5/9

en.newikipedia.org (@niam) 行列の要素数が2^31-1以下ならOK> system.time(i.u.mat<-matrix(0,2^15,2^15))ユーザ システム 経過

3.800 1.991 5.790

17

ちなみに,2^31-1はオイラーが1772年に発見した,8番目のメルセンヌ素数だそうです.2^30-1は素数じゃないようです.http://en.wikipedia.org/wiki/Mersenne_prime

自明(1×nかn×1 )な形でない要素数nが最大の行列を考えると,n=2^31-1は素数なので無理で,n=2^31-2となります.2^30-1=pqと因数分解すると(素数ではないので可能),n=2^31-2=2(2^30-1)=2pqなので,2p×qかp×2qの行列になります.

Page 18: Tsukuba.R #7 5/9

en.newikipedia.org (@niam) ベクトルもいけました

> system.time(v<-numeric(2^31-1))ユーザ システム 経過

2.356 3.443 5.801

> system.time(v<-numeric(2^31))以下にエラー vector("double", length) :指定されたベクトルのサイズが長すぎます

Timing stopped at: 0.001 0 0

top:Mem: 148,441,292k total, 28,767,460k used, 119,673,832k

free, 262,348k buffers

18

2^31-1のmatrixより5G以上小さくて済むようです.

Page 19: Tsukuba.R #7 5/9

en.newikipedia.org (@niam) やってはいけない

> quit()Save workspace image? [y/n/c]: y

19

さすがに,これは,やってみなかった

Page 20: Tsukuba.R #7 5/9

en.newikipedia.org (@niam) ちなみに…There are also limits on individual objects. On all builds of R,

the maximum length (number of elements) of a vector is 2^31 - 1 ~2*10^9, as lengths are stored as signed integers. In addition,the storage space cannot exceed the address limit, and if you tryto exceed that limit, the error message begins ‘cannot allocate

vector of length’. The number of characters in a character

string is in theory only limited by the address space.

文字列なら,2G以上いけるはず.

20

Page 21: Tsukuba.R #7 5/9

en.newikipedia.org (@niam) まとめ

• en.newikipedia.org使ってください.こっちが本題です(笑)

• 行列もベクトルも最大サイズは,要素数が厳密に2^31-1個のものでした.

• 理由は,添字に32bitのintを使っているからです.符号付きなのは,Rでは負の添字はその添字の要素を除いた行列,ベクトルになるという機能があるからです.

• 要素数2^31-1では,行列1個33GB程度,ベクトル1個28GB程度メモリを消費します.要素数が2Gなので,1要素10バイト以上メモリを食う計算になります.

21