annotate paper/cbc.tex @ 93:16dc3337a5a9

Update
author atton <atton@cr.ie.u-ryukyu.ac.jp>
date Thu, 09 Feb 2017 18:54:18 +0900
parents 21cc0181b4cc
children ebe838b83ada
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 \chapter{Continuation based C}
22
c748fb296673 Update introduction
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 19
diff changeset
2 \label{chapter:cbc}
12
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 Continuation based C (CbC)は当研究室で開発しているプログラミング言語であり、OSや組み込みソフトウェアの開発を主な対象としている。
13
99a9be7e6bc9 Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
5 CbC は C言語の下位の言語であり、構文はほぼC言語と同じものを持つが、よりアセンブラに近い形でプログラムを記述する。
12
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 CbC は CodeSegment と呼ばれる単位で処理を定義し、それらを組み合わせることにでプログラム全体を構成する。
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 データの単位は DataSegment と呼ばれる単位で定義し、それら CodeSegment によって変更していくことでプログラムの実行となる。
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 CbC の処理系には llvm/clang による実装\cite{110009766999} と gcc\cite{weko_82695_1}による実装などが存在する。
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9
13
99a9be7e6bc9 Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
10 % {{{ section: CodeSegment と DataSegment
12
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 \section{CodeSegment と DataSegment}
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 本研究室では検証を行ないやすいプログラムの単位として CodeSegment と DataSegment を用いるプラグラミングスタイルを提案している。
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 CodeSegment は処理の単位である。
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 入力を受け取り、それに対して処理を行なった後を出力を行なう。
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 また、CodeSegment は他の CodeSegment と組み合わせることが可能である。
87
21cc0181b4cc Update fig
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
18 あるCodeSegment A を CodeSegment B に接続した場合、 A の出力は B の入力となる(図~\ref{fig:csds})。
12
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 DataSegment は CodeSegment が扱うデータの単位であり、処理に必要なデータが全て入っている。
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 CodeSegment の入力となる DataSegment は Input DataSegment と呼ばれ、出力は Output DataSegment と呼ばれる。
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 CodeSegment A と CodeSegment B を接続した時、A の Output DataSegment は B の入力 Input DataSegment となる。
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23
87
21cc0181b4cc Update fig
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
24 \begin{figure}[htbp]
21cc0181b4cc Update fig
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
25 \begin{center}
21cc0181b4cc Update fig
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
26 \includegraphics[scale=0.5]{fig/csds.pdf}
21cc0181b4cc Update fig
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
27 \caption{CodeSegment と DataSegement}
21cc0181b4cc Update fig
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
28 \label{fig:csds}
21cc0181b4cc Update fig
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
29 \end{center}
21cc0181b4cc Update fig
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
30 \end{figure}
12
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31
13
99a9be7e6bc9 Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
32 % }}}
12
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33
15
6dedd4ed6b6d Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
34 % {{{ Continuation based C における CodeSegment と DataSegment
6dedd4ed6b6d Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
35
12
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 \section{Continuation based C における CodeSegment と DataSegment}
14
6bf2e0196a1e Add goto.cbc and goto.pdf
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
37 最も基本的な CbC のソースコードをリスト\ref{src:goto}に、ソースコードが実行される流れを図\ref{fig:goto}に示す。
13
99a9be7e6bc9 Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
38 Continuation based C における CodeSegment は返り値を持たない関数として表現される。
99a9be7e6bc9 Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
39 CodeSegment を定義するためには、C言語の関数を定義する構文の返り値の型部分に \verb/__code/ キーワードを指定する。
99a9be7e6bc9 Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
40 Input DataSegment は関数の引数として定義される。
99a9be7e6bc9 Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
41 次の CodeSegment へ処理を移す際には \verb/goto/ キーワードの後に CodeSegment 名と Input DataSegment を指定する。
14
6bf2e0196a1e Add goto.cbc and goto.pdf
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
42 処理の移動を軽量継続と呼び、リスト\ref{src:goto}内の \verb/goto cs1(a+b);/ がこれにあたる。
6bf2e0196a1e Add goto.cbc and goto.pdf
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
43 この時の \verb/(a+b)/ が次の CodeSegment である cs1 の Input DataSegment となる cs0 の Output DataSegment である。
6bf2e0196a1e Add goto.cbc and goto.pdf
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
44
6bf2e0196a1e Add goto.cbc and goto.pdf
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
45 \lstinputlisting[label=src:goto, caption=CodeSegment の軽量継続] {src/goto.cbc}
12
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46
14
6bf2e0196a1e Add goto.cbc and goto.pdf
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
47 \begin{figure}[htbp]
6bf2e0196a1e Add goto.cbc and goto.pdf
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
48 \begin{center}
6bf2e0196a1e Add goto.cbc and goto.pdf
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
49 \includegraphics[scale=1.0]{fig/goto.pdf}
6bf2e0196a1e Add goto.cbc and goto.pdf
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
50 \caption{CodeSegment の軽量継続}
6bf2e0196a1e Add goto.cbc and goto.pdf
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
51 \label{fig:goto}
6bf2e0196a1e Add goto.cbc and goto.pdf
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
52 \end{center}
6bf2e0196a1e Add goto.cbc and goto.pdf
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
53 \end{figure}
13
99a9be7e6bc9 Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
54
99a9be7e6bc9 Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
55 Scheme などの call/cc といった継続はトップレベルから現在までの位置を環境として保持する。
99a9be7e6bc9 Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
56 通常環境とは関数の呼び出しスタックの状態である。
99a9be7e6bc9 Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
57 CbC の軽量継続は呼び出し元の情報を持たないため、スタックを破棄しながら処理を続けていく。
14
6bf2e0196a1e Add goto.cbc and goto.pdf
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
58 よって、リスト\ref{src:goto} のプログラムでは cs0 から cs1 へと継続した後にcs0 へ戻ることはできない。
13
99a9be7e6bc9 Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
59
15
6dedd4ed6b6d Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
60 もう少し複雑な CbC のソースコードをリスト\ref{src:factrial}に、実行される流れを図\ref{fig:factrial}に示す。
6dedd4ed6b6d Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
61 このソースコードは整数の階乗を求めるプログラムである。
6dedd4ed6b6d Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
62 CodeSegment factorial0 では自分自身への再帰的な継続を用いて階乗を計算している。
6dedd4ed6b6d Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
63 軽量継続時には関数呼び出しのスタックは存在しないが、計算中の値を DataSegment で持つことで再帰を含むループ処理も行なうことができる。
6dedd4ed6b6d Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
64
6dedd4ed6b6d Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
65 \lstinputlisting[label=src:factrial, caption=階乗を求める CbC プログラム] {src/factrial.cbc}
13
99a9be7e6bc9 Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 12
diff changeset
66
15
6dedd4ed6b6d Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
67 \begin{figure}[htbp]
6dedd4ed6b6d Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
68 \begin{center}
6dedd4ed6b6d Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
69 \includegraphics[scale=0.8]{fig/factorial.pdf}
6dedd4ed6b6d Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
70 \caption{階乗を求める CbC プログラム}
6dedd4ed6b6d Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
71 \label{fig:factrial}
6dedd4ed6b6d Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
72 \end{center}
6dedd4ed6b6d Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
73 \end{figure}
12
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74
15
6dedd4ed6b6d Update cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 14
diff changeset
75 % }}}
12
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
76
16
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
77 % {{{ MetaCodeSegment と MetaDataSegment
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
78
12
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 \section{MetaCodeSegment と MetaDataSegment}
16
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
80 プログラムを記述する際、本来行ないたい計算の他にも記述しなければならない部分が存在する。
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
81 メモリの管理やネットワーク処理、エラーハンドリングや並列処理などがこれにあたり、本来行ないたい計算と区別してメタ計算と呼ぶ。
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
82 プログラムを動作させるためにメタ計算部分は必須であり、しばしば本来の処理よりも複雑度が高い。
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
83
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
84 CodeSegment を用いたプログラミングスタイルでは計算とメタ計算を分離して記述する。
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
85 分離した計算は階層構造を持ち、本来行ないたい処理をノーマルレベルとし、メタ計算はメタレベルとしてノーマルレベルよりも上の存在に位置する。
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
86 複雑なメタ計算部分をライブラリやOS側が提供することで、ユーザはノーマルレベルの計算の記述に集中することができる。
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
87 また、ノーマルレベルのプログラムに必要なメタ計算を追加することで、並列処理やネットワーク処理などを含むプログラムに拡張できる。
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
88 さらに、ノーマルレベルからはメタレベルは隠蔽されているため、メタ計算の実装を切り替えることも可能である。
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
89 例えば、並列処理のメタ計算用いたプログラムを作成する際、CPUで並列処理を行なうメタ計算とGPUで並列処理メタ計算を環境に応じて作成することができる。
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
90
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
91 なお、メタ計算を行なう CodeSegment は Meta CodeSegment と呼び、メタ計算に必要な DataSegment は Meta DataSegment と呼ぶ。
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
92 Meta CodeSegment は CodeSegment の前後にメタ計算を挟むことで実現され、Meta DataSegment は DataSegment を含む上位の DataSegment として実現できる。
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
93 よって、メタ計算は通常の計算を覆うように計算を拡張するものだと考えられる(図\ref{fig:meta})。
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
94
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
95 \begin{figure}[htbp]
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
96 \begin{center}
87
21cc0181b4cc Update fig
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 76
diff changeset
97 \includegraphics[scale=0.5]{fig/meta.pdf}
16
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
98 \caption{Meta CodeSegment と Meta DataSegment}
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
99 \label{fig:meta}
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
100 \end{center}
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
101 \end{figure}
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
102
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
103 % }}}
3ffd17f96e06 Add meta computations
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 15
diff changeset
104
18
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
105 % {{{ Continuation based C におけるメタ計算の例: GearsOS
17
db2909ab202d Add GearsOS
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
106 \section{Continuation based C におけるメタ計算の例: GearsOS}
47
45d3ac176bf5 Mini fixes
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
107 CbC を用いてメタ計算を実現した例として、GearsOS\cite{weko_142109_1}が存在する。
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
108 GearsOS は並列に、信頼性高く動作することを目標としたOS であり、 マルチコアCPUやGPU環境での動作を対象としている。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
109 現在OSの設計と並列処理部分の実装が行なわれている。
93
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 87
diff changeset
110 GearsOS におけるメタ計算はMonad\cite{Moggi:1991:NCM:116981.116984}を用いている~\cite{kkb-master}。
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
111 現在実装済みのメタ計算はメモリの管理、並列に書き込むことが可能な Synchronized Queue、データの保存用の非破壊赤黒木がある。
12
1c9fc852e4ce Add cbc description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
112
17
db2909ab202d Add GearsOS
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
113 GearsOS では CodeSegment と DataSegment はそれぞれ CodeGear と DataGear と呼ばれている。
74
e9ff08a232f7 Add references
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
114 マルチコアCPU環境では CodeGear と CodeSegment は同一だが、GPU 環境では CodeGear には OpenCL~\cite{opencl}/CUDA~\cite{cuda} における kernel も含まれる。
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
115 kernel とは GPU で実行される関数のことであり、GPU上のメモリに配置されたデータ群に対して並列に実行される。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
116 通常 GPU でデータの処理を行なう場合は
17
db2909ab202d Add GearsOS
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 16
diff changeset
117
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
118 \begin{itemize}
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
119 \item データをメインメモリから GPUのメモリへ転送
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
120 \item 転送終了を同期で確認
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
121 \item kernel 起動(GPUメモリ上のデータに対して並列に処理)
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
122 \item 処理終了を同期で確認
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
123 \item 計算結果であるデータを GPU のメモリからメインメモリへ転送
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
124 \item 転送終了を同期で確認
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
125 \end{itemize}
18
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
126
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
127 といった手順が必要であり、ユーザは処理したいデータの位置などを意識しながらプログラミングする必要がある。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
128 GearsOS では CPU/GPU での処理をメタ計算としてユーザから隠すことにより、 CodeGear が実行されるデバイスや DataGear の位置を意識する必要がなくなる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
129
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
130 GearsOS で利用する Meta DataGear には以下のものが含まれる。
18
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
131
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
132 \begin{itemize}
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
133 \item DataGear の型情報
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
134 \item DataGear を格納するメモリの情報
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
135 \item CodeGear の名前と CodeGear の関数ポインタ との対応表
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
136 \item CodeGear が参照する DataGear へのポインタ
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
137 \end{itemize}
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
138
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
139 実際の GearsOS におけるメモリ管理を含むメタ計算用の Meta DataGear の定義例をリスト\ref{src:context}に示す。
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
140 Meta DataGear は Context という名前の構造体で定義されている。
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
141 通常レベルの DataGear も構造体で定義されているが、メタ計算側から見た DataGear はそれぞれの構造体の共用体となっており、一様に扱える。
18
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
142
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
143 \lstinputlisting[label=src:context, caption=GearsOS における Meta DataGearの定義例] {src/context.h}
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
144
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
145 リスト~\ref{src:context}のソースコードは以下のように対応している。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
146
18
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
147 \begin{itemize}
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
148 \item DataGear の型情報
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
149
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
150 DataGear は構造体を用いて定義する(リスト\ref{src:context} 27-46行)。
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
151 Tree や Node、 Allocate 構造体が DataGear に相当する。
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
152 メタ計算は任意の DataGear 扱うために全ての DataGear を扱える必要がある。
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
153 全ての DataGear の共用体を定義することで、 DataGear を一律に扱うことができる(リスト\ref{src:context} 26-47行)。
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
154 メモリを確保する場合はこの型情報からサイズを決定する。
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
155
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
156 \item DataGear を格納するメモリの情報
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
157
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
158 メモリ領域の管理は、事前に領域を確保した後、必要に応じてその領域を割り当てることで実現する。
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
159 そのために Context は割り当て済みの領域 heap と、割り当てた DataGear の数 dataNum を持つ。
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
160
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
161 \item CodeGear の名前と CodeGear の関数ポインタ との対応表
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
162
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
163 CodeGear の名前と CodeGear の関数ポインタの対応は enum と関数ポインタによって実現されている。
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
164 CodeGear の名前は enum (リスト\ref{src:context} 5-9行) で定義され、コンパイル後には整数へと変換される。
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
165 プログラム全体で利用する CodeGear は code フィールドに格納されており、enum を用いてアクセスする。
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
166 この対応表を動的に変更することにより、実行時に比較ルーチンなどを変更することが可能になる。
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
167
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
168
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
169 \item CodeGear が参照する DataGear へのポインタ
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
170
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
171 Meta CodeGear は Context を引数に取る CodeGear として定義されている。
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
172 そのため、Meta CodeGear が DataGear の値を使う為には Context から DataGear を取り出す必要がある。
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
173 取り出す必要がある DataGear は enum を用いて定義し(リスト\ref{src:context} 11-14行)、 CodeGear を実行する際に data フィールドから取り出す。
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
174 \end{itemize}
415fa6d79d00 Add GearsOS description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
175
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
176 Meta CodeGear は定義された Meta DataGear を処理する CodeGear である。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
177 メモリ管理や並列処理の待ち合わせといった処理はこのメタレベルにしか表れない。
26
5510bb043a74 Add rbtree description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 25
diff changeset
178
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
179 GearsOS においては軽量継続もメタ計算として実現されている。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
180 とある CodeGear から次の CodeGear へと軽量継続する際には、次に実行される CodeGear の名前を指定する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
181 その名前を Meta CodeGear が解釈し、対応する CodeGear へと処理を引き渡す(リスト~\ref{src:meta}の \verb/meta/)。
27
243d8dc4a292 Add akasha description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 26
diff changeset
182
28
36ce493604fb Add akasha result
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 27
diff changeset
183 \lstinputlisting[label=src:meta, caption=通常の CodeSegment の軽量継続] {src/meta.c}
27
243d8dc4a292 Add akasha description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 26
diff changeset
184
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
185 CodeGear と名前の対応は Meta DataGear に格納されており、従来の OS の Process や Thread に相当する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
186 名前の対応を動的に切り替えたり、Thread ごとに切り替えることにより、通常レベルのプログラムを変更せず実行を上書きできる。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
187 これは従来の OS の Dynamic Loading Libary や Command の呼び出しに相当する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
188
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
189 また、通常レベルの CodeGear から Meta DataGear を操作できてしまうと、ユーザがメタレベル操作を自由に記述できてしまい、メタ計算を分離した意味が無くなってしまう。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
190 これを防ぐために、CodeGear を実行する際は Meta DataGear から必要な DataGear だけを渡す。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
191 このように、 Meta DataGear から DataGear を取り出す Meta CodeGear を stub と呼ぶ。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
192 stub の例をリスト\ref{src:stub}に示す。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
193 \lstinputlisting[label=src:stub, caption=GearsOS における stub Meta CodeSegment] {src/stub.cbc}
28
36ce493604fb Add akasha result
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 27
diff changeset
194
76
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
195 stub は Context が持つ DataGear のポインタ data に対して enum を用いてアクセスしている。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
196 なお、現在はメタレベルの計算とノーマルレベルの分離はコンパイラ側がサポートしていないため、引数に Meta DataGear である Context が渡されているが、本来はノーマルレベルではアクセスできない。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
197 % また、stub は全ての CodeGear に対してユーザが1つずつ定義する必要があるため、 CodeGear の定義が煩雑になっている。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
198 % レベルの分離と stub の自動生成を行なうスクリプトも存在しているが、コンパイラ側のサポートはまだ行なわれていない。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
199 % 型情報を用いたコンパイル時 stub 生成や、軽量継続時の型チェックを行なうために本研究では CbC の型システムを定義する。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
200
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
201 また、GearsOS におけるメタ計算として CodeGear のモデル検査がある。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
202 通常レベルの CodeGear を変更することなく、その仕様を検証するものである。
a9ed6a6dc1f2 Add chapter akasha
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
203 個々の CodeGear の仕様を検証することにより、より信頼性の高いOSを目指す。
28
36ce493604fb Add akasha result
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 27
diff changeset
204
36ce493604fb Add akasha result
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 27
diff changeset
205 % }}}