PAPI  5.3.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 /*****************************************************************************
2 * This example code shows how to use most of PAPI's High level functions *
3 * to start,count,read and stop on an event set. We use two preset events *
4 * here: *
5 * PAPI_TOT_INS: Total instructions executed in a period of time *
6 * PAPI_TOT_CYC: Total cpu cycles in a period of time *
7 ******************************************************************************/
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include "papi.h"
12 
13 #define NUM_EVENTS 2
14 #define THRESHOLD 10000
15 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }
16 
17 /* stupid codes to be monitored */
19 {
20  double tmp=1.0;
21  int i=1;
22  for( i = 1; i < THRESHOLD; i++ )
23  {
24  tmp = tmp*i;
25  }
26 }
27 
28 /* stupid codes to be monitored */
30 {
31  int tmp = 0;
32  int i=0;
33 
34  for( i = 0; i < THRESHOLD; i++ )
35  {
36  tmp = tmp + i;
37  }
38 
39 }
40 
41 
42 int main()
43 {
44  /*Declaring and initializing the event set with the presets*/
45  int Events[2] = {PAPI_TOT_INS, PAPI_TOT_CYC};
46  /*The length of the events array should be no longer than the
47  value returned by PAPI_num_counters.*/
48 
49  /*declaring place holder for no of hardware counters */
50  int num_hwcntrs = 0;
51  int retval;
52  char errstring[PAPI_MAX_STR_LEN];
53  /*This is going to store our list of results*/
54  long long values[NUM_EVENTS];
55 
56 
57  /***************************************************************************
58  * This part initializes the library and compares the version number of the*
59  * header file, to the version of the library, if these don't match then it *
60  * is likely that PAPI won't work correctly.If there is an error, retval *
61  * keeps track of the version number. *
62  ***************************************************************************/
63 
65  {
66  fprintf(stderr, "Error: %d %s\n",retval, errstring);
67  exit(1);
68  }
69 
70 
71  /**************************************************************************
72  * PAPI_num_counters returns the number of hardware counters the platform *
73  * has or a negative number if there is an error *
74  **************************************************************************/
75  if ((num_hwcntrs = PAPI_num_counters()) < PAPI_OK)
76  {
77  printf("There are no counters available. \n");
78  exit(1);
79  }
80 
81  printf("There are %d counters in this system\n",num_hwcntrs);
82 
83  /**************************************************************************
84  * PAPI_start_counters initializes the PAPI library (if necessary) and *
85  * starts counting the events named in the events array. This function *
86  * implicitly stops and initializes any counters running as a result of *
87  * a previous call to PAPI_start_counters. *
88  **************************************************************************/
89 
90  if ( (retval = PAPI_start_counters(Events, NUM_EVENTS)) != PAPI_OK)
91  ERROR_RETURN(retval);
92 
93  printf("\nCounter Started: \n");
94 
95  /* Your code goes here*/
97 
98 
99 
100 
101  /**********************************************************************
102  * PAPI_read_counters reads the counter values into values array *
103  **********************************************************************/
104 
105  if ( (retval=PAPI_read_counters(values, NUM_EVENTS)) != PAPI_OK)
106  ERROR_RETURN(retval);
107 
108  printf("Read successfully\n");
109 
110 
111 
112  printf("The total instructions executed for addition are %lld \n",values[0]);
113  printf("The total cycles used are %lld \n", values[1] );
114 
115  printf("\nNow we try to use PAPI_accum to accumulate values\n");
116 
117  /* Do some computation here */
118  computation_add();
119 
120 
121  /************************************************************************
122  * What PAPI_accum_counters does is it adds the running counter values *
123  * to what is in the values array. The hardware counters are reset and *
124  * left running after the call. *
125  ************************************************************************/
126 
127  if ( (retval=PAPI_accum_counters(values, NUM_EVENTS)) != PAPI_OK)
128  ERROR_RETURN(retval);
129 
130  printf("We did an additional %d times addition!\n", THRESHOLD);
131  printf("The total instructions executed for addition are %lld \n",
132  values[0] );
133  printf("The total cycles used are %lld \n", values[1] );
134 
135  /***********************************************************************
136  * Stop counting events(this reads the counters as well as stops them *
137  ***********************************************************************/
138 
139  printf("\nNow we try to do some multiplications\n");
141 
142  /******************* PAPI_stop_counters **********************************/
143  if ((retval=PAPI_stop_counters(values, NUM_EVENTS)) != PAPI_OK)
144  ERROR_RETURN(retval);
145 
146  printf("The total instruction executed for multiplication are %lld \n",
147  values[0] );
148  printf("The total cycles used are %lld \n", values[1] );
149  exit(0);
150 }
static int Events[NUM_EVENTS]
Definition: init_fini.c:8
#define PAPI_MAX_STR_LEN
Definition: fpapi.h:43
#define PAPI_TOT_INS
Definition: fpapi.h:185
#define NUM_EVENTS
Definition: high_level.c:13
int PAPI_accum_counters(long long *values, int array_len)
Definition: papi_hl.c:756
return PAPI_OK
Definition: linux-nvml.c:458
#define printf
Definition: papi_test.h:125
Return codes and api definitions.
int PAPI_library_init(int version)
Definition: papi.c:495
int i
Definition: fileop.c:140
#define PAPI_TOT_CYC
Definition: fpapi.h:194
int PAPI_stop_counters(long long *values, int array_len)
Definition: papi_hl.c:803
void computation_mult()
Definition: high_level.c:18
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
#define THRESHOLD
Definition: high_level.c:14
int PAPI_read_counters(long long *values, int array_len)
Definition: papi_hl.c:706
int PAPI_num_counters(void)
Definition: papi_hl.c:537
void computation_add()
Definition: high_level.c:29
int PAPI_start_counters(int *events, int array_len)
Definition: papi_hl.c:593
#define ERROR_RETURN(retval)
Definition: high_level.c:15
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
ssize_t retval
Definition: libasync.c:338
long long tmp
Definition: iozone.c:12031
int main(int argc, char **argv)
List all appio events codes and names.
void exit()