PAPI 7.1.0.0
Loading...
Searching...
No Matches
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
19int rank;
20
21void *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 */
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}
56
57void *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
73int 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)
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}
double tmp
int i
Retrieve a pointer to a thread specific data structure.
initialize the PAPI library.
Lock one of two mutex variables defined in papi.h.
Notify PAPI that a thread has 'appeared'.
Set the current debug level for error output from PAPI.
Store a pointer to a thread specific data structure.
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)
#define SLEEP_VALUE
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
#define PAPI_USR1_LOCK
Definition: f90papi.h:197
Return codes and api definitions.
unsigned long int pthread_t
int main()
Definition: pernode.c:20
rc
Definition: pscanf.h:23
int retval
Definition: zero_fork.c:53