before lisps just part of the past ~#6 introduction to harlan~

15
Before LISPs Just Part of the Past #6 Introduction to Harlan

Upload: samugari

Post on 11-May-2015

3.342 views

Category:

Sports


0 download

DESCRIPTION

Intorduce Harlan, a new programming language for data parallel programming.

TRANSCRIPT

Page 1: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~

BeforeLISPs

Just Part of the Past

〜 #6 Introduction to Harlan 〜

Page 2: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~

ある日寄せられたお便り

最近こんな感じのSchemeベースなGPGPU言語が出たらしいよ

Page 3: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~

ある日寄せられたお便り

(module (extern nanotime () -> u64) (define-datatype point3_t (point3 float float float)) (define (make-points N) (kernel ((i (iota N))) (point3 (int->float i) (int->float i) (int->float i)))) (define (point-diff x y) (match x ((point3 a b c) (match y ((point3 d e f) (point3 (- a d) (- b e) (- c f))))))) (define (point-add x y) (match x ((point3 a b c) (match y ((point3 x y z) (point3 (+ a x) (+ b y) (+ c z)))))))

(define (point-div a y) (match a ((point3 a b c) (point3 (/ a y) (/ b y) (/ c y))))) (define (point-mag p) (match p ((point3 a b c) (sqrt (+ (* a a) (+ (* b b) (* c c))))))) (define (nbody bodies) (kernel ((i bodies)) (reduce point-add (kernel ((j bodies)) (let* ((diff (point-diff i j)) (d (point-mag diff))) (if (< 0 d) (point-div diff (* (* d d) d)) (point3 0 0 0))))))) (define (main) (let* ((bodies (make-points 1000)) (start (nanotime)) (forces (nbody bodies)) (stop (nanotime))) (print "Computed ") (print (length forces)) (print " forces in ") (print (/ (- stop start) 1000000)) (println "ms")) (return 0)))

Page 4: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~

Harlan● ``Harlan is a new programming language for GPU Computing'' - 作者ブログより

● OpenCLにコンパイルされるLisp風言語

– Indiana大学のEric Holk作

– コンパイラはChez Schemeで書かれた

● OpenCLとChez Schemeが必要

– Petit Chez Schemeでも可

Page 5: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~

GPU Computing?

● GPUに普通の計算をさせる

● データをGPUメモリに送り

GPUで計算させて

結果をGPUメモリから取り出す

● コアをたくさん積んでるので並列処理向き

● GPU鬱

Page 6: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~

OpenCL● 異種計算資源混在環境での並列計算のための言

語仕様

– GPUに限定しない

● eg) Cell

– Appleが提案、Khronosが策定

● なのでMacでは環境を整えやすい

Page 7: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~

Hello Harlan● Harlan is a language for data parallelism that has its roots in C and Scheme - Harlan User's Guideより

● (module (define (main) (print “Hello, world”) (return 0)))

● これparenの多いCなんじゃ...

Page 8: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~

並列化構文

● kernel– vectorに対するmap演算の並列処理

– 入れ子にもできる

● reduce– vectorに対する畳み込み演算の並列処理

Page 9: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~

kernel● (kernel ((x (vector 0 2 4)) (y (vector 1 3 5))) (+ x y))

● Thread 0 : (+ x y)● Thread 1 : (+ x y)● Thread 2 : (+ x y)

Page 10: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~

kernel● (kernel ((x (vector 0 2 4)) (y (vector 1 3 5))) (+ x y))

● Thread 0 : (+ x y)● Thread 1 : (+ x y)● Thread 2 : (+ x y)

Page 11: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~

入れ子のkernel

● kernelは入れ子に記述することで2次元配列の

各要素に触るような処理をすることもできる(kernel ((line field)) (kernel ((point line)) (/ 2 point)))

8 1 6

3 5 7

4 9 2

4 0 3

1 2 3

2 4 1

Page 12: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~

reduce

● (reduce + (kernel ((x v0) (y v1)) (* x y)))

● 第一引数はユーザ定義の2引数関数でも良い

Page 13: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~

その他の構文

● let, let*, if● for

– 並列化されないループ構文

● return– main以外はreturn書かなくても値を返せる

Page 14: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~

気になった点● Scheme上に作られた言語ではない

– Schemeで書かれたコンパイラ

– Schemeではない

● マクロが定義できない● コンパイラがびっくりするほど遅い

– 2011年のMacbook Airだとhelloworldが3分かかる

– Petitを使ってるからか?

● ユーザガイドなどに関数一覧とか構文一覧とかがない

Page 15: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~

参考資料● 作者Eric Holkのブログ

http://blog.theincredibleholk.org/blog/2013/06/28/announcing-the-release-of-harlan/

● ソースhttps://github.com/eholk/harlan

● Harlan User's Guide– ソースのdocディレクトリにある

– xelatexが使える環境ならmake docsでpdf版が生成される