intel编译器忽略了大循环?

Roy*_* Li 1 c for-loop intel timing

所有:

我有一个非常简单的C测试代码,使用英特尔编译器为浮点运算的大循环做一些计时,代码(test.c)如下:

#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <omp.h>

int main(char *argc, char **argv) {
      const long N = 1000000000;
      double t0, t1, t2, t3;
      double sum=0.0;
      clock_t start, end;
      struct timeval r_start, r_end;
      long i;
      gettimeofday(&r_start, NULL);
      start = clock();
      for (i=0;i<N;i++)
          sum += i*2.0+i/2.0; // doing some floating point operations
      end = clock();
      gettimeofday(&r_end, NULL);
      double cputime_elapsed_in_seconds = (end - start)/(double)CLOCKS_PER_SEC;
      double realtime_elapsed_in_seconds = ((r_end.tv_sec * 1000000 + r_end.tv_usec)
                - (r_start.tv_sec * 1000000 + r_start.tv_usec))/1000000.0;
      printf("cputime_elapsed_in_sec: %e\n", cputime_elapsed_in_seconds);
      printf("realtime_elapsed_in_sec: %e\n", realtime_elapsed_in_seconds);
      //printf("sum= %4.3e\n", sum);
      return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用Intel 13.0编译器编译并运行它时,似乎忽略了大循环并且执行导致零时序:

$ icc test.c
$ ./a.out
cputime_elapsed_in_sec: 0.000000e+00
realtime_elapsed_in_sec: 9.000000e-06
Run Code Online (Sandbox Code Playgroud)

只有当我打印总和(取消注释第26行)时,才会实际执行循环:

$ icc test.c
$ ./a.out
cputime_elapsed_in_sec: 2.730000e+00
realtime_elapsed_in_sec: 2.736198e+00
sum= 1.250e+18
Run Code Online (Sandbox Code Playgroud)

问题是如果我不打印总和值,为什么循环似乎没有被执行?

gcc-4.4.7编译器不会出现同样的问题,我想intel编译器可能已经做了一些优化,如果没有引用变量,循环可能会被忽略?

系统信息如下:

$ uname -a
Linux node001 2.6.32-642.11.1.el6.x86_64 #1 SMP Wed Oct 26 10:25:23 EDT 2016 x86_64 x86_64 x86_64 GNU/Linux
$ icc -v
icc version 13.0.0 (gcc version 4.4.7 compatibility)
$ gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
Run Code Online (Sandbox Code Playgroud)

谢谢你的任何建议!

罗伊

pax*_*blo 6

鉴于你的观察结果是打印最终值减慢了它(a),优化者很有可能在你计算之后发现你实际上没有使用 sum任何东西,所以它正在优化整个计算循环存在

很久以前,当我们测试我们大学收到的最新VAX 11/780机器的性能时,我实际上看到了类似的东西(显示我的年龄).由于完全相同的原因,它的速度提高了几千%,新的优化编译器已经确定实际上不需要循环.

确切地说,您必须检查装配输出.我相信这可以icc通过使用该-Fa <asmFileName>选项然后检查您使用其名称的文件来完成<asmFileName>.


(a)我想到的另一种可能性似乎在这里打折扣.

这是可能的,给定范围i是常量(基于N)并且计算否则涉及常量,可能是编译器本身在编译时计算了最终值,导致简单的恒定加载操作.

我已经看到gcc-O3"疯狂"的优化级别做这种事情.

我打折了这种可能性,因为打印该值很可能不会影响此操作.