PAPI  5.3.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
PAPI_get_opt.c
Go to the documentation of this file.
1 /*****************************************************************************
2  * This is an example using the low level function PAPI_get_opt to query the *
3  * option settings of the PAPI library or a specific eventset created by the *
4  * PAPI_create_eventset function. PAPI_set_opt is used on the otherhand to *
5  * set PAPI library or event set options. *
6  *****************************************************************************/
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "papi.h" /* This needs to be included every time you use PAPI */
12 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }
13 
15 {
16  float tmp;
17  int i;
18 
19  for(i=1; i<2000; i++)
20  {
21  tmp=(tmp+100)/i;
22  }
23  return 0;
24 }
25 
26 int main()
27 {
28 
29  int num, retval, EventSet = PAPI_NULL;
31  long long values[2];
32 
33  /****************************************************************************
34  * This part initializes the library and compares the version number of the *
35  * header file, to the version of the library, if these don't match then it *
36  * is likely that PAPI won't work correctly.If there is an error, retval *
37  * keeps track of the version number. *
38  ****************************************************************************/
39 
41  {
42  printf("Library initialization error! \n");
43  exit(1);
44  }
45 
46  /*PAPI_get_opt returns a negative number if there is an error */
47 
48  /* This call returns the maximum available hardware counters */
49  if((num = PAPI_get_opt(PAPI_MAX_HWCTRS,NULL)) <= 0)
50  ERROR_RETURN(num);
51 
52 
53  printf("This machine has %d counters.\n",num);
54 
55  if ((retval=PAPI_create_eventset(&EventSet)) != PAPI_OK)
56  ERROR_RETURN(retval);
57 
58  /* Set the domain of this EventSet to counter user and
59  kernel modes for this process. */
60 
61  memset(&options,0x0,sizeof(options));
62 
63  options.domain.eventset = EventSet;
64  /* Default domain is PAPI_DOM_USER */
65  options.domain.domain = PAPI_DOM_ALL;
66  /* this sets the options for the domain */
67  if ((retval=PAPI_set_opt(PAPI_DOMAIN, &options)) != PAPI_OK)
68  ERROR_RETURN(retval);
69  /* Add Total Instructions Executed event to the EventSet */
70  if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_INS)) != PAPI_OK)
71  ERROR_RETURN(retval);
72 
73  /* Add Total Cycles Executed event to the EventSet */
74  if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_CYC)) != PAPI_OK)
75  ERROR_RETURN(retval);
76 
77  /* Start counting */
78  if((retval=PAPI_start(EventSet)) != PAPI_OK)
79  ERROR_RETURN(retval);
80 
82 
83  /* Stop counting */
84  if((retval=PAPI_stop(EventSet, values)) != PAPI_OK)
85  ERROR_RETURN(retval);
86 
87  printf(" Total instructions: %lld Total Cycles: %lld \n", values[0],
88  values[1]);
89 
90  /* clean up */
91  PAPI_shutdown();
92 
93  exit(0);
94 }
memset(eventId, 0, size)
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2225
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1597
#define PAPI_NULL
Definition: fpapi.h:13
#define PAPI_DOM_ALL
Definition: fpapi.h:25
#define PAPI_TOT_INS
Definition: fpapi.h:185
return PAPI_OK
Definition: linux-nvml.c:458
A pointer to the following is passed to PAPI_set/get_opt()
Definition: papi.h:843
#define printf
Definition: papi_test.h:125
int PAPI_set_opt(int option, PAPI_option_t *ptr)
Definition: papi.c:3360
Return codes and api definitions.
int poorly_tuned_function()
Definition: PAPI_get_opt.c:14
int PAPI_library_init(int version)
Definition: papi.c:495
int i
Definition: fileop.c:140
#define PAPI_MAX_HWCTRS
Definition: fpapi.h:58
#define PAPI_TOT_CYC
Definition: fpapi.h:194
void PAPI_shutdown(void)
Definition: papi.c:4344
int PAPI_get_opt(int option, PAPI_option_t *ptr)
Definition: papi.c:4030
void int num
Definition: iozone.c:22151
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
#define PAPI_DOMAIN
Definition: fpapi.h:50
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1399
int EventSet
PAPI_domain_option_t domain
Definition: papi.h:850
#define ERROR_RETURN(retval)
Definition: PAPI_get_opt.c:12
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
long long tmp
Definition: iozone.c:12031
static options_t options
int main(int argc, char **argv)
List all appio events codes and names.
void exit()