before lisps just part of the past ~#6 introduction to harlan~
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~](https://reader036.vdocuments.pub/reader036/viewer/2022082605/554f9e6cb4c9057b298b4766/html5/thumbnails/1.jpg)
BeforeLISPs
Just Part of the Past
〜 #6 Introduction to Harlan 〜
![Page 2: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~](https://reader036.vdocuments.pub/reader036/viewer/2022082605/554f9e6cb4c9057b298b4766/html5/thumbnails/2.jpg)
ある日寄せられたお便り
最近こんな感じのSchemeベースなGPGPU言語が出たらしいよ
![Page 3: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~](https://reader036.vdocuments.pub/reader036/viewer/2022082605/554f9e6cb4c9057b298b4766/html5/thumbnails/3.jpg)
ある日寄せられたお便り
(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~](https://reader036.vdocuments.pub/reader036/viewer/2022082605/554f9e6cb4c9057b298b4766/html5/thumbnails/4.jpg)
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~](https://reader036.vdocuments.pub/reader036/viewer/2022082605/554f9e6cb4c9057b298b4766/html5/thumbnails/5.jpg)
GPU Computing?
● GPUに普通の計算をさせる
● データをGPUメモリに送り
GPUで計算させて
結果をGPUメモリから取り出す
● コアをたくさん積んでるので並列処理向き
● GPU鬱
![Page 6: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~](https://reader036.vdocuments.pub/reader036/viewer/2022082605/554f9e6cb4c9057b298b4766/html5/thumbnails/6.jpg)
OpenCL● 異種計算資源混在環境での並列計算のための言
語仕様
– GPUに限定しない
● eg) Cell
– Appleが提案、Khronosが策定
● なのでMacでは環境を整えやすい
![Page 7: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~](https://reader036.vdocuments.pub/reader036/viewer/2022082605/554f9e6cb4c9057b298b4766/html5/thumbnails/7.jpg)
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~](https://reader036.vdocuments.pub/reader036/viewer/2022082605/554f9e6cb4c9057b298b4766/html5/thumbnails/8.jpg)
並列化構文
● kernel– vectorに対するmap演算の並列処理
– 入れ子にもできる
● reduce– vectorに対する畳み込み演算の並列処理
![Page 9: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~](https://reader036.vdocuments.pub/reader036/viewer/2022082605/554f9e6cb4c9057b298b4766/html5/thumbnails/9.jpg)
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~](https://reader036.vdocuments.pub/reader036/viewer/2022082605/554f9e6cb4c9057b298b4766/html5/thumbnails/10.jpg)
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~](https://reader036.vdocuments.pub/reader036/viewer/2022082605/554f9e6cb4c9057b298b4766/html5/thumbnails/11.jpg)
入れ子の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~](https://reader036.vdocuments.pub/reader036/viewer/2022082605/554f9e6cb4c9057b298b4766/html5/thumbnails/12.jpg)
reduce
● (reduce + (kernel ((x v0) (y v1)) (* x y)))
● 第一引数はユーザ定義の2引数関数でも良い
![Page 13: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~](https://reader036.vdocuments.pub/reader036/viewer/2022082605/554f9e6cb4c9057b298b4766/html5/thumbnails/13.jpg)
その他の構文
● let, let*, if● for
– 並列化されないループ構文
● return– main以外はreturn書かなくても値を返せる
![Page 14: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~](https://reader036.vdocuments.pub/reader036/viewer/2022082605/554f9e6cb4c9057b298b4766/html5/thumbnails/14.jpg)
気になった点● Scheme上に作られた言語ではない
– Schemeで書かれたコンパイラ
– Schemeではない
● マクロが定義できない● コンパイラがびっくりするほど遅い
– 2011年のMacbook Airだとhelloworldが3分かかる
– Petitを使ってるからか?
● ユーザガイドなどに関数一覧とか構文一覧とかがない
![Page 15: Before LISPs Just Part of the Past ~#6 Introduction to Harlan~](https://reader036.vdocuments.pub/reader036/viewer/2022082605/554f9e6cb4c9057b298b4766/html5/thumbnails/15.jpg)
参考資料● 作者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版が生成される