PAPI  5.0.1.0
PAPI_add_remove_events.c
Go to the documentation of this file.
00001 /******************************************************************************
00002  * This is a simple low level function demonstration on using PAPI_add_events *
00003  * to add an array of events to a created eventset, we are going to use these *
00004  * events to monitor a set of instructions, start the counters, read the      *
00005  * counters and then cleanup the eventset when done. In this example we use   *
00006  * the presets PAPI_TOT_INS and PAPI_TOT_CYC. PAPI_add_events,PAPI_start,     *
00007  * PAPI_stop, PAPI_clean_eventset, PAPI_destroy_eventset and                  *
00008  * PAPI_create_eventset all return PAPI_OK(which is 0) when succesful.        *
00009  ******************************************************************************/
00010 
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include "papi.h" /* This needs to be included every time you use PAPI */
00014 
00015 #define NUM_EVENT 2
00016 #define THRESHOLD 100000
00017 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__);  exit(retval); }
00018 
00019 
00020 int main(){
00021  
00022     int i,retval,tmp;
00023     int EventSet = PAPI_NULL;
00024     /*must be initialized to PAPI_NULL before calling PAPI_create_event*/
00025 
00026     int event_codes[NUM_EVENT]={PAPI_TOT_INS,PAPI_TOT_CYC}; 
00027     char errstring[PAPI_MAX_STR_LEN];
00028     long long values[NUM_EVENT];
00029 
00030    /***************************************************************************
00031    * This part initializes the library and compares the version number of the *
00032    * header file, to the version of the library, if these don't match then it *
00033    * is likely that PAPI won't work correctly.If there is an error, retval    *
00034    * keeps track of the version number.                                       *
00035    ****************************************************************************/
00036 
00037     if((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT )
00038     {
00039         fprintf(stderr, "Error: %s\n", errstring);
00040         exit(1);
00041     }
00042 
00043    
00044     /* Creating event set   */
00045     if ((retval=PAPI_create_eventset(&EventSet)) != PAPI_OK)
00046         ERROR_RETURN(retval);
00047 
00048 
00049     /* Add the array of events PAPI_TOT_INS and PAPI_TOT_CYC to the eventset*/
00050     if ((retval=PAPI_add_events(EventSet, event_codes, NUM_EVENT)) != PAPI_OK)
00051         ERROR_RETURN(retval);
00052   
00053        
00054     /* Start counting */
00055     if ( (retval=PAPI_start(EventSet)) != PAPI_OK)
00056         ERROR_RETURN(retval);
00057      
00058     /*** this is where your computation goes *********/
00059     for(i=0;i<1000;i++)
00060     {
00061         tmp = tmp+i;
00062     }  
00063 
00064     /* Stop counting, this reads from the counter as well as stop it. */
00065     if ( (retval=PAPI_stop(EventSet,values)) != PAPI_OK)
00066         ERROR_RETURN(retval);
00067 
00068     printf("\nThe total instructions executed are %lld, total cycles %lld\n",
00069             values[0],values[1]);
00070 
00071     
00072     if ( (retval=PAPI_remove_events(EventSet,event_codes, NUM_EVENT))!=PAPI_OK)
00073         ERROR_RETURN(retval);
00074 
00075     /* Free all memory and data structures, EventSet must be empty. */
00076     if ( (retval=PAPI_destroy_eventset(&EventSet)) != PAPI_OK)
00077         ERROR_RETURN(retval);
00078 
00079     /* free the resources used by PAPI */
00080     PAPI_shutdown();
00081    
00082     exit(0);
00083 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines