天天看點

Vivado_HLS 學習筆記4-嵌套的循環的優化

優化的原理

HLS會自動嘗試最小化循環的延遲. 除了這些自動的優化之外,directive檔案負責

  • 執行并行任務; 例如相同函數的多次執行,以及相同循環的多次疊代. 要進行pipeline設計;
  • 重新設計數組(Block arrays),函數,循環和端口等的實體實作,改善資料的訪存;
  • 提供資料依賴的資訊;

最終的優化手段是修改C源代碼以移除非必要的資料依賴.

參考ug871的第7章.

對有資料依賴的計算如何優化設計

void matrixmul(
      mat_a_t a[MAT_A_ROWS][MAT_A_COLS],
      mat_b_t b[MAT_B_ROWS][MAT_B_COLS],
      result_t res[MAT_A_ROWS][MAT_B_COLS])
{
  // Iterate over the rows of the A matrix
   Row: for(int i = 0; i < MAT_A_ROWS; i++) {
      // Iterate over the columns of the B matrix
      Col: for(int j = 0; j < MAT_B_COLS; j++) {
         res[i][j] = 0;
         // Do the inner product of a row of A and col of B, 
         // 顯然,内積的計算中,每次計算都需要讀取上一次的res[i][j]并更新本次的res[i][j].存在同一循環不同疊代間的資料依賴.
         Product: for(int k = 0; k < MAT_B_ROWS; k++) {
            res[i][j] += a[i][k] * b[k][j];
         }
      }
   }

}
           

解決步驟1-pipeline最底層loop,

  • 操作: 設定留白II以initiaion interval 為1
  • 結果: 當最底層的loop循環進行pipeline不能降低latency(運算一次資料需要的時鐘周期數)和Interval(下一次輸入資料需要等待的間隔,如果等于latency說明沒有pipeline)時,檢視console看一下是否有藍色字型 **Unable to enforce a carried dependence constraint **,執行步驟2;

解決步驟2-pipeline上一層loop

  • 操作: 選擇其上一層loop進行pipeline,而不是最底層pipeline.
  • 如果發現并沒有獲得想要的結果,再次檢視console的warnning!,發現Unable to schedule 'load' operation ('a_load_1', matrixmul.cpp:60) on array 'a' due to limited memory ports. Please consider using a memory core with more ports or partitioning the array 'a'.;

解決步驟3-數組分塊(并行,需要多個端口并行)或reshape(需要一個更寬的端口)

  • 操作1: 本例中,需要将數組a的[k]次元也就是第2維進行展開,數組 a -> Directive -> ARRAY_RESHAPE, dimension:2
  • 操作2: 本例中,需要對數組b的[k]次元也就是第1維進行展開;數組 a -> Directive -> ARRAY_RESHAPE, dimension:1
  • 結果: 能夠看到II已經是1,滿足了流水的設計要求;

解決步驟4-嘗試添加FIFO接口

  • 操作: 對輸入端口a,b,和輸出端口res設定-> Directive -> Interface ,ap_fifo
  • 結果: console顯示錯誤Port 'res' (matrixmul.cpp:48) of function 'matrixmul' cannot be set to a FIFO [SYNCHK 200-91] as it has both write (matrixmul.cpp:60:13) and read (matrixmul.cpp:60:13) operations.
  • 分析: 在57行和60行等,對res[0][0]進行了多次連續的寫操作,不滿足stream的狀況.這不是stream而是random access.
  • 結果: 這種情況不能使用FIFO!

解決步驟5-繼續優化!修改C代碼

在重寫C代碼之前,需要對數組a,b,的訪存位址進行分析:

  • 為了實作序列的流訪存,端口隻能在圖中紅色高亮的部分訪存;
  • 對于數組a,b的其他藍色的讀資料口,需要使用内部緩存cached擷取;
  • 對于輸出res的其他端口,隻能通過臨時變量進行緩存,最終在紅色時輸出.

是以,改寫後的代碼為:

void matrixmul(
      mat_a_t a[MAT_A_ROWS][MAT_A_COLS],
      mat_b_t b[MAT_B_ROWS][MAT_B_COLS],
      result_t res[MAT_A_ROWS][MAT_B_COLS])
{
#pragma HLS ARRAY_RESHAPE variable=b complete dim=1
#pragma HLS ARRAY_RESHAPE variable=a complete dim=2
#pragma HLS INTERFACE ap_fifo port=a
#pragma HLS INTERFACE ap_fifo port=b
#pragma HLS INTERFACE ap_fifo port=res
  mat_a_t a_row[MAT_A_ROWS];
  mat_b_t b_copy[MAT_B_ROWS][MAT_B_COLS];
  int tmp = 0;

  // Iterate over the rowa of the A matrix
  Row: for(int i = 0; i < MAT_A_ROWS; i++) {
    // Iterate over the columns of the B matrix
    Col: for(int j = 0; j < MAT_B_COLS; j++) {
#pragma HLS PIPELINE rewind
      // Do the inner product of a row of A and col of B
      tmp=0;
      // Cache each row (so it's only read once per function)
      if (j == 0)
        Cache_Row: for(int k = 0; k < MAT_A_ROWS; k++)
          a_row[k] = a[i][k];
      
       // Cache all cols (so they are only read once per function)
     if (i == 0)
            Cache_Col: for(int k = 0; k < MAT_B_ROWS; k++)
               b_copy[k][j] = b[k][j];

      Product: for(int k = 0; k < MAT_B_ROWS; k++) {
        tmp += a_row[k] * b_copy[k][j];
      }
      res[i][j] = tmp;
    }
  }
}
           

注意事項

如果沒有pipling loops,那麼每計算一個數字,需要interval,計算tripcount次,就需要tripcount * interval;

pipling loops把循環的latency從$$Latency = iteration\ latency * tripcount $$ 變成了 $$Latency = iteration\ latency + (tripcount * interval)$$.

  • loop的latency是指循環執行完成一次需要的時鐘數;
  • iteration latency是指循環内部單次執行完一次需要的時鐘數;
  • tripcount是指循環執行的次數.
  • initiation interval是指本次資料輸入和下次資料輸入需要的間隔周期數.

注意: 對上層循環/子產品的pipeline将導緻對所有子產品均進行pipeling,将展開所有循環.會極大地消耗資源.獲得更高的吞吐量.