C++ Auto-Vectorize Matrix Multiplication loop

前端 未结 1 1248
走了就别回头了
走了就别回头了 2021-01-16 08:05

When compiling my source code which does basic matrix-matrix multiplication with auto-vectorization and auto-parallelization enabled, I receive these warnings in console:

相关标签:
1条回答
  • 2021-01-16 08:39

    The loop carried dependence is on result[i][j].

    A solution to your problem would be using a temporary variable when summing up the result and do the update outside the inner-most loop like this:

    for (int i = 0; i < dimension; ++i) {
        for (int j = 0; j < dimension; ++j) {
            auto tmp = 0;
            for (int k = 0; k < dimension; ++k) {
                tmp += A[i][k] * B[k][j];
            }
            result[i][j] = tmp;
        }
    }
    

    This is going remove the dependence (since there is more read-after-write of result[i][j] and should help the vectorizer doing a better job.

    0 讨论(0)
提交回复
热议问题