Main Content

行列乗算演算から ANSI/ISO C BLAS へのコード置換

浮動小数点の行列/行列乗算演算と行列/ベクトル乗算演算を MathWorks C BLAS ライブラリで定義されている乗算関数 sgemm に置き換えるためのコード置換ライブラリを開発できます。サードパーティの置換用 C BLAS ライブラリを使用して、自分のライブラリを指すようにこの例のビルド要件を変更します。コード置換ライブラリを開発するには、対話的な方法またはプログラムによる方法のいずれかを使用します。詳細については、コード置換ライブラリの開発を参照してください。

対話的なコード置換ライブラリの開発

  1. MATLAB コマンド ラインから次のコマンドを使用して コード置換ツール (crtool) を開きます。

    >>crtool
  2. table を作成します。

    1. crtool のコンテキスト メニューから、[ファイル][新しいテーブル] を選択します。

    2. 右側のペインで、table に crl_table_cblas と名前を付けます。[適用] をクリックします。

  3. エントリを作成します。crtool のコンテキスト メニューから、[編集][新しいエントリ][CBlas Operation (Atlas)] を選択します。

  4. エントリ パラメーターを作成します。[演算] ドロップダウン リストで、[乗算] を選択します。

  5. 概念表現を作成します。概念表現は、置換する関数のシグネチャを記述します。crtool の [概念関数] サブセクションで、single の [データ型] および行列の [引数タイプ] をもつ戻り引数 y1 と入力引数 u1 および u2 を指定します。

  6. 実装表現を作成します。実装表現は最適化関数のシグネチャを記述します。この例では、概念引数と同じ順序およびプロパティをもつ実装引数を指定するために、[概念引数と実装引数のタイプを同じにする] チェック ボックスをオンにします。[BLAS レベル]3(Vector) に設定します。

    [関数プロトタイプ] の下にある置換関数に対して [名前] を指定します。

  7. ビルド情報を指定します。[ビルド情報] タブをクリックして、ビルド要件のペインを開きます。コード ジェネレーターがコード置換に必要なファイル (ソース、ヘッダー、オブジェクト) を指定します。この例では、ビルド情報を指定する必要はありません。

  8. table を確認して保存します。[マッピング情報] タブで、[エントリの検証] をクリックします。crtool のコンテキスト メニューから、[ファイル][テーブルの保存][保存] を選択します。

  9. コード置換ライブラリを登録します。登録することにより、指定した table で構成されるライブラリが作成されます。[ファイル][登録ファイルの作成] を選択します。[登録ファイルの作成] ダイアログ ボックスで、以下のフィールドに入力します。

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

    >>sl_refresh_customizations

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

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

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

  2. table を作成します。

    1. コード置換ライブラリ テーブルを呼び出す関数を作成します。この関数は引数をもつべきではなく、table オブジェクトを返します。

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

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

    function hTable = crl_table_cblas
    % Create a code replacement library table 
    
    %% Create a table object
    hTable = RTW.TflTable;
    
    %% Create an entry
    %%%%%%%%%%%% Define library path Cblas library file%%%%%%%%%%%%%%%%%%
    
    LibPath = fullfile('<Specify the path of your cblas library>');
    
    if ispc
        libExt = 'lib';
    elseif ismac
        libExt = 'dylib';
    else
        libExt = 'so';
    end
    
    
    % Create table entry for dgemm32
    hEntry = RTW.TflCBlasEntryGenerator;
  4. エントリ パラメーターを作成します。この例では関数を置き換えるため、関数 setTflCFunctionEntryParameters を呼び出してエントリ パラメーターを作成します。

    function hTable = crl_table_cblas
    % Create a code replacement library table 
    
    %% Create a table object
    hTable = RTW.TflTable;
    
    %% Create an entry
    %%%%%%%%%%%% Define library path Cblas library file%%%%%%%%%%%%%%%%%%
    
    LibPath = fullfile('<Specify the path of your cblas library>');
    
    if ispc
        libExt = 'lib';
    elseif ismac
        libExt = 'dylib';
    else
        libExt = 'so';
    end
    
    
    % Create table entry for dgemm32
    hEntry = RTW.TflCBlasEntryGenerator;
    
    %% Create entry parameters
    hEntry.setTflCOperationEntryParameters( ...
        'Key', 'RTW_OP_MUL', ...
        'Priority', 100, ...
        'ImplementationName', 'cblas_sgemm', ...
        'ImplementationSourceFile', 'ccblas.c', ...
        'ImplementationHeaderFile', 'ccblas.h', ...
        'AdditionalIncludePaths',   {LibPath}, ...
        'AdditionalLinkObjs',       {['libcblas' libExt]}, ...
        'AdditionalLinkFlags',        {'-lgfortran'},...
        'SideEffects', true);
  5. 概念表現を作成します。概念表現は、置換する関数のシグネチャを記述します。引数プロパティを明示的に指定するには、関数 createAndAddConceptualArg を呼び出します。関数呼び出しで行列引数を指定するには、引数クラス RTW.TflArgMatrix を使用して、基本データ型と引数が有効な次元を指定します。このような table エントリでは、 [Dim1Min Dim2Min ... DimNMin; Dim1Max Dim2Max ... DimNMax] という形式で指定される次元の範囲がサポートされます。たとえば、[2 2; inf inf] は、サイズが 2 行 2 列以上の 2 次元行列を意味します。

    function hTable = crl_table_cblas
    % Create a code replacement library table 
    
    %% Create a table object
    hTable = RTW.TflTable;
    
    %% Create an entry
    %%%%%%%%%%%% Define library path Cblas library file%%%%%%%%%%%%%%%%%%
    
    LibPath = fullfile('<Specify the path of your cblas library>');
    
    if ispc
        libExt = 'lib';
    elseif ismac
        libExt = 'dylib';
    else
        libExt = 'so';
    end
    
    
    % Create table entry for dgemm32
    hEntry = RTW.TflCBlasEntryGenerator;
    
    %% Create entry parameters
    hEntry.setTflCOperationEntryParameters( ...
        'Key', 'RTW_OP_MUL', ...
        'Priority', 100, ...
        'ImplementationName', 'cblas_sgemm', ...
        'ImplementationSourceFile', 'ccblas.c', ...
        'ImplementationHeaderFile', 'ccblas.h', ...
        'AdditionalIncludePaths',   {LibPath}, ...
        'AdditionalLinkObjs',       {['libcblas' libExt]}, ...
        'AdditionalLinkFlags',      {'-lgfortran'},...
        'SideEffects', true);
    
    %% Create the conceptual representation
    arg = RTW.TflArgMatrix('y1', 'RTW_IO_OUTPUT',  'single');
    arg.DimRange = [2 2; Inf Inf];
    hEnt.addConceptualArg(arg);
    
    arg = RTW.TflArgMatrix('u1', 'RTW_IO_INPUT',  'single');
    arg.DimRange = [2 1; Inf Inf];
    hEnt.addConceptualArg(arg);
    
    arg = RTW.TflArgMatrix('u2', 'RTW_IO_INPUT',  'single');
    arg.DimRange = [1 2; Inf Inf];
    hEnt.addConceptualArg(arg);
  6. 実装表現を作成します。実装表現は最適化関数のシグネチャを記述します。実装引数が概念引数と同じ順序とプロパティをもつように指定するには、関数 getTflArgFromString を呼び出します。コード例では、関数 dgemm と関数 dgemv への置き換えに必要となる特別な実装引数を設定します。便利なメソッド setReturn および addArgument で、引数が戻り値か引数かを指定し、エントリの実装引数の配列に引数を追加します。関数 addEntry を呼び出して、table に完全なエントリを追加します。

    function hTable = crl_table_cblas
    % Create a code replacement library table 
    
    %% Create a table object
    hTable = RTW.TflTable;
    
    %% Create an entry
    %%%%%%%%%%%% Define library path Cblas library file%%%%%%%%%%%%%%%%%%
    
    LibPath = fullfile('<Specify the path of your cblas library>');
    
    if ispc
        libExt = 'lib';
    elseif ismac
        libExt = 'dylib';
    else
        libExt = 'so';
    end
    
    
    % Create table entry for dgemm32
    hEntry = RTW.TflCBlasEntryGenerator;
    
    %% Create entry parameters
    hEntry.setTflCOperationEntryParameters( ...
        'Key', 'RTW_OP_MUL', ...
        'Priority', 100, ...
        'ImplementationName', 'cblas_sgemm', ...
        'ImplementationSourceFile', 'ccblas.c', ...
        'ImplementationHeaderFile', 'ccblas.h', ...
        'AdditionalIncludePaths',   {LibPath}, ...
        'AdditionalLinkObjs',       {['libcblas' libExt]}, ...
        'AdditionalLinkFlags',      {'-lgfortran'},...
        'SideEffects', true);
    
    %% Create the conceptual representation
    
    arg = RTW.TflArgMatrix('y1', 'RTW_IO_OUTPUT',  'single');
    arg.DimRange = [2 2; Inf Inf];
    hEnt.addConceptualArg(arg);
    
    arg = RTW.TflArgMatrix('u1', 'RTW_IO_INPUT',  'single');
    arg.DimRange = [2 1; Inf Inf];
    hEnt.addConceptualArg(arg);
    
    arg = RTW.TflArgMatrix('u2', 'RTW_IO_INPUT',  'single');
    arg.DimRange = [1 2; Inf Inf];
    hEnt.addConceptualArg(arg);
    
    %% Create the Implementation Representation
    % Using RTW.TflCBlasEntryGenerator for sgemm requires the following
    % implementation signature:
    %
    % void no_name( const integer ORDER, const integer TRANSA, const integer TRANSB, 
    % const integer M, const integer N, const integer K, const double ALPHA, 
    % const double* u1, const integer LDA, const double* u2, const integer LDB, 
    % const double BETA, double* y1, const integer LDC );
    %
    % When a match occurs, the code generator computes the
    % values for M, N, K, LDA, LDB, and LDC and inserts them into the
    % generated code. TRANSA and TRANSB are set to 'N'.
    
    % Specify replacement function signature
    arg = hEntry.getTflArgFromString('y2','void');
    arg.IOType = 'RTW_IO_OUTPUT';
    hEntry.Implementation.setReturn(arg); 
    
    arg = hEntry.getTflArgFromString('ORDER','integer',102.000000000000000);
    arg.Type.ReadOnly = true;
    hEntry.Implementation.addArgument(arg);
    
    arg = hEntry.getTflArgFromString('TRANSA','integer',111.000000000000000);
    arg.Type.ReadOnly = true;
    hEntry.Implementation.addArgument(arg);
    
    arg = hEntry.getTflArgFromString('TRANSB','integer',111.000000000000000);
    arg.Type.ReadOnly = true;
    hEntry.Implementation.addArgument(arg);
    
    arg = hEntry.getTflArgFromString('M','integer',0.000000000000000);
    arg.Type.ReadOnly = true;
    hEntry.Implementation.addArgument(arg);
    
    arg = hEntry.getTflArgFromString('N','integer',0.000000000000000);
    arg.Type.ReadOnly = true;
    hEntry.Implementation.addArgument(arg);
    
    arg = hEntry.getTflArgFromString('K','integer',0.000000000000000);
    arg.Type.ReadOnly = true;
    hEntry.Implementation.addArgument(arg);
    
    arg = hEntry.getTflArgFromString('ALPHA','double',1.000000000000000);
    arg.Type.ReadOnly = true;
    hEntry.Implementation.addArgument(arg);
    
    arg = hEntry.getTflArgFromString('u1','single*');
    arg.Type.BaseType.ReadOnly = true;
    hEntry.Implementation.addArgument(arg);
    
    arg = hEntry.getTflArgFromString('LDA','integer',0.000000000000000);
    arg.Type.ReadOnly = true;
    hEntry.Implementation.addArgument(arg);
    
    arg = hEntry.getTflArgFromString('u2','single*');
    arg.Type.BaseType.ReadOnly = true;
    hEntry.Implementation.addArgument(arg);
    
    arg = hEntry.getTflArgFromString('LDB','integer',0.000000000000000);
    arg.Type.ReadOnly = true;
    hEntry.Implementation.addArgument(arg);
    
    arg = hEntry.getTflArgFromString('BETA','double',0.000000000000000);
    arg.Type.ReadOnly = true;
    hEntry.Implementation.addArgument(arg);
    
    arg = hEntry.getTflArgFromString('y1','single*');
    arg.IOType = 'RTW_IO_OUTPUT';
    hEntry.Implementation.addArgument(arg);
    
    arg = hEntry.getTflArgFromString('LDC','integer',0.000000000000000);
    arg.Type.ReadOnly = true;
    hEntry.Implementation.addArgument(arg);
    
    
    %% Add the entry to the table
    addEntry(hTable, hEntry);
  7. ビルド情報を指定します。エントリ パラメーターで、コード ジェネレーターがコード置換に必要なファイル (ソース、ヘッダー、オブジェクト) を指定します。この例では、ビルド情報は必要ありません。

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

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

    function rtwTargetInfo(cm)
     
    cm.registerTargetInfo(@loc_register_crl);
    end
     
    function this = loc_register_crl 
     
    this(1) = RTW.TflRegistry; 
    this(1).Name = 'CRL for matrix multiplication for Mathworks CBlas code’;
    this(1).TableList = {'crl_table_cblas.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を参照してください。コード置換ライブラリを使用するようにモデルを構成し、コードを生成して、その置換が予期したとおりに行われることを確認します。予期しない動作が行われた場合、ヒット ログとミス ログを調べて問題のトラブルシューティングを行います。

関連するトピック