PAPI  5.4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
multiplex.c
Go to the documentation of this file.
1 /****************************************************************************
2  * Multiplexing allows more counters to be used than what is supported by *
3  * the platform, thus allowing a larger number of events to be counted *
4  * simultaneously. When a microprocessor has a very limited number of *
5  * counters that can be counted simultaneously, a large application with *
6  * many hours of run time may require days of profiling in order to gather *
7  * enough information to base a performance analysis. Multiplexing overcomes*
8  * this limitation by the usage of the counters over timesharing. *
9  * This is an example demonstrating how to use PAPI_set_multiplex to *
10  * convert a standard event set to a multiplexed event set. *
11  ****************************************************************************/
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <unistd.h>
15 #include "papi.h"
16 
17 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }
18 
19 #define NUM_ITERS 10000000
20 #define MAX_TO_ADD 6
21 
22 double c = 0.11;
23 void do_flops(int n)
24 {
25  int i;
26  double a = 0.5;
27  double b = 6.2;
28 
29  for (i=0; i < n; i++)
30  c += a * b;
31  return;
32 }
33 
34 /* Tests that we can really multiplex a lot. */
35 int multiplex(void)
36 {
37  int retval, i, EventSet = PAPI_NULL, j = 0;
38  long long *values;
39  PAPI_event_info_t pset;
40  int events[MAX_TO_ADD], number;
41 
42  /* Initialize the library */
44  if (retval != PAPI_VER_CURRENT)
45  {
46  printf("Library initialization error! \n");
47  exit(1);
48  }
49 
50  /* initialize multiplex support */
51  retval = PAPI_multiplex_init();
52  if (retval != PAPI_OK)
53  ERROR_RETURN(retval);
54 
55  retval = PAPI_create_eventset(&EventSet);
56  if (retval != PAPI_OK)
57  ERROR_RETURN(retval);
58 
59  /* convert the event set to a multiplex event set */
60  retval = PAPI_set_multiplex(EventSet);
61  if (retval != PAPI_OK)
62  ERROR_RETURN(retval);
63 /*
64  retval = PAPI_add_event(EventSet, PAPI_TOT_INS);
65  if ((retval != PAPI_OK) && (retval != PAPI_ECNFLCT))
66  ERROR_RETURN(retval);
67  printf("Adding %s\n", "PAPI_TOT_INS");
68 */
69 
70  for (i = 0; i < PAPI_MAX_PRESET_EVENTS; i++)
71  {
72  retval = PAPI_get_event_info(i | PAPI_PRESET_MASK, &pset);
73  if (retval != PAPI_OK)
74  ERROR_RETURN(retval);
75 
76  if ((pset.count) && (pset.event_code != PAPI_TOT_CYC))
77  {
78  printf("Adding %s\n", pset.symbol);
79 
80  retval = PAPI_add_event(EventSet, pset.event_code);
81  if ((retval != PAPI_OK) && (retval != PAPI_ECNFLCT))
82  ERROR_RETURN(retval);
83 
84  if (retval == PAPI_OK)
85  printf("Added %s\n", pset.symbol);
86  else
87  printf("Could not add %s due to resource limitation.\n",
88  pset.symbol);
89 
90  if (retval == PAPI_OK)
91  {
92  if (++j >= MAX_TO_ADD)
93  break;
94  }
95  }
96  }
97 
98  values = (long long *) malloc(MAX_TO_ADD * sizeof(long long));
99  if (values == NULL)
100  {
101  printf("Not enough memory available. \n");
102  exit(1);
103  }
104 
105  if ((retval=PAPI_start(EventSet)) != PAPI_OK)
106  ERROR_RETURN(retval);
107 
109 
110  retval = PAPI_stop(EventSet, values);
111  if (retval != PAPI_OK)
112  ERROR_RETURN(retval);
113 
114  /* get the number of events in the event set */
115  number=MAX_TO_ADD;
116  if ( (retval = PAPI_list_events(EventSet, events, &number)) != PAPI_OK)
117  ERROR_RETURN(retval);
118 
119  /* print the read result */
120  for (i = 0; i < MAX_TO_ADD; i++)
121  {
122  retval = PAPI_get_event_info(events[i], &pset);
123  if (retval != PAPI_OK)
124  ERROR_RETURN(retval);
125  printf("Event name: %s value: %lld \n", pset.symbol, values[i]);
126  }
127 
128  retval = PAPI_cleanup_eventset(EventSet);
129  if (retval != PAPI_OK)
130  ERROR_RETURN(retval);
131 
132  retval = PAPI_destroy_eventset(&EventSet);
133  if (retval != PAPI_OK)
134  ERROR_RETURN(retval);
135 
136  /* free the resources used by PAPI */
137  PAPI_shutdown();
138 
139  return (0);
140 }
141 
142 int main(int argc, char **argv)
143 {
144 
145  printf("Using %d iterations\n\n", NUM_ITERS);
146  printf("Does PAPI_multiplex_init() handle lots of events?\n");
147  multiplex();
148  exit(0);
149 }
unsigned int count
Definition: papi.h:984
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2258
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1622
#define NUM_ITERS
Definition: multiplex.c:19
#define PAPI_NULL
Definition: papi.h:290
int EventSet
Definition: data_range.c:25
#define PAPI_MAX_PRESET_EVENTS
char symbol[PAPI_HUGE_STR_LEN]
Definition: papi.h:963
return PAPI_OK
Definition: linux-nvml.c:458
#define PAPI_PRESET_MASK
#define printf
Definition: papi_test.h:125
double c
Definition: multiplex.c:22
int int argc
Definition: iozone.c:1609
static double a[MATRIX_SIZE][MATRIX_SIZE]
Definition: rapl_basic.c:37
Return codes and api definitions.
int PAPI_get_event_info(int EventCode, PAPI_event_info_t *info)
Definition: papi.c:846
char events[MAX_EVENTS][BUFSIZ]
int multiplex(void)
Definition: multiplex.c:35
char ** argv
Definition: iozone.c:1610
#define MAX_TO_ADD
Definition: multiplex.c:20
int PAPI_library_init(int version)
Definition: papi.c:497
int i
Definition: fileop.c:140
void PAPI_shutdown(void)
Definition: papi.c:4400
#define PAPI_TOT_CYC
#define PAPI_ECNFLCT
Definition: papi.h:259
#define ERROR_RETURN(retval)
Definition: multiplex.c:17
int PAPI_cleanup_eventset(int EventSet)
Definition: papi.c:2834
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1424
void do_flops(int n)
Definition: multiplex.c:23
int PAPI_multiplex_init(void)
Definition: papi.c:2926
static double b[MATRIX_SIZE][MATRIX_SIZE]
Definition: rapl_basic.c:38
int PAPI_set_multiplex(int EventSet)
Definition: papi.c:3277
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:1971
int PAPI_start(int EventSet)
Definition: papi.c:2053
long j
Definition: iozone.c:19135
unsigned int event_code
Definition: papi.h:961
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:223
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
int PAPI_list_events(int EventSet, int *Events, int *number)
Definition: papi.c:5927
int n
Definition: mendes-alt.c:164