krentel_pthreads.c File Reference

Include dependency graph for krentel_pthreads.c:

Go to the source code of this file.

Defines

#define EVENT   PAPI_TOT_CYC

Functions

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

Variables

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

Define Documentation

#define EVENT   PAPI_TOT_CYC

Definition at line 9 of file krentel_pthreads.c.


Function Documentation

void do_cycles ( long  num,
int  len 
)

Definition at line 63 of file krentel_pthreads.c.

00064 {
00065     struct timeval start, now;
00066     double x, sum;
00067 
00068     gettimeofday( &start, NULL );
00069 
00070     for ( ;; ) {
00071         sum = 1.0;
00072         for ( x = 1.0; x < 250000.0; x += 1.0 )
00073             sum += x;
00074         if ( sum < 0.0 )
00075             printf( "==>>  SUM IS NEGATIVE !!  <<==\n" );
00076 
00077         iter[num]++;
00078 
00079         gettimeofday( &now, NULL );
00080         if ( now.tv_sec >= start.tv_sec + len )
00081             break;
00082     }
00083 }

Here is the call graph for this function:

int main ( int  argc,
char **  argv 
)

Definition at line 140 of file krentel_pthreads.c.

00141 {
00142     pthread_t *td = NULL;
00143     long n;
00144 
00145     tests_quiet( argc, argv );  /*Set TESTS_QUIET variable */
00146 
00147     if ( argc < 2 || sscanf( argv[1], "%d", &program_time ) < 1 )
00148         program_time = 6;
00149     if ( argc < 3 || sscanf( argv[2], "%d", &threshold ) < 1 )
00150         threshold = 20000000;
00151     if ( argc < 4 || sscanf( argv[3], "%d", &num_threads ) < 1 )
00152         num_threads = 3;
00153 
00154     td = malloc((num_threads+1) * sizeof(pthread_t));
00155     if (!td)
00156         test_fail( __FILE__, __LINE__, "td malloc failed", 1 );
00157 
00158     printf( "program_time = %d, threshold = %d, num_threads = %d\n\n",
00159             program_time, threshold, num_threads );
00160 
00161     if ( PAPI_library_init( PAPI_VER_CURRENT ) != PAPI_VER_CURRENT )
00162         test_fail( __FILE__, __LINE__, "PAPI_library_init failed", 1 );
00163 
00164     if ( PAPI_thread_init( ( unsigned long ( * )( void ) ) ( pthread_self ) ) !=
00165          PAPI_OK )
00166         test_fail( __FILE__, __LINE__, "PAPI_thread_init failed", 1 );
00167 
00168     if ( pthread_key_create( &key, NULL ) != 0 )
00169         test_fail( __FILE__, __LINE__, "pthread key create failed", 1 );
00170 
00171     gettimeofday( &start, NULL );
00172 
00173     for ( n = 1; n <= num_threads; n++ ) {
00174         if ( pthread_create( &(td[n]), NULL, my_thread, ( void * ) n ) != 0 )
00175             test_fail( __FILE__, __LINE__, "pthread create failed", 1 );
00176     }
00177 
00178     my_thread( ( void * ) 0 );
00179 
00180     /* wait for all the threads */
00181     for ( n = 1; n <= num_threads; n++ ) {
00182         if ( pthread_join( td[n], NULL))
00183             test_fail( __FILE__, __LINE__, "pthread join failed", 1 );
00184     }
00185 
00186     free(td);
00187 
00188     printf( "done\n" );
00189 
00190     test_pass( __FILE__, NULL, 0 );
00191     return ( 0 );
00192 }

Here is the call graph for this function:

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

Definition at line 24 of file krentel_pthreads.c.

00025 {
00026     ( void ) EventSet;
00027     ( void ) pc;
00028     ( void ) ovec;
00029     ( void ) context;
00030 
00031     long num = ( long ) pthread_getspecific( key );
00032 
00033     if ( num < 0 || num > num_threads )
00034         test_fail( __FILE__, __LINE__, "getspecific failed", 1 );
00035     count[num]++;
00036 }

Here is the call graph for this function:

void* my_thread ( void *  v  ) 

Definition at line 86 of file krentel_pthreads.c.

00087 {
00088     long num = ( long ) v;
00089     int n;
00090     int EventSet = PAPI_NULL;
00091     long long value;
00092 
00093     int retval = PAPI_register_thread(  );
00094     if ( retval != PAPI_OK )
00095         test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
00096     pthread_setspecific( key, v );
00097 
00098     count[num] = 0;
00099     iter[num] = 0;
00100     last[num] = start;
00101         
00102     if ( PAPI_create_eventset( &EventSet ) != PAPI_OK )
00103         test_fail( __FILE__, __LINE__, "PAPI_create_eventset failed", 1 );
00104 
00105     if ( PAPI_add_event( EventSet, EVENT ) != PAPI_OK )
00106         test_fail( __FILE__, __LINE__, "PAPI_add_event failed", 1 );
00107 
00108     if ( PAPI_overflow( EventSet, EVENT, threshold, 0, my_handler ) != PAPI_OK )
00109         test_fail( __FILE__, __LINE__, "PAPI_overflow failed", 1 );
00110 
00111     if ( PAPI_start( EventSet ) != PAPI_OK )
00112         test_fail( __FILE__, __LINE__, "PAPI_start failed", 1 );
00113 
00114     printf( "launched timer in thread %ld\n", num );
00115 
00116     for ( n = 1; n <= program_time; n++ ) {
00117         do_cycles( num, 1 );
00118         print_rate( num );
00119     }
00120 
00121     PAPI_stop( EventSet, &value );
00122 
00123         retval = PAPI_overflow( EventSet, EVENT, 0, 0, my_handler);
00124     if ( retval != PAPI_OK )
00125             test_fail( __FILE__, __LINE__, "PAPI_overflow failed to reset the overflow handler", retval );
00126 
00127     if ( PAPI_remove_event( EventSet, EVENT ) != PAPI_OK ) 
00128         test_fail( __FILE__, __LINE__, "PAPI_remove_event", 1 );
00129 
00130     if ( PAPI_destroy_eventset( &EventSet ) != PAPI_OK ) 
00131         test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", 1 );
00132 
00133     if ( PAPI_unregister_thread( ) != PAPI_OK ) 
00134             test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", 1 );
00135 
00136     return ( NULL );
00137 }

Here is the call graph for this function:

Here is the caller graph for this function:

void print_rate ( long  num  ) 

Definition at line 39 of file krentel_pthreads.c.

00040 {
00041     struct timeval now;
00042     long st_secs;
00043     double last_secs;
00044 
00045     gettimeofday( &now, NULL );
00046     st_secs = now.tv_sec - start.tv_sec;
00047     last_secs = ( double ) ( now.tv_sec - last[num].tv_sec )
00048         + ( ( double ) ( now.tv_usec - last[num].tv_usec ) ) / 1000000.0;
00049     if ( last_secs <= 0.001 )
00050         last_secs = 0.001;
00051 
00052     printf( "[%ld] time = %ld, count = %ld, iter = %ld, "
00053             "rate = %.1f/Kiter\n",
00054             num, st_secs, count[num], iter[num],
00055             ( 1000.0 * ( double ) count[num] ) / ( double ) iter[num] );
00056 
00057     count[num] = 0;
00058     iter[num] = 0;
00059     last[num] = now;
00060 }

Here is the call graph for this function:


Variable Documentation

long count[MAX_THREADS]

Definition at line 15 of file krentel_pthreads.c.

long iter[MAX_THREADS]

Definition at line 16 of file krentel_pthreads.c.

pthread_key_t key

Definition at line 19 of file krentel_pthreads.c.

struct timeval last[MAX_THREADS]

Definition at line 17 of file krentel_pthreads.c.

int num_threads = 3

Definition at line 13 of file krentel_pthreads.c.

int program_time = 5

Definition at line 11 of file krentel_pthreads.c.

struct timeval start

Definition at line 21 of file krentel_pthreads.c.

int threshold = 20000000

Definition at line 12 of file krentel_pthreads.c.


Generated on 26 Jan 2016 for PAPI by  doxygen 1.6.1