PAPI 7.1.0.0
Loading...
Searching...
No Matches
rapl_basic.c
Go to the documentation of this file.
1/****************************/
2/* THIS IS OPEN SOURCE CODE */
3/****************************/
4
14#include <stdio.h>
15#include <stdlib.h>
16#include <unistd.h>
17#include <string.h>
18
19#include "papi.h"
20#include "papi_test.h"
21
22#define MAX_RAPL_EVENTS 64
23
24
25#ifdef BASIC_TEST
26
27void run_test(int quiet) {
28
29 if (!quiet) {
30 printf("Sleeping 1 second...\n");
31 }
32
33 /* Sleep */
34 sleep(1);
35}
36
37#else
38
39#define MATRIX_SIZE 1024
40
41 static double a[MATRIX_SIZE][MATRIX_SIZE];
42 static double b[MATRIX_SIZE][MATRIX_SIZE];
43 static double c[MATRIX_SIZE][MATRIX_SIZE];
44
45/* Naive matrix multiply */
46void run_test(int quiet) {
47
48 double s;
49 int i,j,k;
50
51 if (!quiet) {
52 printf("Doing a naive %dx%d MMM...\n",MATRIX_SIZE,MATRIX_SIZE);
53 }
54
55 for(i=0;i<MATRIX_SIZE;i++) {
56 for(j=0;j<MATRIX_SIZE;j++) {
57 a[i][j]=(double)i*(double)j;
58 b[i][j]=(double)i/(double)(j+5);
59 }
60 }
61
62 for(j=0;j<MATRIX_SIZE;j++) {
63 for(i=0;i<MATRIX_SIZE;i++) {
64 s=0;
65 for(k=0;k<MATRIX_SIZE;k++) {
66 s+=a[i][k]*b[k][j];
67 }
68 c[i][j] = s;
69 }
70 }
71
72 s=0.0;
73 for(i=0;i<MATRIX_SIZE;i++) {
74 for(j=0;j<MATRIX_SIZE;j++) {
75 s+=c[i][j];
76 }
77 }
78
79 if (!quiet) printf("Matrix multiply sum: s=%lf\n",s);
80}
81
82#endif
83
84int main (int argc, char **argv)
85{
86
87 int retval,cid,rapl_cid=-1,numcmp;
88 int EventSet = PAPI_NULL;
89 long long *values;
90 int num_events=0;
91 int code;
95 int r,i;
96 const PAPI_component_info_t *cmpinfo = NULL;
97 PAPI_event_info_t evinfo;
98 long long before_time,after_time;
99 double elapsed_time;
100
101#ifdef WRAP_TEST
102
103 int do_wrap=0;
104
105 if ( argc > 1 ) {
106 if ( strstr( argv[1], "-w" ) ) {
107 do_wrap = 1;
108 }
109 }
110
111#endif
112
113 /* Set TESTS_QUIET variable */
114 tests_quiet( argc, argv );
115
116 /* PAPI Initialization */
118 if ( retval != PAPI_VER_CURRENT ) {
119 test_fail(__FILE__, __LINE__,"PAPI_library_init failed\n",retval);
120 }
121
122 if (!TESTS_QUIET) {
123 printf("Trying all RAPL events\n");
124 }
125
126 numcmp = PAPI_num_components();
127
128 for(cid=0; cid<numcmp; cid++) {
129
130 if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
131 test_fail(__FILE__, __LINE__,"PAPI_get_component_info failed\n", 0);
132 }
133
134 if (strstr(cmpinfo->name,"rapl")) {
135
136 rapl_cid=cid;
137
138 if (!TESTS_QUIET) {
139 printf("Found rapl component at cid %d\n",rapl_cid);
140 }
141
142 if (cmpinfo->disabled) {
143 if (!TESTS_QUIET) {
144 printf("RAPL component disabled: %s\n",
145 cmpinfo->disabled_reason);
146 }
147 test_skip(__FILE__,__LINE__,"RAPL component disabled",0);
148 }
149 break;
150 }
151 }
152
153 /* Component not found */
154 if (cid==numcmp) {
155 test_skip(__FILE__,__LINE__,"No rapl component found\n",0);
156 }
157
158 /* Create EventSet */
160 if (retval != PAPI_OK) {
161 test_fail(__FILE__, __LINE__,
162 "PAPI_create_eventset()",retval);
163 }
164
165 /* Add all events */
166
167 code = PAPI_NATIVE_MASK;
168
169 r = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, rapl_cid );
170
171 while ( r == PAPI_OK ) {
172
174 if ( retval != PAPI_OK ) {
175 printf("Error translating %#x\n",code);
176 test_fail( __FILE__, __LINE__,
177 "PAPI_event_code_to_name", retval );
178 }
179
180 retval = PAPI_get_event_info(code,&evinfo);
181 if (retval != PAPI_OK) {
182 test_fail( __FILE__, __LINE__,
183 "Error getting event info\n",retval);
184 }
185
186 strncpy(units[num_events],evinfo.units,sizeof(units[0]));
187 // buffer must be null terminated to safely use strstr operation on it below
188 units[num_events][sizeof(units[0])-1] = '\0';
189
191
192 retval = PAPI_add_event( EventSet, code );
193 if (retval != PAPI_OK) {
194 break; /* We've hit an event limit */
195 }
196 num_events++;
197
198 r = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, rapl_cid );
199 }
200
201 values=calloc(num_events,sizeof(long long));
202 if (values==NULL) {
203 test_fail(__FILE__, __LINE__,
204 "No memory",retval);
205 }
206
207 if (!TESTS_QUIET) {
208 printf("\nStarting measurements...\n\n");
209 }
210
211 /* Start Counting */
214 if (retval != PAPI_OK) {
215 test_fail(__FILE__, __LINE__, "PAPI_start()",retval);
216 }
217
218 /* Run test */
220
221 /* Stop Counting */
224 if (retval != PAPI_OK) {
225 test_fail(__FILE__, __LINE__, "PAPI_stop()",retval);
226 }
227
228 elapsed_time=((double)(after_time-before_time))/1.0e9;
229
230 if (!TESTS_QUIET) {
231 printf("\nStopping measurements, took %.3fs, gathering results...\n\n",
232 elapsed_time);
233
234 printf("Scaled energy measurements:\n");
235
236 for(i=0;i<num_events;i++) {
237 if (strstr(units[i],"nJ")) {
238
239 printf("%-40s%12.6f J\t(Average Power %.1fW)\n",
240 event_names[i],
241 (double)values[i]/1.0e9,
242 ((double)values[i]/1.0e9)/elapsed_time);
243 }
244 }
245
246 printf("\n");
247 printf("Energy measurement counts:\n");
248
249 for(i=0;i<num_events;i++) {
250 if (strstr(event_names[i],"ENERGY_CNT")) {
251 printf("%-40s%12lld\t%#08llx\n", event_names[i], values[i], values[i]);
252 }
253 }
254
255 printf("\n");
256 printf("Scaled Fixed values:\n");
257
258 for(i=0;i<num_events;i++) {
259 if (!strstr(event_names[i],"ENERGY")) {
261
262 union {
263 long long ll;
264 double fp;
265 } result;
266
267 result.ll=values[i];
268 printf("%-40s%12.3f %s\n", event_names[i], result.fp, units[i]);
269 }
270 }
271 }
272
273 printf("\n");
274 printf("Fixed value counts:\n");
275
276 for(i=0;i<num_events;i++) {
277 if (!strstr(event_names[i],"ENERGY")) {
279 printf("%-40s%12lld\t%#08llx\n", event_names[i], values[i], values[i]);
280 }
281 }
282 }
283
284 }
285
286#ifdef WRAP_TEST
287 double max_time;
288 unsigned long long max_value = 0;
289 int repeat;
290
291 for(i=0;i<num_events;i++) {
292 if (strstr(event_names[i],"ENERGY_CNT")) {
293 if (max_value < (unsigned) values[i]) {
294 max_value = values[i];
295 }
296 }
297 }
298 max_time = elapsed_time * ( (double)0xffffffff / (double)max_value );
299 printf("\n");
300 printf ("Approximate time to energy measurement wraparound: %.3f sec or %.3f min.\n",
301 max_time, max_time/60);
302
303 if (do_wrap) {
304 printf ("Beginning wraparound execution.");
305 /* Start Counting */
308 if (retval != PAPI_OK) {
309 test_fail(__FILE__, __LINE__, "PAPI_start()",retval);
310 }
311
312 /* Run test */
313 repeat = (int)(max_time/elapsed_time);
314 for (i=0;i< repeat;i++) {
315 run_test(1);
316 printf("."); fflush(stdout);
317 }
318 printf("\n");
319
320 /* Stop Counting */
323 if (retval != PAPI_OK) {
324 test_fail(__FILE__, __LINE__, "PAPI_stop()",retval);
325 }
326
327 elapsed_time=((double)(after_time-before_time))/1.0e9;
328 printf("\nStopping measurements, took %.3fs\n\n", elapsed_time);
329
330 printf("Scaled energy measurements:\n");
331
332 for(i=0;i<num_events;i++) {
333 if (strstr(units[i],"nJ")) {
334
335 printf("%-40s%12.6f J\t(Average Power %.1fW)\n",
336 event_names[i],
337 (double)values[i]/1.0e9,
338 ((double)values[i]/1.0e9)/elapsed_time);
339 }
340 }
341 printf("\n");
342 printf("Energy measurement counts:\n");
343
344 for(i=0;i<num_events;i++) {
345 if (strstr(event_names[i],"ENERGY_CNT")) {
346 printf("%-40s%12lld\t%#08llx\n", event_names[i], values[i], values[i]);
347 }
348 }
349 }
350
351#endif
352
353 /* Done, clean up */
355 if (retval != PAPI_OK) {
356 test_fail(__FILE__, __LINE__,
357 "PAPI_cleanup_eventset()",retval);
358 }
359
361 if (retval != PAPI_OK) {
362 test_fail(__FILE__, __LINE__,
363 "PAPI_destroy_eventset()",retval);
364 }
365
366 test_pass( __FILE__ );
367
368 return 0;
369}
370
static const char * event_names[2]
Definition: Gamum.c:27
volatile int result
int i
double s
Definition: byte_profile.c:36
add PAPI preset or native hardware event to an event set
Empty and destroy an EventSet.
Create a new empty PAPI EventSet.
Empty and destroy an EventSet.
Enumerate PAPI preset or native events for a given component.
Convert a numeric hardware event code to a name.
get information about a specific software component
Get the event's name and description info.
Get real time counter value in nanoseconds.
initialize the PAPI library.
Get the number of components available on the system.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_ENUM_EVENTS
Definition: f90papi.h:224
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_ENUM_FIRST
Definition: f90papi.h:85
#define PAPI_MIN_STR_LEN
Definition: f90papi.h:208
#define PAPI_NULL
Definition: f90papi.h:78
#define PAPI_DATATYPE_FP64
Definition: f90papi.h:171
#define PAPI_MAX_STR_LEN
Definition: f90papi.h:77
#define PAPI_DATATYPE_UINT64
Definition: f90papi.h:278
static int EventSet
Definition: init_fini.c:8
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
static int num_events
int TESTS_QUIET
Definition: test_utils.c:18
#define PAPI_NATIVE_MASK
Return codes and api definitions.
FILE * stdout
static FILE * fp
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
void PAPI_NORETURN test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:491
void PAPI_NORETURN test_pass(const char *filename)
Definition: test_utils.c:432
void PAPI_NORETURN test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:584
int main()
Definition: pernode.c:20
int data_type[MAX_EVENTS]
Definition: powercap_plot.c:16
char units[MAX_EVENTS][BUFSIZ]
Definition: powercap_plot.c:15
static double a[MATRIX_SIZE][MATRIX_SIZE]
Definition: rapl_basic.c:41
static double b[MATRIX_SIZE][MATRIX_SIZE]
Definition: rapl_basic.c:42
#define MATRIX_SIZE
Definition: rapl_basic.c:39
#define MAX_RAPL_EVENTS
Tests basic functionality of RAPL component.
Definition: rapl_basic.c:22
static double c[MATRIX_SIZE][MATRIX_SIZE]
Definition: rapl_basic.c:43
void run_test(int quiet)
Definition: rapl_basic.c:46
int quiet
Definition: rapl_overflow.c:19
static long long after_time
Definition: rapl_overflow.c:15
static long long before_time
Definition: rapl_overflow.c:15
int
Definition: sde_internal.h:89
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:627
char disabled_reason[PAPI_HUGE_STR_LEN]
Definition: papi.h:634
char units[PAPI_MIN_STR_LEN]
Definition: papi.h:969
int retval
Definition: zero_fork.c:53