PAPI  5.7.0.0
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

void * Master (void *arg)
 
void * Slave (void *arg)
 
int main (int argc, char **argv)
 

Variables

int count
 
int rank
 

Macro Definition Documentation

◆ ERROR_RETURN

#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.

◆ LOOPS

#define LOOPS   100000

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

◆ SLEEP_VALUE

#define SLEEP_VALUE   20000

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

Function Documentation

◆ main()

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)
102 
105 
107  rc = pthread_create(&master, NULL, Master, NULL);
108  if (rc) {
109  retval = PAPI_ESYS;
111  }
112  rc = pthread_create(&slave1, NULL, Slave, NULL);
113  if (rc) {
114  retval = PAPI_ESYS;
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 }
#define PAPI_OK
Definition: fpapi.h:105
rc
Definition: pscanf.h:23
unsigned long int pthread_t
void * Master(void *arg)
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
int retval
Definition: zero_fork.c:53
#define LOOPS
#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 ERROR_RETURN(retval)
#define PAPI_VERB_ECONT
Definition: fpapi.h:39
#define PAPI_USR2_LOCK
Definition: fpapi.h:83
void * Slave(void *arg)
int PAPI_unlock(int lck)
Definition: papi.c:6421
int rand()
int PAPI_lock(int lck)
Definition: papi.c:6401
void exit()
int PAPI_set_debug(int level)
Definition: papi.c:3126
int i
Definition: fileop.c:140
Here is the call graph for this function:

◆ Master()

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 */
32 
33  /* save the pointer for late use */
34  if ( (retval=PAPI_set_thr_specific(1,pointer))!= PAPI_OK )
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 )
50 
51  /* the output value should be 15 */
52  printf("Thread specific data is %d \n", *pointer2);
53 
54  pthread_exit(NULL);
55 }
#define PAPI_OK
Definition: fpapi.h:105
int PAPI_register_thread(void)
Definition: papi.c:208
int retval
Definition: zero_fork.c:53
#define LOOPS
double tmp
int PAPI_get_thr_specific(int tag, void **ptr)
Definition: papi.c:362
#define ERROR_RETURN(retval)
#define PAPI_USR1_LOCK
Definition: fpapi.h:82
int PAPI_set_thr_specific(int tag, void *ptr)
Definition: papi.c:438
int PAPI_unlock(int lck)
Definition: papi.c:6421
int PAPI_lock(int lck)
Definition: papi.c:6401
#define SLEEP_VALUE
int i
Definition: fileop.c:140
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Slave()

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 }
#define LOOPS
#define PAPI_USR1_LOCK
Definition: fpapi.h:82
#define PAPI_USR2_LOCK
Definition: fpapi.h:83
int PAPI_unlock(int lck)
Definition: papi.c:6421
int PAPI_lock(int lck)
Definition: papi.c:6401
int i
Definition: fileop.c:140
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ count

int count

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

◆ rank

int rank

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