|
PAPI
5.0.1.0
|
00001 /**************************************************************************** 00002 * Multiplexing allows more counters to be used than what is supported by * 00003 * the platform, thus allowing a larger number of events to be counted * 00004 * simultaneously. When a microprocessor has a very limited number of * 00005 * counters that can be counted simultaneously, a large application with * 00006 * many hours of run time may require days of profiling in order to gather * 00007 * enough information to base a performance analysis. Multiplexing overcomes* 00008 * this limitation by the usage of the counters over timesharing. * 00009 * This is an example demonstrating how to use PAPI_set_multiplex to * 00010 * convert a standard event set to a multiplexed event set. * 00011 ****************************************************************************/ 00012 #include <stdlib.h> 00013 #include <stdio.h> 00014 #include <unistd.h> 00015 #include "papi.h" 00016 00017 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); } 00018 00019 #define NUM_ITERS 10000000 00020 #define MAX_TO_ADD 6 00021 00022 double c = 0.11; 00023 void do_flops(int n) 00024 { 00025 int i; 00026 double a = 0.5; 00027 double b = 6.2; 00028 00029 for (i=0; i < n; i++) 00030 c += a * b; 00031 return; 00032 } 00033 00034 /* Tests that we can really multiplex a lot. */ 00035 int multiplex(void) 00036 { 00037 int retval, i, EventSet = PAPI_NULL, j = 0; 00038 long long *values; 00039 PAPI_event_info_t pset; 00040 int events[MAX_TO_ADD], number; 00041 00042 /* Initialize the library */ 00043 retval = PAPI_library_init(PAPI_VER_CURRENT); 00044 if (retval != PAPI_VER_CURRENT) 00045 { 00046 printf("Library initialization error! \n"); 00047 exit(1); 00048 } 00049 00050 /* initialize multiplex support */ 00051 retval = PAPI_multiplex_init(); 00052 if (retval != PAPI_OK) 00053 ERROR_RETURN(retval); 00054 00055 retval = PAPI_create_eventset(&EventSet); 00056 if (retval != PAPI_OK) 00057 ERROR_RETURN(retval); 00058 00059 /* convert the event set to a multiplex event set */ 00060 retval = PAPI_set_multiplex(EventSet); 00061 if (retval != PAPI_OK) 00062 ERROR_RETURN(retval); 00063 /* 00064 retval = PAPI_add_event(EventSet, PAPI_TOT_INS); 00065 if ((retval != PAPI_OK) && (retval != PAPI_ECNFLCT)) 00066 ERROR_RETURN(retval); 00067 printf("Adding %s\n", "PAPI_TOT_INS"); 00068 */ 00069 00070 for (i = 0; i < PAPI_MAX_PRESET_EVENTS; i++) 00071 { 00072 retval = PAPI_get_event_info(i | PAPI_PRESET_MASK, &pset); 00073 if (retval != PAPI_OK) 00074 ERROR_RETURN(retval); 00075 00076 if ((pset.count) && (pset.event_code != PAPI_TOT_CYC)) 00077 { 00078 printf("Adding %s\n", pset.symbol); 00079 00080 retval = PAPI_add_event(EventSet, pset.event_code); 00081 if ((retval != PAPI_OK) && (retval != PAPI_ECNFLCT)) 00082 ERROR_RETURN(retval); 00083 00084 if (retval == PAPI_OK) 00085 printf("Added %s\n", pset.symbol); 00086 else 00087 printf("Could not add %s due to resource limitation.\n", 00088 pset.symbol); 00089 00090 if (retval == PAPI_OK) 00091 { 00092 if (++j >= MAX_TO_ADD) 00093 break; 00094 } 00095 } 00096 } 00097 00098 values = (long long *) malloc(MAX_TO_ADD * sizeof(long long)); 00099 if (values == NULL) 00100 { 00101 printf("Not enough memory available. \n"); 00102 exit(1); 00103 } 00104 00105 if ((retval=PAPI_start(EventSet)) != PAPI_OK) 00106 ERROR_RETURN(retval); 00107 00108 do_flops(NUM_ITERS); 00109 00110 retval = PAPI_stop(EventSet, values); 00111 if (retval != PAPI_OK) 00112 ERROR_RETURN(retval); 00113 00114 /* get the number of events in the event set */ 00115 number=MAX_TO_ADD; 00116 if ( (retval = PAPI_list_events(EventSet, events, &number)) != PAPI_OK) 00117 ERROR_RETURN(retval); 00118 00119 /* print the read result */ 00120 for (i = 0; i < MAX_TO_ADD; i++) 00121 { 00122 retval = PAPI_get_event_info(events[i], &pset); 00123 if (retval != PAPI_OK) 00124 ERROR_RETURN(retval); 00125 printf("Event name: %s value: %lld \n", pset.symbol, values[i]); 00126 } 00127 00128 retval = PAPI_cleanup_eventset(EventSet); 00129 if (retval != PAPI_OK) 00130 ERROR_RETURN(retval); 00131 00132 retval = PAPI_destroy_eventset(&EventSet); 00133 if (retval != PAPI_OK) 00134 ERROR_RETURN(retval); 00135 00136 /* free the resources used by PAPI */ 00137 PAPI_shutdown(); 00138 00139 return (0); 00140 } 00141 00142 int main(int argc, char **argv) 00143 { 00144 00145 printf("Using %d iterations\n\n", NUM_ITERS); 00146 printf("Does PAPI_multiplex_init() handle lots of events?\n"); 00147 multiplex(); 00148 exit(0); 00149 }