PAPI  5.0.1.0
high_level.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 *  This example code shows how to use most of PAPI's High level functions    * 
00003 *  to start,count,read and stop on an event set. We use two preset events    *
00004 *  here:                                                                     *
00005 *     PAPI_TOT_INS: Total instructions executed in a period of time          *
00006 *     PAPI_TOT_CYC: Total cpu cycles in a period of time                     *
00007 ******************************************************************************/ 
00008 
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include "papi.h"
00012 
00013 #define NUM_EVENTS 2
00014 #define THRESHOLD 10000
00015 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__);  exit(retval); }
00016 
00017 /* stupid codes to be monitored */ 
00018 void computation_mult()
00019 {
00020    double tmp=1.0;
00021    int i=1;
00022    for( i = 1; i < THRESHOLD; i++ )
00023    {
00024       tmp = tmp*i;
00025    }
00026 }
00027 
00028 /* stupid codes to be monitored */ 
00029 void computation_add()
00030 {
00031    int tmp = 0;
00032    int i=0;
00033 
00034    for( i = 0; i < THRESHOLD; i++ )
00035    {
00036       tmp = tmp + i;
00037    }
00038 
00039 }
00040 
00041 
00042 int main()
00043 {
00044    /*Declaring and initializing the event set with the presets*/
00045    int Events[2] = {PAPI_TOT_INS, PAPI_TOT_CYC};
00046    /*The length of the events array should be no longer than the 
00047      value returned by PAPI_num_counters.*/ 
00048     
00049    /*declaring place holder for no of hardware counters */
00050    int num_hwcntrs = 0;
00051    int retval;
00052    char errstring[PAPI_MAX_STR_LEN];
00053    /*This is going to store our list of results*/
00054    long long values[NUM_EVENTS];
00055 
00056 
00057    /***************************************************************************
00058    *  This part initializes the library and compares the version number of the*
00059    * header file, to the version of the library, if these don't match then it *
00060    * is likely that PAPI won't work correctly.If there is an error, retval    *
00061    * keeps track of the version number.                                       *
00062    ***************************************************************************/
00063 
00064    if((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT )
00065    {
00066       fprintf(stderr, "Error: %d %s\n",retval, errstring);
00067       exit(1);
00068    }
00069 
00070 
00071    /**************************************************************************
00072     * PAPI_num_counters returns the number of hardware counters the platform *
00073     * has or a negative number if there is an error                          *
00074     **************************************************************************/
00075    if ((num_hwcntrs = PAPI_num_counters()) < PAPI_OK)
00076    {
00077       printf("There are no counters available. \n");
00078       exit(1);
00079    }
00080 
00081    printf("There are %d counters in this system\n",num_hwcntrs);
00082         
00083    /**************************************************************************
00084     * PAPI_start_counters initializes the PAPI library (if necessary) and    *
00085     * starts counting the events named in the events array. This function    *
00086     * implicitly stops and initializes any counters running as a result of   *
00087     * a previous call to PAPI_start_counters.                                *
00088     **************************************************************************/
00089     
00090    if ( (retval = PAPI_start_counters(Events, NUM_EVENTS)) != PAPI_OK)
00091        ERROR_RETURN(retval);
00092 
00093    printf("\nCounter Started: \n");
00094 
00095    /* Your code goes here*/
00096    computation_add();
00097     
00098 
00099   
00100 
00101    /**********************************************************************
00102     * PAPI_read_counters reads the counter values into values array      *
00103     **********************************************************************/
00104 
00105    if ( (retval=PAPI_read_counters(values, NUM_EVENTS)) != PAPI_OK)
00106       ERROR_RETURN(retval);
00107 
00108    printf("Read successfully\n");
00109 
00110 
00111 
00112    printf("The total instructions executed for addition are %lld \n",values[0]);
00113    printf("The total cycles used are %lld \n", values[1] );
00114     
00115    printf("\nNow we try to use PAPI_accum to accumulate values\n"); 
00116 
00117    /* Do some computation here */
00118    computation_add();
00119      
00120  
00121    /************************************************************************
00122     * What PAPI_accum_counters does is it adds the running counter values  *
00123     * to what is in the values array. The hardware counters are reset and  *
00124     * left running after the call.                                         *
00125     ************************************************************************/
00126 
00127    if ( (retval=PAPI_accum_counters(values, NUM_EVENTS)) != PAPI_OK)
00128       ERROR_RETURN(retval);
00129 
00130    printf("We did an additional %d times addition!\n", THRESHOLD);
00131    printf("The total instructions executed for addition are %lld \n",
00132             values[0] );
00133    printf("The total cycles used are %lld \n", values[1] );
00134 
00135    /***********************************************************************
00136     * Stop counting events(this reads the counters as well as stops them  *
00137     ***********************************************************************/
00138 
00139    printf("\nNow we try to do some multiplications\n");
00140    computation_mult();
00141 
00142    /******************* PAPI_stop_counters **********************************/
00143    if ((retval=PAPI_stop_counters(values, NUM_EVENTS)) != PAPI_OK)
00144       ERROR_RETURN(retval); 
00145 
00146    printf("The total instruction executed for multiplication are %lld \n",
00147                values[0] );
00148    printf("The total cycles used are %lld \n", values[1] );
00149    exit(0); 
00150 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines