【问题标题】:Automating the profling of C program using GPROF使用 GPROF 自动分析 C 程序
【发布时间】:2014-01-23 12:15:03
【问题描述】:

我正在使用 gprof 分析一个矩阵乘法 C 程序。该 C 程序具有这样的一般结构;

int main()

{

int n;

printf("enter size of square matrices");
scanf("%d", &n);

data(matM); //fill matrices with n x n random data 
data(matN); 

// this is unoptimized algo
matUnopt(int *matM, int *matN, int *MatOut, int size);


// this is optimized algo
matOpt(int *matM, int *matN, int *MatOut, int size);

 return(0);

}   

现在我的分析方式是:

我运行我的可执行文件,将大小设置为100,然后
$ gprof mat gmon.out > analysis1.txt

这会生成 analysis1.txt 我手动记下 matOpt();matUnopt(); 的时间

然后我再次运行可执行mat,并给出n=150,然后

$  gprof mat gmon.out > analysis2.txt  

这会生成 analysis2.txt 我手动记下 matOpt();matUnopt(); 的时间

等等。

这是非常耗时且无聊的方式。我想自动化这个过程。有些是这样的:

int main()

{

int n;


for (n=50; n<50000; n++)

{      
  data(matM); //fill matrices with n x n random data 
  data(matN); 

// this is unoptimized algo
matUnopt(int *matM, int *matN, int *MatOut, int size);


 //gprof records the time of completion of matUnopt for the particular value of n, and 
 puts in a file 

 // this is optimized algo
 matOpt(int *matM, int *matN, int *MatOut, int size);


 //gprof records the time of completion of matOpt for the particular value of n, and   
puts in a file 


  }

  return(0);

  }   

一旦应用程序退出,我期待一个具有如下表格的文件:

run# (x 50)   profiling result (as usual we get from gprof)               

1             matUnopt  time .....
              matOpt    time .....  

2             matUnopt  time .....
              matOpt    time .....  

3             matUnopt  time .....
              matOpt    time .....  

4             matUnopt  time .....
              matOpt    time .....  


and so on.

请注意,上面的“分析结果”是我们通常从 gprof 获得的。 重要的是我有一种自动化的方法来获取可执行文件的多次运行的功能时间,这也具有不同的输入大小。

这个解释只是一个粗略的想法。我很乐意得到与此近似的任何东西。例如,应用程序可能会退出,然后重新启动它自己以获得新的分析结果。这就是我实际在做的事情。但我想自动执行此操作。

我如何实现这个目标?

【问题讨论】:

    标签: c profiling gprof


    【解决方案1】:

    您可以使用脚本语言并将您的大小作为分析二进制文件的参数吗?

    在 c 中使用参数的示例:passing arguments to main

    这个 bash 脚本自动运行你的程序,矩阵大小从 50 到 50000。tee 命令确保输出被打印并保存在相应的文件中:analysisxxx.txt

    #!/bin/bash
    
    for i in {50..50000}
    do
        gprof mat gmon.out $i | tee analysis${i}.txt
    done
    

    希望对你有所帮助。

    【讨论】:

    • 这看起来非常有用。然而这样一来,我会得到很多很多的 analysisxx.txt 文件,我将不得不手动打开每个文件,然后查看功能的时序。我们可以从单个文件中的所有文件中获取所有结果吗?以便我可以轻松比较每种尺寸的结果?
    • grof mat gmon.out $i >> inasinglefile.txt,使用>>代替>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 2010-12-19
    • 2016-07-25
    • 1970-01-01
    • 2016-02-12
    • 2011-01-18
    相关资源
    最近更新 更多