我是一名学生,我正在学习矢量化技术。我正在尝试使编译器矢量化一个将两个矩阵相乘的函数(每个矩阵由大小相等的矩阵元素组成)。代码如下所示:
#define f_dim1 2000
#define f_dim2 240
#define s_dim1 240
#define s_dim2 2000
#define i_dim1 4
#define i_dim2 4
void automaticallyBuilt(float* firstMatrix, float* secondMatrix, float* result) {
for (int i = 0; i < f_dim2; i++) { // rows in frist matrix
for (int j = 0; j < s_dim1; j++) { // columns in second matrix
for (int o = 0; o < f_dim1; o++) { // row element of first matrix = column element of second
for (int k = 0; k < i_dim2; k++) { // rows in inner matrix
for (int l = 0; l < i_dim1; l++) { // columns in inner matrix
for (int h = 0; h < i_dim2; h++) { // row element of inner = column element of inner
*(result + i*s_dim1*i_dim2*i_dim1 + j*i_dim2*i_dim1 + k*i_dim1 + l) +=
*(firstMatrix + i*f_dim1*i_dim2*i_dim1 + o*i_dim2*i_dim1 + k*i_dim1 + h) *
*(secondMatrix + o*s_dim1*i_dim2*i_dim1 + j*i_dim2*i_dim1 + h*i_dim1 + l);
// smth like result[i][j][k][l] += firstMatrix[i][o][k][h] * secondMatrix[o][j][h][l];
}
}
}
}
}
}
}
为了使编译器矢量化它,我修改了这段代码,如下所示:
#define f_dim1 2000
#define f_dim2 240
#define s_dim1 240
#define s_dim2 2000
#define i_dim1 4
#define i_dim2 4
void automaticallyVectorized(float* firstMatrix, float* secondMatrix, float* result) {
for (int i = 0; i < f_dim2; i++) { // rows in frist matrix
for (int o = 0; o < f_dim1; o++) { // row element of first matrix = column element of second
for (int j = 0; j < s_dim1; j++) { // columns in second matrix
for (int k = 0; k < i_dim2; k++) { // rows in inner matrix
for (int h = 0; h < i_dim2; h++) { // row element of inner = column element of inner
float firstMatrixInnerRowElement = *(firstMatrix + i*f_dim1*i_dim2*i_dim1 + o* i_dim2*i_dim1 + k*i_dim1 + h);
float* resultInnerRow = result + i*s_dim1*i_dim2*i_dim1 + j*i_dim2*i_dim1 + k*i_dim1;
float* secondMatrixInnerColumnElementRow = secondMatrix + o*s_dim1*i_dim2*i_dim1 + j*i_dim2*i_dim1 + h*i_dim1;
for (int l = 0; l < i_dim1; l++) { // columns in inner matrix
resultInnerRow[l] += firstMatrixInnerRowElement * secondMatrixInnerColumnElementRow[l];
}
}
}
}
}
}
}
在构建期间编译器发出以下消息:
code.cpp(161) : info C5002: Loop not vectorized 由于原因:“1204”
此处未提及代码 1204 。此外,我还没有在谷歌上找到任何关于它的信息。我尝试使用__restrict
修饰符但没有运气。
我使用 Visual Studio 2019,但我尝试使用 VS 2017 构建它,结果相同。
任何人都可以解释这个原因代码的含义吗?我不相信以前没有人遇到过这个问题。