PAPI 7.1.0.0
Loading...
Searching...
No Matches
libmsr_write_test.c File Reference
Include dependency graph for libmsr_write_test.c:

Go to the source code of this file.

Macros

#define MAX_EVENTS   128
 

Functions

int ompcpuloadprimes (int limit)
 
int main (int argc, char **argv)
 

Variables

char events [MAX_EVENTS][BUFSIZ]
 
char filenames [MAX_EVENTS][BUFSIZ]
 

Macro Definition Documentation

◆ MAX_EVENTS

#define MAX_EVENTS   128
Author
Asim YarKhan (updated)
Vince Weaver (original version)

Definition at line 15 of file libmsr_write_test.c.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 38 of file libmsr_write_test.c.

39{
40 int retval,cid,rapl_cid=-1,numcmp;
41 int EventSet = PAPI_NULL;
42 long long values[MAX_EVENTS];
43 int i,code,enum_retval;
44 const PAPI_component_info_t *cmpinfo = NULL;
45 long long start_time,write_start_time,write_end_time,read_start_time,read_end_time;
46 char event_name[BUFSIZ];
47 union { long long ll; double dbl; } event_value_union;
48 static int num_events=0;
49 FILE *fileout;
50
51 /* PAPI Initialization */
53 if ( retval != PAPI_VER_CURRENT ) {
54 fprintf(stderr,"PAPI_library_init failed\n");
55 exit(1);
56 }
57
58 /* Find the libmsr component */
59 numcmp = PAPI_num_components();
60 for(cid=0; cid<numcmp; cid++) {
61 if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
62 fprintf(stderr,"PAPI_get_component_info failed\n");
63 exit(1);
64 }
65 if (strstr(cmpinfo->name,"libmsr")) {
66 rapl_cid=cid;
67 printf("Found libmsr component at cid %d\n", rapl_cid);
68 if (cmpinfo->disabled) {
69 fprintf(stderr,"No libmsr events found: %s\n", cmpinfo->disabled_reason);
70 exit(1);
71 }
72 break;
73 }
74 }
75
76 /* Component not found */
77 if (cid==numcmp) {
78 fprintf(stderr,"No libmsr component found\n");
79 exit(1);
80 }
81
82 /* Find events in the component */
83 code = PAPI_NATIVE_MASK;
84 enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, cid );
85 while ( enum_retval == PAPI_OK ) {
87 if ( retval != PAPI_OK ) {
88 printf("Error translating %#x\n",code);
89 exit(1);
90 }
91 printf("Found: %s\n",event_name);
92 strncpy(events[num_events],event_name,BUFSIZ);
93 sprintf(filenames[num_events],"results.%s",event_name);
94 num_events++;
96 printf("Too many events! %d\n",num_events);
97 exit(1);
98 }
99 enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, cid );
100 }
101 if (num_events==0) {
102 printf("Error! No libmsr events found!\n");
103 exit(1);
104 }
105
106 /* Open output file */
107 char fileoutname[]="libmsr_write_test_output.txt";
108 fileout=fopen( fileoutname ,"w" );
109 if ( fileout==NULL) { fprintf( stderr,"Could not open %s\n",fileoutname ); exit(1); }
110
111 /* Create EventSet */
113 if (retval != PAPI_OK) {
114 fprintf(stderr,"Error creating eventset!\n");
115 }
116
117 for(i=0;i<num_events;i++) {
119 if (retval != PAPI_OK) fprintf(stderr,"Error adding event %s\n",events[i]);
120 }
121
122 start_time=PAPI_get_real_nsec();
123
124 /* Grab the initial values for the events */
126 if (retval != PAPI_OK) { fprintf(stderr,"PAPI_start() failed\n"); exit(1); }
127 /* Initial checking read */
129 if (retval != PAPI_OK) { fprintf(stderr,"PAPI_read() failed\n"); exit(1); }
130
131 /* Write a header line */
132 fprintf( fileout, "ACTION TIME-STAMP TIME-FOR-UNIT-WORK TIME-OVERHEAD-RW\t" );
133 for(i=0; i<num_events; i++)
134 fprintf( fileout, "%s ", events[i]+9 );
135 fprintf( fileout, "\n" );
136
137 /* Read the initial values */
139 if (retval != PAPI_OK) { fprintf(stderr,"PAPI_read() failed\n"); exit(1); }
140 fprintf( fileout, "INIT %8.3f %8.3f ", ((double)(PAPI_get_real_nsec()-start_time))/1.0e9, 0.0 );
141 fprintf( fileout, "%8.3e ", 0.0);
142 for(i=0; i<num_events; i++) {
143 event_value_union.ll = values[i];
144 fprintf( fileout, "%8.3f ", event_value_union.dbl );
145 }
146 fprintf( fileout, "\n" );
147
148 int rpt=0;
149 int limit1base=10;
150 int limit2base=10;
151 while(rpt++<200) {
152 //printf("rpt %d\n", rpt);
153
154 if ( rpt % 10 == 0 ) {
155 for (i=0; i<num_events; i++) {
156 event_value_union.ll = values[i];
157 if ( !strcmp( events[i], "libmsr:::PKG_POWER_LIMIT_1:PACKAGE0" )) event_value_union.dbl=limit1base+(rpt/2);
158 else if ( !strcmp( events[i], "libmsr:::PKG_TIME_WINDOW_POWER_LIMIT_1:PACKAGE0" )) event_value_union.dbl=1.0;
159 else if ( !strcmp( events[i], "libmsr:::PKG_POWER_LIMIT_2:PACKAGE0" )) event_value_union.dbl=limit2base+(rpt/2);
160 else if ( !strcmp( events[i], "libmsr:::PKG_TIME_WINDOW_POWER_LIMIT_2:PACKAGE0" )) event_value_union.dbl=1.0;
161 else if ( !strcmp( events[i], "libmsr:::PKG_POWER_LIMIT_1:PACKAGE1" )) event_value_union.dbl=limit1base+(rpt/2);
162 else if ( !strcmp( events[i], "libmsr:::PKG_TIME_WINDOW_POWER_LIMIT_1:PACKAGE1" )) event_value_union.dbl=1.0;
163 else if ( !strcmp( events[i], "libmsr:::PKG_POWER_LIMIT_2:PACKAGE1" )) event_value_union.dbl=limit2base+(rpt/2);
164 else if ( !strcmp( events[i], "libmsr:::PKG_TIME_WINDOW_POWER_LIMIT_2:PACKAGE1" )) event_value_union.dbl=1.0;
165 else event_value_union.dbl=PAPI_NULL;
166 values[i]=event_value_union.ll;
167 }
168
169 write_start_time=PAPI_get_real_nsec();
171 write_end_time=PAPI_get_real_nsec();
172 if (retval != PAPI_OK) { fprintf(stderr,"PAPI_write() failed\n"); exit(1); }
173
174 fprintf( fileout, "SET %8.3f %8.3f ", ((double)(PAPI_get_real_nsec()-start_time))/1.0e9, 0.0 );
175 fprintf( fileout, "%8.3e ", ((double)(write_end_time-write_start_time))/1.0e9 );
176 for(i=0; i<num_events; i++) {
177 event_value_union.ll = values[i];
178 fprintf( fileout, "%8.3f ", event_value_union.dbl );
179 }
180 fprintf( fileout, "\n" );
181 }
182
183 /* DO SOME WORK TO USE ENERGY */
184 //usleep(100000);
185 double work_start_time=PAPI_get_real_nsec();
186 ompcpuloadprimes( 100000 );
187 double work_time=PAPI_get_real_nsec()-work_start_time;
188 //printf("primescount %d\n", primescount);
189
190 /* Read and output the values */
191 read_start_time=PAPI_get_real_nsec();
193 read_end_time=PAPI_get_real_nsec();
194 if (retval != PAPI_OK) { fprintf(stderr,"PAPI_read() failed\n"); exit(1); }
195 fprintf( fileout, "READ %8.3f %8.3f ", ((double)(PAPI_get_real_nsec()-start_time))/1.0e9, work_time/1.0e9 );
196 fprintf( fileout, "%8.3e ", ((double)(read_end_time-read_start_time))/1.0e9 );
197 for(i=0; i<num_events; i++) {
198 event_value_union.ll = values[i];
199 fprintf( fileout, "%8.3f ", event_value_union.dbl );
200 }
201 fprintf( fileout, "\n" );
202 }
203
205 return 0;
206}
int i
add PAPI preset or native hardware event by name to an EventSet
Create a new empty PAPI 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 real time counter value in nanoseconds.
initialize the PAPI library.
Get the number of components available on the system.
Read hardware counters from an event set.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
Write counter values into counters.
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:29
#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_NULL
Definition: f90papi.h:78
static int EventSet
Definition: init_fini.c:8
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
char events[MAX_EVENTS][BUFSIZ]
char filenames[MAX_EVENTS][BUFSIZ]
int ompcpuloadprimes(int limit)
#define MAX_EVENTS
static int num_events
#define PAPI_NATIVE_MASK
FILE * stderr
if(file==NULL) goto out
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:627
char disabled_reason[PAPI_HUGE_STR_LEN]
Definition: papi.h:634
int retval
Definition: zero_fork.c:53
Here is the call graph for this function:

◆ ompcpuloadprimes()

int ompcpuloadprimes ( int  limit)

Definition at line 20 of file libmsr_write_test.c.

21{
22 int num, primes=0;
23#pragma omp parallel for schedule(dynamic) reduction(+ : primes)
24 for (num = 1; num <= limit; num++) {
25 int i = 2;
26 while(i <= num) {
27 if(num % i == 0)
28 break;
29 i++;
30 }
31 if(i == num)
32 primes++;
33 }
34 return primes;
35}
Here is the caller graph for this function:

Variable Documentation

◆ events

char events[MAX_EVENTS][BUFSIZ]

Definition at line 17 of file libmsr_write_test.c.

◆ filenames

char filenames[MAX_EVENTS][BUFSIZ]

Definition at line 18 of file libmsr_write_test.c.