【发布时间】:2016-04-01 15:43:27
【问题描述】:
我已成功实施 Apple 的 Accelerate Framework 中的 BLAS 库,以提高我的基本向量和矩阵运算的性能。
对此感到满意,我将注意力转向 vForce 以矢量化我的基本数学函数。与幼稚的实现(使用自动编译器优化 -Os)相比,这里的性能相当差,我有点惊讶。
作为一个简单的基准,我运行了以下测试:Matrix 是基本的 Matrix 类型,使用双指针,AccelerateMatrix 是 Matrix 的子类,它使用 vForce 中的幂函数:
Matrix A(vec_size);
AccelerateMatrix B(vec_size);
for (int i=0; i<vec_size;i++ ) {
A[i] = i;
B[i] = i;
}
double elapsed_time;
clock_t start = clock();
for(int i=0;i<reps;i++){
A.exp();
A.log();
}
clock_t stop = clock();
elapsed_time = (double)(stop-start)/CLOCKS_PER_SEC/reps;
cerr << "Basic matrix exponentiation/log time = " << elapsed_time << endl;
start = clock();
for(int i=0;i<reps;i++){
B.exp();
B.log();
}
stop = clock();
elapsed_time = (double)(stop-start)/CLOCKS_PER_SEC/reps;
cerr << "Accelerate matrix exponentiation/log time = " << elapsed_time << endl;
exponentiate/log 成员函数实现如下:
void AccelerateMatrix::exp(){
int size =(int)this->getSize();
this->goToStart();
vvexp(this->ptr, this->ptr, &size);}
void Matrix::exp(){
double *ptr = data;
while (!atEnd()) {
*ptr = std::exp(*ptr);
ptr++;
}
}
data 是指向双精度数组第一个元素的指针。
以下是表演的输出:
矩阵元素数 = 1000000
基本矩阵求幂/对数时间(秒)= 0.0089806
加速矩阵求幂/对数时间(秒)= 0.0149955
我在发布模式下从 XCode 运行。 我的处理器是 2.3 GHz Intel Core i7。 内存为 8 GB 1600 MHz DDR3。
【问题讨论】:
标签: c++ performance accelerate-framework