PAPI  5.3.2.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
krentel_pthreads.c
Go to the documentation of this file.
1 /*
2  * Test PAPI with multiple threads.
3  */
4 
5 #include <pthread.h>
6 #include <sys/time.h>
7 #include "papi_test.h"
8 
9 #define EVENT PAPI_TOT_CYC
10 
11 int program_time = 5;
12 int threshold = 20000000;
13 int num_threads = 3;
14 
17 struct timeval last[MAX_THREADS];
18 
19 pthread_key_t key;
20 
21 struct timeval start;
22 
23 void
24 my_handler( int EventSet, void *pc, long long ovec, void *context )
25 {
26  ( void ) EventSet;
27  ( void ) pc;
28  ( void ) ovec;
29  ( void ) context;
30 
31  long num = ( long ) pthread_getspecific( key );
32 
33  if ( num < 0 || num > num_threads )
34  test_fail( __FILE__, __LINE__, "getspecific failed", 1 );
35  count[num]++;
36 }
37 
38 void
39 print_rate( long num )
40 {
41  struct timeval now;
42  long st_secs;
43  double last_secs;
44 
45  gettimeofday( &now, NULL );
46  st_secs = now.tv_sec - start.tv_sec;
47  last_secs = ( double ) ( now.tv_sec - last[num].tv_sec )
48  + ( ( double ) ( now.tv_usec - last[num].tv_usec ) ) / 1000000.0;
49  if ( last_secs <= 0.001 )
50  last_secs = 0.001;
51 
52  printf( "[%ld] time = %ld, count = %ld, iter = %ld, "
53  "rate = %.1f/Kiter\n",
54  num, st_secs, count[num], iter[num],
55  ( 1000.0 * ( double ) count[num] ) / ( double ) iter[num] );
56 
57  count[num] = 0;
58  iter[num] = 0;
59  last[num] = now;
60 }
61 
62 void
63 do_cycles( long num, int len )
64 {
65  struct timeval start, now;
66  double x, sum;
67 
68  gettimeofday( &start, NULL );
69 
70  for ( ;; ) {
71  sum = 1.0;
72  for ( x = 1.0; x < 250000.0; x += 1.0 )
73  sum += x;
74  if ( sum < 0.0 )
75  printf( "==>> SUM IS NEGATIVE !! <<==\n" );
76 
77  iter[num]++;
78 
79  gettimeofday( &now, NULL );
80  if ( now.tv_sec >= start.tv_sec + len )
81  break;
82  }
83 }
84 
85 void
87 {
88  if ( PAPI_create_eventset( EventSet ) != PAPI_OK )
89  test_fail( __FILE__, __LINE__, "PAPI_create_eventset failed", 1 );
90 
91  if ( PAPI_add_event( *EventSet, EVENT ) != PAPI_OK )
92  test_fail( __FILE__, __LINE__, "PAPI_add_event failed", 1 );
93 
94  if ( PAPI_overflow( *EventSet, EVENT, threshold, 0, my_handler ) !=
95  PAPI_OK )
96  test_fail( __FILE__, __LINE__, "PAPI_overflow failed", 1 );
97 
98  if ( PAPI_start( *EventSet ) != PAPI_OK )
99  test_fail( __FILE__, __LINE__, "PAPI_start failed", 1 );
100 }
101 
102 void *
103 my_thread( void *v )
104 {
105  long num = ( long ) v;
106  int n;
107  int EventSet = PAPI_NULL;
108  long long value;
109 
110  int retval = PAPI_register_thread( );
111  if ( retval != PAPI_OK )
112  test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
113  pthread_setspecific( key, v );
114 
115  count[num] = 0;
116  iter[num] = 0;
117  last[num] = start;
118 
119  launch_timer( &EventSet );
120  printf( "launched timer in thread %ld\n", num );
121 
122  for ( n = 1; n <= program_time; n++ ) {
123  do_cycles( num, 1 );
124  print_rate( num );
125  }
126 
127  PAPI_stop( EventSet, &value );
128  PAPI_remove_event( EventSet, EVENT );
129  PAPI_destroy_eventset( &EventSet );
130  retval = PAPI_unregister_thread( );
131  if ( retval != PAPI_OK )
132  test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", retval );
133  return ( NULL );
134 }
135 
136 int
137 main( int argc, char **argv )
138 {
139  pthread_t td;
140  long n;
141 
142  tests_quiet( argc, argv ); /*Set TESTS_QUIET variable */
143 
144  if ( argc < 2 || sscanf( argv[1], "%d", &program_time ) < 1 )
145  program_time = 6;
146  if ( argc < 3 || sscanf( argv[2], "%d", &threshold ) < 1 )
147  threshold = 20000000;
148  if ( argc < 4 || sscanf( argv[3], "%d", &num_threads ) < 1 )
149  num_threads = 3;
150 
151  printf( "program_time = %d, threshold = %d, num_threads = %d\n\n",
153 
155  test_fail( __FILE__, __LINE__, "PAPI_library_init failed", 1 );
156 
157  if ( PAPI_thread_init( ( unsigned long ( * )( void ) ) ( pthread_self ) ) !=
158  PAPI_OK )
159  test_fail( __FILE__, __LINE__, "PAPI_thread_init failed", 1 );
160 
161  if ( pthread_key_create( &key, NULL ) != 0 )
162  test_fail( __FILE__, __LINE__, "pthread key create failed", 1 );
163 
164  gettimeofday( &start, NULL );
165 
166  for ( n = 1; n <= num_threads; n++ ) {
167  if ( pthread_create( &td, NULL, my_thread, ( void * ) n ) != 0 )
168  test_fail( __FILE__, __LINE__, "pthread create failed", 1 );
169  }
170 
171  my_thread( ( void * ) 0 );
172 
173  printf( "done\n" );
174 
175  test_pass( __FILE__, NULL, 0 );
176  pthread_exit( NULL );
177  return ( 0 );
178 }
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2232
void do_cycles(int program_time)
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1604
#define PAPI_NULL
Definition: fpapi.h:13
start
Definition: iozone.c:22736
int PAPI_remove_event(int EventSet, int EventCode)
Definition: papi.c:1699
void my_handler(int EventSet, void *pc, long long ovec, void *context)
void * my_thread(void *v)
int PAPI_register_thread(void)
Definition: papi.c:203
return PAPI_OK
Definition: linux-nvml.c:458
int count
Definition: iozone.c:22422
tests_quiet(argc, argv)
int PAPI_overflow(int EventSet, int EventCode, int threshold, int flags, PAPI_overflow_handler_t handler)
Definition: papi.c:4668
void
Definition: iozone.c:18627
#define printf
Definition: papi_test.h:125
void double value
Definition: iozone.c:18781
static double
Definition: fileop.c:1281
test_pass(__FILE__, NULL, 0)
int int argc
Definition: iozone.c:1609
char ** argv
Definition: iozone.c:1610
test_fail(__FILE__, __LINE__,"PAPI_library_init", retval)
int PAPI_thread_init(unsigned long int(*id_fn)(void))
Definition: papi.c:118
int PAPI_library_init(int version)
Definition: papi.c:495
long iter[MAX_THREADS]
void launch_timer(int *EventSet)
int program_time
#define EVENT
int num_threads
void int num
Definition: iozone.c:22151
pthread_key_t key
#define MAX_THREADS
Definition: test_utils.h:7
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
long long
Definition: iozone.c:19827
static struct temp_event * last
pthread_t
Definition: iozone.c:18557
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1406
int threshold
int EventSet
sscanf(mnc->m_child_port,"%d",&mc.m_child_port)
int PAPI_unregister_thread(void)
Definition: papi.c:239
int x
Definition: fileop.c:78
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:1944
int PAPI_start(int EventSet)
Definition: papi.c:2026
ssize_t retval
Definition: libasync.c:338
int main(int argc, char **argv)
List all appio events codes and names.
void print_rate(char *str)
int n
Definition: mendes-alt.c:164