PAPI  5.7.0.0
pthrtough.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <pthread.h>
5 
6 #include "papi.h"
7 #include "papi_test.h"
8 
9 #define NITER 1000
10 
11 void *
12 Thread( void *data )
13 {
14  int i, ret, evtset;
15 
16  ( void ) data;
17 
18  for ( i = 0; i < NITER; i++ ) {
19  if ( ( ret = PAPI_register_thread( ) ) != PAPI_OK )
20  test_fail( __FILE__, __LINE__, "PAPI_thread_init", ret );
21 
22  evtset = PAPI_NULL;
23  if ( ( ret = PAPI_create_eventset( &evtset ) ) != PAPI_OK )
24  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", ret );
25 
26  if ( ( ret = PAPI_destroy_eventset( &evtset ) ) != PAPI_OK )
27  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", ret );
28 
29  if ( ( ret = PAPI_unregister_thread( ) ) != PAPI_OK )
30  test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", ret );
31  }
32  return ( NULL );
33 }
34 
35 int
36 main( int argc, char *argv[] )
37 {
38  int j;
39  pthread_t *th = NULL;
40  pthread_attr_t attr;
41  int ret;
42  long nthr;
43  const PAPI_hw_info_t *hwinfo;
44 
45  tests_quiet( argc, argv ); /*Set TESTS_QUIET variable */
46 
48  if ( ret != PAPI_VER_CURRENT )
49  test_fail( __FILE__, __LINE__, "PAPI_library_init", ret );
50 
51  if ( ( ret =
52  PAPI_thread_init( ( unsigned
53  long ( * )( void ) ) ( pthread_self ) ) ) !=
54  PAPI_OK )
55  test_fail( __FILE__, __LINE__, "PAPI_thread_init", ret );
56 
57  pthread_attr_init( &attr );
58 #ifdef PTHREAD_CREATE_UNDETACHED
59  pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_UNDETACHED );
60 #endif
61 #ifdef PTHREAD_SCOPE_SYSTEM
62  ret=pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );
63  if ( ret != 0 )
64  test_skip( __FILE__, __LINE__, "pthread_attr_setscope", ret );
65 
66 #endif
67 
68  if ( ( hwinfo = PAPI_get_hardware_info( ) ) == NULL )
69  test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 0 );
70 
71  nthr = hwinfo->ncpu;
72 
73  if ( !TESTS_QUIET ) {
74  printf( "Creating %ld threads for %d iterations each of:\n", nthr,
75  NITER );
76  printf( "\tregister\n" );
77  printf( "\tcreate_eventset\n" );
78  printf( "\tdestroy_eventset\n" );
79  printf( "\tunregister\n" );
80  }
81 
82  th = ( pthread_t * ) malloc( ( size_t ) nthr * sizeof ( pthread_t ) );
83  if ( th == NULL )
84  test_fail( __FILE__, __LINE__, "malloc", PAPI_ESYS );
85 
86  for ( j = 0; j < nthr; j++ ) {
87  ret = pthread_create( &th[j], &attr, &Thread, NULL );
88  if ( ret )
89  test_fail( __FILE__, __LINE__, "pthread_create", PAPI_ESYS );
90  }
91 
92  for ( j = 0; j < nthr; j++ ) {
93  pthread_join( th[j], NULL );
94  }
95 
96  test_pass( __FILE__ );
97 
98  return 0;
99 
100 }
#define PAPI_OK
Definition: fpapi.h:105
void test_pass(const char *filename)
Definition: test_utils.c:432
Hardware info structure.
Definition: papi.h:781
unsigned long int pthread_t
int main(int argc, char *argv[])
Definition: pthrtough.c:36
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
int PAPI_register_thread(void)
Definition: papi.c:208
Return codes and api definitions.
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:561
#define PAPI_ESYS
Definition: fpapi.h:108
int PAPI_thread_init(unsigned long int(*id_fn)(void))
Definition: papi.c:123
int PAPI_library_init(int version)
Definition: papi.c:500
#define NITER
Definition: pthrtough.c:9
#define PAPI_NULL
Definition: fpapi.h:13
long long ret
Definition: iozone.c:1346
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
int PAPI_unregister_thread(void)
Definition: papi.c:244
int TESTS_QUIET
Definition: test_utils.c:18
void * Thread(void *data)
Definition: pthrtough.c:12
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
void test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:468
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:2014
const PAPI_hw_info_t * PAPI_get_hardware_info(void)
Definition: papi.c:6185
int i
Definition: fileop.c:140
int ncpu
Definition: papi.h:782