PAPI 7.1.0.0
Loading...
Searching...
No Matches
krentel_pthreads.c
Go to the documentation of this file.
1/*
2 * Test PAPI with multiple threads.
3 */
4
5#define MAX_THREADS 256
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <pthread.h>
10#include <sys/time.h>
11
12#include "papi.h"
13#include "papi_test.h"
14
15#define EVENT PAPI_TOT_CYC
16
17static int program_time = 5;
18static int threshold = 20000000;
19static int num_threads = 3;
20
21static long count[MAX_THREADS];
22static long iter[MAX_THREADS];
23static struct timeval last[MAX_THREADS];
24
26
27static struct timeval start;
28
29static void
30my_handler( int EventSet, void *pc, long long ovec, void *context )
31{
32 ( void ) EventSet;
33 ( void ) pc;
34 ( void ) ovec;
35 ( void ) context;
36
37 long num = ( long ) pthread_getspecific( key );
38
39 if ( num < 0 || num > num_threads )
40 test_fail( __FILE__, __LINE__, "getspecific failed", 1 );
41 count[num]++;
42}
43
44static void
45print_rate( long num )
46{
47 struct timeval now;
48 long st_secs;
49 double last_secs;
50
51 gettimeofday( &now, NULL );
52 st_secs = now.tv_sec - start.tv_sec;
53 last_secs = ( double ) ( now.tv_sec - last[num].tv_sec )
54 + ( ( double ) ( now.tv_usec - last[num].tv_usec ) ) / 1000000.0;
55 if ( last_secs <= 0.001 )
56 last_secs = 0.001;
57
58 if (!TESTS_QUIET) {
59 printf( "[%ld] time = %ld, count = %ld, iter = %ld, "
60 "rate = %.1f/Kiter\n",
61 num, st_secs, count[num], iter[num],
62 ( 1000.0 * ( double ) count[num] ) / ( double ) iter[num] );
63 }
64
65 count[num] = 0;
66 iter[num] = 0;
67 last[num] = now;
68}
69
70static void
71do_cycles( long num, int len )
72{
73 struct timeval start, now;
74 double x, sum;
75
76 gettimeofday( &start, NULL );
77
78 for ( ;; ) {
79 sum = 1.0;
80 for ( x = 1.0; x < 250000.0; x += 1.0 )
81 sum += x;
82 if ( sum < 0.0 )
83 printf( "==>> SUM IS NEGATIVE !! <<==\n" );
84
85 iter[num]++;
86
87 gettimeofday( &now, NULL );
88 if ( now.tv_sec >= start.tv_sec + len )
89 break;
90 }
91}
92
93static void *
94my_thread( void *v )
95{
96 long num = ( long ) v;
97 int n;
98 int EventSet = PAPI_NULL;
99 long long value;
100
101 int retval;
102
104 if ( retval != PAPI_OK ) {
105 test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
106 }
107 pthread_setspecific( key, v );
108
109 count[num] = 0;
110 iter[num] = 0;
111 last[num] = start;
112
114 if ( retval != PAPI_OK ) {
115 test_fail( __FILE__, __LINE__, "PAPI_create_eventset failed", retval );
116 }
117
119 if (retval != PAPI_OK ) {
120 if (!TESTS_QUIET) printf("Trouble adding event\n");
121 test_fail( __FILE__, __LINE__, "PAPI_add_event failed", retval );
122 }
123
125 test_fail( __FILE__, __LINE__, "PAPI_overflow failed", 1 );
126
127 if ( PAPI_start( EventSet ) != PAPI_OK )
128 test_fail( __FILE__, __LINE__, "PAPI_start failed", 1 );
129
130 if (!TESTS_QUIET) printf( "launched timer in thread %ld\n", num );
131
132 for ( n = 1; n <= program_time; n++ ) {
133 do_cycles( num, 1 );
134 print_rate( num );
135 }
136
137 PAPI_stop( EventSet, &value );
138
140 if ( retval != PAPI_OK )
141 test_fail( __FILE__, __LINE__, "PAPI_overflow failed to reset the overflow handler", retval );
142
144 test_fail( __FILE__, __LINE__, "PAPI_remove_event", 1 );
145
147 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", 1 );
148
149 if ( PAPI_unregister_thread( ) != PAPI_OK )
150 test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", 1 );
151
152 return ( NULL );
153}
154
155int
156main( int argc, char **argv )
157{
158 pthread_t *td = NULL;
159 long n;
160 int quiet,retval;
161
162 /* Set TESTS_QUIET variable */
163 quiet=tests_quiet( argc, argv );
164
165 if ( argc < 2 || sscanf( argv[1], "%d", &program_time ) < 1 )
166 program_time = 6;
167 if ( argc < 3 || sscanf( argv[2], "%d", &threshold ) < 1 )
168 threshold = 20000000;
169 if ( argc < 4 || sscanf( argv[3], "%d", &num_threads ) < 1 )
170 num_threads = 3;
171
172 td = malloc((num_threads+1) * sizeof(pthread_t));
173 if (!td) {
174 test_fail( __FILE__, __LINE__, "td malloc failed", 1 );
175 }
176
177 if (!quiet) {
178 printf( "program_time = %d, threshold = %d, num_threads = %d\n\n",
180 }
181
183 test_fail( __FILE__, __LINE__, "PAPI_library_init failed", 1 );
184
185 /* Test to be sure we can add events */
187 if (retval!=PAPI_OK) {
188 if (!quiet) printf("Trouble finding event\n");
189 test_skip(__FILE__,__LINE__,"Event not available",1);
190 }
191
192 if ( PAPI_thread_init( ( unsigned long ( * )( void ) ) ( pthread_self ) ) !=
193 PAPI_OK )
194 test_fail( __FILE__, __LINE__, "PAPI_thread_init failed", 1 );
195
196 if ( pthread_key_create( &key, NULL ) != 0 )
197 test_fail( __FILE__, __LINE__, "pthread key create failed", 1 );
198
199 gettimeofday( &start, NULL );
200
201 for ( n = 1; n <= num_threads; n++ ) {
202 if ( pthread_create( &(td[n]), NULL, my_thread, ( void * ) n ) != 0 )
203 test_fail( __FILE__, __LINE__, "pthread create failed", 1 );
204 }
205
206 my_thread( ( void * ) 0 );
207
208 /* wait for all the threads */
209 for ( n = 1; n <= num_threads; n++ ) {
210 if ( pthread_join( td[n], NULL))
211 test_fail( __FILE__, __LINE__, "pthread join failed", 1 );
212 }
213
214 free(td);
215
216 if (!quiet) printf( "done\n" );
217
218 test_pass( __FILE__ );
219
220 return 0;
221}
add PAPI preset or native hardware event to an event set
Create a new empty PAPI EventSet.
Empty and destroy an EventSet.
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.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
Initialize thread support in the PAPI library.
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_NULL
Definition: f90papi.h:78
int PAPI_register_thread(void)
Definition: papi.c:750
int PAPI_unregister_thread(void)
Definition: papi.c:786
static int EventSet
Definition: init_fini.c:8
static int num_threads
static long iter[MAX_THREADS]
static void * my_thread(void *v)
static int threshold
#define EVENT
static long count[MAX_THREADS]
static int program_time
#define MAX_THREADS
static struct timeval last[MAX_THREADS]
static void print_rate(long num)
static pthread_key_t key
static struct timeval start
static void my_handler(int EventSet, void *pc, long long ovec, void *context)
static void do_cycles(long num, int len)
int TESTS_QUIET
Definition: test_utils.c:18
Return codes and api definitions.
unsigned long int pthread_t
unsigned int pthread_key_t
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 quiet
Definition: rapl_overflow.c:19
long long int long long
Definition: sde_internal.h:85
__time_t tv_sec
__suseconds_t tv_usec
volatile double x
int retval
Definition: zero_fork.c:53