PAPI  5.3.2.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
zero_pthreads.c
Go to the documentation of this file.
1 /* This file performs the following test: start, stop and timer
2 functionality for 2 slave pthreads
3 
4  - It attempts to use the following two counters. It may use less
5 depending on hardware counter resource limitations. These are counted
6 in the default counting domain and default granularity, depending on
7 the platform. Usually this is the user domain (PAPI_DOM_USER) and
8 thread context (PAPI_GRN_THR).
9 
10  + PAPI_FP_INS
11  + PAPI_TOT_CYC
12 
13 Each of 2 slave pthreads:
14  - Get cyc.
15  - Get us.
16  - Start counters
17  - Do flops
18  - Stop and read counters
19  - Get us.
20  - Get cyc.
21 
22 Master pthread:
23  - Get us.
24  - Get cyc.
25  - Fork threads
26  - Wait for threads to exit
27  - Get us.
28  - Get cyc.
29 */
30 
31 #include <pthread.h>
32 
33 #include "papi_test.h"
34 
35 void *
36 Thread( void *arg )
37 {
38  int retval, num_tests = 1;
39  int EventSet1 = PAPI_NULL;
40  int PAPI_event, mask1;
41  int num_events1;
42  long long **values;
43  long long elapsed_us, elapsed_cyc;
45 
46  retval = PAPI_register_thread( );
47  if ( retval != PAPI_OK ) {
48  test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
49  }
50 
51  printf( "Thread %#x started\n", ( int ) pthread_self( ) );
52 
53  /* add PAPI_TOT_CYC and one of the events in
54  PAPI_FP_INS, PAPI_FP_OPS or PAPI_TOT_INS,
55  depending on the availability of the event
56  on the platform */
57  EventSet1 = add_two_events( &num_events1, &PAPI_event, &mask1 );
58 
59  retval = PAPI_event_code_to_name( PAPI_event, event_name );
60  if ( retval != PAPI_OK ) {
61  test_fail( __FILE__, __LINE__, "PAPI_event_code_to_name", retval );
62  }
63 
64  values = allocate_test_space( num_tests, num_events1 );
65 
66  elapsed_us = PAPI_get_real_usec( );
67  elapsed_cyc = PAPI_get_real_cyc( );
68 
69  retval = PAPI_start( EventSet1 );
70  if ( retval != PAPI_OK ) {
71  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
72  }
73 
74  do_flops( *( int * ) arg );
75 
76  retval = PAPI_stop( EventSet1, values[0] );
77  if ( retval != PAPI_OK ) {
78  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
79  }
80 
81  elapsed_us = PAPI_get_real_usec( ) - elapsed_us;
82  elapsed_cyc = PAPI_get_real_cyc( ) - elapsed_cyc;
83 
84  remove_test_events( &EventSet1, mask1 );
85 
86  if ( !TESTS_QUIET ) {
87  printf( "Thread %#x %-12s : \t%lld\n", ( int ) pthread_self( ),
88  event_name, values[0][1] );
89  printf( "Thread %#x PAPI_TOT_CYC : \t%lld\n", (int) pthread_self(),
90  values[0][0] );
91  printf( "Thread %#x Real usec : \t%lld\n",
92  ( int ) pthread_self( ),
93  elapsed_us );
94  printf( "Thread %#x Real cycles : \t%lld\n", (int) pthread_self(),
95  elapsed_cyc );
96  }
97 
98  free_test_space( values, num_tests );
99 
100  retval = PAPI_unregister_thread( );
101  if ( retval != PAPI_OK )
102  test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", retval );
103  return NULL;
104 }
105 
106 int
107 main( int argc, char **argv )
108 {
109  pthread_t e_th, f_th, g_th, h_th;
110  int flops1, flops2, flops3, flops4;
111  int retval, rc;
112  pthread_attr_t attr;
113  long long elapsed_us, elapsed_cyc;
114 
115  /* Set TESTS_QUIET variable */
116  tests_quiet( argc, argv );
117 
118  /* Init PAPI library */
120  if ( retval != PAPI_VER_CURRENT ) {
121  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
122  }
123 
124  retval = PAPI_thread_init( ( unsigned long ( * )( void ) )
125  ( pthread_self ) );
126  if ( retval != PAPI_OK ) {
127  if ( retval == PAPI_ECMP ) {
128  test_skip( __FILE__, __LINE__, "PAPI_thread_init", retval );
129  }
130  else {
131  test_fail( __FILE__, __LINE__, "PAPI_thread_init", retval );
132  }
133  }
134 
135  elapsed_us = PAPI_get_real_usec( );
136 
137  elapsed_cyc = PAPI_get_real_cyc( );
138 
139  pthread_attr_init( &attr );
140 #ifdef PTHREAD_CREATE_UNDETACHED
141  pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_UNDETACHED );
142 #endif
143 #ifdef PTHREAD_SCOPE_SYSTEM
144  retval = pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );
145  if ( retval != 0 )
146  test_skip( __FILE__, __LINE__, "pthread_attr_setscope", retval );
147 #endif
148 
149  flops1 = 1000000;
150  rc = pthread_create( &e_th, &attr, Thread, ( void * ) &flops1 );
151  if ( rc ) {
152  retval = PAPI_ESYS;
153  test_fail( __FILE__, __LINE__, "pthread_create", retval );
154  }
155  flops2 = 2000000;
156  rc = pthread_create( &f_th, &attr, Thread, ( void * ) &flops2 );
157  if ( rc ) {
158  retval = PAPI_ESYS;
159  test_fail( __FILE__, __LINE__, "pthread_create", retval );
160  }
161 
162  flops3 = 4000000;
163  rc = pthread_create( &g_th, &attr, Thread, ( void * ) &flops3 );
164  if ( rc ) {
165  retval = PAPI_ESYS;
166  test_fail( __FILE__, __LINE__, "pthread_create", retval );
167  }
168 
169  flops4 = 8000000;
170  rc = pthread_create( &h_th, &attr, Thread, ( void * ) &flops4 );
171  if ( rc ) {
172  retval = PAPI_ESYS;
173  test_fail( __FILE__, __LINE__, "pthread_create", retval );
174  }
175 
176  pthread_attr_destroy( &attr );
177  flops1 = 500000;
178  Thread( &flops1 );
179  pthread_join( h_th, NULL );
180  pthread_join( g_th, NULL );
181  pthread_join( f_th, NULL );
182  pthread_join( e_th, NULL );
183 
184  elapsed_cyc = PAPI_get_real_cyc( ) - elapsed_cyc;
185  elapsed_us = PAPI_get_real_usec( ) - elapsed_us;
186 
187  if ( !TESTS_QUIET ) {
188  printf( "Master real usec : \t%lld\n", elapsed_us );
189  printf( "Master real cycles : \t%lld\n", elapsed_cyc );
190  }
191 
192  test_pass( __FILE__, NULL, 0 );
193  pthread_exit( NULL );
194  exit( 1 );
195 }
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:23
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2232
int mask1
Definition: zero_fork.c:41
int PAPI_event[2]
Definition: data_range.c:24
#define PAPI_NULL
Definition: fpapi.h:13
#define PAPI_MAX_STR_LEN
Definition: fpapi.h:43
void test_skip(char *file, int line, char *call, int retval)
Definition: test_utils.c:614
int num_events1
Definition: zero_fork.c:42
int PAPI_register_thread(void)
Definition: papi.c:203
return PAPI_OK
Definition: linux-nvml.c:458
tests_quiet(argc, argv)
#define printf
Definition: papi_test.h:125
test_pass(__FILE__, NULL, 0)
int int argc
Definition: iozone.c:1609
int TESTS_QUIET
Definition: test_utils.c:11
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 long elapsed_cyc
Definition: zero_fork.c:43
#define PAPI_ECMP
Definition: fpapi.h:109
int add_two_events(int *num_events, int *papi_event, int *mask)
Definition: test_utils.c:680
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
void free_test_space(long long **values, int num_tests)
Definition: test_utils.c:131
void * Thread(void *arg)
pthread_t
Definition: iozone.c:18557
int PAPI_event_code_to_name(int EventCode, char *out)
Definition: papi.c:924
#define PAPI_ESYS
Definition: fpapi.h:108
int num_tests
Definition: zero_fork.c:46
void do_flops(int n)
Definition: multiplex.c:23
long long PAPI_get_real_usec(void)
Definition: papi.c:6138
int PAPI_unregister_thread(void)
Definition: papi.c:239
int rc
Definition: iozone.c:20281
long long PAPI_get_real_cyc(void)
Definition: papi.c:6091
int PAPI_start(int EventSet)
Definition: papi.c:2026
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
ssize_t retval
Definition: libasync.c:338
long long elapsed_us
Definition: zero_fork.c:43
long long ** allocate_test_space(int num_tests, int num_events)
Definition: test_utils.c:107
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
int remove_test_events(int *EventSet, int mask)
Definition: test_utils.c:277
pthread_attr_t attr
Definition: iozone.c:18466
int EventSet1
Definition: zero_fork.c:40