PAPI  5.7.0.0
branches.c File Reference
Include dependency graph for branches.c:

Go to the source code of this file.

Macros

#define MAXEVENTS   4
 
#define MINCOUNTS   100000
 
#define MPX_TOLERANCE   .20
 

Functions

int main (int argc, char **argv)
 

Macro Definition Documentation

◆ MAXEVENTS

#define MAXEVENTS   4

Definition at line 33 of file branches.c.

◆ MINCOUNTS

#define MINCOUNTS   100000

Definition at line 34 of file branches.c.

◆ MPX_TOLERANCE

#define MPX_TOLERANCE   .20

Definition at line 35 of file branches.c.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 38 of file branches.c.

39 {
40  PAPI_event_info_t info;
41  int i, j, retval, errors=0;
42  int iters = 10000000;
43  double x = 1.1, y;
44  long long t1, t2;
45  long long values[MAXEVENTS], refvalues[MAXEVENTS];
46  double spread[MAXEVENTS];
47  int nevents = MAXEVENTS;
48  int eventset = PAPI_NULL;
49  int events[MAXEVENTS];
50  int quiet;
51  char event_names[MAXEVENTS][256] = {
52  "PAPI_BR_NTK", // not taken
53  "PAPI_BR_PRC", // predicted correctly
54  "PAPI_BR_INS", // total branches
55  "PAPI_BR_MSP", // branches mispredicted
56  };
57 
58  /* Set quiet variable */
59  quiet = tests_quiet( argc, argv );
60 
61  /* Parse command line args */
62  if ( argc > 1 ) {
63  if ( !strcmp( argv[1], "TESTS_QUIET" ) ) {
64 
65  }
66  }
67 
68  events[0] = PAPI_BR_NTK; // not taken
69  events[1] = PAPI_BR_PRC; // predicted correctly
70  events[2] = PAPI_BR_INS; // total branches
71  events[3] = PAPI_BR_MSP; // branches mispredicted
72 
73  /* Why were these disabled?
74  events[3]=PAPI_BR_CN;
75  events[4]=PAPI_BR_UCN;
76  events[5]=PAPI_BR_TKN; */
77 
78 
79  /* Clear out the results to zero */
80  for ( i = 0; i < MAXEVENTS; i++ ) {
81  values[i] = 0;
82  }
83 
84  if ( !quiet ) {
85  printf( "\nAccuracy check of branch presets.\n" );
86  printf( "Comparing a measurement with separate measurements.\n\n" );
87  }
88 
89  /* Initialize library */
91  if (retval != PAPI_VER_CURRENT ) {
92  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
93  }
94 
95  /* Create Eventset */
96  retval = PAPI_create_eventset( &eventset );
97  if ( retval ) {
98  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
99  }
100 
101 #ifdef MPX
103  if ( retval ) {
104  test_fail( __FILE__, __LINE__, "PAPI_multiplex_init", retval );
105  }
106 
107  retval = PAPI_set_multiplex( eventset );
108  if ( retval ) {
109  test_fail( __FILE__, __LINE__, "PAPI_set_multiplex", retval );
110  }
111 #endif
112 
113  nevents = 0;
114 
115  /* Add as many of the 4 events that exist on this machine */
116  for ( i = 0; i < MAXEVENTS; i++ ) {
117  if ( PAPI_query_event( events[i] ) != PAPI_OK )
118  continue;
119  if ( PAPI_add_event( eventset, events[i] ) == PAPI_OK ) {
120  events[nevents] = events[i];
121  nevents++;
122  }
123  }
124 
125  /* If none of the events can be added, skip this test */
126  if ( nevents < 1 ) {
127  test_skip( __FILE__, __LINE__, "Not enough events left...", 0 );
128  }
129 
130  /* Find a reasonable number of iterations (each
131  * event active 20 times) during the measurement
132  */
133 
134  /* Target: 10000 usec/multiplex, 20 repeats */
135  t2 = (long long)(10000 * 20) * nevents;
136 
137  if ( t2 > 30e6 ) {
138  test_skip( __FILE__, __LINE__, "This test takes too much time",
139  retval );
140  }
141 
142  /* Measure one run */
143  t1 = PAPI_get_real_usec( );
144  y = do_flops3( x, iters, 1 );
145  t1 = PAPI_get_real_usec( ) - t1;
146 
147  if ( t2 > t1 ) /* Scale up execution time to match t2 */
148  iters = iters * ( int ) ( t2 / t1 );
149  else if ( t1 > 30e6 ) /* Make sure execution time is < 30s per repeated test */
150  test_skip( __FILE__, __LINE__, "This test takes too much time",
151  retval );
152 
153  x = 1.0;
154 
155  /**********************************/
156  /* First run: Grouped Measurement */
157  /**********************************/
158  if ( !quiet ) {
159  printf( "\nFirst run: Together.\n" );
160  }
161 
162  t1 = PAPI_get_real_usec( );
163 
164  retval = PAPI_start( eventset );
165  if (retval) test_fail( __FILE__, __LINE__, "PAPI_start", retval );
166 
167  y = do_flops3( x, iters, 1 );
168 
169  retval = PAPI_stop( eventset, values );
170  if (retval) test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
171 
172  t2 = PAPI_get_real_usec( );
173 
174  if ( !quiet ) {
175  printf( "\tOperations= %.1f Mflop", y * 1e-6 );
176  printf( "\t(%g Mflop/s)\n\n", ( y / ( double ) ( t2 - t1 ) ) );
177  printf( "PAPI grouped measurement:\n" );
178 
179  for ( j = 0; j < nevents; j++ ) {
180  PAPI_get_event_info( events[j], &info );
181  printf( "%20s = ", info.short_descr );
182  printf( LLDFMT, values[j] );
183  printf( "\n" );
184  }
185  printf( "\n" );
186  }
187 
188  /* Remove all the events, start again */
189  retval = PAPI_remove_events( eventset, events, nevents );
190  if (retval) test_fail( __FILE__, __LINE__, "PAPI_remove_events", retval );
191 
192  retval = PAPI_destroy_eventset( &eventset );
193  if (retval) test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
194 
195  /* Recreate eventset */
196  eventset = PAPI_NULL;
197  retval = PAPI_create_eventset( &eventset );
198  if (retval) test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
199 
200  /* Run events one by one */
201  for ( i = 0; i < nevents; i++ ) {
202 
203  /* Clear out old event */
204  retval = PAPI_cleanup_eventset( eventset );
205  if (retval) test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
206  /* Add the event */
207  retval = PAPI_add_event( eventset, events[i] );
208  if (retval) test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
209 
210  x = 1.0;
211 
212  if ( !quiet ) {
213  printf( "\nReference measurement %d (of %d):\n", i + 1, nevents );
214  }
215 
216  t1 = PAPI_get_real_usec( );
217 
218  retval = PAPI_start( eventset );
219  if (retval) test_fail( __FILE__, __LINE__, "PAPI_start", retval );
220 
221  y = do_flops3( x, iters, 1 );
222 
223  retval = PAPI_stop( eventset, &refvalues[i] );
224  if (retval) test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
225 
226  t2 = PAPI_get_real_usec( );
227 
228 
229  if ( !quiet ) {
230  printf( "\tOperations= %.1f Mflop", y * 1e-6 );
231  printf( "\t(%g Mflop/s)\n\n", ( y / ( double ) ( t2 - t1 ) ) );
232  PAPI_get_event_info( events[i], &info );
233  printf( "PAPI results:\n%20s = ", info.short_descr );
234  printf( LLDFMT, refvalues[i] );
235  printf( "\n" );
236  }
237  }
238 
239  if ( !quiet ) {
240  printf( "\n" );
241  }
242 
243  /* Validate the results */
244 
245  if ( !quiet ) {
246  printf( "\n\nRelative accuracy:\n" );
247  printf( "\tEvent\t\tGroup\t\tIndividual\tSpread\n");
248  }
249 
250  for ( j = 0; j < nevents; j++ ) {
251  spread[j] = abs( ( int ) ( refvalues[j] - values[j] ) );
252  if ( values[j] )
253  spread[j] /= ( double ) values[j];
254  if ( !quiet ) {
255  printf( "\t%02d: ",j);
256  printf( "%s",event_names[j]);
257  printf( "\t%10lld", values[j] );
258  printf( "\t%10lld", refvalues[j] );
259  printf("\t%10.3g\n", spread[j] );
260  }
261 
262  /* Make sure that NaN get counted as errors */
263  if ( spread[j] > MPX_TOLERANCE ) {
264 
265  /* Neglect inprecise results with low counts */
266  if ( refvalues[j] < MINCOUNTS ) {
267  }
268  else {
269  errors++;
270  if (!quiet) {
271  printf("\tError: Spread > %lf\n",MPX_TOLERANCE);
272  }
273  }
274  }
275  }
276  if ( !quiet ) {
277  printf( "\n\n" );
278  }
279 
280  if ( errors ) {
281  test_fail( __FILE__, __LINE__, "Values outside threshold", i );
282  }
283 
284  test_pass( __FILE__ );
285 
286  return 0;
287 }
#define PAPI_OK
Definition: fpapi.h:105
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
void test_pass(const char *filename)
Definition: test_utils.c:432
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1663
double do_flops3(double x, int iters, int quiet)
#define MAXEVENTS
Definition: branches.c:33
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
volatile double t2
int retval
Definition: zero_fork.c:53
int PAPI_remove_events(int EventSet, int *Events, int number)
Definition: papi.c:5931
volatile double t1
int PAPI_get_event_info(int EventCode, PAPI_event_info_t *info)
Definition: papi.c:835
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:561
char events[MAX_EVENTS][BUFSIZ]
int PAPI_library_init(int version)
Definition: papi.c:500
int quiet
Definition: rapl_overflow.c:18
#define PAPI_NULL
Definition: fpapi.h:13
#define MPX_TOLERANCE
Definition: branches.c:35
#define PAPI_BR_INS
Definition: fpapi.h:191
#define MINCOUNTS
Definition: branches.c:34
char short_descr[PAPI_MIN_STR_LEN]
Definition: papi.h:968
int PAPI_cleanup_eventset(int EventSet)
Definition: papi.c:2890
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
int PAPI_query_event(int EventCode)
Definition: papi.c:684
#define LLDFMT
Definition: papi_test.h:94
int PAPI_multiplex_init(void)
Definition: papi.c:2982
long long PAPI_get_real_usec(void)
Definition: papi.c:6264
#define PAPI_BR_MSP
Definition: fpapi.h:182
#define PAPI_BR_PRC
Definition: fpapi.h:183
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
void test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:468
int x
Definition: fileop.c:78
int PAPI_set_multiplex(int EventSet)
Definition: papi.c:3333
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:2014
#define PAPI_BR_NTK
Definition: fpapi.h:181
int PAPI_start(int EventSet)
Definition: papi.c:2096
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
int i
Definition: fileop.c:140
long long y
Definition: iozone.c:1335
Here is the call graph for this function: