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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/times.h>
#include <sys/time.h>
#include <limits.h>
#include <unistd.h>


/* 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 <matrix_dimension> <num_procs> [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.
1. Fix your warnings.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
$ 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

1
2
3
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
Registered users can post here. Sign in or register to post.