PAPI  5.0.1.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 r,i;
00091     const PAPI_component_info_t *cmpinfo = NULL;
00092     PAPI_event_info_t evinfo;
00093     long long before_time,after_time;
00094     double elapsed_time;
00095 
00096         /* Set TESTS_QUIET variable */
00097      tests_quiet( argc, argv );      
00098 
00099     /* PAPI Initialization */
00100      retval = PAPI_library_init( PAPI_VER_CURRENT );
00101      if ( retval != PAPI_VER_CURRENT ) {
00102     test_fail(__FILE__, __LINE__,"PAPI_library_init failed\n",retval);
00103      }
00104 
00105      if (!TESTS_QUIET) {
00106     printf("Trying all RAPL events\n");
00107      }
00108 
00109      numcmp = PAPI_num_components();
00110 
00111      for(cid=0; cid<numcmp; cid++) {
00112 
00113     if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
00114        test_fail(__FILE__, __LINE__,"PAPI_get_component_info failed\n", 0);
00115     }
00116 
00117     if (strstr(cmpinfo->name,"rapl")) {
00118        rapl_cid=cid;
00119        if (!TESTS_QUIET) printf("Found rapl component at cid %d\n",
00120                     rapl_cid);
00121            if (cmpinfo->num_native_events==0) {
00122               test_skip(__FILE__,__LINE__,"No rapl events found",0);
00123            }
00124        break;
00125     }
00126      }
00127 
00128      /* Component not found */
00129      if (cid==numcmp) {
00130        test_skip(__FILE__,__LINE__,"No rapl component found\n",0);
00131      }
00132 
00133      /* Create EventSet */
00134      retval = PAPI_create_eventset( &EventSet );
00135      if (retval != PAPI_OK) {
00136     test_fail(__FILE__, __LINE__, 
00137                               "PAPI_create_eventset()",retval);
00138      }
00139 
00140      /* Add all events */
00141 
00142      code = PAPI_NATIVE_MASK;
00143 
00144      r = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, rapl_cid );
00145 
00146      while ( r == PAPI_OK ) {
00147 
00148         retval = PAPI_event_code_to_name( code, event_names[num_events] );
00149     if ( retval != PAPI_OK ) {
00150        printf("Error translating %x\n",code);
00151        test_fail( __FILE__, __LINE__, 
00152                             "PAPI_event_code_to_name", retval );
00153     }
00154 
00155     retval = PAPI_get_event_info(code,&evinfo);
00156     if (retval != PAPI_OK) {
00157       test_fail( __FILE__, __LINE__,
00158              "Error getting event info\n",retval);
00159     }
00160     
00161     strncpy(units[num_events],evinfo.units,PAPI_MIN_STR_LEN);
00162 
00163         retval = PAPI_add_event( EventSet, code );
00164         if (retval != PAPI_OK) {
00165       break; /* We've hit an event limit */
00166     }
00167     num_events++;
00168           
00169         r = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, rapl_cid );
00170      }
00171 
00172      values=calloc(num_events,sizeof(long long));
00173      if (values==NULL) {
00174     test_fail(__FILE__, __LINE__, 
00175                               "No memory",retval);
00176      }
00177 
00178      if (!TESTS_QUIET) {
00179     printf("\nStarting measurements...\n\n");
00180      }
00181 
00182      /* Start Counting */
00183      before_time=PAPI_get_real_nsec();
00184      retval = PAPI_start( EventSet);
00185      if (retval != PAPI_OK) {
00186     test_fail(__FILE__, __LINE__, "PAPI_start()",retval);
00187      }
00188 
00189      /* Run test */
00190      run_test(TESTS_QUIET);
00191 
00192      /* Stop Counting */
00193      after_time=PAPI_get_real_nsec();
00194      retval = PAPI_stop( EventSet, values);
00195      if (retval != PAPI_OK) {
00196     test_fail(__FILE__, __LINE__, "PAPI_start()",retval);
00197      }
00198 
00199      elapsed_time=((double)(after_time-before_time))/1.0e9;
00200 
00201      if (!TESTS_QUIET) {
00202         printf("\nStopping measurements, took %.3fs, gathering results...\n\n",
00203            elapsed_time);
00204 
00205     printf("Energy measurements:\n");
00206 
00207     for(i=0;i<num_events;i++) {
00208        if (strstr(units[i],"nJ")) {
00209 
00210           printf("%s\t%.6fJ\t(Average Power %.1fW)\n",
00211             event_names[i],
00212             (double)values[i]/1.0e9,
00213             ((double)values[i]/1.0e9)/elapsed_time);
00214        }
00215     }
00216 
00217     printf("\n");
00218     printf("Fixed values:\n");
00219 
00220     for(i=0;i<num_events;i++) {
00221        if (!strstr(units[i],"nJ")) {
00222 
00223          union {
00224            long long ll;
00225            double fp;
00226          } result;
00227 
00228          result.ll=values[i];
00229 
00230           printf("%s\t%0.3f%s\n",
00231             event_names[i],
00232             result.fp,
00233             units[i]);
00234        }
00235     }
00236 
00237      }
00238 
00239      /* Done, clean up */
00240      retval = PAPI_cleanup_eventset( EventSet );
00241      if (retval != PAPI_OK) {
00242     test_fail(__FILE__, __LINE__, 
00243                               "PAPI_cleanup_eventset()",retval);
00244      }
00245 
00246      retval = PAPI_destroy_eventset( &EventSet );
00247      if (retval != PAPI_OK) {
00248     test_fail(__FILE__, __LINE__, 
00249                               "PAPI_destroy_eventset()",retval);
00250      }
00251         
00252      test_pass( __FILE__, NULL, 0 );
00253         
00254      return 0;
00255 }
00256 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines