PAPI  5.6.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
branches.c
Go to the documentation of this file.
1 /*
2  * Test example for branch accuracy and functionality, originally
3  * provided by Timothy Kaiser, SDSC. It was modified to fit the
4  * PAPI test suite by Nils Smeds, <smeds@pdc.kth.se>.
5  * and Phil Mucci <mucci@cs.utk.edu>
6  * This example verifies the accuracy of branch events
7  */
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <math.h>
13 
14 #include "papi.h"
15 #include "papi_test.h"
16 
17 #include "testcode.h"
18 
19 #define MAXEVENTS 4
20 #define SLEEPTIME 100
21 #define MINCOUNTS 100000
22 #define MPX_TOLERANCE .20
23 
24 int
25 main( int argc, char **argv )
26 {
27  PAPI_event_info_t info;
28  int i, j, retval;
29  int iters = 10000000;
30  double x = 1.1, y;
31  long long t1, t2;
32  long long values[MAXEVENTS], refvalues[MAXEVENTS];
33  int sleep_time = SLEEPTIME;
34  double spread[MAXEVENTS];
35  int nevents = MAXEVENTS;
36  int eventset = PAPI_NULL;
37  int events[MAXEVENTS];
38  int quiet;
39 
40  /* Set quiet variable */
41  quiet = tests_quiet( argc, argv );
42 
43  /* Parse command line args */
44  if ( argc > 1 ) {
45  if ( !strcmp( argv[1], "TESTS_QUIET" ) ) {
46 
47  }
48  else {
49  sleep_time = atoi( argv[1] );
50  if ( sleep_time <= 0 )
51  sleep_time = SLEEPTIME;
52  }
53  }
54 
55  events[0] = PAPI_BR_NTK;
56  events[1] = PAPI_BR_PRC;
57  events[2] = PAPI_BR_INS;
58  events[3] = PAPI_BR_MSP;
59 
60  /* Why were these disabled?
61  events[3]=PAPI_BR_CN;
62  events[4]=PAPI_BR_UCN;
63  events[5]=PAPI_BR_TKN; */
64 
65 
66  for ( i = 0; i < MAXEVENTS; i++ ) {
67  values[i] = 0;
68  }
69 
70  if ( !quiet ) {
71  printf( "\nAccuracy check of branch presets.\n" );
72  printf( "Comparing a measurement with separate measurements.\n\n" );
73  }
74 
75  if ( ( retval =
77  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
78 
79  if ( ( retval = PAPI_create_eventset( &eventset ) ) )
80  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
81 
82 #ifdef MPX
83  if ( ( retval = PAPI_multiplex_init( ) ) )
84  test_fail( __FILE__, __LINE__, "PAPI_multiplex_init", retval );
85 
86  if ( ( retval = PAPI_set_multiplex( eventset ) ) )
87  test_fail( __FILE__, __LINE__, "PAPI_set_multiplex", retval );
88 #endif
89 
90  nevents = 0;
91 
92  for ( i = 0; i < MAXEVENTS; i++ ) {
93  if ( PAPI_query_event( events[i] ) != PAPI_OK )
94  continue;
95  if ( PAPI_add_event( eventset, events[i] ) == PAPI_OK ) {
96  events[nevents] = events[i];
97  nevents++;
98  }
99  }
100 
101  if ( nevents < 1 )
102  test_skip( __FILE__, __LINE__, "Not enough events left...", 0 );
103 
104  /* Find a reasonable number of iterations (each
105  * event active 20 times) during the measurement
106  */
107  t2 = (long long)(10000 * 20) * nevents; /* Target: 10000 usec/multiplex, 20 repeats */
108  if ( t2 > 30e6 )
109  test_skip( __FILE__, __LINE__, "This test takes too much time",
110  retval );
111 
112  /* Measure one run */
113  t1 = PAPI_get_real_usec( );
114  y = do_flops3( x, iters, 1 );
115  t1 = PAPI_get_real_usec( ) - t1;
116 
117  if ( t2 > t1 ) /* Scale up execution time to match t2 */
118  iters = iters * ( int ) ( t2 / t1 );
119  else if ( t1 > 30e6 ) /* Make sure execution time is < 30s per repeated test */
120  test_skip( __FILE__, __LINE__, "This test takes too much time",
121  retval );
122 
123  x = 1.0;
124 
125  if ( !quiet )
126  printf( "\nFirst run: Together.\n" );
127 
128  t1 = PAPI_get_real_usec( );
129  if ( ( retval = PAPI_start( eventset ) ) )
130  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
131  y = do_flops3( x, iters, 1 );
132  if ( ( retval = PAPI_stop( eventset, values ) ) )
133  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
134  t2 = PAPI_get_real_usec( );
135 
136  if ( !quiet ) {
137  printf( "\tOperations= %.1f Mflop", y * 1e-6 );
138  printf( "\t(%g Mflop/s)\n\n", ( y / ( double ) ( t2 - t1 ) ) );
139  printf( "PAPI grouped measurement:\n" );
140  }
141  for ( j = 0; j < nevents; j++ ) {
142  PAPI_get_event_info( events[j], &info );
143  if ( !quiet ) {
144  printf( "%20s = ", info.short_descr );
145  printf( LLDFMT, values[j] );
146  printf( "\n" );
147  }
148  }
149  if ( !quiet )
150  printf( "\n" );
151 
152 
153  if ( ( retval = PAPI_remove_events( eventset, events, nevents ) ) )
154  test_fail( __FILE__, __LINE__, "PAPI_remove_events", retval );
155  if ( ( retval = PAPI_destroy_eventset( &eventset ) ) )
156  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
157  eventset = PAPI_NULL;
158  if ( ( retval = PAPI_create_eventset( &eventset ) ) )
159  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
160 
161  for ( i = 0; i < nevents; i++ ) {
162 
163  if ( ( retval = PAPI_cleanup_eventset( eventset ) ) )
164  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
165  if ( ( retval = PAPI_add_event( eventset, events[i] ) ) )
166  test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
167 
168  x = 1.0;
169 
170  if ( !quiet )
171  printf( "\nReference measurement %d (of %d):\n", i + 1, nevents );
172 
173  t1 = PAPI_get_real_usec( );
174  if ( ( retval = PAPI_start( eventset ) ) )
175  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
176  y = do_flops3( x, iters, 1 );
177  if ( ( retval = PAPI_stop( eventset, &refvalues[i] ) ) )
178  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
179  t2 = PAPI_get_real_usec( );
180 
181  if ( !quiet ) {
182  printf( "\tOperations= %.1f Mflop", y * 1e-6 );
183  printf( "\t(%g Mflop/s)\n\n", ( y / ( double ) ( t2 - t1 ) ) );
184  }
185  PAPI_get_event_info( events[i], &info );
186  if ( !quiet ) {
187  printf( "PAPI results:\n%20s = ", info.short_descr );
188  printf( LLDFMT, refvalues[i] );
189  printf( "\n" );
190  }
191  }
192  if ( !quiet )
193  printf( "\n" );
194 
195 
196  if ( !quiet ) {
197  printf( "\n\nRelative accuracy:\n" );
198  for ( j = 0; j < nevents; j++ )
199  printf( " Event %.2d", j );
200  printf( "\n" );
201  }
202 
203  for ( j = 0; j < nevents; j++ ) {
204  spread[j] = abs( ( int ) ( refvalues[j] - values[j] ) );
205  if ( values[j] )
206  spread[j] /= ( double ) values[j];
207  if ( !quiet )
208  printf( "%10.3g ", spread[j] );
209  /* Make sure that NaN get counted as errors */
210  if ( spread[j] < MPX_TOLERANCE )
211  i--;
212  else if ( refvalues[j] < MINCOUNTS ) /* Neglect inprecise results with low counts */
213  i--;
214  }
215  if ( !quiet ) {
216  printf( "\n\n" );
217  }
218 
219  if ( i ) {
220  test_fail( __FILE__, __LINE__, "Values outside threshold", i );
221  }
222 
223  test_pass( __FILE__ );
224 
225  return 0;
226 }
227 
228 
int atoi()
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:19
#define PAPI_BR_INS
#define PAPI_NULL
Definition: papi.h:292
return PAPI_OK
Definition: linux-nvml.c:497
volatile double t2
#define PAPI_BR_MSP
int PAPI_remove_events(int EventSet, int *Events, int number)
Definition: papi.c:5931
volatile double t1
static double
Definition: fileop.c:1281
int int argc
Definition: iozone.c:1609
Return codes and api definitions.
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:559
char events[MAX_EVENTS][BUFSIZ]
char ** argv
Definition: iozone.c:1610
int PAPI_library_init(int version)
Definition: papi.c:500
long long y
Definition: iozone.c:1335
int i
Definition: fileop.c:140
int quiet
Definition: rapl_overflow.c:18
char short_descr[PAPI_MIN_STR_LEN]
Definition: papi.h:967
#define PAPI_BR_NTK
long long
Definition: iozone.c:19827
#define MPX_TOLERANCE
Definition: branches.c:22
#define MINCOUNTS
Definition: branches.c:21
int PAPI_cleanup_eventset(int EventSet)
Definition: papi.c:2890
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
#define SLEEPTIME
Definition: branches.c:20
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
printf("\tTry: -i 0 -i 1 \n\n")
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
Definition: iozone.c:18528
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
int PAPI_start(int EventSet)
Definition: papi.c:2096
long j
Definition: iozone.c:19135
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
ssize_t retval
Definition: libasync.c:338
#define PAPI_VER_CURRENT
Definition: papi.h:225
int main(int argc, char **argv)
List all appio events codes and names.
#define PAPI_BR_PRC