PAPI  5.0.1.0
PAPI_add_remove_event.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * This example shows how to use PAPI_add_event, PAPI_start, PAPI_read,       *
00003 *  PAPI_stop and PAPI_remove_event.                                          *
00004 ******************************************************************************/
00005  
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include "papi.h" /* This needs to be included every time you use PAPI */
00009 
00010 #define NUM_EVENTS 2
00011 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__);  exit(retval); }
00012 
00013 int main()
00014 {
00015    int EventSet = PAPI_NULL;
00016    int tmp, i;
00017    /*must be initialized to PAPI_NULL before calling PAPI_create_event*/
00018 
00019    long long values[NUM_EVENTS];
00020    /*This is where we store the values we read from the eventset */
00021     
00022    int retval,number=NUM_EVENTS,Events[NUM_EVENTS];
00023    /* We use number to keep track of the number of events in the EventSet */ 
00024    char errstring[PAPI_MAX_STR_LEN];
00025   
00026    /*************************************************************************** 
00027    *  This part initializes the library and compares the version number of the*
00028    * header file, to the version of the library, if these don't match then it *
00029    * is likely that PAPI won't work correctly.If there is an error, retval    *
00030    * keeps track of the version number.                                       *
00031    ***************************************************************************/
00032 
00033 
00034    if((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT )
00035       ERROR_RETURN(retval);
00036      
00037      
00038    /* Creating the eventset */              
00039    if ( (retval = PAPI_create_eventset(&EventSet)) != PAPI_OK)
00040       ERROR_RETURN(retval);
00041 
00042    /* Add Total Instructions Executed to the EventSet */
00043    if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_INS)) != PAPI_OK)
00044       ERROR_RETURN(retval);
00045 
00046    /* Add Total Cycles event to the EventSet */
00047    if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_CYC)) != PAPI_OK)
00048       ERROR_RETURN(retval);
00049 
00050    /* get the number of events in the event set */
00051    if ( (retval = PAPI_list_events(EventSet, Events, &number)) != PAPI_OK)
00052       ERROR_RETURN(retval);
00053 
00054    printf("There are %d events in the event set\n", number);
00055 
00056    /* Start counting */
00057 
00058    if ( (retval = PAPI_start(EventSet)) != PAPI_OK)
00059       ERROR_RETURN(retval);
00060    
00061    /* you can replace your code here */
00062    tmp=0;
00063    for (i = 0; i < 2000000; i++)
00064    {
00065       tmp = i + tmp;
00066    }
00067 
00068   
00069    /* read the counter values and store them in the values array */
00070    if ( (retval=PAPI_read(EventSet, values)) != PAPI_OK)
00071       ERROR_RETURN(retval);
00072 
00073    printf("The total instructions executed for the first loop are %lld \n", values[0] );
00074    printf("The total cycles executed for the first loop are %lld \n",values[1]);
00075   
00076    /* our slow code again */
00077    tmp=0;
00078    for (i = 0; i < 2000000; i++)
00079    {
00080       tmp = i + tmp;
00081    }
00082 
00083    /* Stop counting and store the values into the array */
00084    if ( (retval = PAPI_stop(EventSet, values)) != PAPI_OK)
00085       ERROR_RETURN(retval);
00086 
00087    printf("Total instructions executed are %lld \n", values[0] );
00088    printf("Total cycles executed are %lld \n",values[1]);
00089 
00090    /* Remove event: We are going to take the PAPI_TOT_INS from the eventset */
00091    if( (retval = PAPI_remove_event(EventSet, PAPI_TOT_INS)) != PAPI_OK)
00092       ERROR_RETURN(retval);
00093    printf("Removing PAPI_TOT_INS from the eventset\n"); 
00094 
00095    /* Now we list how many events are left on the event set */
00096    if ((retval=PAPI_list_events(EventSet, Events, &number))!= PAPI_OK)
00097       ERROR_RETURN(retval);
00098 
00099    printf("There is only %d event left in the eventset now\n", number);
00100 
00101    /* free the resources used by PAPI */
00102    PAPI_shutdown();
00103  
00104    exit(0);
00105 }
00106 
00107 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines