PAPI 7.1.0.0
Loading...
Searching...
No Matches
examples/overflow_pthreads.c
Go to the documentation of this file.
1/* This file performs the following test: overflow dispatch with pthreads
2
3 - This example tests the dispatch of overflow calls from PAPI. The event
4 set is counted in the default counting domain and default granularity,
5 depending on the platform. Usually this is the user domain
6 (PAPI_DOM_USER) and thread context (PAPI_GRN_THR).
7
8 The Eventset contains:
9 + PAPI_TOT_INS (overflow monitor)
10 + PAPI_TOT_CYC
11
12 Each thread will do the followings :
13 - enable overflow
14 - Start eventset 1
15 - Do flops
16 - Stop eventset 1
17 - disable overflow
18*/
19#include <stdio.h>
20#include <stdlib.h>
21#include <pthread.h>
22#include "papi.h"
23
24#define THRESHOLD 200000
25#define OVER_FMT "handler(%d ) Overflow at %p! bit=%#llx \n"
26#define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }
27
28
29int total = 0;
30
31void do_flops(int n)
32{
33 int i;
34 double c = 0.11;
35 double a = 0.5;
36 double b = 6.2;
37
38 for (i=0; i < n; i++)
39 c += a * b;
40}
41
42/* overflow handler */
43void
44handler(int EventSet, void *address, long long overflow_vector, void *context)
45{
46 fprintf(stderr, OVER_FMT, EventSet, address, overflow_vector);
47 total++;
48}
49
50void *Thread(void *arg)
51{
52 int retval;
54 long long values[2];
55 long long elapsed_us, elapsed_cyc;
56
57 fprintf(stderr,"Thread %lx running PAPI\n",PAPI_thread_id());
58
59 /* create the event set */
62
63 /* query whether the event exists */
68
69 /* add events to the event set */
72
75
77
79
81 if(retval !=PAPI_OK)
83
84 /* start counting */
87
88 do_flops(*(int *)arg);
89
92
94
96
97 /* disable overflowing */
99 if(retval !=PAPI_OK)
101
102 /* remove the event from the eventset */
104 if (retval != PAPI_OK)
106
108 if (retval != PAPI_OK)
110
111 printf("Thread %#x PAPI_TOT_INS : \t%lld\n",(int)PAPI_thread_id(),
112 values[0]);
113 printf(" PAPI_TOT_CYC: \t%lld\n", values[1]);
114 printf(" Real usec : \t%lld\n", elapsed_us);
115 printf(" Real cycles : \t%lld\n", elapsed_cyc);
116
117 pthread_exit(NULL);
118}
119
120int main(int argc, char **argv)
121{
122 pthread_t thread_one;
123 pthread_t thread_two;
124 int flops1, flops2;
125 int rc,retval;
126 pthread_attr_t attr;
127 long long elapsed_us, elapsed_cyc;
128
129
130 /* papi library initialization */
132 {
133 printf("Library initialization error! \n");
134 exit(1);
135 }
136
137 /* thread initialization */
138 retval=PAPI_thread_init((unsigned long(*)(void))(pthread_self));
139 if (retval != PAPI_OK)
141
142 /* return the number of microseconds since some arbitrary starting point */
144
145 /* return the number of cycles since some arbitrary starting point */
147
148 /* pthread attribution init */
149 pthread_attr_init(&attr);
150 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
151
152 /* create the first thread */
153 flops1 = 1000000;
154 rc = pthread_create(&thread_one, &attr, Thread, (void *)&flops1);
155 if (rc)
157
158 /* create the second thread */
159 flops2 = 4000000;
160 rc = pthread_create(&thread_two, &attr, Thread, (void *)&flops2);
161 if (rc)
163
164 /* wait for the threads to finish */
165 pthread_attr_destroy(&attr);
166 pthread_join(thread_one, NULL);
167 pthread_join(thread_two, NULL);
168
169 /* compute the elapsed cycles and microseconds */
171
173
174 printf("Master real usec : \t%lld\n", elapsed_us);
175 printf("Master real cycles : \t%lld\n", elapsed_cyc);
176
177 /* clean up */
179 exit(0);
180}
181
int i
add PAPI preset or native hardware event to an event set
Create a new empty PAPI EventSet.
get real time counter value in clock cycles Returns the total real time passed since some arbitrary s...
get real time counter value in microseconds
initialize the PAPI library.
Set up an event set to begin registering overflows.
Query if PAPI event exists.
removes a hardware event from a PAPI event set.
Finish using PAPI and free all related resources.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
Get the thread identifier of the current thread.
Initialize thread support in the PAPI library.
#define OVER_FMT
void do_flops(int n)
#define THRESHOLD
#define ERROR_RETURN(retval)
void handler(int EventSet, void *address, long long overflow_vector, void *context)
void * Thread(void *arg)
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_NULL
Definition: f90papi.h:78
#define PAPI_TOT_CYC
Definition: f90papi.h:308
#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 double a[MATRIX_SIZE][MATRIX_SIZE]
Definition: libmsr_basic.c:38
static double b[MATRIX_SIZE][MATRIX_SIZE]
Definition: libmsr_basic.c:39
static double c[MATRIX_SIZE][MATRIX_SIZE]
Definition: libmsr_basic.c:40
Return codes and api definitions.
unsigned long int pthread_t
FILE * stderr
int main()
Definition: pernode.c:20
rc
Definition: pscanf.h:23
int EventSet1
Definition: zero_fork.c:47
long long elapsed_cyc
Definition: zero_fork.c:50
long long elapsed_us
Definition: zero_fork.c:50
int retval
Definition: zero_fork.c:53