Main Content

時間カウンターのメモリ使用量の最適化

この例は、コード ジェネレーターが時間カウンターに割り当てるメモリ量を最適化する方法を示しています。この例では、ブロックの 2 つの連続する実行間の時間間隔である経過時間を保存するメモリを最適化します。

コード ジェネレーターは、絶対時間と経過時間のタイマーを符号なし整数として表します。生成されたコードでは、ワード サイズが最適化されます。コード ジェネレーターは、ワード サイズが 8、16、32、または 64 ビットの符号なし整数として時間カウンターを保存します。生成されたコードでは、ワード サイズは、アルゴリズムが使用する最大時間刻み数に対応できる最小サイズに自動的に設定されます。コード ジェネレーターはモデル コンフィギュレーション パラメーターApplication lifespan (days)およびClock resolution (seconds, -1 for inherited)の設定を使用して、最大時間刻み数を計算します。アプリケーションのライフスパンは、経過時間または絶対時間に応じてブロックまたはチャートを含むアプリケーションがタイマー オーバーフローの発生までに実行できる日数です。クロック分解能は、秒単位のクロック値の最小のインクリメントです。コード ジェネレーターがこれらのパラメーターを使用して、アルゴリズムが使用する最大時間刻み数を計算する方法の詳細については、Timer Data Types and Memory Allocationを参照してください。

モデル例を開く

モデル例 TimerMemoryOptimization を開きます。

open_system('TimerMemoryOptimization');

モデルはサブシステム SS1SS2、および SS3 で構成され、コード ジェネレーターが GRT システム ターゲット ファイルと inf 日のライフスパンを使用するように構成されています。

3 つのサブシステムには、出力値を計算するための入力として経過時間を必要とする Discrete-time Integrator が含まれます。サブシステムは、次のように変わります。

  • SS1 - 1 kHz のクロックで動作します。時間カウンターは必要ありません。トリガー端子の [サンプル時間タイプ] パラメーターは、periodic に設定されます。経過時間は 0.001 としてインライン化されます。

  • SS2 - 100 Hz のクロックで動作します。時間カウンターが必要です。1 日のライフスパンに基づいて、32 ビット カウンターに経過時間が保存されます。

  • SS3 - 0.5 Hz のクロックで動作します。時間カウンターが必要です。1 日のライフスパンに基づいて、16 ビット カウンターに経過時間が保存されます。

モデルのシミュレーション

モデルのシミュレーションを実行します。既定の設定では、モデルはサンプル時間を異なる色で表示します。3 つのサブシステムの離散サンプル時間は、赤、緑、青で表示されます。トリガーされたサブシステムは、薄い青のボックスで強調表示されます。

コードとレポートの生成

コードとコード生成レポートを生成します。

slbuild('TimerMemoryOptimization');
### Starting build procedure for: TimerMemoryOptimization
### Successful completion of build procedure for: TimerMemoryOptimization

Build Summary

Top model targets built:

Model                    Action                        Rebuild Reason                                    
=========================================================================================================
TimerMemoryOptimization  Code generated and compiled.  Code generation information file does not exist.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 26.655s

生成コードのレビュー

生成されたソース ファイル TimerMemoryOptimization.h を開きます。

cfile = fullfile('TimerMemoryOptimization_grt_rtw', 'TimerMemoryOptimization.h');
coder.example.extractLines(cfile,'/* Real-time Model Data Structure */', '/* Block states (auto storage) */', 0, 1);
struct tag_RTM_TimerMemoryOptimizati_T {
  const char_T *errorStatus;

  /*
   * Timing:
   * The following substructure contains information regarding
   * the timing information for the model.
   */
  struct {
    uint32_T clockTick1;
    uint32_T clockTickH1;
    uint32_T clockTick2;
    uint32_T clockTickH2;
    struct {
      uint16_T TID[3];
      uint16_T cLimit[3];
    } TaskCounters;
  } Timing;
};

/* Block states (default storage) */
extern DW_TimerMemoryOptimization_T TimerMemoryOptimization_DW;

/* Zero-crossing (trigger) state */
extern PrevZCX_TimerMemoryOptimizati_T TimerMemoryOptimization_PrevZCX;

/* External inputs (root inport signals with default storage) */
extern ExtU_TimerMemoryOptimization_T TimerMemoryOptimization_U;

/* External outputs (root outports fed by signals with default storage) */
extern ExtY_TimerMemoryOptimization_T TimerMemoryOptimization_Y;

/* Model entry point functions */
extern void TimerMemoryOptimization_initialize(void);
extern void TimerMemoryOptimization_step0(void);
extern void TimerMemoryOptimization_step1(void);
extern void TimerMemoryOptimization_step2(void);
extern void TimerMemoryOptimization_terminate(void);

/* Real-time Model object */
extern RT_MODEL_TimerMemoryOptimizat_T *const TimerMemoryOptimization_M;

/*-
 * The generated code includes comments that allow you to trace directly
 * back to the appropriate location in the model.  The basic format
 * is <system>/block_name, where system is the system number (uniquely
 * assigned by Simulink) and block_name is the name of the block.
 *
 * Use the MATLAB hilite_system command to trace the generated code back
 * to the model.  For example,
 *
 * hilite_system('<S3>')    - opens system 3
 * hilite_system('<S3>/Kp') - opens and selects block Kp which resides in S3
 *
 * Here is the system hierarchy for this model
 *
 * '<Root>' : 'TimerMemoryOptimization'
 * '<S1>'   : 'TimerMemoryOptimization/SS1'
 * '<S2>'   : 'TimerMemoryOptimization/SS2'
 * '<S3>'   : 'TimerMemoryOptimization/SS3'
 */
#endif                                 /* TimerMemoryOptimization_h_ */

4 つの 32 ビット符号なし整数の clockTick1clockTickH1clockTick2 および clockTickH2 が、サブシステム SS2SS3 の経過時間を格納するカウンターになります。

最適化の有効化とコードの再生成

1. モデル コンフィギュレーション パラメーター [アプリケーションのライフスパン (日)] (LifeSpan) を 1 日に設定します。

set_param('TimerMemoryOptimization', 'LifeSpan', '1');

2. モデルを保存し、コードを再生成します。

slbuild('TimerMemoryOptimization');
### Starting build procedure for: TimerMemoryOptimization
### Successful completion of build procedure for: TimerMemoryOptimization

Build Summary

Top model targets built:

Model                    Action                        Rebuild Reason                 
======================================================================================
TimerMemoryOptimization  Code generated and compiled.  Incremental checksum changed.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 17.616s

再生成コードのレビュー

cfile = fullfile('TimerMemoryOptimization_grt_rtw', 'TimerMemoryOptimization.h');
coder.example.extractLines(cfile,'/* Real-time Model Data Structure */', '/* Block states (auto storage) */', 0, 1);
struct tag_RTM_TimerMemoryOptimizati_T {
  const char_T *errorStatus;

  /*
   * Timing:
   * The following substructure contains information regarding
   * the timing information for the model.
   */
  struct {
    uint32_T clockTick1;
    uint16_T clockTick2;
    struct {
      uint16_T TID[3];
      uint16_T cLimit[3];
    } TaskCounters;
  } Timing;
};

/* Block states (default storage) */
extern DW_TimerMemoryOptimization_T TimerMemoryOptimization_DW;

/* Zero-crossing (trigger) state */
extern PrevZCX_TimerMemoryOptimizati_T TimerMemoryOptimization_PrevZCX;

/* External inputs (root inport signals with default storage) */
extern ExtU_TimerMemoryOptimization_T TimerMemoryOptimization_U;

/* External outputs (root outports fed by signals with default storage) */
extern ExtY_TimerMemoryOptimization_T TimerMemoryOptimization_Y;

/* Model entry point functions */
extern void TimerMemoryOptimization_initialize(void);
extern void TimerMemoryOptimization_step0(void);
extern void TimerMemoryOptimization_step1(void);
extern void TimerMemoryOptimization_step2(void);
extern void TimerMemoryOptimization_terminate(void);

/* Real-time Model object */
extern RT_MODEL_TimerMemoryOptimizat_T *const TimerMemoryOptimization_M;

/*-
 * The generated code includes comments that allow you to trace directly
 * back to the appropriate location in the model.  The basic format
 * is <system>/block_name, where system is the system number (uniquely
 * assigned by Simulink) and block_name is the name of the block.
 *
 * Use the MATLAB hilite_system command to trace the generated code back
 * to the model.  For example,
 *
 * hilite_system('<S3>')    - opens system 3
 * hilite_system('<S3>/Kp') - opens and selects block Kp which resides in S3
 *
 * Here is the system hierarchy for this model
 *
 * '<Root>' : 'TimerMemoryOptimization'
 * '<S1>'   : 'TimerMemoryOptimization/SS1'
 * '<S2>'   : 'TimerMemoryOptimization/SS2'
 * '<S3>'   : 'TimerMemoryOptimization/SS3'
 */
#endif                                 /* TimerMemoryOptimization_h_ */

[アプリケーションのライフスパン (日)] パラメーターの新しい設定では、コード ジェネレーターに、時間カウンター用に確保するメモリを少なくするよう指示します。生成コードには次が含まれます。

  • 32 ビット符号なし整数 clockTick1SS2 のタスクの経過時間を計算して保存します。

  • 16 ビット符号なし整数 clockTick2SS3 のタスクの経過時間を計算して保存します。

関連するトピック