PAPI 7.1.0.0
Loading...
Searching...
No Matches
examples/sprofile.c
Go to the documentation of this file.
1/* This program shows how to use PAPI_sprofil */
2
3#include <stdlib.h>
4#include <stdio.h>
5#include <unistd.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <fcntl.h>
9#include <string.h>
10
11#include "papi.h" /* This needs to be included every time you use PAPI */
12
13#define NUM_FLOPS 20000000
14#define NUM_ITERS 100000
15#define THRESHOLD 100000
16#define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }
17
18#if (defined(linux) && defined(__ia64__)) || (defined(_AIX))
19#define DO_FLOPS1 (vptr_t)(*(void **)do_flops1)
20#define DO_FLOPS2 (vptr_t)(*(void **)do_flops2)
21#else
22#define DO_FLOPS1 (vptr_t)(do_flops1)
23#define DO_FLOPS2 (vptr_t)(do_flops2)
24#endif
25
26void do_flops2(int);
27volatile double t1 = 0.8, t2 = 0.9;
28void do_flops1(int n)
29{
30 int i;
31 double c = 22222.11;
32
33 for (i = 0; i < n; i++)
34 c -= t1 * t2;
35}
36
37void do_both(int n)
38{
39 int i;
40 const int flops2 = NUM_FLOPS / n;
41 const int flops1 = NUM_FLOPS / n;
42
43 for (i = 0; i < n; i++)
44 {
45 do_flops1(flops1);
46 do_flops2(flops2);
47 }
48}
49
50int main(int argc, char **argv)
51{
52 int i , PAPI_event;
53 int EventSet = PAPI_NULL;
54 unsigned short *profbuf;
55 unsigned short *profbuf2;
56 unsigned short *profbuf3;
57 unsigned long length;
58 vptr_t start, end;
59 long long values[2];
60 const PAPI_exe_info_t *prginfo = NULL;
61 PAPI_sprofil_t sprof[3];
62 int retval;
63
64 /* initializaion */
66 {
67 printf("Library initialization error! \n");
68 exit(1);
69 }
70
71 if ((prginfo = PAPI_get_executable_info()) == NULL)
72 ERROR_RETURN(1);
73
74 start = prginfo->address_info.text_start;
75 end = prginfo->address_info.text_end;
76 length = (end - start)/sizeof(unsigned short) * sizeof(unsigned short);
77 printf("start= %p end =%p \n", start, end);
78
79 profbuf = (unsigned short *) malloc(length);
80 if (profbuf == NULL)
82
83 memset(profbuf, 0x00, length );
84
85 profbuf2 = (unsigned short *) malloc(length);
86 if (profbuf2 == NULL)
88
89 memset(profbuf2, 0x00, length );
90
91 profbuf3 = (unsigned short *) malloc(1 * sizeof(unsigned short));
92 if (profbuf3 == NULL)
94
95 memset(profbuf3, 0x00, 1 * sizeof(unsigned short));
96
97 /* First half */
98 sprof[0].pr_base = profbuf;
99 sprof[0].pr_size = length / 2;
100 sprof[0].pr_off = DO_FLOPS2;
101 fprintf(stderr, "do_flops is at %p %lx\n", &do_flops2, strtoul(sprof[0].pr_off,NULL,0));
102
103 sprof[0].pr_scale = 65536; /* constant needed by PAPI_sprofil */
104 /* Second half */
105 sprof[1].pr_base = profbuf2;
106 sprof[1].pr_size = length / 2;
107 sprof[1].pr_off = DO_FLOPS1;
108 fprintf(stderr, "do_flops1 is at %p %lx\n", &do_flops1, strtoul(sprof[1].pr_off,NULL,0));
109 sprof[1].pr_scale = 65536; /* constant needed by PAPI_sprofil */
110
111 /* Overflow bin */
112 sprof[2].pr_base = profbuf3;
113 sprof[2].pr_size = 1;
114 sprof[2].pr_off = 0;
115 sprof[2].pr_scale = 0x2; /* constant needed by PAPI_sprofil */
116
117 /* Creating the eventset */
120
122 /* Add Total Instructions Executed to our EventSet */
125
126 /* Add Total Instructions Executed to our EventSet */
129
130 /* set profile flag */
134
135 if ((retval = PAPI_start(EventSet)) != PAPI_OK)
137
139
142
143 /* to clear the profile flag before removing the events */
144 if ((retval = PAPI_sprofil(sprof, 3, EventSet, PAPI_event, 0,
147
148 /* free the resources hold by PAPI */
150
151 printf("Test case: PAPI_sprofil()\n");
152 printf("---------Buffer 1--------\n");
153 for (i = 0; i < length / 2; i++)
154 {
155 if (profbuf[i])
156 printf("%#lx\t%d\n", strtoul(DO_FLOPS2,NULL,0) + 2 * i, profbuf[i]);
157 }
158 printf("---------Buffer 2--------\n");
159 for (i = 0; i < length / 2; i++)
160 {
161 if (profbuf2[i])
162 printf("%#lx\t%d\n", strtoul(DO_FLOPS1,NULL,0) + 2 * i, profbuf2[i]);
163 }
164 printf("-------------------------\n");
165 printf("%u samples fell outside the regions.\n", *profbuf3);
166
167 exit(0);
168}
169
170/* Declare a and b to be volatile.
171 This is to try to keep the
172 compiler from optimizing the loop */
173volatile double a = 0.5, b = 2.2;
174void do_flops2(int n)
175{
176 int i;
177 double c = 0.11;
178
179 for (i = 0; i < n; i++)
180 c += a * b;
181}
182
int i
add PAPI preset or native hardware event to an event set
Create a new empty PAPI EventSet.
Get the executable's address space info.
initialize the PAPI library.
Finish using PAPI and free all related resources.
Generate PC histogram data from multiple code regions where hardware counter overflow occurs.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
int PAPI_event[2]
Definition: data_range.c:30
void do_flops1(int n)
#define DO_FLOPS2
void do_flops2(int)
#define THRESHOLD
#define NUM_ITERS
volatile double b
#define ERROR_RETURN(retval)
#define NUM_FLOPS
#define DO_FLOPS1
void do_both(int n)
volatile double t1
volatile double a
volatile double t2
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_NULL
Definition: f90papi.h:78
#define PAPI_PROFIL_POSIX
Definition: f90papi.h:44
#define PAPI_TOT_CYC
Definition: f90papi.h:308
#define PAPI_ESYS
Definition: f90papi.h:136
#define PAPI_TOT_INS
Definition: f90papi.h:317
static int EventSet
Definition: init_fini.c:8
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
static struct timeval start
static double c[MATRIX_SIZE][MATRIX_SIZE]
Definition: libmsr_basic.c:40
Return codes and api definitions.
void * vptr_t
Definition: papi.h:576
FILE * stderr
int main()
Definition: pernode.c:20
void * profbuf[5]
Definition: prof_utils.c:33
unsigned int length
vptr_t text_start
Definition: papi.h:686
vptr_t text_end
Definition: papi.h:687
get the executable's info
Definition: papi.h:696
PAPI_address_map_t address_info
Definition: papi.h:698
vptr_t pr_off
Definition: papi.h:582
unsigned pr_size
Definition: papi.h:581
void * pr_base
Definition: papi.h:580
unsigned pr_scale
Definition: papi.h:583
int retval
Definition: zero_fork.c:53