### Timing code Printing Zero Values

Hi,
I am trying to calculate the time for execution of my application. I think its giving me correct elapsed time because this value is not zero. However, some values are zero like:
 My total CPU time for parent = 0 ms. My system CPU time for parent = 0 ms. My total CPU time for child processes = 0 ms.

My complete code is given below:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178`` ``````#include #include #include #include #include #include #include #include #include /* Program Parameters */ #define MAXN 2000 /* Max value of N */ int N; /* Matrix size i.e rows * cols */ int procs; /* Number of processors to use */ /* Matrices and vectors */ volatile float A[MAXN][MAXN], B[MAXN], X[MAXN]; /* A * X = B, solve for X */ /* returns a seed for srand based on the time */ unsigned int time_seed() { struct timeval t; struct timezone tzdummy; gettimeofday(&t, &tzdummy); return (unsigned int)(t.tv_usec); } /* Set the program parameters from the command-line arguments */ void parameters(int argc, char **argv) { int submit = 0; /* = 1 if submission parameters should be used */ int seed = 0; /* Random seed */ char uid[/*L_cuserid + 2*/50]; /*User name */ /* Read command-line arguments */ srand(time_seed()); /* Randomize */ if (argc != 3) { if ( argc == 2 && !strcmp(argv[1], "submit") ) { /* Use submission parameters */ submit = 1; N = 4; procs = 2; printf("\nSubmission run for \"%s\".\n", getlogin()/*cuserid(uid)*/ ); //srand(randm()); } else { if (argc == 4) { seed = atoi(argv[3]); srand(seed); printf("Random seed = %i\n", seed); } else { printf("Usage: %s [random seed]\n", argv[0]); printf(" %s submit\n", argv[0]); exit(0); } } } /* Interpret command-line args */ if (!submit) { N = atoi(argv[1]); if (N < 1 || N > MAXN) { printf("N = %i is out of range.\n", N); exit(0); } procs = atoi(argv[2]); if (procs < 1) { printf("Warning: Invalid number of processors = %i. Using 1.\n", procs); procs = 1; } // USE CORES? /*if (procs > m_get_numprocs()) { printf("Warning: %i processors requested; only %i available.\n", procs, m_get_numprocs()); procs = m_get_numprocs(); }*/ } /* Print parameters */ printf("\nMatrix dimension N = %i.\n", N); printf("Number of processors = %i.\n", procs); /* Set number of processors */ /*m_set_procs(procs); CORES ? */ } /* Initialize A and B (and X to 0.0s) */ void initialize_inputs() { int row, col; printf("\nInitializing...\n"); for (col = 0; col < N; col++) { for (row = 0; row < N; row++) { A[row][col] = (float)rand() / 32768.0; } B[col] = (float)rand() / 32768.0; X[col] = 0.0; } } /* Print input matrices */ void print_inputs() { int row, col; if (N < 10) { printf("\nA =\n\t"); for (row = 0; row < N; row++) { for (col = 0; col < N; col++) { printf("%5.2f%s", A[row][col], (col < N-1) ? ", " : ";\n\t"); } } printf("\nB = ["); for (col = 0; col < N; col++) { printf("%5.2f%s", B[col], (col < N-1) ? "; " : "]\n"); } } } void main(int argc, char **argv) { /* Timing variables */ struct timeval etstart, etstop; /* Elapsed times using gettimeofday() */ struct timezone tzdummy; clock_t etstart2, etstop2; /* Elapsed times using times() */ unsigned long long usecstart, usecstop; struct tms cputstart, cputstop; /* CPU times for my processes */ /* Process program parameters */ parameters(argc, argv); /* Start Clock */ printf("\nStarting clock.\n"); gettimeofday(&etstart, &tzdummy); etstart2 = times(&cputstart); /* Initialize A and B */ initialize_inputs(); /* Print input matrices */ print_inputs(); /* Stop Clock */ gettimeofday(&etstop, &tzdummy); etstop2 = times(&cputstop); printf("Stopped clock.\n"); usecstart = (unsigned long long)etstart.tv_sec * 1000000 + etstart.tv_usec; usecstop = (unsigned long long)etstop.tv_sec * 1000000 + etstop.tv_usec; /* Display output */ /* Display timing results */ printf("\nElapsed time = %g ms.\n", (float)(usecstop - usecstart)/(float)1000); /*printf(" (%g ms according to times())\n", * (etstop2 - etstart2) / (float)_CLK_TCK) * 1000); */ printf("(CPU times are accurate to the nearest %g ms)\n", 1.0/(float)(sysconf(_SC_CLK_TCK)) * 1000.0);//NEW printf("My total CPU time for parent = %g ms.\n", (float)( (cputstop.tms_utime + cputstop.tms_stime) - (cputstart.tms_utime + cputstart.tms_stime) ) / (float)(sysconf(_SC_CLK_TCK)) * 1000); printf("My system CPU time for parent = %g ms.\n", (float)(cputstop.tms_stime - cputstart.tms_stime) / (float)(sysconf(_SC_CLK_TCK)) * 1000); printf("My total CPU time for child processes = %g ms.\n", (float)( (cputstop.tms_cutime + cputstop.tms_cstime) - (cputstart.tms_cutime + cputstart.tms_cstime) ) / (float)(sysconf(_SC_CLK_TCK)) * 1000); /* Contrary to the man pages, this appears not to include the parent */ printf("--------------------------------------------\n"); } ``````

My complete output is:
 Submission run for "zulfi". Matrix dimension N = 4. Number of processors = 2. Starting clock. Initializing... A = 41362.46, 17993.23, 39584.68, 60143.39; 43188.59, 16488.19, 40613.91, 40246.41; 27072.91, 24723.83, 50802.55, 59139.54; 25274.54, 52512.21, 7269.44, 26239.78; B = [35349.72; 54644.97; 64275.91; 47736.07] Stopped clock. Elapsed time = 0.172 ms. (CPU times are accurate to the nearest 10 ms) My total CPU time for parent = 0 ms. My system CPU time for parent = 0 ms. My total CPU time for child processes = 0 ms.

Can somebody please guide me why I am getting zero values in the output?

Zulfi.
 ``12345678910111213141516`` ``````\$ gcc -g -Wall -Wextra main.c main.c: In function ‘parameters’: main.c:33:8: warning: unused variable ‘uid’ [-Wunused-variable] char uid[/*L_cuserid + 2*/50]; /*User name */ ^ main.c: At top level: main.c:122:6: warning: return type of ‘main’ is not ‘int’ [-Wmain] void main(int argc, char **argv) { ^ main.c: In function ‘main’: main.c:126:21: warning: variable ‘etstop2’ set but not used [-Wunused-but-set-variable] clock_t etstart2, etstop2; /* Elapsed times using times() */ ^ main.c:126:11: warning: variable ‘etstart2’ set but not used [-Wunused-but-set-variable] clock_t etstart2, etstop2; /* Elapsed times using times() */ ^``````

> Can somebody please guide me why I am getting zero values in the output?
Because to a close approximation, the CPU spent zero time in your code.
The Elapsed time counts everything, like waiting for your terminal to scroll a line, and other "outside of your program delays".

If you want the two to approximate to one another, you need to time WITHOUT all the printf.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````\$ gdb -q ./a.out Reading symbols from ./a.out...done. (gdb) b 161 Breakpoint 1 at 0x400dc6: file main.c, line 161. (gdb) run submit Starting program: /home/sc/Documents/a.out submit Submission run for "(null)". Matrix dimension N = 4. Number of processors = 2. Starting clock. Initializing... A = 45414.91, 30763.74, 13820.80, 12768.55; 48488.48, 29524.33, 37303.70, 32703.15; 60592.30, 63970.63, 9712.53, 11859.02; 45788.96, 43055.27, 31798.37, 56445.63; B = [63451.12; 61307.25; 23064.80; 42820.51] Stopped clock. Elapsed time = 0.042 ms. Breakpoint 1, main (argc=2, argv=0x7fffffffdf38) at main.c:161 161 printf("(CPU times are accurate to the nearest %g ms)\n", (gdb) info locals etstart = {tv_sec = 1552807385, tv_usec = 490306} etstop = {tv_sec = 1552807385, tv_usec = 490348} tzdummy = {tz_minuteswest = 0, tz_dsttime = 0} etstart2 = 1718247374 etstop2 = 1718247374 usecstart = 1552807385490306 usecstop = 1552807385490348 cputstart = {tms_utime = 0, tms_stime = 0, tms_cutime = 0, tms_cstime = 0} cputstop = {tms_utime = 0, tms_stime = 0, tms_cutime = 0, tms_cstime = 0} (gdb) p usecstop - usecstart \$1 = 42``````

The actual time in your program (excluding all the waiting for I/O) was just 42uS.
Hi,
I tried with a debugger but I got following:

gdb -q ./gauss2 10 8 2
Excess command line arguments ignored. (8 ...)
Reading symbols from ./gauss2...(no debugging symbols found)...done.
Attaching to program: /home/zulfi/Ass1PPToSubmit/gauss2, process 10
Could not attach to process. If your uid matches the uid of the target
process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try
again as the root user. For more details, see /etc/sysctl.d/10-ptrace.conf
ptrace: Operation not permitted.
/home/zulfi/Ass1PPToSubmit/10: No such file or directory.
(gdb)

I dont know how to fix this problem. I have to pass the arguments. If possible please guide me.

Zulfi.
The arguments are passed on the run command in gdb, not the command line to gdb

 ``123`` ``````gdb -q ./gauss2 ... (gdb) run 10 8 2``````

> Reading symbols from ./gauss2...(no debugging symbols found)...done.
Oh, and if you want a meaningful debug experience, pass the -g flag to the compiler when you compile your code.
Last edited on