PAPI  5.0.1.0
high-level.c
Go to the documentation of this file.
00001 /*  This examples show the essentials in using the PAPI high-level
00002     interface. The program consists of 4 work-loops. The programer
00003     intends to count the total events for loop 1, 2 and 4, but not 
00004     include the number of events in loop 3.
00005 
00006     To accomplish this PAPI_read_counters is used as a counter
00007     reset function, while PAPI_accum_counters is used to sum
00008 
00009     the contributions of loops 2 and 4 into the total count.
00010 */
00011 
00012 #include "papi_test.h"
00013 extern int TESTS_QUIET;                /* Declared in test_utils.c */
00014 
00015 int
00016 main( int argc, char **argv )
00017 {
00018 #define NUM_EVENTS 2
00019     int retval;
00020     long long values[NUM_EVENTS], dummyvalues[NUM_EVENTS];
00021     long long myvalues[NUM_EVENTS];
00022     int Events[NUM_EVENTS];
00023 
00024     tests_quiet( argc, argv );  /* Set TESTS_QUIET variable */
00025 
00026     retval = PAPI_library_init( PAPI_VER_CURRENT );
00027     if ( retval != PAPI_VER_CURRENT )
00028         test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
00029 
00030     /* query and set up the right events to monitor */
00031     if ( PAPI_query_event( PAPI_FP_INS ) == PAPI_OK ) {
00032         Events[0] = PAPI_FP_INS;
00033     } else {
00034         Events[0] = PAPI_TOT_INS;
00035     }
00036     Events[1] = PAPI_TOT_CYC;
00037 
00038     retval = PAPI_start_counters( ( int * ) Events, NUM_EVENTS );
00039     if ( retval != PAPI_OK )
00040         test_fail( __FILE__, __LINE__, "PAPI_start_counters", retval );
00041 
00042     /* Loop 1 */
00043     do_flops( NUM_FLOPS );
00044 
00045     retval = PAPI_read_counters( values, NUM_EVENTS );
00046     if ( retval != PAPI_OK )
00047         test_fail( __FILE__, __LINE__, "PAPI_read_counters", retval );
00048 
00049     if ( !TESTS_QUIET )
00050         printf( TWO12, values[0], values[1], "(Counters continuing...)\n" );
00051 
00052     myvalues[0] = values[0];
00053     myvalues[1] = values[1];
00054     /* Loop 2 */
00055     do_flops( NUM_FLOPS );
00056 
00057     retval = PAPI_accum_counters( values, NUM_EVENTS );
00058     if ( retval != PAPI_OK )
00059         test_fail( __FILE__, __LINE__, "PAPI_accum_counters", retval );
00060 
00061     if ( !TESTS_QUIET )
00062         printf( TWO12, values[0], values[1], "(Counters being ''held'')\n" );
00063 
00064     /* Loop 3 */
00065     /* Simulated code that should not be counted */
00066     do_flops( NUM_FLOPS );
00067 
00068     retval = PAPI_read_counters( dummyvalues, NUM_EVENTS );
00069     if ( retval != PAPI_OK )
00070         test_fail( __FILE__, __LINE__, "PAPI_read_counters", retval );
00071     if ( !TESTS_QUIET )
00072         printf( TWO12, dummyvalues[0], dummyvalues[1], "(Skipped counts)\n" );
00073 
00074     if ( !TESTS_QUIET )
00075         printf( "%12s %12s  (''Continuing'' counting)\n", "xxx", "xxx" );
00076     /* Loop 4 */
00077     do_flops( NUM_FLOPS );
00078 
00079     retval = PAPI_accum_counters( values, NUM_EVENTS );
00080     if ( retval != PAPI_OK )
00081         test_fail( __FILE__, __LINE__, "PAPI_accum_counters", retval );
00082 
00083     if ( !TESTS_QUIET )
00084         printf( TWO12, values[0], values[1], "" );
00085 
00086     if ( !TESTS_QUIET ) {
00087         printf( "----------------------------------\n" );
00088         printf( "Verification: The last line in each experiment should be\n" );
00089         printf( "approximately three times the value of the first line.\n" );
00090     }
00091 
00092     {
00093         long long min, max;
00094         min = ( long long ) ( ( double ) myvalues[0] * .9 );
00095         max = ( long long ) ( ( double ) myvalues[0] * 1.1 );
00096         if ( values[0] < ( 3 * min ) || values[0] > ( 3 * max ) ) {
00097             retval = 1;
00098             if ( PAPI_query_event( PAPI_FP_INS ) == PAPI_OK ) {
00099                 test_fail( __FILE__, __LINE__, "PAPI_FP_INS", 1 );
00100             } else {
00101                 test_fail( __FILE__, __LINE__, "PAPI_TOT_INS", 1 );
00102             }
00103         }
00104         min = ( long long ) ( ( double ) myvalues[1] * .9 );
00105         max = ( long long ) ( ( double ) myvalues[1] * 1.1 );
00106         if ( values[1] < ( 3 * min ) || values[1] > ( 3 * max ) ) {
00107             retval = 1;
00108             test_fail( __FILE__, __LINE__, "PAPI_TOT_CYC", 1 );
00109         }
00110     }
00111     /* The values array is not allocated through allocate_test_space 
00112      * so we need to pass NULL here */
00113     test_pass( __FILE__, NULL, NUM_EVENTS );
00114     exit( 1 );
00115 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines