Exemplo n.º 1
0
void event_create_eventList(int *eventSet, int eventCodeSetSize, int *eventCodeSet, int threadID) {

    int retval, i, maxNumberHwCounters, eventCodeSetMaxSize;
    PAPI_event_info_t info;

    maxNumberHwCounters = PAPI_get_opt( PAPI_MAX_HWCTRS, NULL );
    printf("Max number of hardware counters = %d \n", maxNumberHwCounters);

    eventCodeSetMaxSize = PAPI_get_opt( PAPI_MAX_MPX_CTRS, NULL );
    printf("Max number of multiplexed counters = %d \n", eventCodeSetMaxSize);

    if ( eventCodeSetMaxSize < eventCodeSetSize)
        test_fail( __FILE__, __LINE__, "eventCodeSetMaxSize < eventCodeSetSize, too many performance events defined! ", retval );

    retval = PAPI_register_thread();
    if ( retval != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_register_thread", retval );

    retval = PAPI_create_eventset( eventSet );
    if ( retval != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );

    retval = PAPI_assign_eventset_component( *eventSet, 0 );
    if ( retval != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_assign_eventset_component", retval );

    retval = PAPI_set_multiplex( *eventSet );
    if ( retval != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_set_multiplex", retval );

    for (i = 0; i < eventCodeSetSize; i++) {
        retval = PAPI_get_event_info(eventCodeSet[i], &info);
        if ( retval != PAPI_OK )
            test_fail( __FILE__, __LINE__, "PAPI_get_event_info", retval );

        retval = PAPI_add_event( *eventSet, info.event_code);
        if ( retval != PAPI_OK )
            test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
        else
            printf("Adding %s \n", info.symbol);

    }

    printf("event_create_eventList done \n");
}
Exemplo n.º 2
0
void 
init_test(int SoftwareMPX, int KernelMPX, int* Events)
{
  int i;
  int retval;
  PAPI_option_t option, itimer;

  if ( ( retval = PAPI_assign_eventset_component( SoftwareMPX, 0 ) ) != PAPI_OK )
	test_fail( __FILE__, __LINE__, "PAPI_assign_eventset_component", retval);

  if ( ( retval = PAPI_assign_eventset_component( KernelMPX, 0 ) ) != PAPI_OK )
	test_fail( __FILE__, __LINE__, "PAPI_assign_eventset_component", retval);

  if ( ( retval = PAPI_set_multiplex( KernelMPX ) ) != PAPI_OK )
	test_fail( __FILE__, __LINE__, "PAPI_set_multiplex", retval );

  PAPI_get_opt(PAPI_DEF_ITIMER,&itimer);

  memset(&option,0x0,sizeof(option));

  option.multiplex.flags = PAPI_MULTIPLEX_FORCE_SW;
  option.multiplex.eventset = SoftwareMPX;
  option.multiplex.ns = itimer.itimer.ns;

  PAPI_set_opt( PAPI_MULTIPLEX, &option );

  for (i = 0; i < options.min - 1; i++) {
	if ( options.kernel_mpx ) {
	  if ( ( retval = PAPI_add_event( KernelMPX, Events[i]) ) != PAPI_OK ) {
		test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
	  }
	}

	if ( options.force_sw ) {
	  if ( ( retval = PAPI_add_event( SoftwareMPX, Events[i]) ) != PAPI_OK ) {
		test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
	  }
	}
  }
}
Exemplo n.º 3
0
 /*
 *______________________________________________
 *
 *InitCmd --
 *
 * This function is to add events to papi event set.
 * A list of events is taken as argument.
 * 
 * Results:
 * The eventset is initialized.
 *________________________________________________
 */
int 
InitCmd(ClientData cdata, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{
    if(objc!=2) {
        fprintf(stderr,"Usage: perft::init listOfEvents");
        return 1;
    }


    int retval,eventCode;
    char* arg=Tcl_GetString(objv[1]);
    int argc, eventList;
    const char **argv;
    eventList= Tcl_SplitList(interp, arg, &argc,  &argv);
    char **index=(char **)argv;  
    
    if(papiState.eventsData==NULL) {
        AllocateStringArray(PAPI_MAX_EVENTS,PAPI_MAX_STRING_LENGTH,&papiState.eventsData);
    } else {
        retval=PAPI_cleanup_eventset(papiState.eventSet);
        if ( retval!= PAPI_OK) {
            HandlePapiError(retval);
            return 1;
        }
        // Free all memory and data structures, EventSet must be empty
        retval=PAPI_destroy_eventset(&papiState.eventSet);
        if ( retval!= PAPI_OK) {
            HandlePapiError(retval);
            return 1;
        }
    } 
      
    papiState.eventSet=PAPI_NULL;
    papiState.numberOfEvents=0;
    
    retval=InitPapi();
    if(retval==1) {
        return 1;
    }
    
    retval=InitPapiMultiplexing();
    if(retval==1) {
        return 1;
    }    
    
    retval=PAPI_assign_eventset_component(papiState.eventSet, 0);
    if ( retval!= PAPI_OK) {
        printf("Error in subroutine PAPI_assign_eventset_component");    
        HandlePapiError(retval);
        return 1;
    }    
    
    retval=PAPI_set_multiplex(papiState.eventSet);
    if ( retval!= PAPI_OK) {
        printf("Error in subroutine PAPI_set_multiplex");    
        HandlePapiError(retval);
        return 1;
    }
    
    while(papiState.numberOfEvents<argc) {
        retval=PAPI_event_name_to_code(*index,&eventCode);
        if ( retval!= PAPI_OK) {
            printf("Error in getting information about event name: %s\n",*index);
            HandlePapiError(retval);
            return 1;
        }
        papiState.eventsData[papiState.numberOfEvents]=*index;
        retval=PAPI_add_event(papiState.eventSet, eventCode);
        if(retval!=PAPI_OK) {
            printf("Error in adding event: %s\n",*index);
            HandlePapiError(retval);
            return 1;
        }
        index+=1;
        papiState.numberOfEvents+=1;
    }

    Tcl_SetObjResult(interp, Tcl_NewStringObj("Hardware counters initialized", -1));
    return TCL_OK;        
}    
Exemplo n.º 4
0
int main(int argc, char **argv)
{
   PAPI_event_info_t info;
   int i, j, retval;
   int iters = 10000000;
   double x = 1.1, y;
   long long t1, t2;
   long long values[MAXEVENTS], refvalues[MAXEVENTS];
   int sleep_time = SLEEPTIME;
   double valsqsum[MAXEVENTS];
   double valsum[MAXEVENTS];
   double spread[MAXEVENTS];
   int nevents = MAXEVENTS;
   int eventset = PAPI_NULL;
   int events[MAXEVENTS];

   events[0] = PAPI_BR_NTK;
   events[1] = PAPI_BR_PRC;
   events[2] = PAPI_BR_INS;
   events[3] = PAPI_BR_MSP;
/*
  events[3]=PAPI_BR_CN; 
  events[4]=PAPI_BR_UCN;*/
   /*events[5]=PAPI_BR_TKN; */


   for (i = 0; i < MAXEVENTS; i++) {
      values[i] = 0;
      valsqsum[i] = 0;
      valsum[i] = 0;
   }

   if (argc > 1) {
      if (!strcmp(argv[1], "TESTS_QUIET"))
         tests_quiet(argc, argv);
      else {
         sleep_time = atoi(argv[1]);
         if (sleep_time <= 0)
            sleep_time = SLEEPTIME;
      }
   }

   if (!TESTS_QUIET) {
      printf("\nAccuracy check of branch presets.\n");
      printf("Comparing a measurement with separate measurements.\n\n");
   }

   if ((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT)
      test_fail(__FILE__, __LINE__, "PAPI_library_init", retval);

   if ((retval = PAPI_create_eventset(&eventset)))
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset", retval);

#ifdef MPX
   if ((retval = PAPI_multiplex_init()))
      test_fail(__FILE__, __LINE__, "PAPI_multiplex_init", retval);

   if ((retval = PAPI_set_multiplex(eventset)))
      test_fail(__FILE__, __LINE__, "PAPI_set_multiplex", retval);
#endif

   nevents = 0;

   for (i = 0; i < MAXEVENTS; i++) {
      if (PAPI_query_event(events[i]) != PAPI_OK)
         continue;
      if (PAPI_add_event(eventset, events[i]) == PAPI_OK) {
         events[nevents] = events[i];
         nevents++;
      }
   }

   if (nevents < 1)
      test_skip(__FILE__, __LINE__, "Not enough events left...", 0);

   /* Find a reasonable number of iterations (each 
    * event active 20 times) during the measurement
    */
   t2 = 10000 * 20 * nevents;   /* Target: 10000 usec/multiplex, 20 repeats */
   if (t2 > 30e6)
      test_skip(__FILE__, __LINE__, "This test takes too much time", retval);

   /* Measure one run */
   t1 = PAPI_get_real_usec();
   y = dummy3(x, iters);
   t1 = PAPI_get_real_usec() - t1;

   if (t2 > t1)                 /* Scale up execution time to match t2 */
      iters = iters * (int)(t2 / t1);
   else if (t1 > 30e6)          /* Make sure execution time is < 30s per repeated test */
      test_skip(__FILE__, __LINE__, "This test takes too much time", retval);

   x = 1.0;

   if (!TESTS_QUIET)
      printf("\nFirst run: Together.\n");

   t1 = PAPI_get_real_usec();
   if ((retval = PAPI_start(eventset)))
      test_fail(__FILE__, __LINE__, "PAPI_start", retval);
   y = dummy3(x, iters);
   if ((retval = PAPI_stop(eventset, values)))
      test_fail(__FILE__, __LINE__, "PAPI_stop", retval);
   t2 = PAPI_get_real_usec();

   if (!TESTS_QUIET) {
      printf("\tOperations= %.1f Mflop", y * 1e-6);
      printf("\t(%g Mflop/s)\n\n", ((float) y / (t2 - t1)));
      printf("PAPI grouped measurement:\n");
   }
   for (j = 0; j < nevents; j++) {
      PAPI_get_event_info(events[j], &info);
      if (!TESTS_QUIET) {
         printf("%20s = ", info.short_descr);
         printf(LLDFMT, values[j]);
         printf("\n");
      }
   }
   if (!TESTS_QUIET)
      printf("\n");


   if ((retval = PAPI_remove_events(eventset, events, nevents)))
      test_fail(__FILE__, __LINE__, "PAPI_remove_events", retval);
   if ((retval = PAPI_destroy_eventset(&eventset)))
      test_fail(__FILE__, __LINE__, "PAPI_destroy_eventset", retval);
   eventset = PAPI_NULL;
   if ((retval = PAPI_create_eventset(&eventset)))
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset", retval);

   for (i = 0; i < nevents; i++) {

      if ((retval = PAPI_cleanup_eventset(eventset)))
         test_fail(__FILE__, __LINE__, "PAPI_cleanup_eventset", retval);
      if ((retval = PAPI_add_event(eventset, events[i])))
         test_fail(__FILE__, __LINE__, "PAPI_add_event", retval);

      x = 1.0;

      if (!TESTS_QUIET)
         printf("\nReference measurement %d (of %d):\n", i + 1, nevents);

      t1 = PAPI_get_real_usec();
      if ((retval = PAPI_start(eventset)))
         test_fail(__FILE__, __LINE__, "PAPI_start", retval);
      y = dummy3(x, iters);
      if ((retval = PAPI_stop(eventset, &refvalues[i])))
         test_fail(__FILE__, __LINE__, "PAPI_stop", retval);
      t2 = PAPI_get_real_usec();

      if (!TESTS_QUIET) {
         printf("\tOperations= %.1f Mflop", y * 1e-6);
         printf("\t(%g Mflop/s)\n\n", ((float) y / (t2 - t1)));
      }
      PAPI_get_event_info(events[i], &info);
      if (!TESTS_QUIET) {
         printf("PAPI results:\n%20s = ", info.short_descr);
         printf(LLDFMT, refvalues[i]);
         printf("\n");
      }
   }
   if (!TESTS_QUIET)
      printf("\n");


   if (!TESTS_QUIET) {
      printf("\n\nRelative accuracy:\n");
      for (j = 0; j < nevents; j++)
         printf("   Event %.2d", j);
      printf("\n");
   }

   for (j = 0; j < nevents; j++) {
      spread[j] = abs((int)(refvalues[j] - values[j]));
      if (values[j])
         spread[j] /= (double) values[j];
      if (!TESTS_QUIET)
         printf("%10.3g ", spread[j]);
      /* Make sure that NaN get counted as errors */
      if (spread[j] < MPX_TOLERANCE)
         i--;
      else if (refvalues[j] < MINCOUNTS)        /* Neglect inprecise results with low counts */
         i--;
   }
   if (!TESTS_QUIET)
      printf("\n\n");

   if (i)
      test_fail(__FILE__, __LINE__, "Values outside threshold", i);
   else
      test_pass(__FILE__, NULL, 0);

   return 0;
}
Exemplo n.º 5
0
int main(int argc, char **argv)
{
   PAPI_event_info_t info;
   char name2[PAPI_MAX_STR_LEN];
   int i, j, retval, idx, repeats;
   int iters = NUM_FLOPS;
   double x = 1.1, y, dtmp;
   long long t1, t2;
   long long values[MAXEVENTS], refvals[MAXEVENTS];
   int nsamples[MAXEVENTS], truelist[MAXEVENTS], ntrue;
#ifdef STARTSTOP
   long long dummies[MAXEVENTS];
#endif
   int sleep_time = SLEEPTIME;
   double valsample[MAXEVENTS][REPEATS];
   double valsum[MAXEVENTS];
   double avg[MAXEVENTS];
   double spread[MAXEVENTS];
   int nevents = MAXEVENTS, nev1;
   int eventset = PAPI_NULL;
   int events[MAXEVENTS];
   int eventidx[MAXEVENTS];
   int eventmap[MAXEVENTS];
   int fails;

   events[0] = PAPI_FP_INS;
   events[1] = PAPI_TOT_CYC;
   events[2] = PAPI_TOT_INS;
   events[3] = PAPI_TOT_IIS;
   events[4] = PAPI_INT_INS;
   events[5] = PAPI_STL_CCY;
   events[6] = PAPI_BR_INS;
   events[7] = PAPI_SR_INS;
   events[8] = PAPI_LD_INS;

   for (i = 0; i < MAXEVENTS; i++) {
      values[i] = 0;
      valsum[i] = 0;
      nsamples[i] = 0;
   }

   if (argc > 1) {
      if (!strcmp(argv[1], "TESTS_QUIET"))
         tests_quiet(argc, argv);
      else {
         sleep_time = atoi(argv[1]);
         if (sleep_time <= 0)
            sleep_time = SLEEPTIME;
      }
   }

   if (!TESTS_QUIET) {
      printf("\nFunctional check of multiplexing routines.\n");
      printf("Adding and removing events from an event set.\n\n");
   }

   if ((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT)
      test_fail(__FILE__, __LINE__, "PAPI_library_init", retval);

#ifdef MPX
   init_multiplex();
#endif
   if ((retval = PAPI_create_eventset(&eventset)))
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset", retval);

#ifdef MPX

   /* In Component PAPI, EventSets must be assigned a component index
      before you can fiddle with their internals.
      0 is always the cpu component */
   retval = PAPI_assign_eventset_component(eventset, 0);
   if (retval != PAPI_OK)
      test_fail(__FILE__, __LINE__, "PAPI_assign_eventset_component", retval);

   if ((retval = PAPI_set_multiplex(eventset)))
      test_fail(__FILE__, __LINE__, "PAPI_set_multiplex", retval);
#endif

   nevents = MAXEVENTS;
   for (i = 0; i < nevents; i++) {
      if ((retval = PAPI_add_event(eventset, events[i]))) {
         for (j = i; j < MAXEVENTS; j++)
            events[j] = events[j + 1];
         nevents--;
         i--;
      }
   }
   if (nevents < 3)
      test_skip(__FILE__, __LINE__, "Not enough events left...", 0);

   /* Find a reasonable number of iterations (each 
    * event active 20 times) during the measurement
    */
   t2 = 10000 * 20 * nevents;   /* Target: 10000 usec/multiplex, 20 repeats */
   if (t2 > 30e6)
      test_skip(__FILE__, __LINE__, "This test takes too much time", retval);

   /* Measure one run */
   t1 = PAPI_get_real_usec();
   y = dummy3(x, iters);
   t1 = PAPI_get_real_usec() - t1;

   if (t2 > t1)                 /* Scale up execution time to match t2 */
      iters = iters * (int)(t2 / t1);
   else if (t1 > 30e6)          /* Make sure execution time is < 30s per repeated test */
      test_skip(__FILE__, __LINE__, "This test takes too much time", retval);

   j = nevents;
   for (i = 1; i < nevents; i = i + 2)
      eventidx[--j] = i;
   for (i = 0; i < nevents; i = i + 2)
      eventidx[--j] = i;
   assert(j == 0);
   for (i = 0; i < nevents; i++)
      eventmap[i] = i;

   x = 1.0;

   if (!TESTS_QUIET)
      printf("\nReference run:\n");

   t1 = PAPI_get_real_usec();
   if ((retval = PAPI_start(eventset)))
      test_fail(__FILE__, __LINE__, "PAPI_start", retval);
   y = dummy3(x, iters);
   PAPI_read(eventset, refvals);
   t2 = PAPI_get_real_usec();

   ntrue = nevents;
   PAPI_list_events(eventset, truelist, &ntrue);
   if (!TESTS_QUIET) {
      printf("\tOperations= %.1f Mflop", y * 1e-6);
      printf("\t(%g Mflop/s)\n\n", ((float) y / (t2 - t1)));
      printf("%20s   %16s   %-15s %-15s\n", "PAPI measurement:",
             "Acquired count", "Expected event", "PAPI_list_events");
   }

   if (!TESTS_QUIET) {
      for (j = 0; j < nevents; j++) {
         PAPI_get_event_info(events[j], &info);
         PAPI_event_code_to_name(truelist[j], name2);
         if (!TESTS_QUIET)
            printf("%20s = %16lld   %-15s %-15s %s\n", info.short_descr, refvals[j],
                   info.symbol, name2, strcmp(info.symbol,
                                              name2) ? "*** MISMATCH ***" : "");
      }
      printf("\n");
   }

   nev1 = nevents;
   repeats = nevents * 4;
   for (i = 0; i < repeats; i++) {
      if ((i % nevents) + 1 == nevents)
         continue;

      if (!TESTS_QUIET)
         printf("\nTest %d (of %d):\n", i + 1 - i / nevents, repeats - 4);

      if ((retval = PAPI_stop(eventset, values)))
         test_fail(__FILE__, __LINE__, "PAPI_stop", retval);

      j = eventidx[i % nevents];

      if ((i / nevents) % 2 == 0) {
         PAPI_get_event_info(events[j], &info);
         if (!TESTS_QUIET)
            printf("Removing event[%d]: %s\n", j, info.short_descr);
         if ((retval = PAPI_remove_event(eventset, events[j])))
            test_fail(__FILE__, __LINE__, "PAPI_remove_event", retval);
         nev1--;
         for (idx = 0; eventmap[idx] != j; idx++);
         for (j = idx; j < nev1; j++)
            eventmap[j] = eventmap[j + 1];
      } else {
         PAPI_get_event_info(events[j], &info);
         if (!TESTS_QUIET)
            printf("Adding event[%d]: %s\n", j, info.short_descr);
         if ((retval = PAPI_add_event(eventset, events[j])))
            test_fail(__FILE__, __LINE__, "PAPI_add_event", retval);
         eventmap[nev1] = j;
         nev1++;
      }
      if ((retval = PAPI_start(eventset)))
         test_fail(__FILE__, __LINE__, "PAPI_start", retval);

      x = 1.0;
#ifndef STARTSTOP
      if ((retval = PAPI_reset(eventset)))
         test_fail(__FILE__, __LINE__, "PAPI_reset", retval);
#else
      if ((retval = PAPI_stop(eventset, dummies)))
         test_fail(__FILE__, __LINE__, "PAPI_stop", retval);
      if ((retval = PAPI_start(eventset)))
         test_fail(__FILE__, __LINE__, "PAPI_start", retval);
#endif

      t1 = PAPI_get_real_usec();
      y = dummy3(x, iters);
      PAPI_read(eventset, values);
      t2 = PAPI_get_real_usec();

      if (!TESTS_QUIET) {
         printf("\n(calculated independent of PAPI)\n");
         printf("\tOperations= %.1f Mflop", y * 1e-6);
         printf("\t(%g Mflop/s)\n\n", ((float) y / (t2 - t1)));
         printf("%20s   %16s   %-15s %-15s\n", "PAPI measurement:",
                "Acquired count", "Expected event", "PAPI_list_events");
      }

      ntrue = nev1;
      PAPI_list_events(eventset, truelist, &ntrue);
      for (j = 0; j < nev1; j++) {
         idx = eventmap[j];
         /* printf("Mapping: Counter %d -> slot %d.\n",j,idx); */
         PAPI_get_event_info(events[idx], &info);
         PAPI_event_code_to_name(truelist[j], name2);
         if (!TESTS_QUIET)
            printf("%20s = %16lld   %-15s %-15s %s\n", info.short_descr, values[j],
                   info.symbol, name2, strcmp(info.symbol,
                                              name2) ? "*** MISMATCH ***" : "");
         dtmp = (double) values[j];
         valsum[idx] += dtmp;
         valsample[idx][nsamples[idx]] = dtmp;
         nsamples[idx]++;
      }
      if (!TESTS_QUIET)
         printf("\n");
   }


   if (!TESTS_QUIET) {
      printf("\n\nEstimated variance relative to average counts:\n");
      for (j = 0; j < nev1; j++)
         printf("   Event %.2d", j);
      printf("\n");
   }

   fails = nevents;
   /* Due to limited precision of floating point cannot really use
      typical standard deviation compuation for large numbers with
      very small variations. Instead compute the std devation
      problems with precision.
   */
   for (j = 0; j < nev1; j++) {
      avg[j] = valsum[j] / nsamples[j];
      spread[j] = 0;
      for (i=0; i < nsamples[j]; ++i) {
         double diff = (valsample[j][i] - avg[j]);
         spread[j] += diff * diff;
      }
      spread[j] = sqrt(spread[j] / nsamples[j]) / avg[j];
      if (!TESTS_QUIET)
         printf("%9.2g  ", spread[j]);
      /* Make sure that NaN get counted as errors */
      if (spread[j] < MPX_TOLERANCE)
         fails--;
      else if (values[j] < MINCOUNTS)   /* Neglect inprecise results with low counts */
         fails--;
   }
   if (!TESTS_QUIET) {
      printf("\n\n");
      for (j = 0; j < nev1; j++) {
         PAPI_get_event_info(events[j], &info);
         printf("Event %.2d: mean=%10.0f, sdev/mean=%7.2g nrpt=%2d -- %s\n",
                j, avg[j], spread[j], nsamples[j], info.short_descr);
      }
      printf("\n\n");
   }

   if (fails)
      test_fail(__FILE__, __LINE__, "Values differ from reference", fails);
   else
      test_pass(__FILE__, NULL, 0);

   return 0;
}
Exemplo n.º 6
0
int main(int argc, char **argv)
{
   PAPI_event_info_t info;
   int i, j, retval;
   int iters = NUM_FLOPS;
   double x = 1.1, y;
   long long t1, t2;
   long long values[MAXEVENTS], refvalues[MAXEVENTS];
   int sleep_time = SLEEPTIME;
   double valsqsum[MAXEVENTS];
   double valsum[MAXEVENTS];
   int nevents = MAXEVENTS;
   int eventset = PAPI_NULL;
   int events[MAXEVENTS];

   events[0] = PAPI_FP_INS;
   events[1] = PAPI_TOT_INS;
   events[2] = PAPI_INT_INS;
   events[3] = PAPI_TOT_CYC;
   events[4] = PAPI_STL_CCY;
   events[5] = PAPI_BR_INS;
   events[6] = PAPI_SR_INS;
   events[7] = PAPI_LD_INS;
   events[8] = PAPI_TOT_IIS;
   events[9] = PAPI_FAD_INS;
   events[10] = PAPI_BR_TKN;
   events[11] = PAPI_BR_MSP;
   events[12] = PAPI_L1_ICA;
   events[13] = PAPI_L1_DCA;

   for (i = 0; i < MAXEVENTS; i++) {
      values[i] = 0;
      valsqsum[i] = 0;
      valsum[i] = 0;
   }

   if (argc > 1) {
      if (!strcmp(argv[1], "TESTS_QUIET"))
         tests_quiet(argc, argv);
      else {
         sleep_time = atoi(argv[1]);
         if (sleep_time <= 0)
            sleep_time = SLEEPTIME;
      }
   }

   if (!TESTS_QUIET) {
      printf("\nAccuracy check of multiplexing routines.\n");
      printf("Comparing a multiplex measurement with separate measurements.\n\n");
   }

   if ((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT)
      test_fail(__FILE__, __LINE__, "PAPI_library_init", retval);

   decide_which_events(events, &nevents);

   init_multiplex();

   /* Find a reasonable number of iterations (each 
    * event active 20 times) during the measurement
    */
   t2 = 10000 * 20 * nevents;   /* Target: 10000 usec/multiplex, 20 repeats */
   if (t2 > 30e6)
      test_skip(__FILE__, __LINE__, "This test takes too much time", retval);

   y = dummy3(x, iters);
   /* Measure one run */
   t1 = PAPI_get_real_usec();
   y = dummy3(x, iters);
   t1 = PAPI_get_real_usec() - t1;

   if (t1 < 1000000)                 /* Scale up execution time to match t2 */
     {
       iters = iters * (1000000/t1);
       printf("Modified iteration count to %d\n\n",iters);
     }

   /* Now loop through the items one at a time */

   ref_measurements(iters, &eventset, events, nevents, refvalues); 

   /* Now check multiplexed */

   if ((retval = PAPI_create_eventset(&eventset)))
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset", retval);


   /* In Component PAPI, EventSets must be assigned a component index
      before you can fiddle with their internals.
      0 is always the cpu component */
   retval = PAPI_assign_eventset_component(eventset, 0);
   if (retval != PAPI_OK)
      test_fail(__FILE__, __LINE__, "PAPI_assign_eventset_component", retval);

   if ((retval = PAPI_set_multiplex(eventset)))
     test_fail(__FILE__, __LINE__, "PAPI_set_multiplex", retval);

   if ((retval = PAPI_add_events(eventset, events, nevents)))
     test_fail(__FILE__, __LINE__, "PAPI_add_events", retval);

   printf("\nPAPI multiplexed measurements:\n");
   x = 1.0;
   t1 = PAPI_get_real_usec();
   if ((retval = PAPI_start(eventset)))
      test_fail(__FILE__, __LINE__, "PAPI_start", retval);
   y = dummy3(x, iters);
   if ((retval = PAPI_stop(eventset, values)))
      test_fail(__FILE__, __LINE__, "PAPI_stop", retval);
   t2 = PAPI_get_real_usec();

   for (j = 0; j < nevents; j++) {
      PAPI_get_event_info(events[j], &info);
      if (!TESTS_QUIET) {
         printf("%20s = ", info.short_descr);
         printf(LLDFMT, values[j]);
         printf("\n");
      }
   }

   check_values(eventset, events, nevents, values, refvalues);

   if ((retval = PAPI_remove_events(eventset, events, nevents)))
      test_fail(__FILE__, __LINE__, "PAPI_remove_events", retval);
   if ((retval = PAPI_cleanup_eventset(eventset)))
     test_fail(__FILE__, __LINE__, "PAPI_cleanup_eventset", retval);
   if ((retval = PAPI_destroy_eventset(&eventset)))
      test_fail(__FILE__, __LINE__, "PAPI_destroy_eventset", retval);
   eventset = PAPI_NULL;

   /* Now loop through the items one at a time */

   ref_measurements(iters, &eventset, events, nevents, refvalues);

   check_values(eventset, events, nevents, values, refvalues);

   test_pass(__FILE__, NULL, 0);

   return 0;
}
Exemplo n.º 7
0
int main()
{
    double *a;
    double *b;
    double *c;
    int i = 0, j = 0, k = 0;
    int *events;                        // Array of events
    long long *values;                  // Array of values events
    int EventSet = PAPI_NULL;           // Handle for a PAPI event set as created by PAPI_create_eventset (3) 
    int retval;                         // Test fail function
    int num_event = 0;                  // Number of events
    int max_event;                      // Number of available events
    int EventCode = 0;                  // Event code
    PAPI_event_info_t pset;             // PAPI_event_info_t Struct Reference
    char evname[PAPI_MAX_STR_LEN];      // Symbol event
   
    /* Memory asignament to matrixs*/   
    if((a = (double *)malloc(mrows * ncolumns * sizeof(double))) == NULL)
        printf("Error malloc matrix a[%d]\n",mrows * ncolumns);
    if((b = (double *)malloc(ncolumns * pcolumns * sizeof(double))) == NULL)
        printf("Error malloc matrix b[%d]\n",mrows * ncolumns);
    if((c = (double *)malloc(mrows * pcolumns * sizeof(double))) == NULL)
        printf("Error malloc matrix c[%d]\n",mrows * ncolumns);

    /* Initialize the Matrix arrays */
    initmat(a, b, mrows, ncolumns, pcolumns);

    /* Initialize the PAPI library */
    retval = PAPI_library_init(PAPI_VER_CURRENT);
    if (retval != PAPI_VER_CURRENT)
        test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );

    /* Enable and initialize multiplex support */
    retval = PAPI_multiplex_init();
    if ( retval != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_multiplex_init", retval );
 
    /* Create an EventSet */
    retval = PAPI_create_eventset(&EventSet);
    if ( retval != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
 
    /* Assign it to the CPU component */
    retval = PAPI_assign_eventset_component(EventSet, 0);
    if ( retval != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_assign_eventset_component", retval );
 
    /* Convert the EventSet to a multiplexed event set */
    retval = PAPI_set_multiplex(EventSet);
    if ( retval != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_set_multiplex", retval );

    /* Obtaining the number of available events */
    max_event = PAPI_get_opt( PAPI_MAX_MPX_CTRS, NULL );
    printf("\nNumber of available events: %d", max_event );
 
    /* Fill up the event set with as many non-derived events as we can */
    EventCode = PAPI_PRESET_MASK;
    do {
        if ( PAPI_get_event_info( EventCode, &pset ) == PAPI_OK ) {
            if ( pset.count && ( strcmp( pset.derived, "NOT_DERIVED" ) == 0 ) ) {
                retval = PAPI_add_event( EventSet, ( int ) pset.event_code );
                if ( retval != PAPI_OK )
                    test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
                else {
                    //printf( "Added %s\n", pset.symbol );
                    num_event++;
                }
            }
        }
    } while ( ( PAPI_enum_event( &EventCode, PAPI_PRESET_ENUM_AVAIL ) == PAPI_OK ) && ( num_event < max_event ) );
    
    /* Memory asignament to values and events*/    
    events = ( int * ) malloc( ( size_t ) num_event * sizeof ( int ) );
    if ( events == NULL )
        test_fail( __FILE__, __LINE__, "Error malloc events", 0 );
    values = ( long long * ) malloc( ( size_t ) num_event * sizeof ( long long ) );
    if ( values == NULL )
        test_fail( __FILE__, __LINE__, "Erro malloc values", 0 );

    /* Start counting events */
    if ((retval=PAPI_start(EventSet)) != PAPI_OK)
        test_fail(__FILE__, __LINE__, "PAPI_start", retval);

    /* Matrix-Matrix multiply */
    matmul(a, b, c, mrows, ncolumns, pcolumns);

    /* Read the counters */
    if ((retval=PAPI_read( EventSet, values )) != PAPI_OK)
        test_fail(__FILE__, __LINE__, "PAPI_read_counters", retval);
   
    /* Stop counting events */
    if ((retval=PAPI_stop( EventSet, values )) != PAPI_OK)
        test_fail(__FILE__, __LINE__, "PAPI_stop_counters", retval);

    /* List the events in the event set */
    retval = PAPI_list_events( EventSet, events, &num_event );
    if ( retval != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_list_events", retval );

    /* Print results */
    printf("\nNumber of non-zero events: %d\n", num_event );
    printf( "\nCounts of non-zero available events........................................................\n" );
    printf("Name: \t\t\t  Value: \t Description:\n");
    for ( i = 0; i < num_event; i++ ) {
        PAPI_event_code_to_name( events[i], evname );   // Obtaining name of available events
        PAPI_get_event_info(events[i], &pset);
        if ( values[i] != 0 )  printf("%s \t %15lld \t %s\n", evname, values[i], pset.long_descr);
    }
    printf( "\nCounts of zero available events............................................................\n" );
    printf("Name: \t\t\t  Value: \t Description:\n");
    for ( i = 0; i < num_event; i++ ) {
        PAPI_event_code_to_name( events[i], evname );   // Obtaining name of available events
        PAPI_get_event_info(events[i], &pset);
        if ( values[i] == 0 )  printf("%s \t %15lld \t %s\n", evname, values[i], pset.long_descr);
    }

    /* Check if counter pair(s) had identical values */
    for ( i = 0; i < num_event; i++ ) {
        for ( i = j+1; j < num_event; j++ ) {
            if ( ( i != j ) && ( values[i] == values[j] ) ) k++;  
        }
    }
    if ( k != 0 ) {
        printf( "\nCaution: %d counter pair(s) had identical values\n", k );
    }
    printf("\n");

    /* Free memory */
    free( events );
    free( values );
    free( a );
    free( b );
    free( c );

    /* Cleaning events */
    retval = PAPI_cleanup_eventset( EventSet );
    if ( retval != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
    
    /* Destroying events */
    retval = PAPI_destroy_eventset( &EventSet );
    if ( retval != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );

    return 0;
}
Exemplo n.º 8
0
int GPTLcreate_and_start_events (const int t)  /* thread number */
{
  int ret; /* return code */
  int n;   /* loop index over events */
  char eventname[PAPI_MAX_STR_LEN]; /* returned from PAPI_event_code_to_name */

  /* Create the event set */

  if ((ret = PAPI_create_eventset (&EventSet[t])) != PAPI_OK)
    return GPTLerror ("GPTLcreate_and_start_events: thread %d failure creating eventset: %s\n", 
		      t, PAPI_strerror (ret));

  if (verbose)
    printf ("GPTLcreate_and_start_events: successfully created eventset for thread %d\n", t);

  /* Add requested events to the event set */

  for (n = 0; n < npapievents; n++) {
    if ((ret = PAPI_add_event (EventSet[t], papieventlist[n])) != PAPI_OK) {
      if (verbose) {
	fprintf (stderr, "%s\n", PAPI_strerror (ret));
	ret = PAPI_event_code_to_name (papieventlist[n], eventname);
	fprintf (stderr, "GPTLcreate_and_start_events: failure adding event:%s\n",
		 eventname);
      }

      if (enable_multiplexing) {
        if (verbose)
	  printf ("Trying multiplexing...\n");
	is_multiplexed = true;
	break;
      } else
	return GPTLerror ("enable_multiplexing is false: giving up\n");
    }
  }

  if (is_multiplexed) {

    /* Cleanup the eventset for multiplexing */

    if ((ret = PAPI_cleanup_eventset (EventSet[t])) != PAPI_OK)
      return GPTLerror ("GPTLcreate_and_start_events: %s\n", PAPI_strerror (ret));
    
    if ((ret = PAPI_destroy_eventset (&EventSet[t])) != PAPI_OK)
      return GPTLerror ("GPTLcreate_and_start_events: %s\n", PAPI_strerror (ret));

    if ((ret = PAPI_create_eventset (&EventSet[t])) != PAPI_OK)
      return GPTLerror ("GPTLcreate_and_start_events: failure creating eventset: %s\n", 
			PAPI_strerror (ret));

    if ((ret = PAPI_multiplex_init ()) != PAPI_OK)
      return GPTLerror ("GPTLcreate_and_start_events: failure from PAPI_multiplex_init%s\n", 
			PAPI_strerror (ret));

    if ((ret = PAPI_set_multiplex (EventSet[t])) != PAPI_OK)
      return GPTLerror ("GPTLcreate_and_start_events: failure from PAPI_set_multiplex: %s\n", 
			PAPI_strerror (ret));

    for (n = 0; n < npapievents; n++) {
      if ((ret = PAPI_add_event (EventSet[t], papieventlist[n])) != PAPI_OK) {
	ret = PAPI_event_code_to_name (papieventlist[n], eventname);
	return GPTLerror ("GPTLcreate_and_start_events: failure adding event:%s\n"
			  "  Error was: %s\n", eventname, PAPI_strerror (ret));
      }
    }
  }

  /* Start the event set.  It will only be read from now on--never stopped */

  if ((ret = PAPI_start (EventSet[t])) != PAPI_OK)
    return GPTLerror ("GPTLcreate_and_start_events: failed to start event set: %s\n", 
		      PAPI_strerror (ret));

  return 0;
}
Exemplo n.º 9
0
int main(int argc, char **argv)
{
   PAPI_event_info_t info;
   int i, j, retval;
   int iters = NUM_FLOPS;
   double x = 1.1, y, dtmp;
   long long t1, t2;
   long long values[MAXEVENTS];
   int sleep_time = SLEEPTIME;
#ifdef STARTSTOP
   long long dummies[MAXEVENTS];
#endif
   double valsample[MAXEVENTS][REPEATS];
   double valsum[MAXEVENTS];
   double avg[MAXEVENTS];
   double spread[MAXEVENTS];
   int nevents = MAXEVENTS;
   int eventset = PAPI_NULL;
   int events[MAXEVENTS];
   int fails;

   events[0] = PAPI_FP_INS;
   events[1] = PAPI_TOT_INS;
   events[2] = PAPI_INT_INS;
   events[3] = PAPI_TOT_CYC;
   events[4] = PAPI_STL_CCY;
   events[5] = PAPI_BR_INS;
   events[6] = PAPI_SR_INS;
   events[7] = PAPI_LD_INS;
   events[8] = PAPI_TOT_IIS;

   for (i = 0; i < MAXEVENTS; i++) {
      values[i] = 0;
      valsum[i] = 0;
   }

   if (argc > 1) {
      if (!strcmp(argv[1], "TESTS_QUIET"))
         tests_quiet(argc, argv);
      else {
         sleep_time = atoi(argv[1]);
         if (sleep_time <= 0)
            sleep_time = SLEEPTIME;
      }
   }

   if (!TESTS_QUIET) {
      printf("\nAccuracy check of multiplexing routines.\n");
      printf("Investigating the variance of multiplexed measurements.\n\n");
   }

   if ((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT)
      test_fail(__FILE__, __LINE__, "PAPI_library_init", retval);

#ifdef MPX
   init_multiplex();
#endif

   if ((retval = PAPI_create_eventset(&eventset)))
      test_fail(__FILE__, __LINE__, "PAPI_create_eventset", retval);
#ifdef MPX

   /* In Component PAPI, EventSets must be assigned a component index
      before you can fiddle with their internals.
      0 is always the cpu component */
   retval = PAPI_assign_eventset_component(eventset, 0);
   if (retval != PAPI_OK)
      test_fail(__FILE__, __LINE__, "PAPI_assign_eventset_component", retval);

   if ((retval = PAPI_set_multiplex(eventset)))
      test_fail(__FILE__, __LINE__, "PAPI_set_multiplex", retval);
#endif

   nevents = MAXEVENTS;
   for (i = 0; i < nevents; i++) {
      if ((retval = PAPI_add_event(eventset, events[i]))) {
         for (j = i; j < MAXEVENTS; j++)
            events[j] = events[j + 1];
         nevents--;
         i--;
      }
   }
   if (nevents < 2)
      test_skip(__FILE__, __LINE__, "Not enough events left...", 0);

   /* Find a reasonable number of iterations (each 
    * event active 20 times) during the measurement
    */
   t2 = 10000 * 20 * nevents;   /* Target: 10000 usec/multiplex, 20 repeats */
   if (t2 > 30e6)
      test_skip(__FILE__, __LINE__, "This test takes too much time", retval);

   /* Measure one run */
   t1 = PAPI_get_real_usec();
   y = dummy3(x, iters);
   t1 = PAPI_get_real_usec() - t1;

   if (t2 > t1)                 /* Scale up execution time to match t2 */
      iters = iters * (int)(t2 / t1);
   else if (t1 > 30e6)          /* Make sure execution time is < 30s per repeated test */
      test_skip(__FILE__, __LINE__, "This test takes too much time", retval);

   if ((retval = PAPI_start(eventset)))
      test_fail(__FILE__, __LINE__, "PAPI_start", retval);

   for (i = 1; i <= REPEATS; i++) {
      x = 1.0;

#ifndef STARTSTOP
      if ((retval = PAPI_reset(eventset)))
         test_fail(__FILE__, __LINE__, "PAPI_reset", retval);
#else
      if ((retval = PAPI_stop(eventset, dummies)))
         test_fail(__FILE__, __LINE__, "PAPI_stop", retval);
      if ((retval = PAPI_start(eventset)))
         test_fail(__FILE__, __LINE__, "PAPI_start", retval);
#endif

      if (!TESTS_QUIET)
         printf("\nTest %d (of %d):\n", i, REPEATS);
      t1 = PAPI_get_real_usec();
      y = dummy3(x, iters);
      PAPI_read(eventset, values);
      t2 = PAPI_get_real_usec();

      if (!TESTS_QUIET) {
         printf("\n(calculated independent of PAPI)\n");
         printf("\tOperations= %.1f Mflop", y * 1e-6);
         printf("\t(%g Mflop/s)\n\n", ((float) y / (t2 - t1)));
         printf("PAPI measurements:\n");
      }
      for (j = 0; j < nevents; j++) {
         PAPI_get_event_info(events[j], &info);
         if (!TESTS_QUIET) {
            printf("%20s = ", info.short_descr);
            printf(LLDFMT, values[j]);
            printf("\n");
	 }
         dtmp = (double) values[j];
         valsum[j] += dtmp;
         valsample[j][i-1] = dtmp;
      }
      if (!TESTS_QUIET)
         printf("\n");
   }

   if (!TESTS_QUIET) {
      printf("\n\nEstimated variance relative to average counts:\n");
      for (j = 0; j < nevents; j++)
         printf("   Event %.2d", j);
      printf("\n");
   }

   fails = nevents;
   /* Due to limited precision of floating point cannot really use
      typical standard deviation compuation for large numbers with
      very small variations. Instead compute the std devation
      problems with precision.
   */
   for (j = 0; j < nevents; j++) {
      avg[j] = valsum[j] / REPEATS;
      spread[j] = 0;
      for (i=0; i < REPEATS; ++i) {
	double diff = (valsample[j][i] - avg[j]);
	spread[j] += diff * diff;
      }
      spread[j] = sqrt(spread[j] / REPEATS) / avg[j];
      if (!TESTS_QUIET)
         printf("%9.2g  ", spread[j]);
      /* Make sure that NaN get counted as errors */
      if (spread[j] < MPX_TOLERANCE)
         --fails;
      else if (valsum[j] < MINCOUNTS)   /* Neglect inprecise results with low counts */
         --fails;
   }

   if (!TESTS_QUIET) {
      printf("\n\n");
      for (j = 0; j < nevents; j++) {
         PAPI_get_event_info(events[j], &info);
         printf("Event %.2d: mean=%10.0f, sdev/mean=%7.2g nrpt=%2d -- %s\n",
                j, avg[j], spread[j], REPEATS, info.short_descr);
      }
      printf("\n\n");
   }

   if (fails)
      test_fail(__FILE__, __LINE__, "Values outside threshold", fails);
   else
      test_pass(__FILE__, NULL, 0);

   return 0;
}
Exemplo n.º 10
0
/**
 * Called by the CBTF collector service in order to start data collection.
 */
void cbtf_collector_start(const CBTF_DataHeader* header)
{
/**
 * Start sampling.
 *
 * Starts hardware counter (HWC) sampling for the thread executing this
 * function. Initializes the appropriate thread-local data structures and
 * then enables the sampling counter.
 *
 * @param arguments    Encoded function arguments.
 */
    /* Create and access our thread-local storage */
#ifdef USE_EXPLICIT_TLS
    TLS* tls = malloc(sizeof(TLS));
    Assert(tls != NULL);
    CBTF_SetTLS(TLSKey, tls);
#else
    TLS* tls = &the_tls;
#endif
    Assert(tls != NULL);


    tls->defer_sampling=false;

#ifndef NDEBUG
    IsCollectorDebugEnabled = (getenv("CBTF_DEBUG_COLLECTOR") != NULL);
    IsCollectorDetailsDebugEnabled = (getenv("CBTF_DEBUG_COLLECTOR_DETAILS") != NULL);
#if defined (HAVE_OMPT)
    IsOMPTDebugEnabled = (getenv("CBTF_DEBUG_COLLECTOR_OMPT") != NULL);
#endif
#endif

    /* Decode the passed function arguments */
    // Need to handle the arguments...
    CBTF_hwcsamp_start_sampling_args args;
    memset(&args, 0, sizeof(args));
    args.sampling_rate = 100;

    /* First set defaults */
    int hwcsamp_rate = 100;
    char* hwcsamp_papi_event = "PAPI_TOT_CYC,PAPI_TOT_INS";

#if defined (CBTF_SERVICE_USE_OFFLINE)
    char* hwcsamp_event_param = getenv("CBTF_HWCSAMP_EVENTS");
    if (hwcsamp_event_param != NULL) {
        hwcsamp_papi_event=hwcsamp_event_param;
    }

    const char* sampling_rate = getenv("CBTF_HWCSAMP_RATE");
    if (sampling_rate != NULL) {
        hwcsamp_rate=atoi(sampling_rate);
    }
    args.collector = 1;
    args.experiment = 0;
    tls->data.interval = (uint64_t)(1000000000) / (uint64_t)(hwcsamp_rate);;
#endif


    /* Initialize the actual data blob */
    memcpy(&tls->header, header, sizeof(CBTF_DataHeader));
    initialize_data(tls);


    /* We can not assign mpi rank in the header at this point as it may not
     * be set yet. assign an integer tid value.  omp_tid is used regardless of
     * whether the application is using openmp threads.
     * libmonitor uses the same numbering scheme as openmp.
     */
    tls->header.omp_tid = monitor_get_thread_num();
    tls->header.id = strdup(cbtf_collector_unique_id);
    tls->header.time_begin = CBTF_GetTime();


#ifndef NDEBUG
	if (IsCollectorDebugEnabled) {
	    fprintf(stderr,"[%ld,%d] ENTER cbtf_collector_start\n",tls->header.pid,tls->header.omp_tid);
	}
#endif
    if(hwcsamp_papi_init_done == 0) {
#ifndef NDEBUG
	if (IsCollectorDebugEnabled) {
	    fprintf(stderr,"[%ld,%d] cbtf_collector_start: initialize papi\n",tls->header.pid,tls->header.omp_tid);
	}
#endif
	CBTF_init_papi();
	tls->EventSet = PAPI_NULL;
	tls->data.clock_mhz = (float) hw_info->mhz;
	hwcsamp_papi_init_done = 1;
    } else {
	tls->data.clock_mhz = (float) hw_info->mhz;
    }


    /* PAPI SETUP */
    CBTF_Create_Eventset(&tls->EventSet);

    int rval = PAPI_OK;

#ifndef NDEBUG
    if (IsCollectorDebugEnabled) {
       fprintf(stderr, "PAPI Version: %d.%d.%d.%d\n", PAPI_VERSION_MAJOR( PAPI_VERSION ),
                        PAPI_VERSION_MINOR( PAPI_VERSION ),
                        PAPI_VERSION_REVISION( PAPI_VERSION ),
                        PAPI_VERSION_INCREMENT( PAPI_VERSION ) );
       fprintf(stderr,"System has %d hardware counters.\n", PAPI_num_counters());
    }
#endif

/* In Component PAPI, EventSets must be assigned a component index
 * before you can fiddle with their internals. 0 is always the cpu component */
#if (PAPI_VERSION_MAJOR(PAPI_VERSION)>=4)
    rval = PAPI_assign_eventset_component( tls->EventSet, 0 );
    if (rval != PAPI_OK) {
        CBTF_PAPIerror(rval,"CBTF_Create_Eventset assign_eventset_component");
        return;
    }
#endif

    /* NOTE: if multiplex is turned on, papi internaly uses a SIGPROF handler.
     * Since we are sampling potentially with SIGPROF or now SIGRTMIN and we
     * prefer to limit our events to 6, we do not need multiplexing.
     */
    if (getenv("CBTF_HWCSAMP_MULTIPLEX") != NULL) {
#if !defined(RUNTIME_PLATFORM_BGP) 
	rval = PAPI_set_multiplex( tls->EventSet );
	if ( rval == PAPI_ENOSUPP) {
	    fprintf(stderr,"CBTF_Create_Eventset: Multiplex not supported\n");
	} else if (rval != PAPI_OK)  {
	    CBTF_PAPIerror(rval,"CBTF_Create_Eventset set_multiplex");
	}
#endif
    }

    /* TODO: check return values of direct PAPI calls
     * and handle them as needed.
     */
    /* Rework the code here to call PAPI directly rather than
     * call any OPENSS helper functions due to inconsitent
     * behaviour seen on various lab systems
     */
    int eventcode = 0;
    rval = PAPI_OK;
    if (hwcsamp_papi_event != NULL) {
	char *tfptr, *saveptr=NULL, *tf_token;
	tfptr = strdup(hwcsamp_papi_event);
	for (tf_token = strtok_r(tfptr, ",", &saveptr);
	     tf_token != NULL;
	     tf_token = strtok_r(NULL, ",", &saveptr) ) {

	    PAPI_event_name_to_code(tf_token,&eventcode);
	    rval = PAPI_add_event(tls->EventSet,eventcode);

	    if (rval != PAPI_OK)  {
		CBTF_PAPIerror(rval,"CBTF_Create_Eventset PAPI_event_name_to_code");
	    }
	}

	if (tfptr) free(tfptr);
	
    } else {
	PAPI_event_name_to_code("PAPI_TOT_CYC",&eventcode);
	rval = PAPI_add_event(tls->EventSet,eventcode);
	PAPI_event_name_to_code("PAPI_TOT_INS",&eventcode);
	rval = PAPI_add_event(tls->EventSet,eventcode);
    }

#if defined (HAVE_OMPT)
    /* these are ompt specific.*/
    /* initialize the flags and counts for idle,wait_barrier.  */
    tls->thread_idle =  tls->thread_wait_barrier = tls->thread_barrier = false;
#endif

    /* Begin sampling */
    tls->header.time_begin = CBTF_GetTime();
    CBTF_Start(tls->EventSet);
    CBTF_Timer(tls->data.interval, hwcsampTimerHandler);
}
void *watch_process(void* _pid) {


  pid_t pid = *((pid_t*)_pid);
  printf("%d\n", pid);
  int retval, EventSet = PAPI_NULL;

  //char events_name[NB_EVENTS][PAPI_MAX_STR_LEN] = {"UNHALTED_CORE_CYCLES", "INSTRUCTION_RETIRED", "MEM_LOAD_RETIRED:L1D_HIT", "MEM_LOAD_RETIRED:L2_HIT", "LLC_REFERENCES"};
  //char events_name[NB_EVENTS][PAPI_MAX_STR_LEN] = {"UNHALTED_CORE_CYCLES", "INSTRUCTION_RETIRED", "MEM_LOAD_RETIRED:L2_HIT", "L2_RQSTS:LOADS", "L2_DATA_RQSTS:ANY"};
  //char events_name[NB_EVENTS][PAPI_MAX_STR_LEN] = {"UNHALTED_CORE_CYCLES", "INSTRUCTION_RETIRED", "L1I:READS", "L1D_CACHE_LD:MESI", "L1D_CACHE_ST:MESI"};
  //char events_name[NB_EVENTS][PAPI_MAX_STR_LEN] = {"UNHALTED_CORE_CYCLES", "INSTRUCTION_RETIRED", "L1D_CACHE_LD:MESI", "L1D_CACHE_ST:MESI", "L2_DATA_RQSTS:ANY" };
  char events_name[NB_EVENTS][PAPI_MAX_STR_LEN] = {"UNHALTED_CORE_CYCLES", "INSTRUCTION_RETIRED", "L1I:READS", "L1D_CACHE_LD:MESI", "L1D_CACHE_ST:MESI", "MEM_LOAD_RETIRED:L2_HIT", "L2_RQSTS:LOADS", "L2_DATA_RQSTS:ANY", "BR_INST_RETIRED:ALL_BRANCHES", "BR_INST_EXEC:ANY", "BR_MISP_EXEC:ANY" };
  unsigned int native_events[NB_EVENTS];
  long long values[NB_EVENTS];


  int err;
  /* Initialize the library */
  if ( (err = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT) {
    fprintf(stderr, "PAPI library init error! %d\n", err);
    exit(1);
  }

  if ( (err = PAPI_multiplex_init()) != PAPI_OK) {
    fprintf(stderr, "PAPI multiplex init error! %d\n", err);
    exit(1);
  }

  
  if (PAPI_thread_init(pthread_self) != PAPI_OK) {
    fprintf(stderr, "PAPI thread init error! %d\n", err);
    exit(1);
  }


  //sched_setaffinity(0, 0);
  //if ((retval = PAPI_set_granularity(PAPI_GRN_MAX)) != PAPI_OK) {
  //  fprintf(stderr, "PAPI set granurality error! %d, %d, %d, %d, %d\n", retval, PAPI_EINVAL, PAPI_ENOEVST, PAPI_ENOCMP, PAPI_EISRUN);
  //  exit(1);
  //}

  // Traduction des string en code
  name_to_code(events_name, NB_EVENTS, native_events);
  // Vérification des évènements
  check_events(native_events, NB_EVENTS);
  
  if (PAPI_create_eventset(&EventSet) != PAPI_OK) {    
    fprintf(stderr, "PAPI EventSet init error!\n");
    exit(1);
  }

  if ( (err = PAPI_assign_eventset_component( EventSet, 0 ) ) != PAPI_OK ) {
    fprintf(stderr, "PAPI assign component error!\n%s\n", PAPI_strerror(err));
    exit(1);
  }

  if ( ( err = PAPI_set_multiplex(EventSet) ) != PAPI_OK) {    
    fprintf(stderr, "PAPI set multiplex error!\n%s\n", PAPI_strerror(err));
    exit(1);
  }

  if ( (retval = PAPI_add_events(EventSet, native_events, NB_EVENTS) ) != PAPI_OK) {
    fprintf(stderr, "PAPI add events error!\n%s\n", PAPI_strerror(retval) );
    exit(1);
  }

  if (PAPI_attach(EventSet, pid) != PAPI_OK)
    exit(1);

  while(while_watch) {
    /* Start counting */
    if (PAPI_start(EventSet) != PAPI_OK) {
      fprintf(stderr, "PAPI start error!\n");
      exit(1);
    }
    usleep(1000000);

    if (PAPI_stop(EventSet, values) != PAPI_OK) {
      fprintf(stderr, "PAPI stop error!\n");
      exit(1);
    }

    print_values(events_name, values, NB_EVENTS);


  }

	return 0;
}
Exemplo n.º 12
0
int main(int argc, char **argv)
{
	#if defined(PARALLEL)  || defined(PARTIALPAR) 
		omp_set_num_threads(initParams::nprocs);
		size_t                      threadID;
		std::vector<size_t>         neighbourThreadId;
		std::vector<size_t>         workingThreadId;
		std::vector<cv::KeyPoint>   globalKeypoints;
		cv::Mat                     globalDescriptors;
		std::vector<size_t>         globalIdx(initParams::nprocs/2,   0);
		std::vector<size_t>         levelThreadIdx;
	#endif

	cv::Mat 		frame;
	cv::VideoCapture 	cap;
	class initParams 	params;
	// Set motion field norm threshold. If intra-frame motion greater than this threshold carry out outlier classification, rotation compensation, FOE and TTC computations
	#if !defined(PARALLEL) 
	const float 		OFThresh = 10.0f;
	#endif
	
	#ifdef USE_PAPI
	int events[NUM_EVENTS] = {
					PAPI_L1_DCM,
					PAPI_L2_DCA,
					PAPI_L2_DCM
				 };

	long long values[NUM_EVENTS+1];
	long long start_time = PAPI_get_real_usec();
	double total_time = 0.0f;
	#endif

	// Initialization of parameters
	params.set();
	if (argc < 3)
	{
		std::cerr << "Run Configuration :\n\t./[program name] [input video file] [Total # of frames in video file, 798 for data set-1 and 977 for data set-2]\n";
		exit(EXIT_FAILURE);
	}

	cap.open(argv[1]);
	if(!cap.isOpened()){
		std::cerr << "Could not access video file: " << argv[1] << std::endl;
		exit(EXIT_FAILURE);
	}
	size_t frameTotal = (size_t) std::atoi(argv[2]);

	class agast 			objExtractor;
	class FindRobustFeatures	rFeatures(&objExtractor, &params);

	cv::Mat tmpDescriptors;
	std::vector<cv::KeyPoint> tmpKeypoints;

	#ifdef WRITEDATA
	analysis::TTCvector.resize((size_t)cap.get(CV_CAP_PROP_FRAME_COUNT), 0.0f);
	analysis::kpVector.resize((size_t)cap.get(CV_CAP_PROP_FRAME_COUNT), 0);
	analysis::outliersVector.resize((size_t)cap.get(CV_CAP_PROP_FRAME_COUNT), 0);
	analysis::matchOutliersVector.resize((size_t)cap.get(CV_CAP_PROP_FRAME_COUNT), 0);
	analysis::OFNorm.resize((size_t)cap.get(CV_CAP_PROP_FRAME_COUNT), 0);
	analysis::keypointsVector.resize((size_t)cap.get(CV_CAP_PROP_FRAME_COUNT), 0);
	#endif
	#if defined(PROF) || defined(WRITEDATA)
	time_t          rawtime;
        char            buffer[80];
        time(&rawtime);
        struct  tm      * timeinfo      = localtime(&rawtime);
        strftime(buffer,80, "%d-%m-%y_%H-%M-%S", timeinfo);
	analysis::timeStamp       = std::string(buffer);
	#endif
	#ifdef PROF
	analysis::perfTime.resize((size_t)cap.get(CV_CAP_PROP_FRAME_COUNT), std::vector<float>(3, 0.0f));
	#endif

	#ifdef USE_PAPI
	//********** PAPI Module initialization for performance analysis***********//
	// Setup PAPI library
	int ret = PAPI_library_init(PAPI_VER_CURRENT);
	if (ret != PAPI_VER_CURRENT) {
		printf("Problem while initializing PAPI library: %s\n", PAPI_strerror(ret));
		exit(EXIT_FAILURE);
	}
	// Init multiplexer
	ret = PAPI_multiplex_init();
	if (ret != PAPI_OK) {
		printf("Problem while initializing PAPI multiplexer: %s\n", PAPI_strerror(ret));
		exit(1);
	}
	// Create event set for the events that we want to measure
	int eventset = PAPI_NULL;
	ret = PAPI_create_eventset(&eventset);
	if (ret != PAPI_OK) {
		printf("Problem while creating PAPI event set: %s\n", PAPI_strerror(ret));
		exit(1);
	}
	ret = PAPI_add_event(eventset, PAPI_L1_DCA);
	if (ret != PAPI_OK) {
		printf("Problem while adding first PAPI event: %s\n", PAPI_strerror(ret));
		exit(1);
	}
	// set multiplexer for event set
 	ret = PAPI_set_multiplex(eventset);
	if (ret != PAPI_OK) {
		printf("Problem while setting PAPI multiplex: %s\n", PAPI_strerror(ret));
		exit(1);
	}
	// Add events to event set
	ret = PAPI_add_events(eventset, events, NUM_EVENTS);
	if (ret != PAPI_OK) {
		printf("Problem while adding PAPI events: %s\n", PAPI_strerror(ret));
		exit(1);
	}
	//********** END INITIALIZATION **********//

	// Start measuring computation runtime
	start_time = PAPI_get_real_usec();
	// Start measuring events in event set
	ret = PAPI_start(eventset);
	if (ret != PAPI_OK) {
		printf("Problem while starting PAPI eventset: %s\n", PAPI_strerror(ret));
		exit(1);
	}
	#endif

	try
	{	
		while(( (size_t)cap.get(CV_CAP_PROP_POS_FRAMES) + initParams::frameDelay ) <  frameTotal)
		{

			/****************************PARALLEL-REGION************************************/
			#if defined(PARALLEL) || defined(PARTIALPAR)
				globalIdx.resize(initParams::nprocs/2, 0);
				analysis::refFrameCount = cap.get(CV_CAP_PROP_POS_FRAMES);
				analysis::frameCount    = cap.get(CV_CAP_PROP_POS_FRAMES);
				for (size_t i = 0; i < initParams::nprocs; ++i) 
				{ 
					if (i % 2 == 0) { workingThreadId.push_back(i); }
					else 		{ neighbourThreadId.push_back(i); }
				}

				#pragma omp parallel num_threads(initParams::nprocs) private(threadID, frame) shared(globalIdx, params, neighbourThreadId, workingThreadId, objExtractor)
				{
					threadID         = omp_get_thread_num();
					// get index of the frame to be handled by a particular thread executed only once:
					size_t myFrameID = 0;

					#pragma omp for ordered schedule(static,1)
					for (size_t i = 0; i < initParams::nprocs; ++i)
					{
						#pragma omp ordered
						myFrameID = threadID + analysis::frameCount;
						analysis::frameCount++;
					}


					class agast objExtractorLoc;

					while(cap.get(CV_CAP_PROP_POS_FRAMES) != myFrameID) { std::this_thread::sleep_for(std::chrono::milliseconds(5)); }

					// get first frame and extract new features:
					cap >> objExtractorLoc.frame;
					objExtractorLoc.extractFeatures();

					if (threadID == initParams::nprocs-1) { objExtractorLoc.frame.copyTo(objExtractor.frame); }

					#pragma omp master
					{	
						// keep ref. of the first keypoints and their descriptors:
						objExtractor.descriptorsOld    = objExtractorLoc.descriptors.clone();
						objExtractor.keypointsOld      = objExtractorLoc.keypoints;
						#ifdef VISUALIZE
						objExtractorLoc.frame.copyTo(objExtractor.frameOld);
						#endif
					}

					// follow keypoints over the next framedelay # of frames:
					objExtractorLoc.swap();
					cap >> objExtractorLoc.frame;
					objExtractorLoc.extractFeatures();
					objExtractorLoc.featureMatching();
					#if ! defined(WRITEDATA)
					objExtractorLoc.homographyRANSAC(3.0f);
					#else 
					objExtractorLoc.homographyRANSAC(3.0f, analysis::outliers, analysis::frameCount, analysis::outliersVector);
					#endif
					if((size_t)objExtractorLoc.goodMatches.size() > 5)
						objExtractorLoc.swapGM();

					if (!globalIdx.empty())
					{	
						helperFunc::storeGlobal(globalKeypoints, globalDescriptors, objExtractorLoc, threadID, globalIdx, neighbourThreadId);
						#pragma omp barrier
						helperFunc::recomputeInterThreadGoodMatches(globalKeypoints, globalDescriptors, objExtractorLoc, workingThreadId, globalIdx, threadID, neighbourThreadId);
					}
					
					#pragma omp master
					{
						// re-swaping into global extractors' new points, due to swaping carried out at the end of inter thread matching:
						objExtractor.keypoints   = objExtractorLoc.keypointsOld;
						objExtractor.descriptors = objExtractorLoc.descriptorsOld.clone();
						
						objExtractor.featureMatching();
						rFeatures.homographyRANSAC(3.0f, true);

						#ifdef VISUALIZE
						analysis::frameCount    = cap.get(CV_CAP_PROP_POS_FRAMES) - 1;
						#endif

					}
				}
				//Compute velocity projection vectors:
				rFeatures.compute();
				
				objExtractor.goodMatches.clear();
				
				#ifdef VISUALIZE
				//Analyze and verify data if necessary:
				if((char)cv::waitKey(cap.get(CV_CAP_PROP_FPS))==27) break;
				cv::waitKey(10);
				#endif

			/****************************SERIAL-REGION**************************************/
			#else
				//Get first frame and extract new features:
				analysis::refFrameCount = cap.get(CV_CAP_PROP_POS_FRAMES);
				#if defined(VISUALIZE) || defined(WRITEIMAGE)
				cv::Mat refFrame;
				#endif
				cap >> objExtractor.frame;
				#if defined(VISUALIZE) || defined(WRITEIMAGE)
				objExtractor.frame.copyTo(refFrame);
				#endif
				objExtractor.extractFeatures();
				#ifdef PROF
				analysis::perfTime[analysis::frameCount][2] = objExtractor.elapsedTime.count();
				#endif

				// Keep ref. of the first keypoints and their descriptors:
				objExtractor.descriptors.copyTo(tmpDescriptors);
				tmpKeypoints            = objExtractor.keypoints;
				// Specify initial features as old features:
				objExtractor.swap();

				// Follow keypoints over the next frames:
				while((size_t)cap.get(CV_CAP_PROP_POS_FRAMES) < frameTotal ) 
				{
					analysis::frameCount = cap.get(CV_CAP_PROP_POS_FRAMES);
					analysis::outliers = 0; 

					cap >> objExtractor.frame;
					objExtractor.extractFeatures();

					#ifdef PROF
					analysis::perfTime[analysis::frameCount][2] = objExtractor.elapsedTime.count();
					analysis::start = std::chrono::high_resolution_clock::now();
					#endif

					// FLANN based matching of keypoints:
					objExtractor.featureMatching();

					#ifdef PROF
					analysis::stop                              = std::chrono::high_resolution_clock::now();
					analysis::elapsedTime                       = analysis::stop - analysis::start;
					analysis::perfTime[analysis::frameCount][0] = analysis::elapsedTime.count();
					#endif

					#ifdef VISUALIZE
					analysis::visMatchingPts(objExtractor);
					cv::waitKey(5);
					#endif

					#ifdef WRITEDATA
					analysis::keypointsVector[analysis::frameCount]     = std::min((size_t)objExtractor.keypoints.size(), (size_t)objExtractor.keypointsOld.size());
					analysis::matchOutliersVector[analysis::frameCount] = analysis::keypointsVector[analysis::frameCount] - (size_t)objExtractor.goodMatches.size();
					#endif

					#ifdef PROF
					analysis::start = std::chrono::high_resolution_clock::now();
					#endif

					// RANSAC based outlier classification:
					rFeatures.homographyRANSAC(1.2f, true);

					#ifdef PROF
					analysis::stop                              = std::chrono::high_resolution_clock::now();
					analysis::elapsedTime                       = analysis::stop - analysis::start;
					analysis::perfTime[analysis::frameCount][1] = analysis::elapsedTime.count();
					#endif

					#ifdef VISUALIZE
					analysis::visMatchingPts(objExtractor);
					cv::waitKey(5);
					#endif
					
					float OF = 0.0f;
					// Calculation of motion field norm makes sense only if matching points are greater than 3
					if ((size_t)objExtractor.goodMatches.size() > 3)
					{
						// Calculate intra-frame motion flow norm
						OF = helperFunc::calcMeanOFNorm(objExtractor.keypoints, objExtractor.keypointsOld, objExtractor.goodMatches);
					}
					#ifdef WRITEDATA
					analysis::OFNorm[analysis::frameCount] = helperFunc::calcMeanOFNorm(objExtractor.keypoints, objExtractor.keypointsOld, objExtractor.goodMatches);
					#endif

					// Different cases of computation: 
					if ((size_t)objExtractor.goodMatches.size() < 5 || OF >= OFThresh) 
					{
						// Case 1: Matching(refFrame(n) , refFrame(n+1)) lead to no good matches
						if (analysis::refFrameCount == (analysis::frameCount - 1) ) 
						{ 
							// If large motion field norm was the case then compute ttc: 
							if (OF >= OFThresh)
							{	
								//Compute velocity projection vectors and ttc:
								rFeatures.compute();
							}
							// Define latest frame as refFrame 
							analysis::refFrameCount = analysis::frameCount;
							objExtractor.descriptors.copyTo(tmpDescriptors);
							tmpKeypoints            = objExtractor.keypoints;
							#if defined(VISUALIZE) || defined(WRITEIMAGE)
							refFrame = objExtractor.frame.clone();
							#endif

							// Move latest frame data to old
							objExtractor.swap();   
						}
						// Case 2: Matching(refFrame(n+i) , refFrame(n+i+1)) lead to no good matches
						else 
						{
							// In case of motion flow greater than threshold compute ttc for the given intra-frame motion otherwise move consistent features and compute motion flow
							if (OF >= OFThresh)
							{	
								// Compute velocity projection vectors:
								rFeatures.compute();
								// Define latest frame as refFrame 
								analysis::refFrameCount = analysis::frameCount;
								objExtractor.descriptors.copyTo(tmpDescriptors);
								tmpKeypoints            = objExtractor.keypoints;
								#if defined(VISUALIZE) || defined(WRITEIMAGE)
								refFrame = objExtractor.frame.clone();
								#endif

								// Move latest frame data to old
								objExtractor.swap();
							}
							else 	
							{

								// Move consistent good matching features to new
								objExtractor.keypoints.swap(objExtractor.keypointsOld);
								objExtractor.keypointsOld.swap(tmpKeypoints);

								cv::Mat placeHolder(objExtractor.descriptors.size(), objExtractor.descriptors.type());
								#ifdef OPTIMIZED
								objExtractor.descriptors.copyTo(placeHolder);
								#else
								placeHolder = objExtractor.descriptors.clone();
								#endif
								objExtractor.descriptors.resize(0);
								objExtractor.descriptorsOld.copyTo(objExtractor.descriptors); 
								tmpDescriptors.copyTo(objExtractor.descriptorsOld );
								tmpDescriptors.resize(0);
								#ifdef OPTIMIZED
								placeHolder.copyTo(tmpDescriptors);
								#else
								tmpDescriptors = placeHolder.clone();
								#endif
								placeHolder.release();
								#if defined(VISUALIZE) || defined(WRITEIMAGE)
								cv::Mat tmpFrame;
								tmpFrame = objExtractor.frame.clone();
								objExtractor.frame    = objExtractor.frameOld.clone();
								objExtractor.frameOld = refFrame.clone();
								refFrame              = tmpFrame.clone();
								tmpFrame.release();
								#endif
								
								// Match refFrame features with consistent good matching features uptil refFrame(n+i)
								objExtractor.featureMatching();

								#ifdef VISUALIZE
								analysis::visMatchingPts(objExtractor, "Using Consistent Features");
								cv::waitKey(10);
								#endif

								// Calculate Motion Flow norm
								OF = helperFunc::calcMeanOFNorm(objExtractor.keypoints, objExtractor.keypointsOld, objExtractor.goodMatches);

								if( OF > OFThresh ) 
								{
									// Compute motion flow and ttc:
									rFeatures.compute();
								}
								objExtractor.goodMatches.clear();
								objExtractor.keypointsOld = tmpKeypoints;
								tmpDescriptors.copyTo(objExtractor.descriptorsOld);	

								// Define latest frame as refFrame 
								analysis::refFrameCount = analysis::frameCount;
							}
						}
					}
					else { objExtractor.swapGM(); }
					#ifdef VISUALIZE
					if((char)cv::waitKey(cap.get(CV_CAP_PROP_FPS))==27) break;
					cv::waitKey(10);
					#endif
				}				
			#endif
		}
	}
	catch(cv::Exception) { std::cerr << "Exception @ frame : " << cap.get(CV_CAP_PROP_POS_FRAMES) << "\n"; }

	#ifdef USE_PAPI
	// Collect data from the event set
 	if ( PAPI_stop(eventset, values) != PAPI_OK ) {
		printf("Problem while reading PAPI event set!\n");
		exit(EXIT_FAILURE);
	}
	// Print results
	total_time = ((double)(PAPI_get_real_usec() - start_time))/1000000;
	printf("Computation execution time: %lf seconds\n", total_time);
	printf("L1 Cache miss rate = %lf \n",((double)values[1])/(values[0]));
	printf("L2 Cache miss rate = %lf \n",((double)values[3])/(values[2]));
	#endif

			/*************************WRITE DATA TO FILE BLOCK********************************/

	/*<---------------------------- REGQUIRES SETTING OF PERSONAL PATH TO SAVING GENERATED DATA -------------------------->*/
	#ifdef WRITEDATA
	std::ofstream file;
	std::string fileName;
	fileName = "/home/ragesam/gitThesis/testCase/poseFeatures/build/ttc_" + analysis::timeStamp + ".dat"; 
	std::cout << "\nWRITING TTC DATA TO FILE \n";
	file.open(fileName);
	if(!file.is_open())
	{
		std::cout << "\nCan not write TTC to file\n";
		std::exit(EXIT_FAILURE);
	}
	else
	{
		for (size_t i = 0; i < (size_t)analysis::TTCvector.size(); ++i)
		{
			file << i << "\t" << analysis::TTCvector[i] << "\t" << analysis::kpVector[i]  <<"\n";
		}
	}
	file.close();

	fileName = "/home/ragesam/gitThesis/testCase/poseFeatures/build/outliers_" + analysis::timeStamp + ".dat"; 
	std::cout << "\nWRITING OUTLIER DATA TO FILE \n";
	file.open(fileName);
	if(!file.is_open())
	{
		std::cout << "\nCan not write outliers to file\n";
		std::exit(EXIT_FAILURE);
	}
	else
	{
		file << "Frame #\t# of Keypoints\tFLANN based outliers\tRANSAC based outliers\tIntra-fram projection vector norm\n";
		for (size_t i = 0; i < (size_t)analysis::outliersVector.size(); ++i)
		{
			file << i << "\t" << analysis::keypointsVector[i] << "\t" << analysis::matchOutliersVector[i] << "\t"  << analysis::outliersVector[i] << "\t" << analysis::OFNorm[i]  <<"\n";
		}
	}
	file.close();
	#endif
	
	#ifdef PROF
	std::ofstream fileProf;
	std::string fileNameProf;
	fileNameProf = "/home/ragesam/gitThesis/testCase/poseFeatures/build/profile_" + analysis::timeStamp + ".dat"; 
	std::cout << "\nWRITING PROFILING DATA TO FILE \n";
	fileProf.open(fileNameProf);
	if(!fileProf.is_open())
	{
		std::cout << "\nCan not write profiling data to file\n";
		std::exit(EXIT_FAILURE);
	}
	else
	{
		fileProf << "Frame #\telapsedTime Matching\telapsedTime RANSAC\telapsedTimeDetection\n";
		for (size_t i = 0; i < (size_t)analysis::perfTime.size(); ++i)
		{
			fileProf << i << "\t" << analysis::perfTime[i][0]  << "\t" << analysis::perfTime[i][1]  << "\t" << analysis::perfTime[i][2]  << "\n";
		}
	}
	fileProf.close();

	#endif

	cap.release();

	return 0;

}
Exemplo n.º 13
0
int case1(void)
{
	int retval, i, EventSet = PAPI_NULL, j = 0, k = 0, allvalid = 1;
	int max_mux, nev, *events;
	long long *values;
	PAPI_event_info_t pset;
	char evname[PAPI_MAX_STR_LEN];

	init_papi();
	init_multiplex();

	retval = PAPI_create_eventset(&EventSet);
	if (retval != PAPI_OK)
		test_fail(__FILE__, __LINE__, "PAPI_create_eventset", retval);

	/* In Component PAPI, EventSets must be assigned a component index
		before you can fiddle with their internals.
		0 is always the cpu component */
	retval = PAPI_assign_eventset_component(EventSet, 0);
	if (retval != PAPI_OK)
		test_fail(__FILE__, __LINE__, "PAPI_assign_eventset_component", retval);

	retval = PAPI_set_multiplex(EventSet);
	if (retval != PAPI_OK)
		test_fail(__FILE__, __LINE__, "PAPI_set_multiplex", retval);

	max_mux = PAPI_get_opt(PAPI_MAX_MPX_CTRS, NULL);
	if (max_mux > 32) max_mux = 32;

	/* Fill up the event set with as many non-derived events as we can */
	printf("\nFilling the event set with as many non-derived events as we can...\n");

	i = PAPI_PRESET_MASK;
	do {
		if (PAPI_get_event_info(i, &pset) == PAPI_OK) 
		{
			if (pset.count && (strcmp(pset.derived,"NOT_DERIVED") == 0))
			{
				retval = PAPI_add_event(EventSet, pset.event_code);
				if (retval != PAPI_OK)
					test_fail(__FILE__, __LINE__, "PAPI_add_event", retval);
				else
				{
					printf("Added %s\n", pset.symbol);
					j++;
				}
			}
		}
	} while ((PAPI_enum_event(&i, PAPI_PRESET_ENUM_AVAIL) == PAPI_OK) && (j < max_mux));

	events = (int *) malloc(j * sizeof(int));
	if (events == NULL)
		test_fail(__FILE__, __LINE__, "malloc events", 0);

	values = (long long *) malloc(j * sizeof(long long));
	if (values == NULL)
		test_fail(__FILE__, __LINE__, "malloc values", 0);

	do_stuff();

	if (PAPI_start(EventSet) != PAPI_OK)
		test_fail(__FILE__, __LINE__, "PAPI_start", retval);

	do_stuff();

	retval = PAPI_stop(EventSet, values);
	if (retval != PAPI_OK)
		test_fail(__FILE__, __LINE__, "PAPI_stop", retval);

	nev =j;
	retval = PAPI_list_events(EventSet, events, &nev);
	if (retval != PAPI_OK)
		test_fail(__FILE__, __LINE__, "PAPI_list_events", retval);

	printf("\nEvent Counts:\n");
	for (i = 0, allvalid = 0; i < j; i++) {
		PAPI_event_code_to_name(events[i], evname);
		printf(TAB1, evname, values[i]);
		if (values[i] == 0)
			allvalid++;
	}
	printf("\n");
	if (allvalid){
		printf("Caution: %d counters had zero values\n", allvalid);
	}

	for (i = 0, allvalid = 0; i < j; i++) {
		for (k = i+1; k < j; k++) {
			if ((i != k) && (values[i] == values[k]))
			{
				allvalid++;
				break;
			}
		}
	}

	if (allvalid){
		printf("Caution: %d counter pair(s) had identical values\n", allvalid);
	}

	free(events);
	free(values);

	retval = PAPI_cleanup_eventset(EventSet);
	if (retval != PAPI_OK)
		test_fail(__FILE__, __LINE__, "PAPI_cleanup_eventset", retval);

	retval = PAPI_destroy_eventset(&EventSet);
	if (retval != PAPI_OK)
		test_fail(__FILE__, __LINE__, "PAPI_destroy_eventset", retval);

	return (SUCCESS);
}
Exemplo n.º 14
0
void
mainloop( int arg )
{
	int allvalid;
	long long *values;
	int EventSet = PAPI_NULL;
	int retval, i, j = 2, skipped_counters=0;
	PAPI_event_info_t pset;

	( void ) arg;

	/* Initialize the library */

	retval = PAPI_library_init( PAPI_VER_CURRENT );
	if ( retval != PAPI_VER_CURRENT )
		test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );

	init_multiplex(  );

	retval = PAPI_create_eventset( &EventSet );
	if ( retval != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );

	/* In Component PAPI, EventSets must be assigned a component index
	   before you can fiddle with their internals.
	   0 is always the cpu component */
	retval = PAPI_assign_eventset_component( EventSet, 0 );
	if ( retval != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_assign_eventset_component",
				   retval );

	retval = PAPI_set_multiplex( EventSet );
        if ( retval == PAPI_ENOSUPP) {
	   test_skip(__FILE__, __LINE__, "Multiplex not supported", 1);
	} else if ( retval != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_set_multiplex", retval );

	if ( ( retval =
		   PAPI_thread_init( ( unsigned
							   long ( * )( void ) ) ( pthread_self ) ) ) !=
		 PAPI_OK ) {
		if ( retval == PAPI_ECMP )
			test_skip( __FILE__, __LINE__, "PAPI_thread_init", retval );
		else
			test_fail( __FILE__, __LINE__, "PAPI_thread_init", retval );
	}

	retval = PAPI_add_event( EventSet, PAPI_TOT_INS );
	if ( ( retval != PAPI_OK ) && ( retval != PAPI_ECNFLCT ) )
		test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
	if ( !TESTS_QUIET ) {
		printf( "Added %s\n", "PAPI_TOT_INS" );
	}

	retval = PAPI_add_event( EventSet, PAPI_TOT_CYC );
	if ( ( retval != PAPI_OK ) && ( retval != PAPI_ECNFLCT ) )
		test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
	if ( !TESTS_QUIET ) {
		printf( "Added %s\n", "PAPI_TOT_CYC" );
	}

	values = ( long long * ) malloc( MAX_TO_ADD * sizeof ( long long ) );
	if ( values == NULL )
		test_fail( __FILE__, __LINE__, "malloc", 0 );

	for ( i = 0; i < PAPI_MAX_PRESET_EVENTS; i++ ) {
		retval = PAPI_get_event_info( i | PAPI_PRESET_MASK, &pset );
		if ( retval != PAPI_OK )
			test_fail( __FILE__, __LINE__, "PAPI_get_event_info", retval );

		if ( pset.count ) {
			printf( "Adding %s\n", pset.symbol );

			retval = PAPI_add_event( EventSet, ( int ) pset.event_code );
			if ( ( retval != PAPI_OK ) && ( retval != PAPI_ECNFLCT ) )
				test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );

			if ( retval == PAPI_OK ) {
				printf( "Added %s\n", pset.symbol );
			} else {
				printf( "Could not add %s\n", pset.symbol );
			}

			do_stuff(  );

			if ( retval == PAPI_OK ) {
				retval = PAPI_start( EventSet );
				if ( retval != PAPI_OK )
					test_fail( __FILE__, __LINE__, "PAPI_start", retval );

				do_stuff(  );

				retval = PAPI_stop( EventSet, values );
				if ( retval != PAPI_OK )
					test_fail( __FILE__, __LINE__, "PAPI_stop", retval );

				if ( values[j] ) {
					if ( ++j >= MAX_TO_ADD )
						break;
				} else {
					retval =
						PAPI_remove_event( EventSet, ( int ) pset.event_code );
					if ( retval == PAPI_OK )
						printf( "Removed %s\n", pset.symbol );
				        /* This added because the test */
				        /* can take a long time if mplexing */
				        /* is broken and all values are 0   */
				        skipped_counters++;
				        if (skipped_counters>MAX_TO_ADD) break;

				}
			}
		}
	}

	retval = PAPI_start( EventSet );
	if ( retval != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_start", retval );

	do_stuff(  );

	retval = PAPI_stop( EventSet, values );
	if ( retval != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_stop", retval );

	test_print_event_header( "multiplex3_pthreads:\n", EventSet );
	allvalid = 0;
	for ( i = 0; i < MAX_TO_ADD; i++ ) {
		printf( ONENUM, values[i] );
		if ( values[i] != 0 )
			allvalid++;
	}
	printf( "\n" );
	if ( !allvalid )
		test_fail( __FILE__, __LINE__, "all counter registered no counts", 1 );

	retval = PAPI_cleanup_eventset( EventSet );	/* JT */
	if ( retval != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );

	retval = PAPI_destroy_eventset( &EventSet );
	if ( retval != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );

	free( values );
	PAPI_shutdown(  );
}
Exemplo n.º 15
0
int main(int argc, char **argv) {

    int retval,max_multiplex,i,EventSet=PAPI_NULL;
    PAPI_event_info_t info;
    int added=0;
    int events_tried=0;

    /* Set TESTS_QUIET variable */
    tests_quiet( argc, argv );

    /* Initialize the library */
    retval = PAPI_library_init( PAPI_VER_CURRENT );
    if ( retval != PAPI_VER_CURRENT ) {
        test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
    }

    retval = PAPI_multiplex_init(  );
    if ( retval != PAPI_OK) {
        test_fail(__FILE__, __LINE__, "Multiplex not supported", 1);
    }

    max_multiplex=PAPI_get_opt( PAPI_MAX_MPX_CTRS, NULL );

    if (!TESTS_QUIET) {
        printf("Maximum multiplexed counters=%d\n",max_multiplex);
    }

    if (!TESTS_QUIET) {
        printf("Trying to multiplex as many as possible:\n");
    }

    retval = PAPI_create_eventset( &EventSet );
    if ( retval != PAPI_OK ) {
        test_fail(__FILE__, __LINE__, "PAPI_create_eventset", retval );
    }

    retval = PAPI_assign_eventset_component( EventSet, 0 );
    if ( retval != PAPI_OK ) {
        test_fail(__FILE__, __LINE__, "PAPI_assign_eventset_component",
                  retval );
    }

    retval = PAPI_set_multiplex( EventSet );
    if ( retval != PAPI_OK ) {
        test_fail(__FILE__, __LINE__, "PAPI_create_multiplex", retval );
    }


    i = 0 | PAPI_PRESET_MASK;
    PAPI_enum_event( &i, PAPI_ENUM_FIRST );
    do {
        retval = PAPI_get_event_info( i, &info );
        if (retval==PAPI_OK) {
            if (!TESTS_QUIET) printf("Adding %s: ",info.symbol);
        }

        retval = PAPI_add_event( EventSet, info.event_code );
        if (retval!=PAPI_OK) {
            if (!TESTS_QUIET) printf("Fail!\n");
        }
        else {
            if (!TESTS_QUIET) printf("Success!\n");
            added++;
        }
        events_tried++;

    } while (PAPI_enum_event( &i, PAPI_PRESET_ENUM_AVAIL ) == PAPI_OK );

    PAPI_shutdown(  );

    if (!TESTS_QUIET) {
        printf("Added %d of theoretical max %d\n",added,max_multiplex);
    }

    if (events_tried<max_multiplex) {
        if (!TESTS_QUIET) {
            printf("Ran out of events before we ran out of room\n");
        }
    }
    else if (added!=max_multiplex) {
        test_fail(__FILE__, __LINE__,
                  "Couldn't max out multiplexed events", 1);
    }

    test_pass( __FILE__, NULL, 0 );
    exit( 0 );
}
Exemplo n.º 16
0
/*
** GPTLcreate_and_start_events: Create and start the PAPI eventset.
**   Threaded routine to create the "event set" (PAPI terminology) and start
**   the counters. This is only done once, and is called from get_thread_num
**   for the first time for the thread.
** 
** Input args: 
**   t: thread number
**
** Return value: 0 (success) or GPTLerror (failure)
*/
int GPTLcreate_and_start_events (const int t)  /* thread number */
{
  int ret; /* return code */
  int n;   /* loop index over events */
  char eventname[PAPI_MAX_STR_LEN]; /* returned from PAPI_event_code_to_name */
  static const char *thisfunc = "GPTLcreate_and_start_events";

  /* 
  ** Set the domain to count all contexts. Only needs to be set once for all threads
  */
  if ((ret = PAPI_set_domain (PAPI_DOM_ALL)) != PAPI_OK)
    return GPTLerror ("%s: thread %d failure setting PAPI domain: %s\n", 
		      thisfunc, t, PAPI_strerror (ret));
  
  /* Create the event set */
  if ((ret = PAPI_create_eventset (&EventSet[t])) != PAPI_OK)
    return GPTLerror ("%s: thread %d failure creating eventset: %s\n", 
		      thisfunc, t, PAPI_strerror (ret));

  if (verbose)
    printf ("%s: successfully created eventset for thread %d\n", thisfunc, t);

  /* Add requested events to the event set */
  for (n = 0; n < npapievents; n++) {
    if ((ret = PAPI_add_event (EventSet[t], papieventlist[n])) != PAPI_OK) {
      if (verbose) {
	fprintf (stderr, "%s\n", PAPI_strerror (ret));
	ret = PAPI_event_code_to_name (papieventlist[n], eventname);
	fprintf (stderr, "%s: failure adding event:%s\n", thisfunc, eventname);
      }

      if (enable_multiplexing) {
        if (verbose)
	  printf ("Trying multiplexing...\n");
	is_multiplexed = true;
	break;
      } else
	return GPTLerror ("enable_multiplexing is false: giving up\n");
    }
  }

  if (is_multiplexed) {

    /* Cleanup the eventset for multiplexing */
    if ((ret = PAPI_cleanup_eventset (EventSet[t])) != PAPI_OK)
      return GPTLerror ("%s: %s\n", thisfunc, PAPI_strerror (ret));
    
    if ((ret = PAPI_destroy_eventset (&EventSet[t])) != PAPI_OK)
      return GPTLerror ("%s: %s\n", thisfunc, PAPI_strerror (ret));

    if ((ret = PAPI_create_eventset (&EventSet[t])) != PAPI_OK)
      return GPTLerror ("%s: failure creating eventset: %s\n", thisfunc, PAPI_strerror (ret));
			
    /* 
    ** Assign EventSet to component 0 (cpu). This step is MANDATORY in recent PAPI releases
    ** in order to enable event multiplexing
    */
    if ((ret = PAPI_assign_eventset_component (EventSet[t], 0)) != PAPI_OK)
      return GPTLerror ("%s: thread %d failure in PAPI_assign_eventset_component: %s\n", 
			thisfunc, t, PAPI_strerror (ret));

    if ((ret = PAPI_multiplex_init ()) != PAPI_OK)
      return GPTLerror ("%s: failure from PAPI_multiplex_init%s\n", thisfunc, PAPI_strerror (ret));

    if ((ret = PAPI_set_multiplex (EventSet[t])) != PAPI_OK)
      return GPTLerror ("%s: failure from PAPI_set_multiplex: %s\n", thisfunc, PAPI_strerror (ret));

    for (n = 0; n < npapievents; n++) {
      if ((ret = PAPI_add_event (EventSet[t], papieventlist[n])) != PAPI_OK) {
	ret = PAPI_event_code_to_name (papieventlist[n], eventname);
	return GPTLerror ("%s: failure adding event:%s Error was: %s\n", 
			  thisfunc, eventname, PAPI_strerror (ret));
      }
    }
  }

  /* Start the event set.  It will only be read from now on--never stopped */
  if ((ret = PAPI_start (EventSet[t])) != PAPI_OK)
    return GPTLerror ("%s: failed to start event set: %s\n", thisfunc, PAPI_strerror (ret));

  return 0;
}
Exemplo n.º 17
0
void *
thread( void *arg )
{
	( void ) arg;			 /*unused */
	int eventset = PAPI_NULL;
	long long values[PAPI_MPX_DEF_DEG];

	int ret = PAPI_register_thread(  );
	if ( ret != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_register_thread", ret );
	ret = PAPI_create_eventset( &eventset );
	if ( ret != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_create_eventset", ret );

	printf( "Event set %d created\n", eventset );

	/* In Component PAPI, EventSets must be assigned a component index
	   before you can fiddle with their internals.
	   0 is always the cpu component */
	ret = PAPI_assign_eventset_component( eventset, 0 );
	if ( ret != PAPI_OK ) {
		test_fail( __FILE__, __LINE__, "PAPI_assign_eventset_component", ret );
	}

	ret = PAPI_set_multiplex( eventset );
        if ( ret == PAPI_ENOSUPP) {
	   test_skip( __FILE__, __LINE__, "Multiplexing not supported", 1 );
	}
	else if ( ret != PAPI_OK ) {
		test_fail( __FILE__, __LINE__, "PAPI_set_multiplex", ret );
	}

	ret = PAPI_add_events( eventset, events, numevents );
	if ( ret < PAPI_OK ) {
		test_fail( __FILE__, __LINE__, "PAPI_add_events", ret );
	}

	ret = PAPI_start( eventset );
	if ( ret != PAPI_OK ) {
		test_fail( __FILE__, __LINE__, "PAPI_start", ret );
	}

	do_stuff(  );

	ret = PAPI_stop( eventset, values );
	if ( ret != PAPI_OK ) {
		test_fail( __FILE__, __LINE__, "PAPI_stop", ret );
	}

	ret = PAPI_cleanup_eventset( eventset );
	if ( ret != PAPI_OK ) {
		test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", ret );
	}

	ret = PAPI_destroy_eventset( &eventset );
	if ( ret != PAPI_OK ) {
		test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", ret );
	}

	ret = PAPI_unregister_thread(  );
	if ( ret != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_unregister_thread", ret );
	return ( NULL );
}
Exemplo n.º 18
0
Arquivo: papi.c Projeto: DundalkIT/pcp
static int
refresh_metrics(int log)
{
    int sts = 0;
    int state = 0;
    int i;
    int number_of_active_counters = 0;
    time_t now;

    now = time(NULL);

    /* Shut down, save previous state. */
    state = check_papi_state();
    if (state & PAPI_RUNNING) {
	sts = PAPI_stop(EventSet, values);
	if (sts != PAPI_OK) {
	    /* futile to continue */
	    handle_papi_error(sts, log);
	    return PM_ERR_VALUE;
	}

	/* Save previous values */ 
	for (i = 0; i < number_of_events; i++){
	    if(papi_info[i].position >= 0) {
		papi_info[i].prev_value += values[papi_info[i].position];
		papi_info[i].position = -1;
	    }
	}

	/* Clean up eventset */
	sts = PAPI_cleanup_eventset(EventSet);
	if (sts != PAPI_OK) {
	    handle_papi_error(sts, log);
	    /* FALLTHROUGH */
	}
	
	sts = PAPI_destroy_eventset(&EventSet); /* sets EventSet=NULL */
	if (sts != PAPI_OK) {
	    handle_papi_error(sts, log);
	    /* FALLTHROUGH */
	}
    }

    /* Initialize new EventSet */
    EventSet = PAPI_NULL;
    if ((sts = PAPI_create_eventset(&EventSet)) != PAPI_OK) {
	handle_papi_error(sts, log);
	return PM_ERR_GENERIC;
    }
    if ((sts = PAPI_assign_eventset_component(EventSet, 0 /*CPU*/)) != PAPI_OK) {
	handle_papi_error(sts, log);
	return PM_ERR_GENERIC;
    }
    if (enable_multiplexing && (sts = PAPI_set_multiplex(EventSet)) != PAPI_OK) {
	handle_papi_error(sts, log);
	/* not fatal - FALLTHROUGH */
    }

    /* Add all survivor events to new EventSet */
    number_of_active_counters = 0;
    for (i = 0; i < number_of_events; i++) {
	if (papi_info[i].metric_enabled == METRIC_ENABLED_FOREVER ||
	    papi_info[i].metric_enabled >= now) {
	    sts = PAPI_add_event(EventSet, papi_info[i].info.event_code);
	    if (sts != PAPI_OK) {
		if (pmDebug & DBG_TRACE_APPL0) {
		    char eventname[PAPI_MAX_STR_LEN];
		    PAPI_event_code_to_name(papi_info[i].info.event_code, eventname);
		    __pmNotifyErr(LOG_DEBUG, "Unable to add: %s due to error: %s\n",
				  eventname, PAPI_strerror(sts));
		}
		handle_papi_error(sts, log);
                /*
                 * We may be called to make drastic changes to the PAPI
                 * eventset.  Partial successes/failures are quite reasonable,
                 * as a user may be asking to activate a mix of good and bad
                 * counters.  Diagnosing the partial failure is left to the 
                 * caller, by examining the papi_info[i].position value
                 * after this function returns. */
		continue;
	    }
	    papi_info[i].position = number_of_active_counters++;
	}
    }

    /* Restart counting. */
    if (number_of_active_counters > 0) {
	sts = PAPI_start(EventSet);
	if (sts != PAPI_OK) {
	    handle_papi_error(sts, log);
	    return PM_ERR_VALUE;
	}
    }
    return 0;
}