papi_test.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*****************************************************************************
  2. * This example shows how to use PAPI_add_event, PAPI_start, PAPI_read, *
  3. * PAPI_stop and PAPI_remove_event. *
  4. ******************************************************************************/
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "papi.h" /* This needs to be included every time you use PAPI */
  8. #define NUM_EVENTS 2
  9. #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }
  10. int main()
  11. {
  12. int EventSet = PAPI_NULL;
  13. int tmp, i;
  14. /*must be initialized to PAPI_NULL before calling PAPI_create_event*/
  15. long long values[NUM_EVENTS];
  16. /*This is where we store the values we read from the eventset */
  17. /* We use number to keep track of the number of events in the EventSet */
  18. int retval, number;
  19. char errstring[PAPI_MAX_STR_LEN];
  20. /***************************************************************************
  21. * This part initializes the library and compares the version number of the*
  22. * header file, to the version of the library, if these don't match then it *
  23. * is likely that PAPI won't work correctly.If there is an error, retval *
  24. * keeps track of the version number. *
  25. ***************************************************************************/
  26. if((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT )
  27. ERROR_RETURN(retval);
  28. /* Creating the eventset */
  29. if ( (retval = PAPI_create_eventset(&EventSet)) != PAPI_OK)
  30. ERROR_RETURN(retval);
  31. /* Add Total Instructions Executed to the EventSet */
  32. if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_INS)) != PAPI_OK)
  33. ERROR_RETURN(retval);
  34. /* Add Total Cycles event to the EventSet */
  35. if ( (retval = PAPI_add_event(EventSet, PAPI_TOT_CYC)) != PAPI_OK)
  36. ERROR_RETURN(retval);
  37. /* get the number of events in the event set */
  38. number = 0;
  39. if ( (retval = PAPI_list_events(EventSet, NULL, &number)) != PAPI_OK)
  40. ERROR_RETURN(retval);
  41. printf("There are %d events in the event set\n", number);
  42. /* Start counting */
  43. if ( (retval = PAPI_start(EventSet)) != PAPI_OK)
  44. ERROR_RETURN(retval);
  45. /* you can replace your code here */
  46. tmp=0;
  47. for (i = 0; i < 2000000; i++)
  48. {
  49. tmp = i + tmp;
  50. }
  51. /* read the counter values and store them in the values array */
  52. if ( (retval=PAPI_read(EventSet, values)) != PAPI_OK)
  53. ERROR_RETURN(retval);
  54. printf("The total instructions executed for the first loop are %lld \n", values[0] );
  55. printf("The total cycles executed for the first loop are %lld \n",values[1]);
  56. /* our slow code again */
  57. tmp=0;
  58. for (i = 0; i < 2000000; i++)
  59. {
  60. tmp = i + tmp;
  61. }
  62. /* Stop counting and store the values into the array */
  63. if ( (retval = PAPI_stop(EventSet, values)) != PAPI_OK)
  64. ERROR_RETURN(retval);
  65. printf("Total instructions executed are %lld \n", values[0] );
  66. printf("Total cycles executed are %lld \n",values[1]);
  67. /* Remove event: We are going to take the PAPI_TOT_INS from the eventset */
  68. if( (retval = PAPI_remove_event(EventSet, PAPI_TOT_INS)) != PAPI_OK)
  69. ERROR_RETURN(retval);
  70. printf("Removing PAPI_TOT_INS from the eventset\n");
  71. /* Now we list how many events are left on the event set */
  72. number = 0;
  73. if ((retval=PAPI_list_events(EventSet, NULL, &number))!= PAPI_OK)
  74. ERROR_RETURN(retval);
  75. printf("There is only %d event left in the eventset now\n", number);
  76. /* free the resources used by PAPI */
  77. PAPI_shutdown();
  78. exit(0);
  79. }