【发布时间】:2018-05-14 05:53:13
【问题描述】:
当我在mac上使用icc编译器时,我无法用gcc、clang等其他编译器得到相同的答案。 使用icc编译器,结果如下
0.000000e+00
0.000000e+00
0.000000e+00
0.000000e+00
0.000000e+00
0.000000e+00
0.000000e+00
0.000000e+00
期待的答案就在这里
1.000000e+00
2.000000e+00
3.000000e+00
4.000000e+00
2.500000e+01
3.000000e+01
3.500000e+01
4.000000e+01
我是这样编译的:
- icc:
icc test1.c -fopenmp -mavx -Wall - gcc:
gcc test1.c -fopenmp -mavx -Wall - 叮当声:
clang test1.c -fopenmp -mavx -Wall
我的代码如下:
#include "stdio.h"
#include "time.h"
#include "math.h"
#include "stdlib.h"
#include "omp.h"
#include "x86intrin.h"
void dd_m_dd(double *ahi, double *bhi, double *chi, int m, int n)
{
int j;
#pragma omp parallel
{
__m256d vahi,vbhi,vchi;
#pragma omp for private(vahi,vbhi,vchi)
for (j = 0; j < m*n; j+=4) {
vbhi = _mm256_broadcast_sd(&bhi[j]);
vahi = _mm256_load_pd(&ahi[j]);
vchi = _mm256_load_pd(&chi[j]);
vchi=vahi*vbhi;
chi[j]=vchi[0];
chi[j+1]=vchi[1];
chi[j+2]=vchi[2];
chi[j+3]=vchi[3];
}
}
}
int main(int argc, const char * argv[]){
// Matrix Vector Product with DD
// set variables
int m;
double* xhi;
double* yhi;
double* z;
int i;
m=(int)pow(2,3);
// main program
// set vector or matrix
xhi=(double *)malloc(sizeof(double) * m*1);
yhi=(double *)malloc(sizeof(double) * m*1);
z=(double *)malloc(sizeof(double) * m*1);
//preset
for (i=0;i<m;i++) {
xhi[i]=i+1;
yhi[i]=i+1;
z[i]=0;
}
dd_m_dd(xhi,yhi,z,m,1);
for (i=0;i<m;i++) {
printf("%e\n",z[i]);
}
free(xhi);
free(yhi);
free(z);
return 0;
}
这里发生了什么?
【问题讨论】:
-
在这里停止对旧 cppcon 视频的记忆,但我认为 icc 默认启用了
-ffast-math。我不知道这是否会对您的示例起作用,但可能值得测试。 -
我建议使用
-march=native,而不仅仅是-mavx,来针对您的目标机器进行调优,而不是在针对通用进行调优的同时启用AVX。 (尤其是 gcc)。 -
vchi = _mm256_load_pd(&chi[j]);立即被vchi=vahi*vbhi;覆盖,这看起来不对;您的意思是添加+=而不是分配?