-
Notifications
You must be signed in to change notification settings - Fork 0
/
papi_helper.c
executable file
·166 lines (142 loc) · 4.77 KB
/
papi_helper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//===- papi_helper.cpp - Contains helper function to call PAPI lib" --------===//
//
// This file implements function in papi.h. These are helper functions to
// to start and stop counter using PAPI library.
// Events that are measured are defined in events.h file.
//
// Note:
// Global variables: _G_EVENTS, _G_EVENT_COUNT, _G_COUNTERS defined in events.h
//
//===-----------------------------------------------------------------------===//
#include <papi.h>
#include "events.h"
#include "papi_helper.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define _CALL_PAPI( CALL ) ({ \
int err = (CALL); \
if ( err < PAPI_OK ) { \
fprintf( stderr, #CALL " failed: %s\n", PAPI_strerror( err ) ); \
abort(); \
} \
err; \
})
static int NUM_EVENTS;
static int numIteration;
static int * _G_EVENTS;
void initializeCounters(int num){
char *a = getenv ("NUM_EVENTS");
char *b = getenv ("ITER_COUNT");
NUM_EVENTS = atoi(a);
numIteration = atoi(b);
#ifdef DBG
printf("******* %d ********", atoi(a));
printf("Number of events: %d \n Iteration number: %d\n", NUM_EVENTS, numIteration);
#endif
int i=0, j=0, delta=0;
switch(numIteration){
default:
delta = 0;
break;
case 1:
delta = 0;
break;
case 2:
delta = 0 + IT1;
break;
case 3:
delta = 0 + IT1 + IT2;
break;
}
i=delta;
#ifdef DBG
printf("Running i from: %d\n", i);
#endif
_G_EVENTS = malloc(NUM_EVENTS * sizeof(int));
for(j=0; i < NUM_EVENTS+delta ; i++){
#ifdef DBG
printf("i=%d, j=%d", i, j);
#endif
_G_EVENTS[j++] = _G_EVENTS_AVAIL[i];
}
}
void printGEvents(){
int i;
for(i=0; i<NUM_EVENTS; i++){
char name[PAPI_MAX_STR_LEN];
(void) PAPI_event_code_to_name( _G_EVENTS[i], name );
printf("Event %d: %s\n",i ,name);
}
}
//
// This method should be placed at the start of instrumented code
//
void startPapiCounters(){
initializeCounters(0);
#ifdef DBG
printGEvents();
printf("********* STARTING COUNTERS *************\n");
//assert(NUM_EVENTS == _G_EVENT_COUNT);
#endif
// initialize papi library and assert that it's successful
_CALL_PAPI(PAPI_library_init( PAPI_VER_CURRENT ));
// check that all the events can be counted at once.
int numCounters = PAPI_num_counters() ;
assert( NUM_EVENTS <= numCounters );
#ifdef DBG
printf("Number of hardware counters available on this machine: %d", numCounters);
#endif
for ( int i = 0; i < NUM_EVENTS; i++ ) {
char name[PAPI_MAX_STR_LEN];
(void) _CALL_PAPI(PAPI_event_code_to_name( _G_EVENTS[i], name ));
if(PAPI_query_event( _G_EVENTS[i] ) < PAPI_OK) {
fprintf(stderr, "Event %s could not be counted on this machine.\n", name);
abort();
}
}
//******* Start Counters ******
(void) _CALL_PAPI(PAPI_start_counters(_G_EVENTS, NUM_EVENTS));
}
//
// This method should be placed at the end of instrumented code
//
void stopPapiCounters(){
#ifdef DBG
printf("********* STOPING COUNTERS *************\n");
#endif
long long _G_COUNTERS[NUM_EVENTS];
int i;
//******* Stop Counters ******
assert(PAPI_stop_counters(_G_COUNTERS, NUM_EVENTS) >= PAPI_OK);
// get the counter information for each event.
// currently printing on stdout.
for( i = 0; i < NUM_EVENTS; ++i ) {
PAPI_event_info_t info;
PAPI_get_event_info(_G_EVENTS[i], &info);
#ifdef DBG
printf("%20lld %-15s %s\n", _G_COUNTERS[i], info.symbol, info.long_descr);
#else
fprintf(stderr, "%lld ", _G_COUNTERS[i]);
#endif
}
}
void printAllAvailableCounters(){
// initialize papi library and assert that it's successful
assert( PAPI_library_init( PAPI_VER_CURRENT ) == PAPI_VER_CURRENT );
// print header
printf( "--- Available events ---\n" );
printf( "%-15s %s\n", "Name", "Description" );
// loop through all PAPI events supported on this plattform
int event = 0 | PAPI_PRESET_MASK; // the first event
do {
// ignore unsupported events
if ( PAPI_query_event( event ) != PAPI_OK ) continue;
// get event info
PAPI_event_info_t info;
(void) PAPI_get_event_info( event, &info);
// print
printf( "%-15s %s\n", info.symbol, info.long_descr );
} while ( PAPI_enum_event( &event, PAPI_ENUM_ALL ) == PAPI_OK );
}