Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

勾配バイアス スケーリングのコード置換

固定小数点データ型に対する演算のコード置換を、演算子の入力と出力における勾配バイアス スケーリングの組み合わせと一致するように定義できます。この例では、division 演算子に対するコード置換の定義方法に関する情報を提供することで、固定小数点データ型演算のパフォーマンスを最適化するコード置換ライブラリを開発する方法を示します。コード置換ライブラリを開発するには、プログラムによる方法のみを使用します。詳細については、コード置換ライブラリの開発を参照してください。

この例では、固定小数点の加算の置き換えを、実装関数が入力データ型と出力データ型の小数部の長さを引数として渡すように変更します。

コード置換のマッチングと置換プロセスにロジックを追加するカスタム コード置換エントリを作成するには、Customize Code Match and Replacement for Scalar Operationsを参照してください。クラス定義内またはこのクラスをインスタンス化する各コード置換エントリ定義内の小数部の長さを渡すために 3 つの追加の実装関数引数を作成して追加できます。この例では、引数を作成してコード置換テーブル定義ファイルに追加し、クラス定義コード内の特定の値に設定します。

classdef TflCustomOperationEntrySlopeBias < RTW.TflCOperationEntryML
  methods
    function ent = do_match(hThis, ...
        hCSO, ... %#ok
        targetBitPerChar, ... %#ok
        targetBitPerShort, ... %#ok
        targetBitPerInt, ... %#ok
        targetBitPerLong, ... %#ok
        targetBitPerLongLong) %#ok

      % DO_MATCH - Create a custom match function. The base class
      % checks the types of the arguments prior to calling this
      % method. This class will check additional data and can
      % modify the implementation function.

      % The base class checks word size and signedness. Slopes and biases
      % have been wildcarded, so the only additional checking to do is
      % to check that the biases are zero and that there are only three
      % conceptual arguments (one output, two inputs)

      ent = []; % default the return to empty, indicating the match failed

      if length(hCSO.ConceptualArgs) == 3

        % Modify the default implementation. Since this is a
        % generator entry, a concrete entry is created using this entry
        % as a template. The type of entry being created is a standard
        % TflCOperationEntry. Using the standard operation entry
        % provides required information, and you do not need
        % a custom match function.
        ent = RTW.TflCOperationEntry(hThis);

        % Since this entry is modifying the implementation for specific
        % fraction-length values (arguments 3, 4, and 5), the conceptual argument
        % wild cards must be removed (the wildcards were inherited from the
        % generator when it was used as a template for the concrete entry).
        % This concrete entry is now for a specific slope and bias. 
        % hCSO holds the slope and bias values (created by the code generator).
        for idx=1:3
          ent.ConceptualArgs(idx).CheckSlope = true;
          ent.ConceptualArgs(idx).CheckBias = true;

          % Set the specific Slope and Biases
          ent.ConceptualArgs(idx).Type.Slope = hCSO.ConceptualArgs(idx).Type.Slope;
          ent.ConceptualArgs(idx).Type.Bias = hCSO.ConceptualArgs(idx).Type.Bias;
        end

        % Set the Slope and Bias values in the implementation function.
        ent.Implementation.Arguments(3).Value = hCSO.ConceptualArgs(2).Type.Slope;
        ent.Implementation.Arguments(4).Value = hCSO.ConceptualArgs(2).Type.Bias;
        ent.Implementation.Arguments(5).Value = hCSO.ConceptualArgs(3).Type.Slope;
        ent.Implementation.Arguments(6).Value = hCSO.ConceptualArgs(3).Type.Bias;
        ent.Implementation.Arguments(7).Value = hCSO.ConceptualArgs(1).Type.Slope;
        ent.Implementation.Arguments(8).Value = hCSO.ConceptualArgs(1).Type.Bias;
      end
    end
  end
end

プログラムによるコード置換ライブラリの開発

  1. [新規][関数] を選択し、[MATLAB] メニューからプログラム インターフェイスを開きます。

  2. table を作成します。

    1. 引数をもたず table オブジェクトを返す、コード置換ライブラリ テーブルの名前をもつ関数を作成します。この関数を使用して、コード置換ライブラリ テーブルを呼び出すことができます。

    2. RTW.TflTable を呼び出して table オブジェクトを作成します。

    function hTable = crl_table_custom_add_ufix32_slopebias
    % Create a function to call the code replacement library table 
    
    %% Create a table object
    hTable = RTW.TflTable;
    
  3. エントリを作成します。この例では関数を置き換えるため、カスタム エントリ関数を呼び出して table にコード置換エントリを作成します。

    function hTable = crl_table_custom_add_ufix32_slopebias
    % Create a function to call the code replacement library table 
    
    %% Create a table object
    hTable = RTW.TflTable;
    
    %% Create an entry
    hEntry = TflCustomOperationEntrySlopeBias;;
  4. エントリ パラメーターを作成します。この例では関数を置き換えるため、関数 setTflCFunctionEntryParameters を呼び出してエントリ パラメーターを作成します。

    function hTable = crl_table_custom_add_ufix32_slopebias
    % Create a function to call the code replacement library table 
    
    %% Create a table object
    hTable = RTW.TflTable;
    
    %% Create an entry
    hEntry = TflCustomOperationEntrySlopeBias;;
    
    %% Create entry parameters
    setTflCOperationEntryParameters(hEntry, ...
        'Key',                      'RTW_OP_ADD', ...
        'Priority',                 30, ...
        'SaturationMode',           'RTW_SATURATE_ON_OVERFLOW', ...
        'RoundingModes',            {'RTW_ROUND_FLOOR'}, ...
        'ImplementationName',       'myFixptAdd_slopebias', ...
        'ImplementationHeaderFile', 'myFixptAdd.h', ...
        'ImplementationSourceFile', 'myFixptAdd.c');
  5. 概念表現を作成します。概念表現は、置換する関数のシグネチャを記述します。引数プロパティを明示的に指定するには、関数 createAndAddConceptualArg を呼び出します。

    function hTable = crl_table_custom_add_ufix32_slopebias
    % Create a function to call the code replacement library table 
    
    %% Create a table object
    hTable = RTW.TflTable;
    
    %% Create an entry
    hEntry = TflCustomOperationEntrySlopeBias;;
    
    %% Create entry parameters
    setTflCOperationEntryParameters(hEntry, ...
        'Key',                      'RTW_OP_ADD', ...
        'Priority',                 30, ...
        'SaturationMode',           'RTW_SATURATE_ON_OVERFLOW', ...
        'RoundingModes',            {'RTW_ROUND_FLOOR'}, ...
        'ImplementationName',       'myFixptAdd_slopebias', ...
        'ImplementationHeaderFile', 'myFixptAdd.h', ...
        'ImplementationSourceFile', 'myFixptAdd.c');
    
    %% Create the conceptual representation
    createAndAddConceptualArg(hEntry, 'RTW.TflArgNumeric', ...
         'Name',       'y1', ...
         'IOType',     'RTW_IO_OUTPUT', ...
         'CheckSlope', false, ...
         'CheckBias',  false, ...
         'DataType',   'Fixed', ...
         'Scaling',    'SlopeBias', ...
         'IsSigned',   false, ...
         'WordLength', 32);
    
    createAndAddConceptualArg(hEntry, 'RTW.TflArgNumeric', ...
          'Name',       'u1', ...
          'IOType',     'RTW_IO_INPUT', ...
          'CheckSlope', false, ...
          'CheckBias',  false, ...
          'DataType',   'Fixed', ...
          'Scaling',    'SlopeBias', ...
          'IsSigned',   false, ...
          'WordLength', 32);
    
    createAndAddConceptualArg(hEntry, 'RTW.TflArgNumeric', ...
           'Name',       'u2', ...
           'IOType',     'RTW_IO_INPUT', ...
           'CheckSlope', false, ...
           'CheckBias',  false, ...
           'DataType',   'Fixed', ...
           'Scaling',    'SlopeBias', ...
           'IsSigned',   false, ...
           'WordLength', 32);
  6. 実装表現を作成します。実装表現は最適化関数のシグネチャを記述します。実装引数が概念引数と同じ順序とプロパティをもつように指定するには、関数 createAndSetCImplementationReturn および createAndAddImplementationArg を呼び出します。関数 addEntry を呼び出して、table に完全なエントリを追加します。

    function hTable = crl_table_custom_add_ufix32_slopebias
    % Create a function to call the code replacement library table 
    
    %% Create a table object
    hTable = RTW.TflTable;
    
    %% Create an entry
    hEntry = TflCustomOperationEntrySlopeBias;;
    
    %% Create entry parameters
    setTflCOperationEntryParameters(hEntry, ...
        'Key',                      'RTW_OP_ADD', ...
        'Priority',                 30, ...
        'SaturationMode',           'RTW_SATURATE_ON_OVERFLOW', ...
        'RoundingModes',            {'RTW_ROUND_FLOOR'}, ...
        'ImplementationName',       'myFixptAdd_slopebias', ...
        'ImplementationHeaderFile', 'myFixptAdd.h', ...
        'ImplementationSourceFile', 'myFixptAdd.c');
    
    %% Create the conceptual representation
    createAndAddConceptualArg(hEntry, 'RTW.TflArgNumeric', ...
         'Name',       'y1', ...
         'IOType',     'RTW_IO_OUTPUT', ...
         'CheckSlope', false, ...
         'CheckBias',  false, ...
         'DataType',   'Fixed', ...
         'Scaling',    'SlopeBias', ...
         'IsSigned',   false, ...
         'WordLength', 32);
    
    createAndAddConceptualArg(hEntry, 'RTW.TflArgNumeric', ...
          'Name',       'u1', ...
          'IOType',     'RTW_IO_INPUT', ...
          'CheckSlope', false, ...
          'CheckBias',  false, ...
          'DataType',   'Fixed', ...
          'Scaling',    'SlopeBias', ...
          'IsSigned',   false, ...
          'WordLength', 32);
    
    createAndAddConceptualArg(hEntry, 'RTW.TflArgNumeric', ...
           'Name',       'u2', ...
           'IOType',     'RTW_IO_INPUT', ...
           'CheckSlope', false, ...
           'CheckBias',  false, ...
           'DataType',   'Fixed', ...
           'Scaling',    'SlopeBias', ...
           'IsSigned',   false, ...
           'WordLength', 32);
    
    %% Create the implementation Representation
    % Specify replacement function signature
    createAndSetCImplementationReturn(hEntry, 'RTW.TflArgNumeric', ...
        'Name',       'y1', ...
        'IOType',     'RTW_IO_OUTPUT', ...
        'IsSigned',   false, ...
        'WordLength', 32, ...
        'FractionLength', 0);
    
    createAndAddImplementationArg(hEntry, 'RTW.TflArgNumeric', ...
        'Name',       'u1', ...
        'IOType',     'RTW_IO_INPUT', ...
        'IsSigned',   false, ...
        'WordLength', 32, ...
        'FractionLength', 0);
    
    createAndAddImplementationArg(hEntry, 'RTW.TflArgNumeric', ...
        'Name',       'u2', ...
        'IOType',     'RTW_IO_INPUT', ...
        'IsSigned',   false, ...
        'WordLength', 32, ...
        'FractionLength', 0);
    
    % Add 3 fraction-length args. Actual values are set during code generation.
    createAndSetCImplementationReturn(hEntry, 'RTW.TflArgNumeric', ...
        'Name',       'y1', ...
        'IOType',     'RTW_IO_OUTPUT', ...
        'IsSigned',   false, ...
        'WordLength', 32, ...
        'FractionLength', 0);
    
    createAndAddImplementationArg(hEntry, 'RTW.TflArgNumeric', ...
        'Name',       'u1', ...
        'IOType',     'RTW_IO_INPUT', ...
        'IsSigned',   false, ...
        'WordLength', 32, ...
        'FractionLength', 0);
    
    createAndAddImplementationArg(hEntry, 'RTW.TflArgNumeric', ...
        'Name',       'u2', ...
        'IOType',     'RTW_IO_INPUT', ...
        'IsSigned',   false, ...
        'WordLength', 32, ...
        'FractionLength', 0);
    
    % Add 6 args for slopes and biases. 
    % Actual values are set during code generation.
    createAndAddImplementationArg(hEntry, 'RTW.TflArgNumericConstant', ...
        'Name',       'slope_in1', ...
        'IOType',     'RTW_IO_INPUT', ...
        'IsSigned',   false, ...
        'WordLength', 32, ...
        'FractionLength', 0, ...
        'Value',       0);
    
    createAndAddImplementationArg(hEntry, 'RTW.TflArgNumericConstant', ...
        'Name',       'bias_in1', ...
        'IOType',     'RTW_IO_INPUT', ...
        'IsSigned',   true, ...
        'WordLength', 32, ...
        'FractionLength', 0, ...
        'Value',       0);
    
    createAndAddImplementationArg(hEntry, 'RTW.TflArgNumericConstant', ...
        'Name',       'slope_in2', ...
        'IOType',     'RTW_IO_INPUT', ...
        'IsSigned',   false, ...
        'WordLength', 32, ...
        'FractionLength', 0, ...
        'Value',       0);
    
    createAndAddImplementationArg(hEntry, 'RTW.TflArgNumericConstant', ...
        'Name',       'bias_in2', ...
        'IOType',     'RTW_IO_INPUT', ...
        'IsSigned',   true, ...
        'WordLength', 32, ...
        'FractionLength', 0, ...
        'Value',       0);
    
    createAndAddImplementationArg(hEntry, 'RTW.TflArgNumericConstant', ...
        'Name',       'slope_out1', ...
        'IOType',     'RTW_IO_INPUT', ...
        'IsSigned',   false, ...
        'WordLength', 32, ...
        'FractionLength', 0, ...
        'Value',       0);
    
    createAndAddImplementationArg(hEntry, 'RTW.TflArgNumericConstant', ...
        'Name',       'bias_out1', ...
        'IOType',     'RTW_IO_INPUT', ...
        'IsSigned',   true, ...
        'WordLength', 32, ...
        'FractionLength', 0, ...
        'Value',       0);
    
    addEntry(hTable, hEntry);
  7. ビルド情報を指定します。エントリ パラメーターで、コード ジェネレーターがコード置換に必要なファイル (ソース、ヘッダー、オブジェクト) を指定します。この例では、ビルド情報は必要ありません。

  8. カスタマイズ ファイルを確認して保存します。[MATLAB] メニューから、[ファイル][保存] を選択して、このカスタマイズ ファイルを保存します。コマンド ラインから、次のようにしてコード置換ライブラリ テーブルを呼び出し、確認します。

    >> hTable = crl_table_custom_add_ufix32_slopebias
  9. コード置換ライブラリを登録します。登録では、ライブラリ名、コード置換テーブル、その他の情報を定義することにより、コード置換ライブラリが作成されます。以下の仕様を使用して登録ファイル (新しい関数ファイル) を作成します。

    function rtwTargetInfo(cm)
     
    cm.registerTargetInfo(@loc_register_crl);
    end
     
    function this = loc_register_crl 
     
    this(1) = RTW.TflRegistry; 
    this(1).Name = 'CRL for slope bias scaling function replacement’;
    this(1).TableList = {'crl_table_custom_add_ufix32_slopebias.m'}; 
    % table created in this example
    this(1).TargetHWDeviceType = {'*'};
    this(1).Description = 'Example code replacement library';
    
    end
    

    コード置換ライブラリを使用するには、現在の MATLAB セッションを次のコマンドで更新します。

    >>sl_refresh_customizations

  10. コード置換ライブラリを確認します。MATLAB コマンド ラインから、コード置換ビューアーを使用してライブラリを開き、table およびエントリが正しく指定されていることを確認します。詳細については、Verify Code Replacement Libraryを参照してください。コード置換ライブラリを使用するようにモデルを構成し、コードを生成して、その置換が予期したとおりに行われることを確認します。予期しない動作が行われた場合、ヒット ログとミス ログを調べて問題のトラブルシューティングを行います。

関連するトピック