|
PAPI
5.0.1.0
|
00001 /***************************************************************************** 00002 * PAPI_reset - resets the hardware event counters used by an EventSet. * 00003 *****************************************************************************/ 00004 00005 #include <stdio.h> 00006 #include <stdlib.h> 00007 #include "papi.h" /* This needs to be included every time you use PAPI */ 00008 00009 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); } 00010 00011 int poorly_tuned_function() 00012 { 00013 float tmp; 00014 int i; 00015 00016 for(i=1; i<2000; i++) 00017 { 00018 tmp=(tmp+100)/i; 00019 } 00020 return 0; 00021 } 00022 00023 int main() 00024 { 00025 int EventSet = PAPI_NULL; 00026 /*must be initialized to PAPI_NULL before calling PAPI_create_event*/ 00027 00028 int retval; 00029 unsigned int event_code=PAPI_TOT_INS; 00030 /* By default monitor total instructions */ 00031 00032 char errstring[PAPI_MAX_STR_LEN]; 00033 long long values[1]; 00034 00035 /**************************************************************************** 00036 * This part initializes the library and compares the version number of the * 00037 * header file, to the version of the library, if these don't match then it * 00038 * is likely that PAPI won't work correctly.If there is an error, retval * 00039 * keeps track of the version number. * 00040 ****************************************************************************/ 00041 00042 if((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT ) 00043 { 00044 printf("Library initialization error! \n"); 00045 exit(1); 00046 } 00047 00048 /* Creating the eventset */ 00049 if ( (retval=PAPI_create_eventset(&EventSet)) != PAPI_OK) 00050 ERROR_RETURN(retval); 00051 00052 /* Add Total Instructions Executed to our EventSet */ 00053 if ((retval=PAPI_add_event(EventSet, event_code)) != PAPI_OK) 00054 ERROR_RETURN(retval); 00055 00056 /* Start counting */ 00057 if((retval=PAPI_start(EventSet)) != PAPI_OK) 00058 ERROR_RETURN(retval); 00059 00060 poorly_tuned_function(); 00061 00062 /* Stop counting */ 00063 if((retval=PAPI_stop(EventSet, values)) != PAPI_OK) 00064 ERROR_RETURN(retval); 00065 00066 00067 printf("The first time read value is %lld\n",values[0]); 00068 00069 /* This zeroes out the counters on the eventset that was created */ 00070 if((retval=PAPI_reset(EventSet)) != PAPI_OK) 00071 ERROR_RETURN(retval); 00072 00073 /* Start counting */ 00074 if((retval=PAPI_start(EventSet)) != PAPI_OK) 00075 ERROR_RETURN(retval); 00076 00077 poorly_tuned_function(); 00078 00079 /* Stop counting */ 00080 if((retval=PAPI_stop(EventSet, values)) != PAPI_OK) 00081 ERROR_RETURN(retval); 00082 00083 printf("The second time read value is %lld\n",values[0]); 00084 00085 /* free the resources used by PAPI */ 00086 PAPI_shutdown(); 00087 00088 exit(0); 00089 } 00090 00091