利用MKL的VML优化超越函数的计算
下面一个有关使用Intel MKL优化的例子. Intel MKL 函数中提供了VML (vector Math Library). VML 包括了主要是对向量的超越函数的计算. 这个例子以前有网友讨论过,应用的比较普遍,再贴有一次。
假如我们有如下的计算c[i]的代码:
int i;
double a, b, c[N];
for( i = 0; i < n; i++ ){
a = pi/sqrt( 3 * (double)(1 + i ) );
b = sin(a);
c[i] = b/a;
}
这个例子中超越函数 sqrt() 与Sina()的计算,是代码的最主要的工作量. 我们可以将这样的代码改为向量化的形式. 然有调用Intel MKL的 VML的库函数: 下面例子中vdSin(), vdInvSqrt() 是Intel MKL 中的两个VML 函数.
int i, j, count;
double a, b, c[N], temp;
double atemp1[M], atemp2[M], atemp3[M];
for( j = 0; j < N; j += M ){
temp = (double)j;
for( i = j; i < j + M; j++ }
atemp1[ i - j ] = 3 * temp++;
count = min( M, N - j );
vdInvSqrt( count, atemp1, atemp2 );
for( i = j; i < j + M; j++ )
atemp2[ i - j ] *= pi;
vdSin( count, atemp2, atemp3 );
vdDiv( count, atemp1, atemp3 );
}