46
田中昌宏 Masahiro Tanaka 2010-8-29 RubyKaigi2010 1

NArray and scientific computing with Ruby - RubyKaigi2010

Embed Size (px)

DESCRIPTION

NArray is an n-dimensional numerical array library developed for use in science fields, but it is usable for handling binary data efficiently. I talk about NArray for the first time after 10 years from the first release. I will also present topics including the next version of NArray, and distributed scientific computing using Ruby for multi-core era.

Citation preview

Page 1: NArray and scientific computing with Ruby - RubyKaigi2010

田中昌宏

Masahiro Tanaka

2010-8-29RubyKaigi2010 1

Page 2: NArray and scientific computing with Ruby - RubyKaigi2010

NArrayの開発者◦ NArray developer

昨年度からつくば市在住◦ I live in Tsukuba since Apr 2009

2010-8-29RubyKaigi2010 2

Page 3: NArray and scientific computing with Ruby - RubyKaigi2010

NArrayを使ったサイエンス◦ Science using NArray

NArrayの概要◦ NArray overview

◦ 詳しい話はしません。No details.

並列分散ワークフロー実行システム◦ Parallel Distributed Workflow System

2010-8-29RubyKaigi2010 3

Page 4: NArray and scientific computing with Ruby - RubyKaigi2010

2010-8-29RubyKaigi2010 4

Page 5: NArray and scientific computing with Ruby - RubyKaigi2010

2010-8-29RubyKaigi2010 5

Page 6: NArray and scientific computing with Ruby - RubyKaigi2010

http://www.gfd-dennou.org/

GPhys◦ 多次元物理量表現ライブラリ

◦ multi-dimensional physical quantity library

RubyDCL◦ グラフ描画ライブラリ

◦ Plotting Graph

RubyNetCDF◦ 物理データフォーマットNetCDFのIOライブラリ

◦ Physical data format library

2010-8-29RubyKaigi2010 6

Page 7: NArray and scientific computing with Ruby - RubyKaigi2010

◦ Gtkベース格子点可視化・解析ツール

◦ Gtk-base visualize & analysis tool for lattice data

2010-8-29RubyKaigi2010 7

Page 8: NArray and scientific computing with Ruby - RubyKaigi2010

◦ Railsベースの地球流体デー

タベース・解析・可視化ツール

◦ Rails-base visualize & analysis tool for Geophysical database

2010-8-29RubyKaigi2010 8

Page 9: NArray and scientific computing with Ruby - RubyKaigi2010

2010-8-29RubyKaigi2010 9

Page 10: NArray and scientific computing with Ruby - RubyKaigi2010

物質科学における、多次元試料データの管理を効率的に行う

For managing multi-dimensional sample data in material science.

2010-8-29RubyKaigi2010 10

Page 11: NArray and scientific computing with Ruby - RubyKaigi2010

2010-8-29RubyKaigi2010 11

Shin-ichi Todoroki, Meas. Sci. Technol. 16 (2005) 285–291

波長

位置

光の強さ

Page 12: NArray and scientific computing with Ruby - RubyKaigi2010

2010-8-29RubyKaigi2010 12

Page 13: NArray and scientific computing with Ruby - RubyKaigi2010

2010-8-29RubyKaigi2010 13

Page 14: NArray and scientific computing with Ruby - RubyKaigi2010

電子顕微鏡写真から、生体微小管などの三次元構造を構築するためのプログラム

Program set for Helical image analysis in combination with electron microscopy used to study three-dimensional structures of microtubules etc.

2010-8-29RubyKaigi2010 14

Page 15: NArray and scientific computing with Ruby - RubyKaigi2010

2010-8-29RubyKaigi2010 15

The EMBO Journal (2004) 23, 2459 - 2467

電子顕微鏡写真Electron Microscope image

画像から構築した3次元構造3-D structure constructed from the EM image

Page 16: NArray and scientific computing with Ruby - RubyKaigi2010

2010-8-29RubyKaigi2010 16

Page 17: NArray and scientific computing with Ruby - RubyKaigi2010

http://www.artcompsci.org/~makino/rikigaku/

A book in dynamics for general readers

2010-8-29RubyKaigi2010 17

Page 18: NArray and scientific computing with Ruby - RubyKaigi2010

2010-8-29RubyKaigi2010 18

Program and explanation 飛跡の計算結果Result flight trajectory

Page 19: NArray and scientific computing with Ruby - RubyKaigi2010

2010-8-29RubyKaigi2010 19

Page 20: NArray and scientific computing with Ruby - RubyKaigi2010

2010-8-29RubyKaigi2010 20

Page 21: NArray and scientific computing with Ruby - RubyKaigi2010

処理を簡単に書きたい

◦ write processing codes shortly

結果をすぐ確認したい

◦ confirm processing result immediately

インタラクティブな操作もしたい

◦ manipulate interactively

2010-8-29RubyKaigi2010 21

Page 22: NArray and scientific computing with Ruby - RubyKaigi2010

プログラムが書ける – Ruby

◦ Programmable

インタラクティブな実行 – irb

◦ Interactive execution

データ配列操作 – NArray

◦ Data Array Manipulation

数値計算ライブラリ

◦ Library for Numerical Algorithms

グラフ表示機能

◦ Plotting Graphs

2010-8-29RubyKaigi2010 22

Page 23: NArray and scientific computing with Ruby - RubyKaigi2010

多次元数値配列◦ Numerical N-dimensional Array

http://narray.rubyforge.org

http://github.com/masa16/narray

2010-8-29RubyKaigi2010 23

Page 24: NArray and scientific computing with Ruby - RubyKaigi2010

0.3.0 – 1999

0.5.0 – 2000

0.5.9p7 – 2009 (current version)

0.7.0 – 20?? (next version, under development)

2010-8-29RubyKaigi2010 24

Page 25: NArray and scientific computing with Ruby - RubyKaigi2010

IDL (Interactive Data Language)

Yorick

Python Numeric

2010-8-29RubyKaigi2010 25

Page 26: NArray and scientific computing with Ruby - RubyKaigi2010

rank

◦ 次元数 the number of dimension

shape

◦ 配列の「形」 array shape

type

◦ 要素の型 element type

memory block

◦ データを格納するメモリーブロック

2010-8-29RubyKaigi2010 26

Page 27: NArray and scientific computing with Ruby - RubyKaigi2010

Ruby Array : ◦ VALUE *ptr; →

NArray◦ void *ptr; →

◦ or

2010-8-29RubyKaigi2010 27

struct RVALUE {…

}

double[0] double[1] double[2] double[3] …

int[0] int[1] int[2] int[3] …

value[0] value[1] value[2] value[3] …

Page 28: NArray and scientific computing with Ruby - RubyKaigi2010

整数

◦ BYTE (8bit)

◦ SINT (16bit)

◦ INT (32bit)

浮動小数点数

◦ SFLOAT (32bit)

◦ FLOAT

◦ = DFLOAT (64bit)

複素数

◦ SCOMPLEX (64bit)

◦ COMPLEX

◦ = DCOMPLEX (128bit)

Rubyオブジェクト

◦ ROBJECT

2010-8-29RubyKaigi2010 28

Page 29: NArray and scientific computing with Ruby - RubyKaigi2010

現版では、ビルトインのみ

◦ Current version has Built-in types only

次版 Next version

◦後から付け足すことが可能

Append-able types

2010-8-29RubyKaigi2010 29

Page 30: NArray and scientific computing with Ruby - RubyKaigi2010

shape = [4]◦ 1次元配列

◦ one-dimensional array

shape = [4,4]◦ 2次元配列

◦ two-dimensional array

shape = [4,4,4]◦ 3次元配列

◦ three-dimensional array

2010-8-29RubyKaigi2010 30

a[0,0] a[1,0] a[2,0] a[3,0]

a[0,1] a[1,1] a[2,1] a[3,1]

a[0,2] a[1,2] a[2,2] a[3,2]

a[0,3] a[1,3] a[2,3] a[3,3]

a[0] a[1] a[2] a[3]

a[0,0,0] a[1,0,0] a[2,0,0] a[3,0,0]

a[0,1,0] a[1,1,0] a[2,1,0] a[3,1,0]

a[0,2,0] a[1,2,0] a[2,2,0] a[3,2,0]

a[0,3,0] a[1,3,0] a[2,3,0] a[3,3,0]

行列でいう次元とは異なるDifferent from Matrix dimension

Page 31: NArray and scientific computing with Ruby - RubyKaigi2010

a[i,j]

左の次元(i)がはやく回る◦ Left dimension(i) is contiguous

◦ Fortran-order

◦ Row-major order

If a.shape == [m,n] then

a[i,j] == a[i+j*m]

a[j][i]と逆 reverse order

次版では変更予定◦ This will be changed for the next version.

2010-8-29RubyKaigi2010 31

データが連続する順番contiguous order

a[0,0] a[1,0] a[2,0] a[3,0]

a[0,1] a[1,1] a[2,1] a[3,1]

a[0,2] a[1,2] a[2,2] a[3,2]

a[0,3] a[1,3] a[2,3] a[3,3]

Page 32: NArray and scientific computing with Ruby - RubyKaigi2010

NArray.float(3,2)

=> NArray.float(3,2):

[ [ 0.0, 0.0, 0.0 ],

[ 0.0, 0.0, 0.0 ] ]

NArray.float(3,2).indgen!

=> NArray.float(3,2):

[ [ 0.0, 1.0, 2.0 ],

[ 3.0, 4.0, 5.0 ] ]

NArray[[1,2,3],[4,5,6]]

=> NArray.int(3,2):

[ [ 1, 2, 3 ],

[ 4, 5, 6 ] ]

NArray[1..10]

=> NArray.int(10):

[ 1, 2, 3, 4, 5, 6, 7, 8,

9, 10 ]

2010-8-29RubyKaigi2010 32

Page 33: NArray and scientific computing with Ruby - RubyKaigi2010

a[1]

◦ 要素参照

◦ reference by element

a[1..2]

◦ 範囲参照

◦ reference by range

a[[1,3]]

◦ インデックス配列参照

◦ reference by index array

2010-8-29RubyKaigi2010 33

a[0] a[1] a[2] a[3]

a[0] a[1] a[2] a[3]

a[0] a[1] a[2] a[3]

Page 34: NArray and scientific computing with Ruby - RubyKaigi2010

演算は要素ごと◦ Operation is element-wise

演算Operations

◦ 四則演算

arithmetic operations

+, -, *, /, %, **

◦ 数学関数演算

math operations

NMath module

2010-8-29RubyKaigi2010 34

a * b = a[0]*b[0] a[1]*b[1] a[2]*b[2] a[3]*b[3]

Page 35: NArray and scientific computing with Ruby - RubyKaigi2010

Float Array with 106 elements

Ruby 1.9.2 :

(0...n).map{|i| a[i]*b[i]}

◦ elapsed time : 180 ms

NArray with Ruby 1.9.2 :

a*b

◦ elapsed time : 6.4 ms

NArray is ~ 28 times faster 28倍速い

8 times less characters 8倍短く書ける

2010-8-29RubyKaigi2010 35

Page 36: NArray and scientific computing with Ruby - RubyKaigi2010

0

10

20

30

40

50

60

0.E+00 2.E+05 4.E+05 6.E+05 8.E+05 1.E+06速度向上の倍率

Spe

ed

up

fac

tor

データサイズ data size

NArrayによる性能向上Speedup by NArray

Speedup by NArray

2010-8-29RubyKaigi2010 36

コードはNArrayのサイトにあります

Page 37: NArray and scientific computing with Ruby - RubyKaigi2010

NArrayのメモリアクセス Memory access for NArray

実際のPCアーキテクチャ

Actual PC architechture

2010-8-29RubyKaigi2010 37

CPUcore

MainMemory

CPUcore

MainMemory

L1 cache

L2 cache

fast

slow

NArray’sBottleneckボトルネック

Page 38: NArray and scientific computing with Ruby - RubyKaigi2010

2010-8-29RubyKaigi2010 38

CPUcore

cache

CPUcore

cache

CPU Socket

Main Memory

cache

Node

CPUcore

cache

CPUcore

cache

CPU Socket

Main Memory

cache

GPU

Node

Storage

Node …

Network

Page 39: NArray and scientific computing with Ruby - RubyKaigi2010

Ruby and NArray◦ コードを書きやすい Easy to write code

◦複雑さの隠蔽 Hide complexity

アーキテクチャの違い different architecture

データの分散 data distribution

2010-8-29RubyKaigi2010 39

Page 40: NArray and scientific computing with Ruby - RubyKaigi2010

プログラムが書ける – Ruby

◦ Programmable

インタラクティブな実行 – irb

◦ Interactive execution

データ配列操作 – NArray

◦ Data Array Manipulation

数値計算ライブラリ – Ruby/GSL , etc.

◦ Library for Numerical Algorithms

グラフ表示機能 – ?

◦ Plotting Graphs

2010-8-29RubyKaigi2010 40

Page 41: NArray and scientific computing with Ruby - RubyKaigi2010

NArrayの各機能◦ NArray functions

NArrayの使い方◦ NArray usage

次版NArray◦ Next version of NArray

他の類似数値配列との比較◦ Comparison with other numerical arrays, e.g. Numpy

Python が科学分野で主流になりつつある件◦ Python is becoming major in Science field.

2010-8-29RubyKaigi2010 41

Page 42: NArray and scientific computing with Ruby - RubyKaigi2010

田中昌宏

Masahiro Tanaka

筑波大学

University of Tsukuba

2010-8-29RubyKaigi2010 42

Page 43: NArray and scientific computing with Ruby - RubyKaigi2010

科学データ処理

◦ Scientific data processing

複雑なワークフローである

◦ is a complicated workflow.

ワークフロー記述言語が必要

◦ It needs definition language

Makefileによる記述が提案

◦ Use of Makefile has been proposed

◦ e.g. GXP make

2010-8-29RubyKaigi2010 43

ワークフローのグラフWorkflow graph

Page 44: NArray and scientific computing with Ruby - RubyKaigi2010

Rakeによるワークフロー記述を提案

We propose defining workflows Rake

◦ Makeより記述力が高い

◦ More descriptive power than Make

Rakeの並列分散拡張として、Pwrake (プレイク)を開

We are developing Pwrake, a Parallel workflow

extension for Rake

◦ http://github.com/masa16/pwrake

2010-8-29RubyKaigi2010 44

Pwrake

Process

Process

Process

Page 45: NArray and scientific computing with Ruby - RubyKaigi2010

NArrayを使った科学計算の例について紹介◦ Examples of Scientific computing using NArray

NArrayとその特徴を紹介◦ NArray and its features

並列分散ワークフロー実行システムPwrake◦ Parallel Distributed Workflow System Pwrake

2010-8-29RubyKaigi2010 45

Page 46: NArray and scientific computing with Ruby - RubyKaigi2010

ご質問

Questions?

2010-8-29RubyKaigi2010 46