PAPI 7.1.0.0
Loading...
Searching...
No Matches
krentel_pthreads_race.c File Reference
Include dependency graph for krentel_pthreads_race.c:

Go to the source code of this file.

Macros

#define MAX_THREADS   256
 
#define EVENT   PAPI_TOT_CYC
 

Functions

static void my_handler (int EventSet, void *pc, long long ovec, void *context)
 
static void print_rate (long num)
 
static void do_cycles (long num, int len)
 
static void * my_thread (void *v)
 
int main (int argc, char **argv)
 

Variables

static int program_time = 5
 
static int threshold = 20000000
 
static int num_threads = 3
 
static long count [MAX_THREADS]
 
static long iter [MAX_THREADS]
 
static struct timeval last [MAX_THREADS]
 
static pthread_key_t key
 
static struct timeval start
 

Macro Definition Documentation

◆ EVENT

#define EVENT   PAPI_TOT_CYC

Definition at line 23 of file krentel_pthreads_race.c.

◆ MAX_THREADS

#define MAX_THREADS   256

Definition at line 13 of file krentel_pthreads_race.c.

Function Documentation

◆ do_cycles()

static void do_cycles ( long  num,
int  len 
)
static

Definition at line 79 of file krentel_pthreads_race.c.

80{
81 struct timeval start, now;
82 double x, sum;
83
84 gettimeofday( &start, NULL );
85
86 for ( ;; ) {
87 sum = 1.0;
88 for ( x = 1.0; x < 250000.0; x += 1.0 )
89 sum += x;
90 if ( sum < 0.0 )
91 printf( "==>> SUM IS NEGATIVE !! <<==\n" );
92
93 iter[num]++;
94
95 gettimeofday( &now, NULL );
96 if ( now.tv_sec >= start.tv_sec + len )
97 break;
98 }
99}
static long iter[MAX_THREADS]
static struct timeval start
__time_t tv_sec
volatile double x
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 171 of file krentel_pthreads_race.c.

172{
173 pthread_t *td = NULL;
174 long n;
175 int quiet,retval;
176
177 /* Set TESTS_QUIET variable */
178 quiet=tests_quiet( argc, argv );
179
180 if ( argc < 2 || sscanf( argv[1], "%d", &program_time ) < 1 )
181 program_time = 6;
182 if ( argc < 3 || sscanf( argv[2], "%d", &threshold ) < 1 )
183 threshold = 20000000;
184 if ( argc < 4 || sscanf( argv[3], "%d", &num_threads ) < 1 )
185 num_threads = 32;
186
187 td = malloc((num_threads+1) * sizeof(pthread_t));
188 if (!td) {
189 test_fail( __FILE__, __LINE__, "td malloc failed", 1 );
190 }
191
192 if (!quiet) {
193 printf( "program_time = %d, threshold = %d, num_threads = %d\n\n",
195 }
196
198 test_fail( __FILE__, __LINE__, "PAPI_library_init failed", 1 );
199
200 /* Test to be sure we can add events */
202 if (retval!=PAPI_OK) {
203 if (!quiet) printf("Trouble finding event\n");
204 test_skip(__FILE__,__LINE__,"Event not available",1);
205 }
206
207 if ( PAPI_thread_init( ( unsigned long ( * )( void ) ) ( pthread_self ) ) !=
208 PAPI_OK )
209 test_fail( __FILE__, __LINE__, "PAPI_thread_init failed", 1 );
210
211 if ( pthread_key_create( &key, NULL ) != 0 )
212 test_fail( __FILE__, __LINE__, "pthread key create failed", 1 );
213
214 gettimeofday( &start, NULL );
215
216 for ( n = 1; n <= num_threads; n++ ) {
217 if ( pthread_create( &(td[n]), NULL, my_thread, ( void * ) n ) != 0 )
218 test_fail( __FILE__, __LINE__, "pthread create failed", 1 );
219 }
220
221 my_thread( ( void * ) 0 );
222
223 /* wait for all the threads */
224 for ( n = 1; n <= num_threads; n++ ) {
225 if ( pthread_join( td[n], NULL))
226 test_fail( __FILE__, __LINE__, "pthread join failed", 1 );
227 }
228
229 free(td);
230
231 if (!quiet) printf( "done\n" );
232
233 test_pass( __FILE__ );
234
235 return 0;
236}
initialize the PAPI library.
Query if PAPI event exists.
Initialize thread support in the PAPI library.
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_OK
Definition: f90papi.h:73
static int num_threads
static void * my_thread(void *v)
static int threshold
#define EVENT
static int program_time
static pthread_key_t key
unsigned long int pthread_t
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
void PAPI_NORETURN test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:491
void PAPI_NORETURN test_pass(const char *filename)
Definition: test_utils.c:432
void PAPI_NORETURN test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:584
int quiet
Definition: rapl_overflow.c:19
int retval
Definition: zero_fork.c:53
Here is the call graph for this function:

◆ my_handler()

static void my_handler ( int  EventSet,
void *  pc,
long long  ovec,
void *  context 
)
static

Definition at line 38 of file krentel_pthreads_race.c.

39{
40 ( void ) EventSet;
41 ( void ) pc;
42 ( void ) ovec;
43 ( void ) context;
44
45 long num = ( long ) pthread_getspecific( key );
46
47 if ( num < 0 || num > num_threads )
48 test_fail( __FILE__, __LINE__, "getspecific failed", 1 );
49 count[num]++;
50}
static int EventSet
Definition: init_fini.c:8
static long count[MAX_THREADS]
long long int long long
Definition: sde_internal.h:85
Here is the call graph for this function:
Here is the caller graph for this function:

◆ my_thread()

static void * my_thread ( void *  v)
static

Definition at line 102 of file krentel_pthreads_race.c.

103{
104 long num = ( long ) v;
105 int n;
106 int EventSet = PAPI_NULL;
107 int event_code;
108 long long value;
109
110 int retval;
111
113 if ( retval != PAPI_OK ) {
114 test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
115 }
116 pthread_setspecific( key, v );
117
118 count[num] = 0;
119 iter[num] = 0;
120 last[num] = start;
121
123 if ( retval != PAPI_OK ) {
124 test_fail( __FILE__, __LINE__, "PAPI_create_eventset failed", retval );
125 }
126
127 retval = PAPI_event_name_to_code("PAPI_TOT_CYC", &event_code);
128 if (retval != PAPI_OK ) {
129 if (!TESTS_QUIET) printf("Trouble creating event name\n");
130 test_fail( __FILE__, __LINE__, "PAPI_event_name_to_code failed", retval );
131 }
132
134 if (retval != PAPI_OK ) {
135 if (!TESTS_QUIET) printf("Trouble adding event\n");
136 test_fail( __FILE__, __LINE__, "PAPI_add_event failed", retval );
137 }
138
140 test_fail( __FILE__, __LINE__, "PAPI_overflow failed", 1 );
141
142 if ( PAPI_start( EventSet ) != PAPI_OK )
143 test_fail( __FILE__, __LINE__, "PAPI_start failed", 1 );
144
145 if (!TESTS_QUIET) printf( "launched timer in thread %ld\n", num );
146
147 for ( n = 1; n <= program_time; n++ ) {
148 do_cycles( num, 1 );
149 print_rate( num );
150 }
151
152 PAPI_stop( EventSet, &value );
153
155 if ( retval != PAPI_OK )
156 test_fail( __FILE__, __LINE__, "PAPI_overflow failed to reset the overflow handler", retval );
157
159 test_fail( __FILE__, __LINE__, "PAPI_remove_event", 1 );
160
162 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", 1 );
163
164 if ( PAPI_unregister_thread( ) != PAPI_OK )
165 test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", 1 );
166
167 return ( NULL );
168}
add PAPI preset or native hardware event to an event set
Create a new empty PAPI EventSet.
Empty and destroy an EventSet.
Convert a name to a numeric hardware event code.
Set up an event set to begin registering overflows.
Notify PAPI that a thread has 'appeared'.
removes a hardware event from a PAPI event set.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
Notify PAPI that a thread has 'disappeared'.
#define PAPI_NULL
Definition: f90papi.h:78
static struct timeval last[MAX_THREADS]
static void print_rate(long num)
static void my_handler(int EventSet, void *pc, long long ovec, void *context)
static void do_cycles(long num, int len)
int TESTS_QUIET
Definition: test_utils.c:18
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_rate()

static void print_rate ( long  num)
static

Definition at line 53 of file krentel_pthreads_race.c.

54{
55 struct timeval now;
56 long st_secs;
57 double last_secs;
58
59 gettimeofday( &now, NULL );
60 st_secs = now.tv_sec - start.tv_sec;
61 last_secs = ( double ) ( now.tv_sec - last[num].tv_sec )
62 + ( ( double ) ( now.tv_usec - last[num].tv_usec ) ) / 1000000.0;
63 if ( last_secs <= 0.001 )
64 last_secs = 0.001;
65
66 if (!TESTS_QUIET) {
67 printf( "[%ld] time = %ld, count = %ld, iter = %ld, "
68 "rate = %.1f/Kiter\n",
69 num, st_secs, count[num], iter[num],
70 ( 1000.0 * ( double ) count[num] ) / ( double ) iter[num] );
71 }
72
73 count[num] = 0;
74 iter[num] = 0;
75 last[num] = now;
76}
__suseconds_t tv_usec
Here is the caller graph for this function:

Variable Documentation

◆ count

long count[MAX_THREADS]
static

Definition at line 29 of file krentel_pthreads_race.c.

◆ iter

long iter[MAX_THREADS]
static

Definition at line 30 of file krentel_pthreads_race.c.

◆ key

pthread_key_t key
static

Definition at line 33 of file krentel_pthreads_race.c.

◆ last

struct timeval last[MAX_THREADS]
static

Definition at line 31 of file krentel_pthreads_race.c.

◆ num_threads

int num_threads = 3
static

Definition at line 27 of file krentel_pthreads_race.c.

◆ program_time

int program_time = 5
static

Definition at line 25 of file krentel_pthreads_race.c.

◆ start

struct timeval start
static

Definition at line 35 of file krentel_pthreads_race.c.

◆ threshold

int threshold = 20000000
static

Definition at line 26 of file krentel_pthreads_race.c.