|
Packit |
577717 |
/*****************************************************************************
|
|
Packit |
577717 |
* This example code shows how to use most of PAPI's High level functions *
|
|
Packit |
577717 |
* to start,count,read and stop on an event set. We use two preset events *
|
|
Packit |
577717 |
* here: *
|
|
Packit |
577717 |
* PAPI_TOT_INS: Total instructions executed in a period of time *
|
|
Packit |
577717 |
* PAPI_TOT_CYC: Total cpu cycles in a period of time *
|
|
Packit |
577717 |
******************************************************************************/
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#include <stdio.h>
|
|
Packit |
577717 |
#include <stdlib.h>
|
|
Packit |
577717 |
#include "papi.h"
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#define NUM_EVENTS 2
|
|
Packit |
577717 |
#define THRESHOLD 10000
|
|
Packit |
577717 |
#define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/* stupid codes to be monitored */
|
|
Packit |
577717 |
void computation_mult()
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
double tmp=1.0;
|
|
Packit |
577717 |
int i=1;
|
|
Packit |
577717 |
for( i = 1; i < THRESHOLD; i++ )
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
tmp = tmp*i;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/* stupid codes to be monitored */
|
|
Packit |
577717 |
void computation_add()
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
int tmp = 0;
|
|
Packit |
577717 |
int i=0;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
for( i = 0; i < THRESHOLD; i++ )
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
tmp = tmp + i;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
int main()
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
/*Declaring and initializing the event set with the presets*/
|
|
Packit |
577717 |
int Events[2] = {PAPI_TOT_INS, PAPI_TOT_CYC};
|
|
Packit |
577717 |
/*The length of the events array should be no longer than the
|
|
Packit |
577717 |
value returned by PAPI_num_counters.*/
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*declaring place holder for no of hardware counters */
|
|
Packit |
577717 |
int num_hwcntrs = 0;
|
|
Packit |
577717 |
int retval;
|
|
Packit |
577717 |
char errstring[PAPI_MAX_STR_LEN];
|
|
Packit |
577717 |
/*This is going to store our list of results*/
|
|
Packit |
577717 |
long long values[NUM_EVENTS];
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/***************************************************************************
|
|
Packit |
577717 |
* This part initializes the library and compares the version number of the*
|
|
Packit |
577717 |
* header file, to the version of the library, if these don't match then it *
|
|
Packit |
577717 |
* is likely that PAPI won't work correctly.If there is an error, retval *
|
|
Packit |
577717 |
* keeps track of the version number. *
|
|
Packit |
577717 |
***************************************************************************/
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT )
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
fprintf(stderr, "Error: %d %s\n",retval, errstring);
|
|
Packit |
577717 |
exit(1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/**************************************************************************
|
|
Packit |
577717 |
* PAPI_num_counters returns the number of hardware counters the platform *
|
|
Packit |
577717 |
* has or a negative number if there is an error *
|
|
Packit |
577717 |
**************************************************************************/
|
|
Packit |
577717 |
if ((num_hwcntrs = PAPI_num_counters()) < PAPI_OK)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
printf("There are no counters available. \n");
|
|
Packit |
577717 |
exit(1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("There are %d counters in this system\n",num_hwcntrs);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/**************************************************************************
|
|
Packit |
577717 |
* PAPI_start_counters initializes the PAPI library (if necessary) and *
|
|
Packit |
577717 |
* starts counting the events named in the events array. This function *
|
|
Packit |
577717 |
* implicitly stops and initializes any counters running as a result of *
|
|
Packit |
577717 |
* a previous call to PAPI_start_counters. *
|
|
Packit |
577717 |
**************************************************************************/
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if ( (retval = PAPI_start_counters(Events, NUM_EVENTS)) != PAPI_OK)
|
|
Packit |
577717 |
ERROR_RETURN(retval);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("\nCounter Started: \n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/* Your code goes here*/
|
|
Packit |
577717 |
computation_add();
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/**********************************************************************
|
|
Packit |
577717 |
* PAPI_read_counters reads the counter values into values array *
|
|
Packit |
577717 |
**********************************************************************/
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if ( (retval=PAPI_read_counters(values, NUM_EVENTS)) != PAPI_OK)
|
|
Packit |
577717 |
ERROR_RETURN(retval);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("Read successfully\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("The total instructions executed for addition are %lld \n",values[0]);
|
|
Packit |
577717 |
printf("The total cycles used are %lld \n", values[1] );
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("\nNow we try to use PAPI_accum to accumulate values\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/* Do some computation here */
|
|
Packit |
577717 |
computation_add();
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/************************************************************************
|
|
Packit |
577717 |
* What PAPI_accum_counters does is it adds the running counter values *
|
|
Packit |
577717 |
* to what is in the values array. The hardware counters are reset and *
|
|
Packit |
577717 |
* left running after the call. *
|
|
Packit |
577717 |
************************************************************************/
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if ( (retval=PAPI_accum_counters(values, NUM_EVENTS)) != PAPI_OK)
|
|
Packit |
577717 |
ERROR_RETURN(retval);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("We did an additional %d times addition!\n", THRESHOLD);
|
|
Packit |
577717 |
printf("The total instructions executed for addition are %lld \n",
|
|
Packit |
577717 |
values[0] );
|
|
Packit |
577717 |
printf("The total cycles used are %lld \n", values[1] );
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/***********************************************************************
|
|
Packit |
577717 |
* Stop counting events(this reads the counters as well as stops them *
|
|
Packit |
577717 |
***********************************************************************/
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("\nNow we try to do some multiplications\n");
|
|
Packit |
577717 |
computation_mult();
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/******************* PAPI_stop_counters **********************************/
|
|
Packit |
577717 |
if ((retval=PAPI_stop_counters(values, NUM_EVENTS)) != PAPI_OK)
|
|
Packit |
577717 |
ERROR_RETURN(retval);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
printf("The total instruction executed for multiplication are %lld \n",
|
|
Packit |
577717 |
values[0] );
|
|
Packit |
577717 |
printf("The total cycles used are %lld \n", values[1] );
|
|
Packit |
577717 |
exit(0);
|
|
Packit |
577717 |
}
|