1

对于 c 中的函数 system(),如果您试图查看您运行的命令是如何执行的,它会影响硬件计数器吗例如,假设我使用 Performance API(PAPI) 并且程序是预编译的矩阵乘法应用程序

PAPI_start_counters();
system("./matmul");
PAPI_read_counters();
//Print out values 
PAPI_stop_counters();

我显然遗漏了一点,但我试图找出的是,通过使用所述计数器来获得正在运行的程序的性能是可能的。从我的测试中,我会得到如下所示的狂野数字。他们显然错了,只是想找出原因

Total Cycles =========== 140733358872510 
Instructions Completed =========== 4203968 
Floating Point Instructions =========== 0 
Floating Point Operations =========== 4196867 
Loads =========== 140733358872804 
Stores =========== 4204037 
Branches Taken =========== 15774436 
4

3 回答 3

4

system()通常是一个非常慢的功能。在 Linux 上,它产生/bin/sh(分叉并执行一个完整的 shell 进程),它解析你的命令,并产生第二个程序。加载这两个程序需要将代码加载到内存,初始化它们的所有库,执行启动代码等。只有这样程序代码才会真正开始执行。

由于磁盘访问和 Linux 进程调度的不可预测性,定时system()调用具有非常高的固有可变性。因此,即使使用高性能计数器,也不会得到准确的结果。

更好的解决方案是将目标程序编译为库。在初始化计数器之前加载它,然后从库中执行 main 函数。这样,所有代码都在您的进程中执行,并且您的启动时间可以忽略不计。这样,您的性能数字将更加精确。

于 2014-04-26T04:47:07.403 回答
2

Do you have access to the code of matmul? If so, it's much more precise to instrument and measure only the code you're interested in. That means you wrap only those instructions (or C statements) in counters that you want to measure.

For more information see:

  • Related discussion here
  • Intel® Performance Counter Monitor here
  • Performance measurements with x86 RDTSC instruction here

As stated above, measuring using PAPI to wrap system() invocations carries way too much process overhead to give you any idea of how fast your math code is actually running.

于 2014-04-26T05:38:16.680 回答
0

你得到的数字是奇数,但不一定是错的。指令完成和周期之间的巨大差异可能表明可执行“matmul”正在等待外部进程(例如磁盘 I/O)完成。我不知道 msg FP Instructions 和 FP ops 的细节,但如果它们以不同的方式显示这些值,PAPI 是有原因的。

有趣的是,负载和周期以及指令/fp 操作和存储显然是相互关联的。

为了给您更好的描述,我必须了解“matmul”的内部结构。

于 2014-04-26T04:55:23.853 回答