PAPI  5.6.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pthrtough2.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 2000
10 
11 void *
12 Thread( void *data )
13 {
14  int ret, evtset;
15 
16  ( void ) data;
17 
18  if ( ( ret = PAPI_register_thread( ) ) != PAPI_OK )
19  test_fail( __FILE__, __LINE__, "PAPI_thread_init", ret );
20 
21  evtset = PAPI_NULL;
22  if ( ( ret = PAPI_create_eventset( &evtset ) ) != PAPI_OK )
23  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", ret );
24 
25  if ( ( ret = PAPI_destroy_eventset( &evtset ) ) != PAPI_OK )
26  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", ret );
27 
28  if ( ( ret = PAPI_unregister_thread( ) ) != PAPI_OK )
29  test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", ret );
30 
31  return ( NULL );
32 }
33 
34 int
35 main( int argc, char *argv[] )
36 {
37  int j;
38  pthread_t *th = NULL;
39  pthread_attr_t attr;
40  int ret;
41  long nthr;
42 
43  tests_quiet( argc, argv ); /*Set TESTS_QUIET variable */
44 
46  if ( ret != PAPI_VER_CURRENT )
47  test_fail( __FILE__, __LINE__, "PAPI_library_init", ret );
48 
49  if ( ( ret =
50  PAPI_thread_init( ( unsigned
51  long ( * )( void ) ) ( pthread_self ) ) ) !=
52  PAPI_OK )
53  test_fail( __FILE__, __LINE__, "PAPI_thread_init", ret );
54 
55  pthread_attr_init( &attr );
56 #ifdef PTHREAD_CREATE_UNDETACHED
57  pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_UNDETACHED );
58 #endif
59 #ifdef PTHREAD_SCOPE_SYSTEM
60  ret = pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );
61  if ( ret != 0 )
62  test_skip( __FILE__, __LINE__, "pthread_attr_setscope", ret );
63 
64 #endif
65 
66  nthr = NITER;
67 
68  if ( !TESTS_QUIET ) {
69  printf( "Creating %d threads for %d iterations each of:\n",
70  ( int ) nthr, 1 );
71  printf( "\tregister\n" );
72  printf( "\tcreate_eventset\n" );
73  printf( "\tdestroy_eventset\n" );
74  printf( "\tunregister\n" );
75  }
76  th = ( pthread_t * ) malloc( ( size_t ) nthr * sizeof ( pthread_t ) );
77  if ( th == NULL )
78  test_fail( __FILE__, __LINE__, "malloc", PAPI_ESYS );
79 
80  for ( j = 0; j < nthr; j++ ) {
81  ret = pthread_create( &th[j], &attr, &Thread, NULL );
82  if ( ret ) {
83  printf( "Failed to create thread: %d\n", j );
84  if ( j < 10 )
85  test_fail( __FILE__, __LINE__, "pthread_create", PAPI_ESYS );
86  printf( "Continuing test with %d threads.\n", j - 1 );
87  nthr = j - 1;
88  th = ( pthread_t * ) realloc( th,
89  ( size_t ) nthr *
90  sizeof ( pthread_t ) );
91  break;
92  }
93  }
94 
95  for ( j = 0; j < nthr; j++ ) {
96  pthread_join( th[j], NULL );
97  }
98 
99  test_pass( __FILE__ );
100 
101  return 0;
102 }
void test_pass(const char *filename)
Definition: test_utils.c:432
#define PAPI_NULL
Definition: papi.h:292
int PAPI_register_thread(void)
Definition: papi.c:208
return PAPI_OK
Definition: linux-nvml.c:497
void
Definition: iozone.c:18627
int int argc
Definition: iozone.c:1609
Return codes and api definitions.
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:559
long long ret
Definition: iozone.c:1346
char ** argv
Definition: iozone.c:1610
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: pthrtough2.c:9
#define PAPI_ESYS
Definition: papi.h:255
pthread_t
Definition: iozone.c:18557
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
printf("\tTry: -i 0 -i 1 \n\n")
int PAPI_unregister_thread(void)
Definition: papi.c:244
int TESTS_QUIET
Definition: test_utils.c:18
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
static void * Thread(void *arg)
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:2014
long j
Definition: iozone.c:19135
#define PAPI_VER_CURRENT
Definition: papi.h:225
int main(int argc, char **argv)
List all appio events codes and names.
pthread_attr_t attr
Definition: iozone.c:18466