bambooflow Note

並列処理

最終更新:

bambooflow

- view
メンバー限定 登録/ログイン

SystemC 並列処理動作について


ここでは、SystemCの機能のひとつである並列処理動作について説明する。
2つのプロセスを作り、並列動作を体感してみる。


サンプル

multi_process.tgz
  • main.cpp
  • Makefile

プログラム

  • main.cpp
#include <systemc.h>

 
SC_MODULE( MyModel )
{
    // constructor
    SC_CTOR( MyModel ) {
 
        SC_THREAD( thread0 );
        SC_THREAD( thread1 );
    }
 
    // process0
    void thread0() {
 
        while (true) {
            cout << "----thread0----------" << endl;
            wait( 10.0, SC_US );
        }
 
    }
 
    // process1
    void thread1() {
        while (true) {
            cout << "-----------thread1---" << endl;
            wait( 10.0, SC_US );
        }
    }
};
 
int sc_main( int argc, char* argv[] )
{
    MyModel *myModel;
 
    // module instance
    myModel = new MyModel( "MyModel" );
    sc_assert( myModel );
 
    /////////////////////////////////////////////////////////////////////////
 
    // initialize
    sc_start( SC_ZERO_TIME );
    cout << "now time is " << sc_time_stamp() << endl;
 
    // simulation
    sc_start( 10.0, SC_US );
    cout << "now time is " << sc_time_stamp() << endl;
 
    sc_start( 10.0, SC_US );
    cout << "now time is " << sc_time_stamp() << endl;
 
    sc_start( 100.0, SC_US );
    cout << "now time is " << sc_time_stamp() << endl;
 
    sc_start( 50.0, SC_US );
    cout << "now time is " << sc_time_stamp() << endl;
 
    /////////////////////////////////////////////////////////////////////////
 
    delete myModel;
 
    return 0;
}
 

実行結果

            SystemC 2.2.0 --- Jul  1 2008 00:09:35
       Copyright (c) 1996-2006 by all Contributors
                   ALL RIGHTS RESERVED
----thread0----------
-----------thread1---
now time is 0 s
now time is 10 us
----thread0----------
-----------thread1---
now time is 20 us
-----------thread1---
----thread0----------
-----------thread1---
  ・・・
----thread0----------
-----------thread1---
----thread0----------
now time is 120 us
----thread0----------
-----------thread1---
----thread0----------
   ・・・
-----------thread1---
----thread0----------
-----------thread1---
now time is 170 us


説明

このプログラムではMyModelというSC_MODULEを1つ定義してsc_main()でインスタンスしている。
このMyModelは2つのプロセスをもつ。プロセス名は、thread0とthread1。
この2つのプロセスは、シミュレーション開始(sc_start)とともに実行される。
ここで、注目してほしいことは、thread0とthread1はどちらもwhile(true){}の無限ループを構成していること。
SC_THREADの処理はwaitによりいったん停止することができる。
wait( 10.0, SC_US );
この記述は、ここで処理を一端停止し、シミュレーション時間が10us進んだらこのwaitを抜けて続きの処理をするものである。第1引数はdouble型、第2引数は時間の単位を指定する。
時間の単位はSC_SEC(秒)からSC_FS(フェムト秒)まで指定できる。
今回は、SC_US(マイクロ秒)を単位として指定した。

thread0とthread1のwaitの時間はともに10usなのでどちらの処理が先に実行されるかはわからない。
どちらが先に処理されるかの順番はSystemCでは保証されない。
実行結果をみるとsc_start()を実行するたびにthread0とthread1の実行の順番が入れ替わっていることに注目してほしい。

プロセスは、posixもしくはpthreadというCライブラリのスレッド・モデルを利用して実現しているようだ。

タグ:

SystemC 並列処理
添付ファイル
記事メニュー
目安箱バナー