PAPI  5.3.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
low-level.c
Go to the documentation of this file.
1 /* This examples show the essentials in using the PAPI low-level
2  interface. The program consists of 3 examples where the work
3  done over some work-loops. The example tries to illustrate
4  some simple mistakes that are easily made and how a correct
5  code would accomplish the same thing.
6 
7  Example 1: The total count over two work loops (Loops 1 and 2)
8  are supposed to be measured. Due to a mis-understanding of the
9  semantics of the API the total count gets wrong.
10  The example also illustrates that it is legal to read both
11  running and stopped counters.
12 
13  Example 2: The total count over two work loops (Loops 1 and 3)
14  is supposed to be measured while discarding the counts made in
15  loop 2. Instead the counts in loop1 are counted twice and the
16  counts in loop2 are added to the total number of counts.
17 
18  Example 3: One correct way of accomplishing the result aimed for
19  in example 2.
20 */
21 
22 #include "papi_test.h"
23 
24 extern int TESTS_QUIET; /* Declared in test_utils.c */
25 
26 int
27 main( int argc, char **argv )
28 {
29  int retval;
30 #define NUM_EVENTS 2
32  int Events[NUM_EVENTS];
33  int EventSet = PAPI_NULL;
34 
35  tests_quiet( argc, argv ); /* Set TESTS_QUIET variable */
36 
37 
38  if ( ( retval =
40  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
41 
42  /* query and set up the right events to monitor */
43  if ( PAPI_query_event( PAPI_FP_INS ) == PAPI_OK ) {
44  Events[0] = PAPI_FP_INS;
45  Events[1] = PAPI_TOT_CYC;
46  } else {
47  Events[0] = PAPI_TOT_INS;
48  Events[1] = PAPI_TOT_CYC;
49  }
50 
51  if ( ( retval = PAPI_create_eventset( &EventSet ) ) != PAPI_OK )
52  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
53 
54  if ( ( retval =
55  PAPI_add_events( EventSet, ( int * ) Events,
56  NUM_EVENTS ) ) < PAPI_OK )
57  test_fail( __FILE__, __LINE__, "PAPI_add_events", retval );
58 
59  if ( !TESTS_QUIET ) {
60  printf( "\n Incorrect usage of read and accum.\n" );
61  printf( " Some cycles are counted twice\n" );
62  }
63  if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK )
64  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
65 
66  /* Loop 1 */
68 
69  if ( ( retval = PAPI_read( EventSet, values ) ) != PAPI_OK )
70  test_fail( __FILE__, __LINE__, "PAPI_read", retval );
71 
72  if ( !TESTS_QUIET )
73  printf( TWO12, values[0], values[1], "(Counters continuing...)\n" );
74 
75  /* Loop 2 */
77 
78  /* Using PAPI_accum here is incorrect. The result is that Loop 1 *
79  * is being counted twice */
80  if ( ( retval = PAPI_accum( EventSet, values ) ) != PAPI_OK )
81  test_fail( __FILE__, __LINE__, "PAPI_accum", retval );
82 
83  if ( !TESTS_QUIET )
84  printf( TWO12, values[0], values[1], "(Counters being accumulated)\n" );
85 
86  /* Loop 3 */
88 
89  if ( ( retval = PAPI_stop( EventSet, dummyvalues ) ) != PAPI_OK )
90  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
91 
92  if ( ( retval = PAPI_read( EventSet, dummyvalues ) ) != PAPI_OK )
93  test_fail( __FILE__, __LINE__, "PAPI_read", retval );
94 
95  if ( !TESTS_QUIET ) {
96  printf( TWO12, dummyvalues[0], dummyvalues[1],
97  "(Reading stopped counters)\n" );
98 
99  printf( TWO12, values[0], values[1], "" );
100 
101  printf( "\n Incorrect usage of read and accum.\n" );
102  printf( " Another incorrect use\n" );
103  }
104  if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK )
105  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
106 
107  /* Loop 1 */
108  do_flops( NUM_FLOPS );
109 
110  if ( ( retval = PAPI_read( EventSet, values ) ) != PAPI_OK )
111  test_fail( __FILE__, __LINE__, "PAPI_read", retval );
112 
113  if ( !TESTS_QUIET )
114  printf( TWO12, values[0], values[1], "(Counters continuing...)\n" );
115 
116  /* Loop 2 */
117  /* Code that should not be counted */
118  do_flops( NUM_FLOPS );
119 
120  if ( ( retval = PAPI_read( EventSet, dummyvalues ) ) != PAPI_OK )
121  test_fail( __FILE__, __LINE__, "PAPI_read", retval );
122 
123  if ( !TESTS_QUIET )
124  printf( TWO12, dummyvalues[0], dummyvalues[1],
125  "(Intermediate counts...)\n" );
126 
127  /* Loop 3 */
128  do_flops( NUM_FLOPS );
129 
130  /* Since PAPI_read does not reset the counters it's use above after *
131  * loop 2 is incorrect. Instead Loop1 will in effect be counted twice. *
132  * and the counts in loop 2 are included in the total counts */
133  if ( ( retval = PAPI_accum( EventSet, values ) ) != PAPI_OK )
134  test_fail( __FILE__, __LINE__, "PAPI_accum", retval );
135  if ( !TESTS_QUIET )
136  printf( TWO12, values[0], values[1], "" );
137 
138  if ( ( retval = PAPI_stop( EventSet, dummyvalues ) ) != PAPI_OK )
139  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
140 
141  if ( !TESTS_QUIET ) {
142  printf( "\n Correct usage of read and accum.\n" );
143  printf( " PAPI_reset and PAPI_accum used to skip counting\n" );
144  printf( " a section of the code.\n" );
145  }
146  if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK )
147  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
148 
149  do_flops( NUM_FLOPS );
150 
151  if ( ( retval = PAPI_read( EventSet, values ) ) != PAPI_OK )
152  test_fail( __FILE__, __LINE__, "PAPI_read", retval );
153  if ( !TESTS_QUIET )
154  printf( TWO12, values[0], values[1], "(Counters continuing)\n" );
155 
156  /* Code that should not be counted */
157  do_flops( NUM_FLOPS );
158 
159  if ( ( retval = PAPI_reset( EventSet ) ) != PAPI_OK )
160  test_fail( __FILE__, __LINE__, "PAPI_reset", retval );
161 
162  if ( !TESTS_QUIET )
163  printf( "%12s %12s (Counters reset)\n", "", "" );
164 
165  do_flops( NUM_FLOPS );
166 
167  if ( ( retval = PAPI_accum( EventSet, values ) ) != PAPI_OK )
168  test_fail( __FILE__, __LINE__, "PAPI_accum", retval );
169 
170  if ( !TESTS_QUIET )
171  printf( TWO12, values[0], values[1], "" );
172 
173  if ( !TESTS_QUIET ) {
174  printf( "----------------------------------\n" );
175  printf( "Verification: The last line in each experiment should be\n" );
176  printf( "approximately twice the value of the first line.\n" );
177  printf
178  ( "The third case illustrates one possible way to accomplish this.\n" );
179  }
180  test_pass( __FILE__, NULL, 0 );
181  exit( 1 );
182 }
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2225
#define NUM_FLOPS
#define TWO12
Definition: papi_test.h:117
int PAPI_reset(int EventSet)
Definition: papi.c:2370
#define PAPI_NULL
Definition: fpapi.h:13
static int Events[NUM_EVENTS]
Definition: init_fini.c:8
#define PAPI_TOT_INS
Definition: fpapi.h:185
return PAPI_OK
Definition: linux-nvml.c:458
tests_quiet(argc, argv)
#define printf
Definition: papi_test.h:125
test_pass(__FILE__, NULL, 0)
int int argc
Definition: iozone.c:1609
int PAPI_add_events(int EventSet, int *Events, int number)
Definition: papi.c:5707
int TESTS_QUIET
Definition: test_utils.c:11
char ** argv
Definition: iozone.c:1610
int PAPI_accum(int EventSet, long long *values)
Definition: papi.c:2653
test_fail(__FILE__, __LINE__,"PAPI_library_init", retval)
int PAPI_library_init(int version)
Definition: papi.c:495
#define PAPI_TOT_CYC
Definition: fpapi.h:194
long long dummyvalues[2]
#define NUM_EVENTS
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
#define PAPI_FP_INS
Definition: fpapi.h:187
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1399
void do_flops(int n)
Definition: multiplex.c:23
int PAPI_query_event(int EventCode)
Definition: papi.c:695
int EventSet
int PAPI_read(int EventSet, long long *values)
Definition: papi.c:2469
int PAPI_start(int EventSet)
Definition: papi.c:2019
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
ssize_t retval
Definition: libasync.c:338
int main(int argc, char **argv)
List all appio events codes and names.
void exit()