PAPI  5.0.1.0
cycle_ratio.c File Reference
Include dependency graph for cycle_ratio.c:

Go to the source code of this file.

Functions

static void work (int EventSet, int mhz)
int main (int argc, char **argv)

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 27 of file cycle_ratio.c.

{
    int retval;
    int EventSet = PAPI_NULL;
    int numflops = NUM_FLOPS;
    const PAPI_hw_info_t *hwinfo = NULL;
    long long elapsed_cyc;
    long long values[2];
    int mhz;

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

    /* Init the PAPI library */
    retval = PAPI_library_init( PAPI_VER_CURRENT );
    if ( retval != PAPI_VER_CURRENT ) {
       test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
    }

    retval = PAPI_query_named_event("PAPI_REF_CYC");
    if (PAPI_OK!=retval) {
        test_skip( __FILE__, __LINE__, "PAPI_REF_CYC is not defined on this platform.", PAPI_OK );
    }

    /* create an eventset */
    retval = PAPI_create_eventset( &EventSet );
    if ( retval != PAPI_OK ) {
        test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
  }

    /* add core cycle event */
    retval = PAPI_add_named_event( EventSet, "PAPI_TOT_CYC");
    if ( retval != PAPI_OK ) {
        test_fail( __FILE__, __LINE__, "PAPI_add_named_event: PAPI_TOT_CYC", retval );
  }

    /* add ref cycle event */
    retval = PAPI_add_named_event( EventSet, "PAPI_REF_CYC");
    if ( retval != PAPI_OK ) {
        test_fail( __FILE__, __LINE__, "PAPI_add_events: PAPI_REF_CYC", retval );
  }
  
    retval = papi_print_header
    ( "Test case CycleRatio.c: Compute the ratio of TOT and REF cycles.\n",
      &hwinfo );
    if ( retval != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 2 );
    
    /* compute a nominal bus clock frequency */
    retval = PAPI_start( EventSet );
    if ( retval != PAPI_OK ) {
       test_fail( __FILE__, __LINE__, "PAPI_start", retval );
    }
    
    elapsed_cyc = PAPI_get_real_cyc(  );
    usleep(1000000);
    elapsed_cyc = PAPI_get_real_cyc(  ) - elapsed_cyc;
    mhz = elapsed_cyc / 1000000;
    
    retval = PAPI_stop( EventSet, values );
    if ( retval != PAPI_OK ) {
       test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
    }
    
    if ( values[1] == 0 ) {
       test_warn( __FILE__, __LINE__, "PAPI_REF_CYC = 0\nTry upgrading your kernel.", 0 );
    }
    
    printf( "CPU Computed Megahertz   : %d\n", mhz );
    printf( "Measure TOT and REF cycles from a cold start\n" );

    work(EventSet, mhz);
    do_flops(10*numflops);
    
    printf( "\nMeasure again after working for a while\n" );

    work(EventSet, mhz);
    test_pass( __FILE__, NULL, 0 );
    return ( 0 );
}

Here is the call graph for this function:

static void work ( int  EventSet,
int  mhz 
) [static]

Definition at line 108 of file cycle_ratio.c.

{
    int retval;
    long long values[2];
    long long elapsed_us, elapsed_cyc, elapsed_virt_us, elapsed_virt_cyc;
    double cycles_error;
    float ratio, ratio1;
    int numflops = NUM_FLOPS;

    ratio = ratio1 = 0;

    /* Gather before stats */
    elapsed_us = PAPI_get_real_usec(  );
    elapsed_cyc = PAPI_get_real_cyc(  );
    elapsed_virt_us = PAPI_get_virt_usec(  );
    elapsed_virt_cyc = PAPI_get_virt_cyc(  );

    /* Start PAPI */
    retval = PAPI_start( EventSet );
    if ( retval != PAPI_OK ) {
       test_fail( __FILE__, __LINE__, "PAPI_start", retval );
    }

    /* our test code */
    do_flops( numflops );

    /* Stop PAPI */
    retval = PAPI_stop( EventSet, values );
    if ( retval != PAPI_OK ) {
       test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
    }
    ratio = (float)values[0]/(float)values[1];

    /* Calculate total values */
    elapsed_virt_us = PAPI_get_virt_usec(  ) - elapsed_virt_us;
    elapsed_virt_cyc = PAPI_get_virt_cyc(  ) - elapsed_virt_cyc;
    elapsed_us = PAPI_get_real_usec(  ) - elapsed_us;
    elapsed_cyc = PAPI_get_real_cyc(  ) - elapsed_cyc;

   printf( "-------------------------------------------------------------------------\n" );
   printf( "Using %d iterations of c += a*b\n", numflops );
   printf( "-------------------------------------------------------------------------\n" );

   printf( TAB1, "PAPI_TOT_CYC             : \t", values[0] );
   printf( TAB1, "PAPI_REF_CYC             : \t", values[1] );
   printf( "%-12s %12f\n", "Cycle Ratio              : \t", ratio );
   printf( "%-12s %12d\n", "Effective MHz            : \t", (int)(ratio * mhz) );
   printf( TAB1, "Real usec                : \t", elapsed_us );
   printf( TAB1, "Real cycles              : \t", elapsed_cyc );
   printf( TAB1, "Virt usec                : \t", elapsed_virt_us );
   printf( TAB1, "Virt cycles              : \t", elapsed_virt_cyc );

   printf( "-------------------------------------------------------------------------\n" );

   printf( "Verification: PAPI_REF_CYC should be roughly equal to real_cycles\n" );
   cycles_error=100.0*((double)values[1] - (double)elapsed_cyc)/(double)elapsed_cyc;

   if ((cycles_error>10.0) || (cycles_error<-10.0)) {
     printf("Error of %.2f%%\n",cycles_error);
     test_warn( __FILE__, __LINE__, "validation", 0 );
   }

}

Here is the call graph for this function:

Here is the caller graph for this function:

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines