Blame src/ctests/low-level.c

Packit 577717
/*  This examples show the essentials in using the PAPI low-level
Packit 577717
    interface. The program consists of 3 examples where the work
Packit 577717
    done over some work-loops. The example tries to illustrate
Packit 577717
    some simple mistakes that are easily made and how a correct
Packit 577717
    code would accomplish the same thing.
Packit 577717
Packit 577717
    Example 1: The total count over two work loops (Loops 1 and 2)
Packit 577717
    are supposed to be measured. Due to a mis-understanding of the
Packit 577717
    semantics of the API the total count gets wrong.
Packit 577717
    The example also illustrates that it is legal to read both
Packit 577717
    running and stopped counters.
Packit 577717
Packit 577717
    Example 2: The total count over two work loops (Loops 1 and 3)
Packit 577717
    is supposed to be measured while discarding the counts made in
Packit 577717
    loop 2. Instead the counts in loop1 are counted twice and the
Packit 577717
    counts in loop2 are added to the total number of counts.
Packit 577717
Packit 577717
    Example 3: One correct way of accomplishing the result aimed for
Packit 577717
    in example 2.
Packit 577717
*/
Packit 577717
Packit 577717
#include <stdio.h>
Packit 577717
#include <stdlib.h>
Packit 577717
Packit 577717
#include "papi.h"
Packit 577717
#include "papi_test.h"
Packit 577717
Packit 577717
#include "do_loops.h"
Packit 577717
Packit 577717
#define NUM_EVENTS 2
Packit 577717
Packit 577717
int
Packit 577717
main( int argc, char **argv )
Packit 577717
{
Packit 577717
	int retval;
Packit 577717
	long long values[NUM_EVENTS], dummyvalues[NUM_EVENTS];
Packit 577717
	int Events[NUM_EVENTS];
Packit 577717
	int EventSet = PAPI_NULL;
Packit 577717
	int quiet;
Packit 577717
Packit 577717
	/* Set TESTS_QUIET variable */
Packit 577717
	quiet=tests_quiet( argc, argv );
Packit 577717
Packit 577717
	retval = PAPI_library_init( PAPI_VER_CURRENT );
Packit 577717
	if (retval != PAPI_VER_CURRENT ) {
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
Packit 577717
	}
Packit 577717
Packit 577717
	/* query and set up the right events to monitor */
Packit 577717
	if ( PAPI_query_event( PAPI_FP_INS ) == PAPI_OK ) {
Packit 577717
		Events[0] = PAPI_FP_INS;
Packit 577717
		Events[1] = PAPI_TOT_CYC;
Packit 577717
	} else {
Packit 577717
		Events[0] = PAPI_TOT_INS;
Packit 577717
		Events[1] = PAPI_TOT_CYC;
Packit 577717
	}
Packit 577717
Packit 577717
	retval = PAPI_create_eventset( &EventSet );
Packit 577717
	if (retval != PAPI_OK ) {
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
Packit 577717
	}
Packit 577717
Packit 577717
	retval = PAPI_add_events( EventSet, ( int * ) Events, NUM_EVENTS );
Packit 577717
	if (retval < PAPI_OK ) {
Packit 577717
		if (!quiet) printf("Trouble adding events\n");
Packit 577717
		test_skip( __FILE__, __LINE__, "PAPI_add_events", retval );
Packit 577717
	}
Packit 577717
Packit 577717
	if ( !quiet ) {
Packit 577717
		printf( "\n   Incorrect usage of read and accum.\n" );
Packit 577717
		printf( "   Some cycles are counted twice\n" );
Packit 577717
	}
Packit 577717
	if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK )
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_start", retval );
Packit 577717
Packit 577717
	/* Loop 1 */
Packit 577717
	do_flops( NUM_FLOPS );
Packit 577717
Packit 577717
	if ( ( retval = PAPI_read( EventSet, values ) ) != PAPI_OK )
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_read", retval );
Packit 577717
Packit 577717
	if ( !quiet )
Packit 577717
		printf( TWO12, values[0], values[1], "(Counters continuing...)\n" );
Packit 577717
Packit 577717
	/* Loop 2 */
Packit 577717
	do_flops( NUM_FLOPS );
Packit 577717
Packit 577717
	/* Using PAPI_accum here is incorrect. The result is that Loop 1 *
Packit 577717
	 * is being counted twice                                        */
Packit 577717
	if ( ( retval = PAPI_accum( EventSet, values ) ) != PAPI_OK )
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_accum", retval );
Packit 577717
Packit 577717
	if ( !quiet )
Packit 577717
		printf( TWO12, values[0], values[1], "(Counters being accumulated)\n" );
Packit 577717
Packit 577717
	/* Loop 3 */
Packit 577717
	do_flops( NUM_FLOPS );
Packit 577717
Packit 577717
	if ( ( retval = PAPI_stop( EventSet, dummyvalues ) ) != PAPI_OK )
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
Packit 577717
Packit 577717
	if ( ( retval = PAPI_read( EventSet, dummyvalues ) ) != PAPI_OK )
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_read", retval );
Packit 577717
Packit 577717
	if ( !quiet ) {
Packit 577717
		printf( TWO12, dummyvalues[0], dummyvalues[1],
Packit 577717
				"(Reading stopped counters)\n" );
Packit 577717
Packit 577717
		printf( TWO12, values[0], values[1], "" );
Packit 577717
Packit 577717
		printf( "\n   Incorrect usage of read and accum.\n" );
Packit 577717
		printf( "   Another incorrect use\n" );
Packit 577717
	}
Packit 577717
	if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK )
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_start", retval );
Packit 577717
Packit 577717
	/* Loop 1 */
Packit 577717
	do_flops( NUM_FLOPS );
Packit 577717
Packit 577717
	if ( ( retval = PAPI_read( EventSet, values ) ) != PAPI_OK )
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_read", retval );
Packit 577717
Packit 577717
	if ( !quiet )
Packit 577717
		printf( TWO12, values[0], values[1], "(Counters continuing...)\n" );
Packit 577717
Packit 577717
	/* Loop 2 */
Packit 577717
	/* Code that should not be counted */
Packit 577717
	do_flops( NUM_FLOPS );
Packit 577717
Packit 577717
	if ( ( retval = PAPI_read( EventSet, dummyvalues ) ) != PAPI_OK )
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_read", retval );
Packit 577717
Packit 577717
	if ( !quiet )
Packit 577717
		printf( TWO12, dummyvalues[0], dummyvalues[1],
Packit 577717
				"(Intermediate counts...)\n" );
Packit 577717
Packit 577717
	/* Loop 3 */
Packit 577717
	do_flops( NUM_FLOPS );
Packit 577717
Packit 577717
	/* Since PAPI_read does not reset the counters it's use above after    *
Packit 577717
	 * loop 2 is incorrect. Instead Loop1 will in effect be counted twice. *
Packit 577717
	 * and the counts in loop 2 are included in the total counts           */
Packit 577717
	if ( ( retval = PAPI_accum( EventSet, values ) ) != PAPI_OK )
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_accum", retval );
Packit 577717
	if ( !quiet )
Packit 577717
		printf( TWO12, values[0], values[1], "" );
Packit 577717
Packit 577717
	if ( ( retval = PAPI_stop( EventSet, dummyvalues ) ) != PAPI_OK )
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
Packit 577717
Packit 577717
	if ( !quiet ) {
Packit 577717
		printf( "\n   Correct usage of read and accum.\n" );
Packit 577717
		printf( "   PAPI_reset and PAPI_accum used to skip counting\n" );
Packit 577717
		printf( "   a section of the code.\n" );
Packit 577717
	}
Packit 577717
	if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK )
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_start", retval );
Packit 577717
Packit 577717
	do_flops( NUM_FLOPS );
Packit 577717
Packit 577717
	if ( ( retval = PAPI_read( EventSet, values ) ) != PAPI_OK )
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_read", retval );
Packit 577717
	if ( !quiet )
Packit 577717
		printf( TWO12, values[0], values[1], "(Counters continuing)\n" );
Packit 577717
Packit 577717
	/* Code that should not be counted */
Packit 577717
	do_flops( NUM_FLOPS );
Packit 577717
Packit 577717
	if ( ( retval = PAPI_reset( EventSet ) ) != PAPI_OK )
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_reset", retval );
Packit 577717
Packit 577717
	if ( !quiet )
Packit 577717
		printf( "%12s %12s  (Counters reset)\n", "", "" );
Packit 577717
Packit 577717
	do_flops( NUM_FLOPS );
Packit 577717
Packit 577717
	if ( ( retval = PAPI_accum( EventSet, values ) ) != PAPI_OK )
Packit 577717
		test_fail( __FILE__, __LINE__, "PAPI_accum", retval );
Packit 577717
Packit 577717
	if ( !quiet )
Packit 577717
		printf( TWO12, values[0], values[1], "" );
Packit 577717
Packit 577717
	if ( !quiet ) {
Packit 577717
		printf( "----------------------------------\n" );
Packit 577717
		printf( "Verification: The last line in each experiment should be\n" );
Packit 577717
		printf( "approximately twice the value of the first line.\n" );
Packit 577717
		printf
Packit 577717
			( "The third case illustrates one possible way to accomplish this.\n" );
Packit 577717
	}
Packit 577717
	test_pass( __FILE__ );
Packit 577717
Packit 577717
	return 0;
Packit 577717
}