PAPI  5.3.0.0
rapl_basic.c
Go to the documentation of this file.
00001 /****************************/
00002 /* THIS IS OPEN SOURCE CODE */
00003 /****************************/
00004 
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include "papi_test.h"
00017 
00018 #define MAX_RAPL_EVENTS 64
00019 
00020 
00021 #ifdef BASIC_TEST
00022 
00023 void run_test(int quiet) {
00024 
00025      if (!quiet) {
00026     printf("Sleeping 1 second...\n");
00027      }
00028 
00029      /* Sleep */
00030      sleep(1);
00031 }
00032 
00033 #else
00034 
00035 #define MATRIX_SIZE 1024
00036 
00037      static double a[MATRIX_SIZE][MATRIX_SIZE];
00038      static double b[MATRIX_SIZE][MATRIX_SIZE];
00039      static double c[MATRIX_SIZE][MATRIX_SIZE];
00040 
00041 /* Naive matrix multiply */
00042 void run_test(int quiet) {
00043 
00044        double s;
00045        int i,j,k;
00046 
00047        if (!quiet) {
00048             printf("Doing a naive %dx%d MMM...\n",MATRIX_SIZE,MATRIX_SIZE);
00049        }
00050 
00051        for(i=0;i<MATRIX_SIZE;i++) {
00052      for(j=0;j<MATRIX_SIZE;j++) {
00053        a[i][j]=(double)i*(double)j;
00054        b[i][j]=(double)i/(double)(j+5);
00055      }
00056        }
00057 
00058        for(j=0;j<MATRIX_SIZE;j++) {
00059      for(i=0;i<MATRIX_SIZE;i++) {
00060        s=0;
00061        for(k=0;k<MATRIX_SIZE;k++) {
00062          s+=a[i][k]*b[k][j];
00063        }
00064        c[i][j] = s;
00065      }
00066        }
00067 
00068        s=0.0;
00069        for(i=0;i<MATRIX_SIZE;i++) {
00070         for(j=0;j<MATRIX_SIZE;j++) {
00071             s+=c[i][j];
00072         }
00073        }
00074 
00075        if (!quiet) printf("Matrix multiply sum: s=%lf\n",s);
00076 }
00077 
00078 #endif
00079 
00080 int main (int argc, char **argv)
00081 {
00082 
00083     int retval,cid,rapl_cid=-1,numcmp;
00084     int EventSet = PAPI_NULL;
00085     long long *values;
00086     int num_events=0;
00087     int code;
00088     char event_names[MAX_RAPL_EVENTS][PAPI_MAX_STR_LEN];
00089     char units[MAX_RAPL_EVENTS][PAPI_MIN_STR_LEN];
00090     int data_type[MAX_RAPL_EVENTS];
00091     int r,i, do_wrap = 0;
00092     const PAPI_component_info_t *cmpinfo = NULL;
00093     PAPI_event_info_t evinfo;
00094     long long before_time,after_time;
00095     double elapsed_time;
00096 
00097         /* Set TESTS_QUIET variable */
00098      tests_quiet( argc, argv );
00099      if ( argc > 1 )
00100         if ( strstr( argv[1], "-w" ) )
00101             do_wrap = 1;
00102 
00103     /* PAPI Initialization */
00104      retval = PAPI_library_init( PAPI_VER_CURRENT );
00105      if ( retval != PAPI_VER_CURRENT ) {
00106     test_fail(__FILE__, __LINE__,"PAPI_library_init failed\n",retval);
00107      }
00108 
00109      if (!TESTS_QUIET) {
00110         printf("Trying all RAPL events\n");
00111      }
00112 
00113      numcmp = PAPI_num_components();
00114 
00115      for(cid=0; cid<numcmp; cid++) {
00116 
00117     if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
00118        test_fail(__FILE__, __LINE__,"PAPI_get_component_info failed\n", 0);
00119     }
00120 
00121     if (strstr(cmpinfo->name,"rapl")) {
00122 
00123        rapl_cid=cid;
00124 
00125        if (!TESTS_QUIET) {
00126           printf("Found rapl component at cid %d\n",rapl_cid);
00127        }
00128 
00129            if (cmpinfo->disabled) {
00130           if (!TESTS_QUIET) {
00131          printf("RAPL component disabled: %s\n",
00132                         cmpinfo->disabled_reason);
00133           } 
00134               test_skip(__FILE__,__LINE__,"RAPL component disabled",0);
00135            }
00136        break;
00137     }
00138      }
00139 
00140      /* Component not found */
00141      if (cid==numcmp) {
00142        test_skip(__FILE__,__LINE__,"No rapl component found\n",0);
00143      }
00144 
00145      /* Create EventSet */
00146      retval = PAPI_create_eventset( &EventSet );
00147      if (retval != PAPI_OK) {
00148     test_fail(__FILE__, __LINE__, 
00149                               "PAPI_create_eventset()",retval);
00150      }
00151 
00152      /* Add all events */
00153 
00154      code = PAPI_NATIVE_MASK;
00155 
00156      r = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, rapl_cid );
00157 
00158      while ( r == PAPI_OK ) {
00159 
00160         retval = PAPI_event_code_to_name( code, event_names[num_events] );
00161     if ( retval != PAPI_OK ) {
00162        printf("Error translating %#x\n",code);
00163        test_fail( __FILE__, __LINE__, 
00164                             "PAPI_event_code_to_name", retval );
00165     }
00166 
00167     retval = PAPI_get_event_info(code,&evinfo);
00168     if (retval != PAPI_OK) {
00169       test_fail( __FILE__, __LINE__,
00170              "Error getting event info\n",retval);
00171     }
00172     
00173     strncpy(units[num_events],evinfo.units,PAPI_MIN_STR_LEN);
00174     data_type[num_events] = evinfo.data_type;
00175 
00176         retval = PAPI_add_event( EventSet, code );
00177         if (retval != PAPI_OK) {
00178       break; /* We've hit an event limit */
00179     }
00180     num_events++;
00181           
00182         r = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, rapl_cid );
00183      }
00184 
00185      values=calloc(num_events,sizeof(long long));
00186      if (values==NULL) {
00187     test_fail(__FILE__, __LINE__, 
00188                               "No memory",retval);
00189      }
00190 
00191      if (!TESTS_QUIET) {
00192     printf("\nStarting measurements...\n\n");
00193      }
00194 
00195      /* Start Counting */
00196      before_time=PAPI_get_real_nsec();
00197      retval = PAPI_start( EventSet);
00198      if (retval != PAPI_OK) {
00199     test_fail(__FILE__, __LINE__, "PAPI_start()",retval);
00200      }
00201 
00202      /* Run test */
00203      run_test(TESTS_QUIET);
00204 
00205      /* Stop Counting */
00206      after_time=PAPI_get_real_nsec();
00207      retval = PAPI_stop( EventSet, values);
00208      if (retval != PAPI_OK) {
00209     test_fail(__FILE__, __LINE__, "PAPI_stop()",retval);
00210      }
00211 
00212      elapsed_time=((double)(after_time-before_time))/1.0e9;
00213 
00214      if (!TESTS_QUIET) {
00215         printf("\nStopping measurements, took %.3fs, gathering results...\n\n",
00216            elapsed_time);
00217 
00218         printf("Scaled energy measurements:\n");
00219 
00220         for(i=0;i<num_events;i++) {
00221            if (strstr(units[i],"nJ")) {
00222 
00223               printf("%-40s%12.6f J\t(Average Power %.1fW)\n",
00224                 event_names[i],
00225                 (double)values[i]/1.0e9,
00226                 ((double)values[i]/1.0e9)/elapsed_time);
00227            }
00228         }
00229 
00230         printf("\n");
00231         printf("Energy measurement counts:\n");
00232 
00233         for(i=0;i<num_events;i++) {
00234            if (strstr(event_names[i],"ENERGY_CNT")) {
00235               printf("%-40s%12lld\t0x%08x\n", event_names[i], values[i], values[i]);
00236            }
00237         }
00238 
00239         printf("\n");
00240         printf("Scaled Fixed values:\n");
00241 
00242         for(i=0;i<num_events;i++) {
00243            if (!strstr(event_names[i],"ENERGY")) {
00244              if (data_type[i] == PAPI_DATATYPE_FP64) {
00245 
00246                  union {
00247                    long long ll;
00248                    double fp;
00249                  } result;
00250 
00251                 result.ll=values[i];
00252                 printf("%-40s%12.3f %s\n", event_names[i], result.fp, units[i]);
00253               }
00254            }
00255         }
00256     
00257         printf("\n");
00258         printf("Fixed value counts:\n");
00259 
00260         for(i=0;i<num_events;i++) {
00261            if (!strstr(event_names[i],"ENERGY")) {
00262               if (data_type[i] == PAPI_DATATYPE_UINT64) {
00263                 printf("%-40s%12lld\t0x%08x\n", event_names[i], values[i], values[i]);
00264               }
00265            }
00266         }
00267 
00268      }
00269 
00270 #ifdef WRAP_TEST
00271     double max_time;
00272     unsigned long long max_value = 0;
00273     int repeat;
00274     
00275     for(i=0;i<num_events;i++) {
00276         if (strstr(event_names[i],"ENERGY_CNT")) {
00277             if (max_value < values[i]) {
00278                 max_value = values[i];
00279             }
00280         }
00281     }
00282     max_time = elapsed_time * (0xffffffff / max_value);
00283     printf("\n");
00284     printf ("Approximate time to energy measurement wraparound: %.3f sec or %.3f min.\n", 
00285         max_time, max_time/60);
00286     
00287     if (do_wrap) {
00288          printf ("Beginning wraparound execution.");
00289          /* Start Counting */
00290          before_time=PAPI_get_real_nsec();
00291          retval = PAPI_start( EventSet);
00292          if (retval != PAPI_OK) {
00293             test_fail(__FILE__, __LINE__, "PAPI_start()",retval);
00294          }
00295 
00296          /* Run test */
00297         repeat = (int)(max_time/elapsed_time);
00298         for (i=0;i< repeat;i++) {
00299             run_test(1);
00300             printf("."); fflush(stdout);
00301         }
00302         printf("\n");
00303      
00304          /* Stop Counting */
00305          after_time=PAPI_get_real_nsec();
00306          retval = PAPI_stop( EventSet, values);
00307          if (retval != PAPI_OK) {
00308             test_fail(__FILE__, __LINE__, "PAPI_stop()",retval);
00309          }
00310 
00311         elapsed_time=((double)(after_time-before_time))/1.0e9;
00312         printf("\nStopping measurements, took %.3fs\n\n", elapsed_time);
00313 
00314         printf("Scaled energy measurements:\n");
00315 
00316         for(i=0;i<num_events;i++) {
00317            if (strstr(units[i],"nJ")) {
00318 
00319               printf("%-40s%12.6f J\t(Average Power %.1fW)\n",
00320                 event_names[i],
00321                 (double)values[i]/1.0e9,
00322                 ((double)values[i]/1.0e9)/elapsed_time);
00323            }
00324         }
00325         printf("\n");
00326         printf("Energy measurement counts:\n");
00327 
00328         for(i=0;i<num_events;i++) {
00329            if (strstr(event_names[i],"ENERGY_CNT")) {
00330               printf("%-40s%12lld\t0x%08x\n", event_names[i], values[i], values[i]);
00331            }
00332         }
00333     }
00334 
00335 #endif
00336 
00337      /* Done, clean up */
00338      retval = PAPI_cleanup_eventset( EventSet );
00339      if (retval != PAPI_OK) {
00340     test_fail(__FILE__, __LINE__, 
00341                               "PAPI_cleanup_eventset()",retval);
00342      }
00343 
00344      retval = PAPI_destroy_eventset( &EventSet );
00345      if (retval != PAPI_OK) {
00346     test_fail(__FILE__, __LINE__, 
00347                               "PAPI_destroy_eventset()",retval);
00348      }
00349         
00350      test_pass( __FILE__, NULL, 0 );
00351         
00352      return 0;
00353 }
00354 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines