PAPI  5.6.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 <stdio.h>
23 #include <stdlib.h>
24 
25 #include "papi.h"
26 #include "papi_test.h"
27 
28 #include "do_loops.h"
29 
30 #define NUM_EVENTS 2
31 
32 int
33 main( int argc, char **argv )
34 {
35  int retval;
37  int Events[NUM_EVENTS];
38  int EventSet = PAPI_NULL;
39  int quiet;
40 
41  /* Set TESTS_QUIET variable */
42  quiet=tests_quiet( argc, argv );
43 
45  if (retval != PAPI_VER_CURRENT ) {
46  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
47  }
48 
49  /* query and set up the right events to monitor */
50  if ( PAPI_query_event( PAPI_FP_INS ) == PAPI_OK ) {
51  Events[0] = PAPI_FP_INS;
52  Events[1] = PAPI_TOT_CYC;
53  } else {
54  Events[0] = PAPI_TOT_INS;
55  Events[1] = PAPI_TOT_CYC;
56  }
57 
58  retval = PAPI_create_eventset( &EventSet );
59  if (retval != PAPI_OK ) {
60  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
61  }
62 
63  retval = PAPI_add_events( EventSet, ( int * ) Events, NUM_EVENTS );
64  if (retval < PAPI_OK ) {
65  if (!quiet) printf("Trouble adding events\n");
66  test_skip( __FILE__, __LINE__, "PAPI_add_events", retval );
67  }
68 
69  if ( !quiet ) {
70  printf( "\n Incorrect usage of read and accum.\n" );
71  printf( " Some cycles are counted twice\n" );
72  }
73  if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK )
74  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
75 
76  /* Loop 1 */
78 
79  if ( ( retval = PAPI_read( EventSet, values ) ) != PAPI_OK )
80  test_fail( __FILE__, __LINE__, "PAPI_read", retval );
81 
82  if ( !quiet )
83  printf( TWO12, values[0], values[1], "(Counters continuing...)\n" );
84 
85  /* Loop 2 */
87 
88  /* Using PAPI_accum here is incorrect. The result is that Loop 1 *
89  * is being counted twice */
90  if ( ( retval = PAPI_accum( EventSet, values ) ) != PAPI_OK )
91  test_fail( __FILE__, __LINE__, "PAPI_accum", retval );
92 
93  if ( !quiet )
94  printf( TWO12, values[0], values[1], "(Counters being accumulated)\n" );
95 
96  /* Loop 3 */
98 
99  if ( ( retval = PAPI_stop( EventSet, dummyvalues ) ) != PAPI_OK )
100  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
101 
102  if ( ( retval = PAPI_read( EventSet, dummyvalues ) ) != PAPI_OK )
103  test_fail( __FILE__, __LINE__, "PAPI_read", retval );
104 
105  if ( !quiet ) {
106  printf( TWO12, dummyvalues[0], dummyvalues[1],
107  "(Reading stopped counters)\n" );
108 
109  printf( TWO12, values[0], values[1], "" );
110 
111  printf( "\n Incorrect usage of read and accum.\n" );
112  printf( " Another incorrect use\n" );
113  }
114  if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK )
115  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
116 
117  /* Loop 1 */
118  do_flops( NUM_FLOPS );
119 
120  if ( ( retval = PAPI_read( EventSet, values ) ) != PAPI_OK )
121  test_fail( __FILE__, __LINE__, "PAPI_read", retval );
122 
123  if ( !quiet )
124  printf( TWO12, values[0], values[1], "(Counters continuing...)\n" );
125 
126  /* Loop 2 */
127  /* Code that should not be counted */
128  do_flops( NUM_FLOPS );
129 
130  if ( ( retval = PAPI_read( EventSet, dummyvalues ) ) != PAPI_OK )
131  test_fail( __FILE__, __LINE__, "PAPI_read", retval );
132 
133  if ( !quiet )
134  printf( TWO12, dummyvalues[0], dummyvalues[1],
135  "(Intermediate counts...)\n" );
136 
137  /* Loop 3 */
138  do_flops( NUM_FLOPS );
139 
140  /* Since PAPI_read does not reset the counters it's use above after *
141  * loop 2 is incorrect. Instead Loop1 will in effect be counted twice. *
142  * and the counts in loop 2 are included in the total counts */
143  if ( ( retval = PAPI_accum( EventSet, values ) ) != PAPI_OK )
144  test_fail( __FILE__, __LINE__, "PAPI_accum", retval );
145  if ( !quiet )
146  printf( TWO12, values[0], values[1], "" );
147 
148  if ( ( retval = PAPI_stop( EventSet, dummyvalues ) ) != PAPI_OK )
149  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
150 
151  if ( !quiet ) {
152  printf( "\n Correct usage of read and accum.\n" );
153  printf( " PAPI_reset and PAPI_accum used to skip counting\n" );
154  printf( " a section of the code.\n" );
155  }
156  if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK )
157  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
158 
159  do_flops( NUM_FLOPS );
160 
161  if ( ( retval = PAPI_read( EventSet, values ) ) != PAPI_OK )
162  test_fail( __FILE__, __LINE__, "PAPI_read", retval );
163  if ( !quiet )
164  printf( TWO12, values[0], values[1], "(Counters continuing)\n" );
165 
166  /* Code that should not be counted */
167  do_flops( NUM_FLOPS );
168 
169  if ( ( retval = PAPI_reset( EventSet ) ) != PAPI_OK )
170  test_fail( __FILE__, __LINE__, "PAPI_reset", retval );
171 
172  if ( !quiet )
173  printf( "%12s %12s (Counters reset)\n", "", "" );
174 
175  do_flops( NUM_FLOPS );
176 
177  if ( ( retval = PAPI_accum( EventSet, values ) ) != PAPI_OK )
178  test_fail( __FILE__, __LINE__, "PAPI_accum", retval );
179 
180  if ( !quiet )
181  printf( TWO12, values[0], values[1], "" );
182 
183  if ( !quiet ) {
184  printf( "----------------------------------\n" );
185  printf( "Verification: The last line in each experiment should be\n" );
186  printf( "approximately twice the value of the first line.\n" );
187  printf
188  ( "The third case illustrates one possible way to accomplish this.\n" );
189  }
190  test_pass( __FILE__ );
191 
192  return 0;
193 }
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
#define TWO12
Definition: papi_test.h:93
void test_pass(const char *filename)
Definition: test_utils.c:432
int PAPI_reset(int EventSet)
Definition: papi.c:2459
static int Events[NUM_EVENTS]
Definition: init_fini.c:8
#define PAPI_NULL
Definition: papi.h:292
#define PAPI_TOT_INS
int EventSet
return PAPI_OK
Definition: linux-nvml.c:497
#define NUM_FLOPS
Definition: sdsc-mpx.c:24
int int argc
Definition: iozone.c:1609
int PAPI_add_events(int EventSet, int *Events, int number)
Definition: papi.c:5843
Return codes and api definitions.
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:559
char ** argv
Definition: iozone.c:1610
int PAPI_accum(int EventSet, long long *values)
Definition: papi.c:2745
int PAPI_library_init(int version)
Definition: papi.c:500
int quiet
Definition: rapl_overflow.c:18
#define PAPI_TOT_CYC
long long dummyvalues[2]
#define NUM_EVENTS
Definition: low-level.c:30
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
void do_flops(int n)
Definition: multiplex.c:23
int PAPI_query_event(int EventCode)
Definition: papi.c:684
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 PAPI_read(int EventSet, long long *values)
Definition: papi.c:2559
int PAPI_start(int EventSet)
Definition: papi.c:2096
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_FP_INS