PAPI  5.0.1.0
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 launch_timer (int *EventSet)
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.

{
    struct timeval start, now;
    double x, sum;

    gettimeofday( &start, NULL );

    for ( ;; ) {
        sum = 1.0;
        for ( x = 1.0; x < 250000.0; x += 1.0 )
            sum += x;
        if ( sum < 0.0 )
            printf( "==>>  SUM IS NEGATIVE !!  <<==\n" );

        iter[num]++;

        gettimeofday( &now, NULL );
        if ( now.tv_sec >= start.tv_sec + len )
            break;
    }
}
void launch_timer ( int *  EventSet)

Definition at line 86 of file krentel_pthreads.c.

{
    if ( PAPI_create_eventset( EventSet ) != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_create_eventset failed", 1 );

    if ( PAPI_add_event( *EventSet, EVENT ) != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_add_event failed", 1 );

    if ( PAPI_overflow( *EventSet, EVENT, threshold, 0, my_handler ) !=
         PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_overflow failed", 1 );

    if ( PAPI_start( *EventSet ) != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_start failed", 1 );
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char **  argv 
)

Definition at line 137 of file krentel_pthreads.c.

{
    pthread_t td;
    long n;

    tests_quiet( argc, argv );  /*Set TESTS_QUIET variable */

    if ( argc < 2 || sscanf( argv[1], "%d", &program_time ) < 1 )
        program_time = 6;
    if ( argc < 3 || sscanf( argv[2], "%d", &threshold ) < 1 )
        threshold = 20000000;
    if ( argc < 4 || sscanf( argv[3], "%d", &num_threads ) < 1 )
        num_threads = 3;

    printf( "program_time = %d, threshold = %d, num_threads = %d\n\n",
            program_time, threshold, num_threads );

    if ( PAPI_library_init( PAPI_VER_CURRENT ) != PAPI_VER_CURRENT )
        test_fail( __FILE__, __LINE__, "PAPI_library_init failed", 1 );

    if ( PAPI_thread_init( ( unsigned long ( * )( void ) ) ( pthread_self ) ) !=
         PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_thread_init failed", 1 );

    if ( pthread_key_create( &key, NULL ) != 0 )
        test_fail( __FILE__, __LINE__, "pthread key create failed", 1 );

    gettimeofday( &start, NULL );

    for ( n = 1; n <= num_threads; n++ ) {
        if ( pthread_create( &td, NULL, my_thread, ( void * ) n ) != 0 )
            test_fail( __FILE__, __LINE__, "pthread create failed", 1 );
    }

    my_thread( ( void * ) 0 );

    printf( "done\n" );

    test_pass( __FILE__, NULL, 0 );
    pthread_exit( NULL );
    return ( 0 );
}

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.

{
    ( void ) EventSet;
    ( void ) pc;
    ( void ) ovec;
    ( void ) context;

    long num = ( long ) pthread_getspecific( key );

    if ( num < 0 || num > num_threads )
        test_fail( __FILE__, __LINE__, "getspecific failed", 1 );
    count[num]++;
}

Here is the call graph for this function:

void* my_thread ( void *  v)

Definition at line 103 of file krentel_pthreads.c.

{
    long num = ( long ) v;
    int n;
    int EventSet = PAPI_NULL;
    long long value;

    int retval = PAPI_register_thread(  );
    if ( retval != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );
    pthread_setspecific( key, v );

    count[num] = 0;
    iter[num] = 0;
    last[num] = start;

    launch_timer( &EventSet );
    printf( "launched timer in thread %ld\n", num );

    for ( n = 1; n <= program_time; n++ ) {
        do_cycles( num, 1 );
        print_rate( num );
    }

    PAPI_stop( EventSet, &value );
    PAPI_remove_event( EventSet, EVENT );
    PAPI_destroy_eventset( &EventSet );
    retval = PAPI_unregister_thread(  );
    if ( retval != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", retval );
    return ( NULL );
}

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.

{
    struct timeval now;
    long st_secs;
    double last_secs;

    gettimeofday( &now, NULL );
    st_secs = now.tv_sec - start.tv_sec;
    last_secs = ( double ) ( now.tv_sec - last[num].tv_sec )
        + ( ( double ) ( now.tv_usec - last[num].tv_usec ) ) / 1000000.0;
    if ( last_secs <= 0.001 )
        last_secs = 0.001;

    printf( "[%ld] time = %ld, count = %ld, iter = %ld, "
            "rate = %.1f/Kiter\n",
            num, st_secs, count[num], iter[num],
            ( 1000.0 * ( double ) count[num] ) / ( double ) iter[num] );

    count[num] = 0;
    iter[num] = 0;
    last[num] = now;
}

Variable Documentation

Definition at line 15 of file krentel_pthreads.c.

Definition at line 16 of file krentel_pthreads.c.

Definition at line 19 of file krentel_pthreads.c.

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.

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines