|
PAPI
5.3.0.0
|
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 /* We use number to keep track of the number of events in the EventSet */ 00023 int retval, number; 00024 00025 char errstring[PAPI_MAX_STR_LEN]; 00026 00027 /*************************************************************************** 00028 * This part initializes the library and compares the version number of the* 00029 * header file, to the version of the library, if these don't match then it * 00030 * is likely that PAPI won't work correctly.If there is an error, retval * 00031 * keeps track of the version number. * 00032 ***************************************************************************/ 00033 00034 00035 if((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT ) 00036 ERROR_RETURN(retval); 00037 00038 00039 /* Creating the eventset */ 00040 if ( (retval = PAPI_create_eventset(&EventSet)) != PAPI_OK) 00041 ERROR_RETURN(retval); 00042 00043 /* Add Total Instructions Executed to the EventSet */ 00044 if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_INS)) != PAPI_OK) 00045 ERROR_RETURN(retval); 00046 00047 /* Add Total Cycles event to the EventSet */ 00048 if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_CYC)) != PAPI_OK) 00049 ERROR_RETURN(retval); 00050 00051 /* get the number of events in the event set */ 00052 number = 0; 00053 if ( (retval = PAPI_list_events(EventSet, NULL, &number)) != PAPI_OK) 00054 ERROR_RETURN(retval); 00055 00056 printf("There are %d events in the event set\n", number); 00057 00058 /* Start counting */ 00059 00060 if ( (retval = PAPI_start(EventSet)) != PAPI_OK) 00061 ERROR_RETURN(retval); 00062 00063 /* you can replace your code here */ 00064 tmp=0; 00065 for (i = 0; i < 2000000; i++) 00066 { 00067 tmp = i + tmp; 00068 } 00069 00070 00071 /* read the counter values and store them in the values array */ 00072 if ( (retval=PAPI_read(EventSet, values)) != PAPI_OK) 00073 ERROR_RETURN(retval); 00074 00075 printf("The total instructions executed for the first loop are %lld \n", values[0] ); 00076 printf("The total cycles executed for the first loop are %lld \n",values[1]); 00077 00078 /* our slow code again */ 00079 tmp=0; 00080 for (i = 0; i < 2000000; i++) 00081 { 00082 tmp = i + tmp; 00083 } 00084 00085 /* Stop counting and store the values into the array */ 00086 if ( (retval = PAPI_stop(EventSet, values)) != PAPI_OK) 00087 ERROR_RETURN(retval); 00088 00089 printf("Total instructions executed are %lld \n", values[0] ); 00090 printf("Total cycles executed are %lld \n",values[1]); 00091 00092 /* Remove event: We are going to take the PAPI_TOT_INS from the eventset */ 00093 if( (retval = PAPI_remove_event(EventSet, PAPI_TOT_INS)) != PAPI_OK) 00094 ERROR_RETURN(retval); 00095 printf("Removing PAPI_TOT_INS from the eventset\n"); 00096 00097 /* Now we list how many events are left on the event set */ 00098 number = 0; 00099 if ((retval=PAPI_list_events(EventSet, NULL, &number))!= PAPI_OK) 00100 ERROR_RETURN(retval); 00101 00102 printf("There is only %d event left in the eventset now\n", number); 00103 00104 /* free the resources used by PAPI */ 00105 PAPI_shutdown(); 00106 00107 exit(0); 00108 } 00109 00110