【发布时间】:2017-08-28 05:22:49
【问题描述】:
我们目前正在用 C++ 编写一些性能关键代码,这些代码可在许多大型矩阵和向量上运行。关于我们的研究,std::array 和标准 C 数组之间应该没有太大的性能差异
(见This question 或this)。
然而,在测试过程中,通过使用 C 数组而不是 std::array,我们体验到了巨大的性能提升。
这是我们的演示代码:
#include <iostream>
#include <array>
#include <sys/time.h>
#define ROWS 784
#define COLS 100
#define RUNS 50
using std::array;
void DotPComplex(array<double, ROWS> &result, array<double, ROWS> &vec1, array<double, ROWS> &vec2){
for(int i = 0; i < ROWS; i++){
result[i] = vec1[i] * vec2[i];
}
}
void DotPSimple(double result[ROWS], double vec1[ROWS], double vec2[ROWS]){
for(int i = 0; i < ROWS; i++){
result[i] = vec1[i] * vec2[i];
}
}
void MatMultComplex(array<double, ROWS> &result, array<array<double, COLS>, ROWS> &mat, array<double, ROWS> &vec){
for (int i = 0; i < COLS; ++i) {
for (int j = 0; j < ROWS; ++j) {
result[i] += mat[i][j] * vec[j];
}
}
}
void MatMultSimple(double result[ROWS], double mat[ROWS][COLS], double vec[ROWS]){
for (int i = 0; i < COLS; ++i) {
for (int j = 0; j < ROWS; ++j) {
result[i] += mat[i][j] * vec[j];
}
}
}
double getTime(){
struct timeval currentTime;
gettimeofday(¤tTime, NULL);
double tmp = (double)currentTime.tv_sec * 1000.0 + (double)currentTime.tv_usec/1000.0;
return tmp;
}
array<double, ROWS> inputVectorComplex = {{ 0 }};
array<double, ROWS> resultVectorComplex = {{ 0 }};
double inputVectorSimple[ROWS] = { 0 };
double resultVectorSimple[ROWS] = { 0 };
array<array<double, COLS>, ROWS> inputMatrixComplex = {{0}};
double inputMatrixSimple[ROWS][COLS] = { 0 };
int main(){
double start;
std::cout << "DotP test with C array: " << std::endl;
start = getTime();
for(int i = 0; i < RUNS; i++){
DotPSimple(resultVectorSimple, inputVectorSimple, inputVectorSimple);
}
std::cout << "Duration: " << getTime() - start << std::endl;
std::cout << "DotP test with C++ array: " << std::endl;
start = getTime();
for(int i = 0; i < RUNS; i++){
DotPComplex(resultVectorComplex, inputVectorComplex, inputVectorComplex);
}
std::cout << "Duration: " << getTime() - start << std::endl;
std::cout << "MatMult test with C array : " << std::endl;
start = getTime();
for(int i = 0; i < RUNS; i++){
MatMultSimple(resultVectorSimple, inputMatrixSimple, inputVectorSimple);
}
std::cout << "Duration: " << getTime() - start << std::endl;
std::cout << "MatMult test with C++ array: " << std::endl;
start = getTime();
for(int i = 0; i < RUNS; i++){
MatMultComplex(resultVectorComplex, inputMatrixComplex, inputVectorComplex);
}
std::cout << "Duration: " << getTime() - start << std::endl;
}
编译:icpc demo.cpp -std=c++11 -O0
结果如下:
DotP test with C array:
Duration: 0.289795 ms
DotP test with C++ array:
Duration: 1.98413 ms
MatMult test with C array :
Duration: 28.3459 ms
MatMult test with C++ array:
Duration: 175.15 ms
带有-O3 标志:
DotP test with C array:
Duration: 0.0280762 ms
DotP test with C++ array:
Duration: 0.0288086 ms
MatMult test with C array :
Duration: 1.78296 ms
MatMult test with C++ array:
Duration: 4.90991 ms
在没有编译器优化的情况下,C 数组实现要快得多。为什么?
使用编译器优化,点积同样快。但是对于矩阵乘法,使用 C 数组时仍然有显着的加速。
有没有办法在使用std::array时达到同等性能?
更新:
使用的编译器:icpc 17.0.0
使用gcc 4.8.5,我们的代码运行速度比使用任何优化级别的英特尔编译器慢得多。因此,我们主要对 intel 编译器的行为感兴趣。
根据Jonas 的建议,我们调整了RUNS 50.000,结果如下(英特尔编译器):
带有-O0 标志:
DotP test with C array:
Duration: 201.764 ms
DotP test with C++ array:
Duration: 1020.67 ms
MatMult test with C array :
Duration: 15069.2 ms
MatMult test with C++ array:
Duration: 123826 ms
带有-O3 标志:
DotP test with C array:
Duration: 16.583 ms
DotP test with C++ array:
Duration: 15.635 ms
MatMult test with C array :
Duration: 980.582 ms
MatMult test with C++ array:
Duration: 2344.46 ms
【问题讨论】:
-
另外,你的综合基准测试并不是很有用。您主要是在测量编译器是否可以确定结果是未使用的(在这种情况下,所有计算都可以省略)还是恒定的(在这种情况下,编译器可以在编译时完成所有计算)。在这两种情况下,您都没有衡量任何有用的东西。
-
因为您已经禁用优化而没有被内联的函数调用而支付了抽象损失?
-
如果您希望编译器优化抽象,请启用优化。
-
在 godbolt.org/g/9MnTLs 上,
MatMultComplex和MatMultSimple似乎都产生了相同的程序集 -
这不是 C 题,所以我去掉了 C 标签。
标签: c++ arrays performance c++11 stdarray