PAPI  5.6.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
high-level.c
Go to the documentation of this file.
1 /* These examples show the essentials in using the PAPI high-level
2  interface. The program consists of 4 work-loops. The programmer
3  intends to count the total events for loop 1, 2 and 4, but not
4  include the number of events in loop 3.
5 
6  To accomplish this PAPI_read_counters is used as a counter
7  reset function, while PAPI_accum_counters is used to sum
8  the contributions of loops 2 and 4 into the total count.
9 */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 
14 #include "papi.h"
15 #include "papi_test.h"
16 
17 #include "do_loops.h"
18 
19 #define NUM_EVENTS 2
20 
21 int
22 main( int argc, char **argv )
23 {
24  int retval;
26  long long myvalues[NUM_EVENTS];
27  int Events[NUM_EVENTS];
28  int quiet;
29 
30  /* Set TESTS_QUIET variable */
31  quiet=tests_quiet( argc, argv );
32 
34  if ( retval != PAPI_VER_CURRENT ) {
35  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
36  }
37 
38  /* query and set up the right events to monitor */
39  if ( PAPI_query_event( PAPI_FP_INS ) == PAPI_OK ) {
40  Events[0] = PAPI_FP_INS;
41  } else {
42  Events[0] = PAPI_TOT_INS;
43  }
44  Events[1] = PAPI_TOT_CYC;
45 
46  retval = PAPI_start_counters( ( int * ) Events, NUM_EVENTS );
47  if ( retval != PAPI_OK ) {
48  if (!quiet) printf("Cannot start events\n");
49  test_skip( __FILE__, __LINE__, "PAPI_start_counters", retval );
50  }
51 
52  /* Loop 1 */
54 
55  retval = PAPI_read_counters( values, NUM_EVENTS );
56  if ( retval != PAPI_OK )
57  test_fail( __FILE__, __LINE__, "PAPI_read_counters", retval );
58 
59  if ( !quiet )
60  printf( TWO12, values[0], values[1], "(Counters continuing...)\n" );
61 
62  myvalues[0] = values[0];
63  myvalues[1] = values[1];
64  /* Loop 2 */
66 
67  retval = PAPI_accum_counters( values, NUM_EVENTS );
68  if ( retval != PAPI_OK )
69  test_fail( __FILE__, __LINE__, "PAPI_accum_counters", retval );
70 
71  if ( !quiet )
72  printf( TWO12, values[0], values[1], "(Counters being ''held'')\n" );
73 
74  /* Loop 3 */
75  /* Simulated code that should not be counted */
77 
78  retval = PAPI_read_counters( dummyvalues, NUM_EVENTS );
79  if ( retval != PAPI_OK )
80  test_fail( __FILE__, __LINE__, "PAPI_read_counters", retval );
81  if ( !quiet )
82  printf( TWO12, dummyvalues[0], dummyvalues[1], "(Skipped counts)\n" );
83 
84  if ( !quiet )
85  printf( "%12s %12s (''Continuing'' counting)\n", "xxx", "xxx" );
86  /* Loop 4 */
88 
89  retval = PAPI_accum_counters( values, NUM_EVENTS );
90  if ( retval != PAPI_OK )
91  test_fail( __FILE__, __LINE__, "PAPI_accum_counters", retval );
92 
93  if ( !quiet )
94  printf( TWO12, values[0], values[1], "" );
95 
96  if ( !quiet ) {
97  printf( "----------------------------------\n" );
98  printf( "Verification: The last line in each experiment should be\n" );
99  printf( "approximately three times the value of the first line.\n" );
100  }
101 
102  {
103  long long min, max;
104  min = ( long long ) ( ( double ) myvalues[0] * .9 );
105  max = ( long long ) ( ( double ) myvalues[0] * 1.1 );
106  if ( values[0] < ( 3 * min ) || values[0] > ( 3 * max ) ) {
107  retval = 1;
108  if ( PAPI_query_event( PAPI_FP_INS ) == PAPI_OK ) {
109  test_fail( __FILE__, __LINE__, "PAPI_FP_INS", 1 );
110  } else {
111  test_fail( __FILE__, __LINE__, "PAPI_TOT_INS", 1 );
112  }
113  }
114  min = ( long long ) ( ( double ) myvalues[1] * .9 );
115  max = ( long long ) ( ( double ) myvalues[1] * 1.1 );
116  if ( values[1] < ( 3 * min ) || values[1] > ( 3 * max ) ) {
117  retval = 1;
118  test_fail( __FILE__, __LINE__, "PAPI_TOT_CYC", 1 );
119  }
120  }
121  /* The values array is not allocated through allocate_test_space
122  * so we need to pass NULL here */
123  test_pass( __FILE__ );
124 
125  return 0;
126 
127 }
#define TWO12
Definition: papi_test.h:93
off64_t max
Definition: libasync.c:331
void test_pass(const char *filename)
Definition: test_utils.c:432
#define NUM_EVENTS
Definition: high-level.c:19
static int Events[NUM_EVENTS]
Definition: init_fini.c:8
int PAPI_accum_counters(long long *values, int array_len)
Definition: papi_hl.c:756
#define PAPI_TOT_INS
return PAPI_OK
Definition: linux-nvml.c:497
#define NUM_FLOPS
Definition: sdsc-mpx.c:24
int int argc
Definition: iozone.c:1609
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_library_init(int version)
Definition: papi.c:500
int quiet
Definition: rapl_overflow.c:18
#define PAPI_TOT_CYC
long long dummyvalues[2]
long long
Definition: iozone.c:19827
#define min(x, y)
Definition: darwin-common.h:4
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_counters(long long *values, int array_len)
Definition: papi_hl.c:706
int PAPI_start_counters(int *events, int array_len)
Definition: papi_hl.c:593
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