PAPI  5.4.0.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  retval = PAPI_remove_event( EventSet, EVENT );
129  if ( PAPI_OK != retval ) {
130  test_fail( __FILE__, __LINE__, "PAPI_remove_event", retval );
131  }
132  retval = PAPI_destroy_eventset( &EventSet );
133  if ( PAPI_OK != retval ) {
134  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
135  }
136  retval = PAPI_unregister_thread( );
137  if ( PAPI_OK != retval ) {
138  test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", retval );
139  }
140  return ( NULL );
141 }
142 
143 int
144 main( int argc, char **argv )
145 {
146  pthread_t td;
147  long n;
148 
149  tests_quiet( argc, argv ); /*Set TESTS_QUIET variable */
150 
151  if ( argc < 2 || sscanf( argv[1], "%d", &program_time ) < 1 )
152  program_time = 6;
153  if ( argc < 3 || sscanf( argv[2], "%d", &threshold ) < 1 )
154  threshold = 20000000;
155  if ( argc < 4 || sscanf( argv[3], "%d", &num_threads ) < 1 )
156  num_threads = 3;
157 
158  printf( "program_time = %d, threshold = %d, num_threads = %d\n\n",
160 
162  test_fail( __FILE__, __LINE__, "PAPI_library_init failed", 1 );
163 
164  if ( PAPI_thread_init( ( unsigned long ( * )( void ) ) ( pthread_self ) ) !=
165  PAPI_OK )
166  test_fail( __FILE__, __LINE__, "PAPI_thread_init failed", 1 );
167 
168  if ( pthread_key_create( &key, NULL ) != 0 )
169  test_fail( __FILE__, __LINE__, "pthread key create failed", 1 );
170 
171  gettimeofday( &start, NULL );
172 
173  for ( n = 1; n <= num_threads; n++ ) {
174  if ( pthread_create( &td, NULL, my_thread, ( void * ) n ) != 0 )
175  test_fail( __FILE__, __LINE__, "pthread create failed", 1 );
176  }
177 
178  my_thread( ( void * ) 0 );
179 
180  printf( "done\n" );
181 
182  test_pass( __FILE__, NULL, 0 );
183  pthread_exit( NULL );
184  return ( 0 );
185 }
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2258
void do_cycles(int program_time)
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1622
start
Definition: iozone.c:22736
int PAPI_remove_event(int EventSet, int EventCode)
Definition: papi.c:1717
#define PAPI_NULL
Definition: papi.h:290
int EventSet
Definition: data_range.c:25
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:205
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:4714
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:120
int PAPI_library_init(int version)
Definition: papi.c:497
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
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:1424
int threshold
sscanf(mnc->m_child_port,"%d",&mc.m_child_port)
int PAPI_unregister_thread(void)
Definition: papi.c:241
int x
Definition: fileop.c:78
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:1971
int PAPI_start(int EventSet)
Definition: papi.c:2053
ssize_t retval
Definition: libasync.c:338
#define PAPI_VER_CURRENT
Definition: papi.h:223
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