PAPI  5.3.2.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
examples/locks_pthreads.c
Go to the documentation of this file.
1 /****************************************************************************
2  * This program shows how to use PAPI_register_thread, PAPI_lock, *
3  * PAPI_unlock, PAPI_set_thr_specific, PAPI_get_thr_specific. *
4  * Warning: Don't use PAPI_lock and PAPI_unlock on platforms on which the *
5  * locking mechanisms are not implemented. *
6  ****************************************************************************/
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <pthread.h>
11 #include "papi.h" /* This needs to be included every time you use PAPI */
12 
13 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }
14 
15 #define LOOPS 100000
16 #define SLEEP_VALUE 20000
17 
18 int count;
19 int rank;
20 
21 void *Master(void *arg)
22 {
23  int i, retval, tmp;
24  int *pointer, * pointer2;
25 
26  tmp = 20;
27  pointer = &tmp;
28 
29  /* register the thread */
30  if ( (retval=PAPI_register_thread())!= PAPI_OK )
31  ERROR_RETURN(retval);
32 
33  /* save the pointer for late use */
34  if ( (retval=PAPI_set_thr_specific(1,pointer))!= PAPI_OK )
35  ERROR_RETURN(retval);
36  /* change the value of tmp */
37  tmp = 15;
38 
39  usleep(SLEEP_VALUE);
41  /* Make sure Slaves are not sleeping */
42  for (i = 0; i < LOOPS; i++) {
43  count = 2 * count - i;
44  }
46 
47  /* retrieve the pointer saved by PAPI_set_thr_specific */
48  if ( (retval=PAPI_get_thr_specific(1, (void *)&pointer2)) != PAPI_OK )
49  ERROR_RETURN(retval);
50 
51  /* the output value should be 15 */
52  printf("Thread specific data is %d \n", *pointer2);
53 
54  pthread_exit(NULL);
55 }
56 
57 void *Slave(void *arg)
58 {
59  int i;
60 
63  for (i = 0; i < LOOPS; i++) {
64  count += i;
65  }
68  pthread_exit(NULL);
69 }
70 
71 
72 
73 int main(int argc, char **argv)
74 {
75  pthread_t master;
76  pthread_t slave1;
77  int result_m, result_s, rc, i;
78  int retval;
79 
80  /* Setup a random number so compilers can't optimize it out */
81  count = rand();
82  result_m = count;
83  rank = 0;
84 
85  for (i = 0; i < LOOPS; i++) {
86  result_m = 2 * result_m - i;
87  }
88  result_s = result_m;
89 
90  for (i = 0; i < LOOPS; i++) {
91  result_s += i;
92  }
93 
95  {
96  printf("Library initialization error! \n");
97  exit(-1);
98  }
99 
100  if ((retval = PAPI_thread_init(&pthread_self)) != PAPI_OK)
101  ERROR_RETURN(retval);
102 
103  if ((retval = PAPI_set_debug(PAPI_VERB_ECONT)) != PAPI_OK)
104  ERROR_RETURN(retval);
105 
107  rc = pthread_create(&master, NULL, Master, NULL);
108  if (rc) {
109  retval = PAPI_ESYS;
110  ERROR_RETURN(retval);
111  }
112  rc = pthread_create(&slave1, NULL, Slave, NULL);
113  if (rc) {
114  retval = PAPI_ESYS;
115  ERROR_RETURN(retval);
116  }
117  pthread_join(master, NULL);
118  printf("Master: Expected: %d Recieved: %d\n", result_m, count);
119  if (result_m != count)
120  ERROR_RETURN(1);
122 
123  pthread_join(slave1, NULL);
124  printf("Slave: Expected: %d Recieved: %d\n", result_s, count);
125 
126  if (result_s != count)
127  ERROR_RETURN(1);
128 
129  exit(0);
130 }
void * Master(void *arg)
#define PAPI_VERB_ECONT
Definition: fpapi.h:39
int PAPI_register_thread(void)
Definition: papi.c:203
return PAPI_OK
Definition: linux-nvml.c:458
int count
Definition: iozone.c:22422
#define PAPI_USR2_LOCK
Definition: fpapi.h:83
#define LOOPS
#define printf
Definition: papi_test.h:125
int int argc
Definition: iozone.c:1609
Return codes and api definitions.
void * Slave(void *arg)
char ** argv
Definition: iozone.c:1610
int PAPI_get_thr_specific(int tag, void **ptr)
Definition: papi.c:357
#define PAPI_USR1_LOCK
Definition: fpapi.h:82
int PAPI_thread_init(unsigned long int(*id_fn)(void))
Definition: papi.c:118
int PAPI_library_init(int version)
Definition: papi.c:495
int i
Definition: fileop.c:140
#define ERROR_RETURN(retval)
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
pthread_t
Definition: iozone.c:18557
#define PAPI_ESYS
Definition: fpapi.h:108
int PAPI_set_thr_specific(int tag, void *ptr)
Definition: papi.c:433
int PAPI_unlock(int lck)
Definition: papi.c:6295
int rand()
int rc
Definition: iozone.c:20281
ssize_t retval
Definition: libasync.c:338
int PAPI_lock(int lck)
Definition: papi.c:6275
long long tmp
Definition: iozone.c:12031
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
int PAPI_set_debug(int level)
Definition: papi.c:3037
#define SLEEP_VALUE