PAPI 7.1.0.0
Loading...
Searching...
No Matches
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) {
111 }
112 rc = pthread_create(&slave1, NULL, Slave, NULL);
113 if (rc) {
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}
int i
initialize the PAPI library.
Lock one of two mutex variables defined in papi.h.
Set the current debug level for error output from PAPI.
Initialize thread support in the PAPI library.
Unlock one of the mutex variables defined in papi.h.
void * Master(void *arg)
#define ERROR_RETURN(retval)
void * Slave(void *arg)
#define LOOPS
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_USR2_LOCK
Definition: f90papi.h:187
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_VERB_ECONT
Definition: f90papi.h:164
#define PAPI_ESYS
Definition: f90papi.h:136
unsigned long int pthread_t
rc
Definition: pscanf.h:23
int retval
Definition: zero_fork.c:53
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}
double tmp
Retrieve a pointer to a thread specific data structure.
Notify PAPI that a thread has 'appeared'.
Store a pointer to a thread specific data structure.
#define SLEEP_VALUE
#define PAPI_USR1_LOCK
Definition: f90papi.h:197
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}
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.