PAPI  5.0.1.0
overflow_pthreads.c File Reference
Include dependency graph for examples/overflow_pthreads.c:

Go to the source code of this file.

Defines

#define THRESHOLD   200000
#define OVER_FMT   "handler(%d ) Overflow at %p! bit=0x%llx \n"
#define ERROR_RETURN(retval)   { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }

Functions

void do_flops (int n)
void handler (int EventSet, void *address, long long overflow_vector, void *context)
void * Thread (void *arg)
int main (int argc, char **argv)

Variables

int total = 0

Define Documentation

#define ERROR_RETURN (   retval)    { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }

Definition at line 26 of file examples/overflow_pthreads.c.

#define OVER_FMT   "handler(%d ) Overflow at %p! bit=0x%llx \n"

Definition at line 25 of file examples/overflow_pthreads.c.

#define THRESHOLD   200000

Definition at line 24 of file examples/overflow_pthreads.c.


Function Documentation

void do_flops ( int  n)

Definition at line 31 of file examples/overflow_pthreads.c.

{
    int i;
    double c = 0.11;
    double a = 0.5;
    double b = 6.2;

    for (i=0; i < n; i++) 
        c += a * b;
}
void handler ( int  EventSet,
void *  address,
long long  overflow_vector,
void *  context 
)

Definition at line 44 of file examples/overflow_pthreads.c.

{
   fprintf(stderr, OVER_FMT, EventSet, address, overflow_vector);
   total++;
}
int main ( int  argc,
char **  argv 
)

Definition at line 120 of file examples/overflow_pthreads.c.

{
    pthread_t thread_one;
    pthread_t thread_two;
    int flops1, flops2;
    int rc,retval;
    pthread_attr_t attr;
    long long elapsed_us, elapsed_cyc;


    /* papi library initialization */
    if ((retval=PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT)
    {
        printf("Library initialization error! \n");
        exit(1);
    }

    /* thread initialization */
    retval=PAPI_thread_init((unsigned long(*)(void))(pthread_self));
    if (retval != PAPI_OK)
        ERROR_RETURN(retval);

    /* return the number of microseconds since some arbitrary starting point */
    elapsed_us = PAPI_get_real_usec();

    /* return the number of cycles since some arbitrary starting point */
    elapsed_cyc = PAPI_get_real_cyc();

    /* pthread attribution init */
    pthread_attr_init(&attr);
    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

    /* create the first thread */
    flops1 = 1000000;
    rc = pthread_create(&thread_one, &attr, Thread, (void *)&flops1);
    if (rc)
        ERROR_RETURN(rc);

    /* create the second thread */
    flops2 = 4000000;
    rc = pthread_create(&thread_two, &attr, Thread, (void *)&flops2);
    if (rc)
        ERROR_RETURN(rc);

    /* wait for the threads to finish */
    pthread_attr_destroy(&attr);
    pthread_join(thread_one, NULL); 
    pthread_join(thread_two, NULL);

    /* compute the elapsed cycles and microseconds */
    elapsed_cyc = PAPI_get_real_cyc() - elapsed_cyc;

    elapsed_us = PAPI_get_real_usec() - elapsed_us;

    printf("Master real usec   : \t%lld\n", elapsed_us);
    printf("Master real cycles : \t%lld\n", elapsed_cyc);

    /* clean up */
    PAPI_shutdown();
    exit(0);
}

Here is the call graph for this function:

void* Thread ( void *  arg)

Definition at line 50 of file examples/overflow_pthreads.c.

{
    int retval;
    int EventSet1=PAPI_NULL;
    long long values[2];
    long long elapsed_us, elapsed_cyc;
  
    fprintf(stderr,"Thread %lx running PAPI\n",PAPI_thread_id());

    /* create the event set */
    if ( (retval = PAPI_create_eventset(&EventSet1))!=PAPI_OK)
        ERROR_RETURN(retval);

    /* query whether the event exists */
    if ((retval=PAPI_query_event(PAPI_TOT_INS)) != PAPI_OK) 
        ERROR_RETURN(retval);
    if ((retval=PAPI_query_event(PAPI_TOT_CYC)) != PAPI_OK) 
        ERROR_RETURN(retval);

    /* add events to the event set */
    if ( (retval = PAPI_add_event(EventSet1, PAPI_TOT_INS))!= PAPI_OK)
        ERROR_RETURN(retval);

    if ( (retval = PAPI_add_event(EventSet1, PAPI_TOT_CYC)) != PAPI_OK)
        ERROR_RETURN(retval);

    elapsed_us = PAPI_get_real_usec();

    elapsed_cyc = PAPI_get_real_cyc();

    retval = PAPI_overflow(EventSet1, PAPI_TOT_CYC, THRESHOLD, 0, handler);
    if(retval !=PAPI_OK)
        ERROR_RETURN(retval);

    /* start counting */
    if((retval = PAPI_start(EventSet1))!=PAPI_OK)
        ERROR_RETURN(retval);

    do_flops(*(int *)arg);
  
    if ((retval = PAPI_stop(EventSet1, values))!=PAPI_OK)
        ERROR_RETURN(retval);

    elapsed_us = PAPI_get_real_usec() - elapsed_us;

    elapsed_cyc = PAPI_get_real_cyc() - elapsed_cyc;

    /* disable overflowing */
    retval = PAPI_overflow(EventSet1, PAPI_TOT_CYC, 0, 0, handler);
    if(retval !=PAPI_OK)
        ERROR_RETURN(retval);

    /* remove the event from the eventset */
    retval = PAPI_remove_event(EventSet1, PAPI_TOT_INS);
    if (retval != PAPI_OK)
        ERROR_RETURN(retval);

    retval = PAPI_remove_event(EventSet1, PAPI_TOT_CYC);
    if (retval != PAPI_OK)
        ERROR_RETURN(retval);

    printf("Thread 0x%x PAPI_TOT_INS : \t%lld\n",(int)PAPI_thread_id(),
     values[0]);
    printf("            PAPI_TOT_CYC: \t%lld\n", values[1]);
    printf("            Real usec   : \t%lld\n", elapsed_us);
    printf("            Real cycles : \t%lld\n", elapsed_cyc);

    pthread_exit(NULL);
}

Here is the call graph for this function:


Variable Documentation

int total = 0

Definition at line 29 of file examples/overflow_pthreads.c.

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines