使用GPROF自动执行C程序的配置 [英] Automating the profling of C program using GPROF
问题描述
我正在使用gprof分析矩阵乘法C程序.该C程序具有这样的一般结构;
I am profiling a matrix multiplication C program using gprof. That C program has a general structure like this;
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
I run my executable mat, give size as 100
and then
$ gprof mat gmon.out > analysis1.txt
这将生成analysis1.txt
,我从此处手动记下matOpt();
和matUnopt();
的时间
this generate analysis1.txt
from where I manually note the timing for matOpt();
and matUnopt();
然后我再次运行可执行文件,并提供n=150
,然后提供
Then I again run the executable mat, and give n=150
, and then
$ gprof mat gmon.out > analysis2.txt
这会生成analysis2.txt
,从这里我手动记下matOpt();
和matUnopt();
this generate analysis2.txt
from where I manually note the timing for matOpt();
and matUnopt();
,依此类推.
这是非常耗时且无聊的方式.我想使这个过程自动化.像这样的一些:
This is very time consuming and boring way. I want to automate this procedure. Some this like this:
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);
}
一旦应用程序退出,我希望文件中包含如下表格:
Once the application exits, I am expecting a file having a table like this:
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获得的结果. 重要的是,我有一种自动化的方法来获取可执行文件多次运行的功能时序,而输入大小也不同.
Please note that 'profiling result' above is what we generally get from gprof. the important thing is I have an automated way to get the timing for the functions for multiple runs of the executable, that too with different input size.
此说明只是一个粗略的想法.我很乐意得到与此近似的任何东西.例如,应用程序可能会退出,然后自行重启以获取新的分析结果.那就是我实际上在做的.但我想自动执行此操作.
我如何实现这个目标?
How do I achieve this goal?
推荐答案
您可以使用脚本语言并将大小作为分析二进制文件的参数吗?
Can you use a scripting language and give your size as an argument to the profiling binary?
在c中使用参数的示例:将参数传递给main .
For an example using arguments in c: passing arguments to main.
此bash脚本会自动运行矩阵大小为50到50000的程序.tee命令确保输出已打印并保存在相应的文件中:analysisxxx.txt
This bash script automatically runs your program with a matrix size from 50 till 50000. The tee command makes sure the output is printed and saved in the corresponding file: analysisxxx.txt
#!/bin/bash
for i in {50..50000}
do
gprof mat gmon.out $i | tee analysis${i}.txt
done
希望这对您有所帮助.
这篇关于使用GPROF自动执行C程序的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!