Blame src/examples/PAPI_add_remove_event.c

Packit 577717
/*****************************************************************************
Packit 577717
* This example shows how to use PAPI_add_event, PAPI_start, PAPI_read,       *
Packit 577717
*  PAPI_stop and PAPI_remove_event.                                          *
Packit 577717
******************************************************************************/
Packit 577717
 
Packit 577717
#include <stdio.h>
Packit 577717
#include <stdlib.h>
Packit 577717
#include "papi.h" /* This needs to be included every time you use PAPI */
Packit 577717
Packit 577717
#define NUM_EVENTS 2
Packit 577717
#define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__);  exit(retval); }
Packit 577717
Packit 577717
int main()
Packit 577717
{
Packit 577717
   int EventSet = PAPI_NULL;
Packit 577717
   int tmp, i;
Packit 577717
   /*must be initialized to PAPI_NULL before calling PAPI_create_event*/
Packit 577717
Packit 577717
   long long values[NUM_EVENTS];
Packit 577717
   /*This is where we store the values we read from the eventset */
Packit 577717
    
Packit 577717
   /* We use number to keep track of the number of events in the EventSet */ 
Packit 577717
   int retval, number;
Packit 577717
   
Packit 577717
   char errstring[PAPI_MAX_STR_LEN];
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
Packit 577717
   if((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT )
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
     
Packit 577717
     
Packit 577717
   /* Creating the eventset */              
Packit 577717
   if ( (retval = PAPI_create_eventset(&EventSet)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   /* Add Total Instructions Executed to the EventSet */
Packit 577717
   if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_INS)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   /* Add Total Cycles event to the EventSet */
Packit 577717
   if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_CYC)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   /* get the number of events in the event set */
Packit 577717
   number = 0;
Packit 577717
   if ( (retval = PAPI_list_events(EventSet, NULL, &number)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   printf("There are %d events in the event set\n", number);
Packit 577717
Packit 577717
   /* Start counting */
Packit 577717
Packit 577717
   if ( (retval = PAPI_start(EventSet)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
   
Packit 577717
   /* you can replace your code here */
Packit 577717
   tmp=0;
Packit 577717
   for (i = 0; i < 2000000; i++)
Packit 577717
   {
Packit 577717
      tmp = i + tmp;
Packit 577717
   }
Packit 577717
Packit 577717
  
Packit 577717
   /* read the counter values and store them in the values array */
Packit 577717
   if ( (retval=PAPI_read(EventSet, values)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   printf("The total instructions executed for the first loop are %lld \n", values[0] );
Packit 577717
   printf("The total cycles executed for the first loop are %lld \n",values[1]);
Packit 577717
  
Packit 577717
   /* our slow code again */
Packit 577717
   tmp=0;
Packit 577717
   for (i = 0; i < 2000000; i++)
Packit 577717
   {
Packit 577717
      tmp = i + tmp;
Packit 577717
   }
Packit 577717
Packit 577717
   /* Stop counting and store the values into the array */
Packit 577717
   if ( (retval = PAPI_stop(EventSet, values)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   printf("Total instructions executed are %lld \n", values[0] );
Packit 577717
   printf("Total cycles executed are %lld \n",values[1]);
Packit 577717
Packit 577717
   /* Remove event: We are going to take the PAPI_TOT_INS from the eventset */
Packit 577717
   if( (retval = PAPI_remove_event(EventSet, PAPI_TOT_INS)) != PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
   printf("Removing PAPI_TOT_INS from the eventset\n"); 
Packit 577717
Packit 577717
   /* Now we list how many events are left on the event set */
Packit 577717
   number = 0;
Packit 577717
   if ((retval=PAPI_list_events(EventSet, NULL, &number))!= PAPI_OK)
Packit 577717
      ERROR_RETURN(retval);
Packit 577717
Packit 577717
   printf("There is only %d event left in the eventset now\n", number);
Packit 577717
Packit 577717
   /* free the resources used by PAPI */
Packit 577717
   PAPI_shutdown();
Packit 577717
 
Packit 577717
   exit(0);
Packit 577717
}
Packit 577717
Packit 577717