PAPI 7.1.0.0
Loading...
Searching...
No Matches
smoke_tests/threads.c
Go to the documentation of this file.
1#include <stdlib.h>
2#include <unistd.h>
3#include <stdio.h>
4#include <pthread.h>
5#include <papi.h>
6#define NUM_PTHREADS 2
7#define NUM_EVENTS 2
8
9void *
10Thread(void *arg)
11{
12 int retval, i;
13 long long values[NUM_EVENTS];
14 int EventSet = PAPI_NULL;
15 int events[NUM_EVENTS];
16 char *EventName[] = { "PAPI_TOT_CYC", "PAPI_TOT_INS" };
17
18 int thread;
19 thread = *(int *) arg;
20
22 if ( retval != PAPI_OK ) {
23 printf("ERROR: PAPI_register_thread: %d: %s\n", retval, PAPI_strerror(retval));
24 exit(EXIT_FAILURE);
25 }
26
27 for( i = 0; i < NUM_EVENTS; i++ ) {
28 retval = PAPI_event_name_to_code( EventName[i], &events[i] );
29 if ( retval != PAPI_OK ) {
30 printf("ERROR: PAPI_event_name_to_code: %d: %s\n", retval, PAPI_strerror(retval));
31 exit(EXIT_FAILURE);
32 }
33 }
34
36 if ( retval != PAPI_OK ) {
37 printf("ERROR: PAPI_create_eventset: %d: %s\n", retval, PAPI_strerror(retval));
38 exit(EXIT_FAILURE);
39 }
40
42 if ( retval != PAPI_OK ) {
43 printf("ERROR: PAPI_add_events: %d: %s\n", retval, PAPI_strerror(retval));
44 exit(EXIT_FAILURE);
45 }
46
48 if ( retval != PAPI_OK ) {
49 printf("ERROR: PAPI_start: %d: %s\n", retval, PAPI_strerror(retval));
50 exit(EXIT_FAILURE);
51 }
52
53 // do work
54 sleep(3);
55
57 if ( retval != PAPI_OK ) {
58 printf("ERROR: PAPI_stop: %d: %s\n", retval, PAPI_strerror(retval));
59 exit(EXIT_FAILURE);
60 }
61
62 for( i = 0; i < NUM_EVENTS; i++ ) {
63 printf( "%12lld \t\t --> %s (thread %d) \n", values[i],
64 EventName[i], thread );
65 }
66
68 if ( retval != PAPI_OK ) {
69 printf("ERROR: PAPI_unregister_thread: %d: %s\n", retval, PAPI_strerror(retval));
70 exit(EXIT_FAILURE);
71 }
72
73 return 0;
74}
75
76
77int main( int argc, char **argv )
78{
80 int i, vals[NUM_PTHREADS];
81 int retval, rc;
82 void* retval2;
83
84 /* Init PAPI library */
86 if ( retval != PAPI_VER_CURRENT ) {
87 printf("ERROR: PAPI_library_init: %d: %s\n", retval, PAPI_strerror(retval) );
88 exit(EXIT_FAILURE);
89 } else {
90 printf ( "PAPI_VERSION : %4d %6d %7d\n",
94 }
95
96 retval = PAPI_thread_init( ( unsigned long ( * )( void ) )( pthread_self ) );
97 if ( retval != PAPI_OK ) {
98 printf("ERROR: PAPI_thread_init: %d: %s\n", retval, PAPI_strerror(retval) );
99 exit(EXIT_FAILURE);
100 }
101
102 for ( i = 0; i < NUM_PTHREADS; i++) {
103 vals[i] = i;
104 retval = pthread_create( &tids[i], NULL, Thread, &vals[i] );
105 if ( retval != 0 ) {
106 printf("ERROR: pthread_create: %d\n", retval );
107 exit(EXIT_FAILURE);
108 }
109 }
110
111 for ( i = 0; i < NUM_PTHREADS; i++) {
112 printf("Trying to join with tid %d\n", i);
113 retval = pthread_join(tids[i], &retval2);
114 if ( retval != 0 ) {
115 printf("ERROR: pthread_join: %d\n", retval );
116 exit(EXIT_FAILURE);
117 } else {
118 printf("Joined with tid %d\n", i);
119 }
120 }
121
123 return EXIT_SUCCESS;
124}
int i
add multiple PAPI presets or native hardware events to an event set
Create a new empty PAPI EventSet.
Convert a name to a numeric hardware event code.
initialize the PAPI library.
Notify PAPI that a thread has 'appeared'.
Finish using PAPI and free all related resources.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
Returns a string describing the PAPI error code.
Initialize thread support in the PAPI library.
Notify PAPI that a thread has 'disappeared'.
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_VERSION
Definition: f90papi.h:193
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_NULL
Definition: f90papi.h:78
char events[MAX_EVENTS][BUFSIZ]
static int EventSet
Definition: init_fini.c:8
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
void * thread(void *arg)
Definition: kufrin.c:38
Return codes and api definitions.
#define PAPI_VERSION_REVISION(x)
Definition: papi.h:221
#define PAPI_VERSION_MAJOR(x)
Definition: papi.h:219
#define PAPI_VERSION_MINOR(x)
Definition: papi.h:220
unsigned long int pthread_t
int main()
Definition: pernode.c:20
rc
Definition: pscanf.h:23
#define NUM_PTHREADS
void * Thread(void *arg)
#define NUM_EVENTS
int retval
Definition: zero_fork.c:53