问题
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);
}
Now at present how I am profiling is:
I run my executable mat, give size as 100
and then$ gprof mat gmon.out > analysis1.txt
this generate analysis1.txt
from where I manually note the timing for matOpt();
and matUnopt();
Then I again run the executable mat, and give n=150
, and then
$ gprof mat gmon.out > analysis2.txt
this generate analysis2.txt
from where I manually note the timing for matOpt();
and matUnopt();
and so on.
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.
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.
This explanation is just a rough idea. I would be happy to get anything that approximates this. For example, the application might exit, and then restart of its own to get new profiling result. That is what I am actually doing. But I want to do this automatically.
How do I achieve this goal?
回答1:
Can you use a scripting language and give your size as an argument to the profiling binary?
For an example using arguments in c: passing arguments to main.
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
I hope this helps you a little bit.
来源:https://stackoverflow.com/questions/20944097/automating-the-profling-of-c-program-using-gprof