PAPI 7.1.0.0
Loading...
Searching...
No Matches
krentel_pthreads.c File Reference
Include dependency graph for krentel_pthreads.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 15 of file krentel_pthreads.c.

◆ MAX_THREADS

#define MAX_THREADS   256

Definition at line 5 of file krentel_pthreads.c.

Function Documentation

◆ do_cycles()

static void do_cycles ( long  num,
int  len 
)
static

Definition at line 71 of file krentel_pthreads.c.

72{
73 struct timeval start, now;
74 double x, sum;
75
76 gettimeofday( &start, NULL );
77
78 for ( ;; ) {
79 sum = 1.0;
80 for ( x = 1.0; x < 250000.0; x += 1.0 )
81 sum += x;
82 if ( sum < 0.0 )
83 printf( "==>> SUM IS NEGATIVE !! <<==\n" );
84
85 iter[num]++;
86
87 gettimeofday( &now, NULL );
88 if ( now.tv_sec >= start.tv_sec + len )
89 break;
90 }
91}
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 156 of file krentel_pthreads.c.

157{
158 pthread_t *td = NULL;
159 long n;
160 int quiet,retval;
161
162 /* Set TESTS_QUIET variable */
163 quiet=tests_quiet( argc, argv );
164
165 if ( argc < 2 || sscanf( argv[1], "%d", &program_time ) < 1 )
166 program_time = 6;
167 if ( argc < 3 || sscanf( argv[2], "%d", &threshold ) < 1 )
168 threshold = 20000000;
169 if ( argc < 4 || sscanf( argv[3], "%d", &num_threads ) < 1 )
170 num_threads = 3;
171
172 td = malloc((num_threads+1) * sizeof(pthread_t));
173 if (!td) {
174 test_fail( __FILE__, __LINE__, "td malloc failed", 1 );
175 }
176
177 if (!quiet) {
178 printf( "program_time = %d, threshold = %d, num_threads = %d\n\n",
180 }
181
183 test_fail( __FILE__, __LINE__, "PAPI_library_init failed", 1 );
184
185 /* Test to be sure we can add events */
187 if (retval!=PAPI_OK) {
188 if (!quiet) printf("Trouble finding event\n");
189 test_skip(__FILE__,__LINE__,"Event not available",1);
190 }
191
192 if ( PAPI_thread_init( ( unsigned long ( * )( void ) ) ( pthread_self ) ) !=
193 PAPI_OK )
194 test_fail( __FILE__, __LINE__, "PAPI_thread_init failed", 1 );
195
196 if ( pthread_key_create( &key, NULL ) != 0 )
197 test_fail( __FILE__, __LINE__, "pthread key create failed", 1 );
198
199 gettimeofday( &start, NULL );
200
201 for ( n = 1; n <= num_threads; n++ ) {
202 if ( pthread_create( &(td[n]), NULL, my_thread, ( void * ) n ) != 0 )
203 test_fail( __FILE__, __LINE__, "pthread create failed", 1 );
204 }
205
206 my_thread( ( void * ) 0 );
207
208 /* wait for all the threads */
209 for ( n = 1; n <= num_threads; n++ ) {
210 if ( pthread_join( td[n], NULL))
211 test_fail( __FILE__, __LINE__, "pthread join failed", 1 );
212 }
213
214 free(td);
215
216 if (!quiet) printf( "done\n" );
217
218 test_pass( __FILE__ );
219
220 return 0;
221}
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 30 of file krentel_pthreads.c.

31{
32 ( void ) EventSet;
33 ( void ) pc;
34 ( void ) ovec;
35 ( void ) context;
36
37 long num = ( long ) pthread_getspecific( key );
38
39 if ( num < 0 || num > num_threads )
40 test_fail( __FILE__, __LINE__, "getspecific failed", 1 );
41 count[num]++;
42}
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 94 of file krentel_pthreads.c.

95{
96 long num = ( long ) v;
97 int n;
98 int EventSet = PAPI_NULL;
99 long long value;
100
101 int retval;
102
104 if ( retval != PAPI_OK ) {
105 test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
106 }
107 pthread_setspecific( key, v );
108
109 count[num] = 0;
110 iter[num] = 0;
111 last[num] = start;
112
114 if ( retval != PAPI_OK ) {
115 test_fail( __FILE__, __LINE__, "PAPI_create_eventset failed", retval );
116 }
117
119 if (retval != PAPI_OK ) {
120 if (!TESTS_QUIET) printf("Trouble adding event\n");
121 test_fail( __FILE__, __LINE__, "PAPI_add_event failed", retval );
122 }
123
125 test_fail( __FILE__, __LINE__, "PAPI_overflow failed", 1 );
126
127 if ( PAPI_start( EventSet ) != PAPI_OK )
128 test_fail( __FILE__, __LINE__, "PAPI_start failed", 1 );
129
130 if (!TESTS_QUIET) printf( "launched timer in thread %ld\n", num );
131
132 for ( n = 1; n <= program_time; n++ ) {
133 do_cycles( num, 1 );
134 print_rate( num );
135 }
136
137 PAPI_stop( EventSet, &value );
138
140 if ( retval != PAPI_OK )
141 test_fail( __FILE__, __LINE__, "PAPI_overflow failed to reset the overflow handler", retval );
142
144 test_fail( __FILE__, __LINE__, "PAPI_remove_event", 1 );
145
147 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", 1 );
148
149 if ( PAPI_unregister_thread( ) != PAPI_OK )
150 test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", 1 );
151
152 return ( NULL );
153}
add PAPI preset or native hardware event to an event set
Create a new empty PAPI EventSet.
Empty and destroy an EventSet.
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 45 of file krentel_pthreads.c.

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

Variable Documentation

◆ count

long count[MAX_THREADS]
static

Definition at line 21 of file krentel_pthreads.c.

◆ iter

long iter[MAX_THREADS]
static

Definition at line 22 of file krentel_pthreads.c.

◆ key

pthread_key_t key
static

Definition at line 25 of file krentel_pthreads.c.

◆ last

struct timeval last[MAX_THREADS]
static

Definition at line 23 of file krentel_pthreads.c.

◆ num_threads

int num_threads = 3
static

Definition at line 19 of file krentel_pthreads.c.

◆ program_time

int program_time = 5
static

Definition at line 17 of file krentel_pthreads.c.

◆ start

struct timeval start
static

Definition at line 27 of file krentel_pthreads.c.

◆ threshold

int threshold = 20000000
static

Definition at line 18 of file krentel_pthreads.c.