PAPI  5.3.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
examples/locks_pthreads.c File Reference
Include dependency graph for examples/locks_pthreads.c:

Go to the source code of this file.

Macros

#define ERROR_RETURN(retval)   { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }
 
#define LOOPS   100000
 
#define SLEEP_VALUE   20000
 

Functions

voidMaster (void *arg)
 
voidSlave (void *arg)
 
int main (int argc, char **argv)
 

Variables

int count
 
int rank
 

Macro Definition Documentation

#define ERROR_RETURN (   retval)    { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }

Definition at line 13 of file examples/locks_pthreads.c.

#define LOOPS   100000

Definition at line 15 of file examples/locks_pthreads.c.

#define SLEEP_VALUE   20000

Definition at line 16 of file examples/locks_pthreads.c.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 73 of file examples/locks_pthreads.c.

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
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
void * Slave(void *arg)
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_unlock(int lck)
Definition: papi.c:6282
int rand()
int rc
Definition: iozone.c:20281
ssize_t retval
Definition: libasync.c:338
int PAPI_lock(int lck)
Definition: papi.c:6262
void exit()
int PAPI_set_debug(int level)
Definition: papi.c:3027

Here is the call graph for this function:

void* Master ( void arg)

Definition at line 21 of file examples/locks_pthreads.c.

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 }
int PAPI_register_thread(void)
Definition: papi.c:203
return PAPI_OK
Definition: linux-nvml.c:458
int count
Definition: iozone.c:22422
#define LOOPS
#define printf
Definition: papi_test.h:125
int PAPI_get_thr_specific(int tag, void **ptr)
Definition: papi.c:357
#define PAPI_USR1_LOCK
Definition: fpapi.h:82
int i
Definition: fileop.c:140
#define ERROR_RETURN(retval)
int PAPI_set_thr_specific(int tag, void *ptr)
Definition: papi.c:433
int PAPI_unlock(int lck)
Definition: papi.c:6282
ssize_t retval
Definition: libasync.c:338
int PAPI_lock(int lck)
Definition: papi.c:6262
long long tmp
Definition: iozone.c:12031
#define SLEEP_VALUE

Here is the call graph for this function:

Here is the caller graph for this function:

void* Slave ( void arg)

Definition at line 57 of file examples/locks_pthreads.c.

58 {
59  int i;
60 
63  for (i = 0; i < LOOPS; i++) {
64  count += i;
65  }
68  pthread_exit(NULL);
69 }
int count
Definition: iozone.c:22422
#define PAPI_USR2_LOCK
Definition: fpapi.h:83
#define LOOPS
#define PAPI_USR1_LOCK
Definition: fpapi.h:82
int i
Definition: fileop.c:140
int PAPI_unlock(int lck)
Definition: papi.c:6282
int PAPI_lock(int lck)
Definition: papi.c:6262

Here is the call graph for this function:

Variable Documentation

int count

Definition at line 18 of file examples/locks_pthreads.c.

int rank

Definition at line 19 of file examples/locks_pthreads.c.