コード例 #1
0
void initTimers() {
#if USING_PERFCTR == 0
	int retval;
	fprintf(stderr, "1\n");

	if ((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT) {
		printf("PAPI Library initialization error! \n");
		exit(1);
	}

	fprintf(stderr, "2\n");
	if ((retval = PAPI_set_debug(PAPI_VERB_ESTOP)) != PAPI_OK) {
		printf("PAPI set debug error! \n");
		exit(1);
	}
	fprintf(stderr, "3\n");
	if (PAPI_thread_init(pthread_self) != PAPI_OK) {
		printf("PAPI thread Library initialisation error! \n");
		exit(1);
	}
	fprintf(stderr, "4\n");
	PAPI_thread_init(gettid);
	PAPI_register_thread();
#else
	PERFCTR_initialize();
	PERFCTR_register_thread();
#endif

	gettimeofday(&program_start, NULL);
}
コード例 #2
0
int main(int argc, char **argv)
{
   int i, retval;
   long long elapsed_us, elapsed_cyc;

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

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

   hw_info = PAPI_get_hardware_info();
   if (hw_info == NULL)
     test_fail(__FILE__, __LINE__, "PAPI_get_hardware_info", 2);

   elapsed_us = PAPI_get_real_usec();

   elapsed_cyc = PAPI_get_real_cyc();

#if defined(_AIX)
   retval = PAPI_thread_init((unsigned long (*)(void)) (pthread_self));
   if (retval != PAPI_OK) {
      if (retval == PAPI_ESBSTR)
         test_skip(__FILE__, __LINE__, "PAPI_thread_init", retval);
      else
         test_fail(__FILE__, __LINE__, "PAPI_thread_init", retval);
   }
#pragma ibm parallel_loop
#elif defined(sgi) && defined(mips)
   retval = PAPI_thread_init((unsigned long (*)(void)) (mp_my_threadnum));
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_thread_init", retval);
   }
#pragma parallel
#pragma local(i)
#pragma pfor
#elif defined(sun) && defined(sparc)
   retval = PAPI_thread_init((unsigned long (*)(void)) (thr_self));
   if (retval != PAPI_OK) {
      test_fail(__FILE__, __LINE__, "PAPI_thread_init", retval);
   }
#pragma MP taskloop private(i)
#else
#error "Architecture not included in this test file yet."
#endif
   for (i = 1; i < 3; i++)
      Thread(i, 10000000 * i);

   elapsed_cyc = PAPI_get_real_cyc() - elapsed_cyc;

   elapsed_us = PAPI_get_real_usec() - elapsed_us;

   if (!TESTS_QUIET) {
      printf("Master real usec   : \t%lld\n", elapsed_us);
      printf("Master real cycles : \t%lld\n", elapsed_cyc);
   }
   test_pass(__FILE__, NULL, 0);
   exit(1);
}
コード例 #3
0
ファイル: gptl_papi.c プロジェクト: ACME-Climate/cime
int GPTL_PAPIinitialize (const int maxthreads,     /* number of threads */
			 const bool verbose_flag,  /* output verbosity */
			 int *nevents_out,         /* nevents needed by gptl.c */
			 Entry *pr_event_out)      /* events needed by gptl.c */
{
  int ret;       /* return code */
  int n;         /* loop index */
  int t;         /* thread index */

  verbose = verbose_flag;

  if (maxthreads < 1)
    return GPTLerror ("GPTL_PAPIinitialize: maxthreads = %d\n", maxthreads);

  /* Ensure that PAPI_library_init has already been called */

  if ((ret = GPTL_PAPIlibraryinit ()) < 0)
    return GPTLerror ("GPTL_PAPIinitialize: GPTL_PAPIlibraryinit failure\n");

  /* PAPI_thread_init needs to be called if threading enabled */

#if ( defined THREADED_OMP )
  if (PAPI_thread_init ((unsigned long (*)(void)) (omp_get_thread_num)) != PAPI_OK)
    return GPTLerror ("GPTL_PAPIinitialize: PAPI_thread_init failure\n");
#elif ( defined THREADED_PTHREADS )
  if (PAPI_thread_init ((unsigned long (*)(void)) (pthread_self)) != PAPI_OK)
    return GPTLerror ("GPTL_PAPIinitialize: PAPI_thread_init failure\n");
#endif

  /* allocate and initialize static local space */

  EventSet     = (int *)        GPTLallocate (maxthreads * sizeof (int));
  papicounters = (long_long **) GPTLallocate (maxthreads * sizeof (long_long *));

  for (t = 0; t < maxthreads; t++) {
    EventSet[t] = PAPI_NULL;
    papicounters[t] = (long_long *) GPTLallocate (MAX_AUX * sizeof (long_long));
  }

  *nevents_out = nevents;
  for (n = 0; n < nevents; ++n) {
    pr_event_out[n].counter = pr_event[n].event.counter;
    pr_event_out[n].namestr = pr_event[n].event.namestr;
    pr_event_out[n].str8    = pr_event[n].event.str8;
    pr_event_out[n].str16   = pr_event[n].event.str16;
    pr_event_out[n].longstr = pr_event[n].event.longstr;
  }
  return 0;
}
コード例 #4
0
ファイル: papi.c プロジェクト: jreybert/power
void papi_thread_init() {
  int retval;
  if ( (retval = PAPI_thread_init(pthread_self)) != PAPI_OK) {
    fprintf(stderr, "PAPI thread init error!\n");
  }
  
}
コード例 #5
0
void counter_init( int *eventset, int *num_papi_events )
{
    char error_str[PAPI_MAX_STR_LEN];
    //  int events[] = {PAPI_TOT_INS,PAPI_BR_INS,PAPI_SR_INS};
    int events[] = {PAPI_TOT_CYC,PAPI_L3_TCM};
    int stat;

    int thread = omp_get_thread_num();
    if( thread == 0 )
        printf("Initializing PAPI counters...\n");

    *num_papi_events = sizeof(events) / sizeof(int);

    if ((stat = PAPI_thread_init((long unsigned int (*)(void)) omp_get_thread_num)) != PAPI_OK) {
        PAPI_perror("PAPI_thread_init");
        exit(1);
    }

    if ( (stat= PAPI_create_eventset(eventset)) != PAPI_OK) {
        PAPI_perror("PAPI_create_eventset");
        exit(1);
    }

    for( int i = 0; i < *num_papi_events; i++ ) {
        if ((stat=PAPI_add_event(*eventset,events[i])) != PAPI_OK) {
            PAPI_perror("PAPI_add_event");
            exit(1);
        }
    }

    if ((stat=PAPI_start(*eventset)) != PAPI_OK) {
        PAPI_perror("PAPI_start");
        exit(1);
    }
}
コード例 #6
0
ファイル: Node.C プロジェクト: wware/namd-povray-cloud
static void namdInitPapiCounters(){
	if(CkMyRank()==0){
		//only initialize per OS process (i.e. a charm node)
		int retval = PAPI_library_init(PAPI_VER_CURRENT);
		if(retval != PAPI_VER_CURRENT) {
			if(CkMyPe()==0){
				CkPrintf("ERROR: PAPI library is not compatitible!");
				CkExit();
			}
		}
	#if CMK_SMP
		//now only consider systems that are compatible with POSIX
		if(PAPI_thread_init(pthread_self)!=PAPI_OK) {
			if(CkMyPe()==0){
				CkPrintf("ERROR: multi-thread mode in PAPI could not be initialized!");
				CkExit();
			}
		}
	#endif
	}
	CkpvInitialize(int *, papiEvents);
	CkpvAccess(papiEvents) = new int[NUM_PAPI_EVENTS];

#if MEASURE_PAPI_CACHE
	if(PAPI_query_event(PAPI_L1_DCM)==PAPI_OK) {
		CkpvAccess(papiEvents)[0] = PAPI_L1_DCM;
	}else{
		if(CkMyPe()==0){
			CkPrintf("WARNING: PAPI_L1_DCM doesn't exsit on this platform!\n");			
		}
		//if not default to PAPI_TOT_INS
		CkpvAccess(papiEvents)[0] = PAPI_TOT_INS;
	}

	if(PAPI_query_event(PAPI_L2_DCM)==PAPI_OK) {
		CkpvAccess(papiEvents)[1] = PAPI_L2_DCM;
	}else{
		//if not default to PAPI_TOT_CYC
		CkpvAccess(papiEvents)[1] = PAPI_TOT_CYC;
	}	
#elif MEASURE_PAPI_FLOPS
	if(PAPI_query_event(PAPI_FP_INS)==PAPI_OK) {
		CkpvAccess(papiEvents)[0] = PAPI_FP_INS;
	}else{
		if(CkMyPe()==0){
			CkPrintf("WARNING: PAPI_FP_INS doesn't exsit on this platform!\n");
		}
		//if not default to PAPI_TOT_INS
		CkpvAccess(papiEvents)[0] = PAPI_TOT_INS;
	}

	if(PAPI_query_event(PAPI_FMA_INS)==PAPI_OK) {
		CkpvAccess(papiEvents)[1] = PAPI_FMA_INS;
	}else{
		//if not default to PAPI_TOT_CYC
		CkpvAccess(papiEvents)[1] = PAPI_TOT_CYC;
	}
#endif
}
コード例 #7
0
void 
monitor_init_thread_support(void)
{
  int rval;
  if ((rval = PAPI_thread_init(pthread_self)) != PAPI_OK) {
    DIEx("error: PAPI error (%d): %s.", rval, PAPI_strerror(rval));
  }
}
コード例 #8
0
ファイル: perf.c プロジェクト: jiachengy/numa-db
void
perf_lib_init(const char *perfcfg, const char *perfout)
{
  int retval;

  retval = PAPI_library_init(PAPI_VER_CURRENT);
  assert(retval == PAPI_VER_CURRENT);

  retval = PAPI_thread_init(pthread_self);
  assert(retval == PAPI_OK);

  if (perfcfg)
    PERF_CONFIG = mystrdup(perfcfg);

  if (perfout)
    PERF_OUT = mystrdup(perfout);

  int max_counters = PAPI_num_counters();
  if (max_counters > MAX_PERF_EVENTS)
    max_counters = MAX_PERF_EVENTS;

  PERF_EVENT_NAMES = (char**)malloc(sizeof(char*) * max_counters);  
  assert(PERF_EVENT_NAMES != NULL);
  memset(PERF_EVENT_NAMES, 0x0, sizeof(char*) * max_counters);

  if (PERF_CONFIG) {
    char line[80];
    FILE *config = fopen(PERF_CONFIG, "r");
    assert(config != NULL);

    while (fgets(line, 80, config) != NULL && NUM_EVENTS < max_counters) {
      if (line[0]=='#')
        continue;
      PERF_EVENT_NAMES[NUM_EVENTS] = mystrdup(line);
      NUM_EVENTS++;
    }
    if (!feof(config))
      fprintf(stderr, "Too many counters added. Only take the first %d.\n", max_counters);

    fclose(config);
  }
  else { /* if no config file is specified, add default events only */
    NUM_EVENTS = sizeof(DEFAULT_EVENTS) / sizeof(char*);
    if (NUM_EVENTS > max_counters) {
      NUM_EVENTS = max_counters;
      fprintf(stderr, "Too many counters added. Only take the first %d.\n", max_counters);
    }
    
    for (int i = 0; i < NUM_EVENTS; i++)
      PERF_EVENT_NAMES[i] = mystrdup(DEFAULT_EVENTS[i]);
  }

  for (int i = 0; i != NUM_EVENTS; ++i)
    PERF_COUNTER_INITIALIZER.value[i] = 0;
}
コード例 #9
0
int main(int argc, char **argv)
{
   pthread_t slaves[MAX_THREADS];
   int rc, i, nthr;
   int retval;
  const PAPI_hw_info_t *hwinfo = NULL;

#if defined(__ALPHA) && defined(__osf__)
   test_skip(__FILE__, __LINE__, "thread support not available on this platform!", PAPI_ESBSTR);
#endif

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

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

   if ((hwinfo = PAPI_get_hardware_info()) == NULL)
    test_fail(__FILE__, __LINE__, "PAPI_get_hardware_info", 2);
   
   retval = PAPI_thread_init((unsigned long (*)(void)) (pthread_self));
   if (retval != PAPI_OK) {
      if (retval == PAPI_ESBSTR)
         test_skip(__FILE__, __LINE__, "PAPI_thread_init", retval);
      else
         test_fail(__FILE__, __LINE__, "PAPI_thread_init", retval);
   }

   if (hwinfo->ncpu > MAX_THREADS) 
     nthr = MAX_THREADS;
   else
     nthr = hwinfo->ncpu;

   printf("Creating %d threads\n", nthr);

   for (i=0;i<nthr;i++)
     {
       rc = pthread_create(&slaves[i], NULL, Slave, NULL);
       if (rc) {
	 retval = PAPI_ESYS;
	 test_fail(__FILE__, __LINE__, "pthread_create", retval);
       }
     }

   for (i=0;i<nthr;i++)
     {
       pthread_join(slaves[i], NULL);
     }

   printf("Expected: %lld Received: %lld\n", (long long)nthr*num_iters, count);
   if (nthr*num_iters != count)
      test_fail(__FILE__, __LINE__, "Thread Locks", 1);

   test_pass(__FILE__, NULL, 0);
   exit(1);
}
コード例 #10
0
ファイル: krentel_pthreads.c プロジェクト: arm-hpc/papi
int
main( int argc, char **argv )
{
	pthread_t *td = NULL;
	long n;

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

	if ( argc < 2 || sscanf( argv[1], "%d", &program_time ) < 1 )
		program_time = 6;
	if ( argc < 3 || sscanf( argv[2], "%d", &threshold ) < 1 )
		threshold = 20000000;
	if ( argc < 4 || sscanf( argv[3], "%d", &num_threads ) < 1 )
		num_threads = 3;

	td = malloc((num_threads+1) * sizeof(pthread_t));
	if (!td)
		test_fail( __FILE__, __LINE__, "td malloc failed", 1 );

	printf( "program_time = %d, threshold = %d, num_threads = %d\n\n",
			program_time, threshold, num_threads );

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

	if ( PAPI_thread_init( ( unsigned long ( * )( void ) ) ( pthread_self ) ) !=
		 PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_thread_init failed", 1 );

	if ( pthread_key_create( &key, NULL ) != 0 )
		test_fail( __FILE__, __LINE__, "pthread key create failed", 1 );

	gettimeofday( &start, NULL );

	for ( n = 1; n <= num_threads; n++ ) {
		if ( pthread_create( &(td[n]), NULL, my_thread, ( void * ) n ) != 0 )
			test_fail( __FILE__, __LINE__, "pthread create failed", 1 );
	}

	my_thread( ( void * ) 0 );

	/* wait for all the threads */
	for ( n = 1; n <= num_threads; n++ ) {
	  	if ( pthread_join( td[n], NULL))
			test_fail( __FILE__, __LINE__, "pthread join failed", 1 );
	}

	free(td);

	printf( "done\n" );

	test_pass( __FILE__, NULL, 0 );
	return ( 0 );
}
コード例 #11
0
ファイル: papi.c プロジェクト: ANL-CESAR/SimpleMOC-kernel
// initialize papi with one thread first
void papi_serial_init(void)
{
	if ( PAPI_library_init(PAPI_VER_CURRENT) != PAPI_VER_CURRENT){
		fprintf(stderr, "PAPI library init error!\n");
		exit(1);
	}
	if (( PAPI_thread_init((long unsigned int (*)(void))
					pthread_self )) != PAPI_OK){
		PAPI_perror("PAPI_thread_init");
		exit(1);
	}
}
コード例 #12
0
void vt_metric_thread_init(long (*id_fn)(void))
{
  int retval;

  if ( nmetrics == 0 )
    return;

  retval = PAPI_thread_init((unsigned long (*)(void))(id_fn));
  if ( retval != PAPI_OK)
    metric_error(retval, "PAPI_thread_init");
  vt_cntl_msg(2, "PAPI thread support initialized");
}
コード例 #13
0
int main(int argc, char** argv) {
  pthread_t *callThd;
  int i, numthrds;
  int retval;
  pthread_attr_t attr;

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

  int version = PAPI_library_init (PAPI_VER_CURRENT);
  if (version != PAPI_VER_CURRENT) {
    fprintf(stderr, "PAPI_library_init version mismatch\n");
    exit(1);
  }


  pthread_attr_init(&attr);
  if (PAPI_thread_init(pthread_self) != PAPI_OK) {
    fprintf(stderr, "PAPI_thread_init returned an error\n");
    exit(1);
  }
#ifdef PTHREAD_CREATE_UNDETACHED
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_UNDETACHED);
#endif
#ifdef PTHREAD_SCOPE_SYSTEM
  retval = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
  if (retval != 0) {
    fprintf(stderr,"This system does not support kernel scheduled pthreads.\n");
    exit(1);
  }
#endif

  numthrds = NUM_INFILES;
  if (!TESTS_QUIET) printf("%d threads\n",numthrds);
  callThd = (pthread_t *)malloc(numthrds*sizeof(pthread_t));

  int rc ;
  for (i=0;i<(numthrds-1);i++) {
    rc = pthread_create(callThd+i, &attr, ThreadIO, (void *) files[i]);
    if (rc != 0) perror("Error creating thread using pthread_create()");
  }
  ThreadIO((void *)files[numthrds-1]);
  pthread_attr_destroy(&attr);

  for (i=0;i<(numthrds-1);i++)
    pthread_join(callThd[i], NULL);

  test_pass( __FILE__, NULL, 0 );
  return 0;
}
コード例 #14
0
int main(int argc, char **argv)
{
   pthread_t t1, t2, t3, t4;
   pthread_attr_t attr;
   int retval;

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

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

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

   if (!TESTS_QUIET) {
      printf("Test case: Clock latency and resolution.\n");
      printf("Note: Virtual timers are proportional to # CPU's.\n");
      printf("-------------------------------------------------\n");
   }

   pthread_attr_init(&attr);
#ifdef PTHREAD_CREATE_UNDETACHED
   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_UNDETACHED);
#endif
#ifdef PTHREAD_SCOPE_SYSTEM
   retval = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
   if (retval != 0)
      test_skip(__FILE__, __LINE__, "pthread_attr_setscope", retval);
#endif

   pthread_create(&t1, &attr, pthread_main, NULL);
   pthread_create(&t2, &attr, pthread_main, NULL);
   pthread_create(&t3, &attr, pthread_main, NULL);
   pthread_create(&t4, &attr, pthread_main, NULL);
   pthread_main(NULL);

   pthread_join(t1, NULL);
   pthread_join(t2, NULL);
   pthread_join(t3, NULL);
   pthread_join(t4, NULL);

   test_pass(__FILE__, NULL, 0);
   exit(0);
}
コード例 #15
0
void HWCBE_PAPI_Initialize (int TRCOptions)
{
	UNREFERENCED_PARAMETER(TRCOptions);

	int rc;
	void *thread_identifier_function;

	/* PAPI initialization */
	rc = PAPI_library_init (PAPI_VER_CURRENT);
	if (rc != PAPI_VER_CURRENT)
	{
		if (rc > 0)
		{
			fprintf (stderr,
				PACKAGE_NAME": PAPI library version mismatch!\n"
				"          "PACKAGE_NAME" is compiled against PAPI v%d.%d , and \n"
				"          PAPI_library_init reported v%d.%d ,\n"
				"          Check that LD_LIBRARY_PATH points to the correct PAPI library.\n",
				PAPI_VERSION_MAJOR(PAPI_VER_CURRENT),
				PAPI_VERSION_MINOR(PAPI_VER_CURRENT),
				PAPI_VERSION_MAJOR(rc),
				PAPI_VERSION_MINOR(rc));
		}
		fprintf (stderr, PACKAGE_NAME": Can't use hardware counters!\n");
		fprintf (stderr, PACKAGE_NAME": PAPI library error: %s\n", PAPI_strerror (rc));

		if (rc == PAPI_ESYS)
			perror (PACKAGE_NAME": PAPI system error is ");

		return;
	}

#if defined(PAPI_SAMPLING_SUPPORT)
	/* Use any kind of sampling -- software or hardware */
	SamplingSupport = TRUE;
#endif

	thread_identifier_function = Extrae_get_thread_number_function();

	if (thread_identifier_function != NULL)
	{
		if ((rc = PAPI_thread_init ((unsigned long (*)(void)) thread_identifier_function)) != PAPI_OK)
		{
			fprintf (stderr, PACKAGE_NAME": PAPI_thread_init failed! Reason: %s\n", PAPI_strerror(rc));
			return;
		}
	}
}
コード例 #16
0
MeasureTimePAPI::MeasureTimePAPI(unsigned long int (*threadHandle)()) : MeasureTime(), threadHandle(threadHandle)
{
  _events = new int[NUM_PAPI_EVENTS];

#ifdef USE_PAPI
  _events[0] = PAPI_TOT_CYC;
  _events[1] = PAPI_L2_TCM;
  _events[2] = PAPI_TOT_INS;

  _eventSet = PAPI_NULL;
  int initRetVal = PAPI_library_init(PAPI_VER_CURRENT);

  if (initRetVal != PAPI_VER_CURRENT && initRetVal > 0)
  {
    std::cerr << "PAPI library init failed!" << std::endl;
    exit(1);
  }

  if (PAPI_thread_init(threadHandle) != PAPI_OK)
  {
    std::cerr << "PAPI thread init failed!" << std::endl;
    exit(1);
  }

  if (PAPI_create_eventset(&_eventSet) != PAPI_OK)
  {
    std::cerr << "PAPI create eventset failed!" << " Error: " << PAPI_create_eventset(&_eventSet) << std::endl;
    exit(1);
  }

  if (PAPI_add_events(_eventSet, _events, NUM_PAPI_EVENTS) != PAPI_OK)
  {
    std::cerr << "PAPI add events failed!" << std::endl;
    exit(1);
  }

  if (PAPI_start(_eventSet) != PAPI_OK)
  {
    std::cerr << "PAPI_start_counters - FAILED" << std::endl;
    throw ModelicaSimulationError(UTILITY,"PAPI_start_counters - FAILED");
  }
#else
  _eventSet = 0;
  throw ModelicaSimulationError(UTILITY,"Papi not supported!");
#endif
}
コード例 #17
0
ファイル: performance.c プロジェクト: MattBBaker/systemburn
/**
 * \brief Sets up the performance data gathering system.
 * Calls functions to calibrate the system timers and to initialize the global
 * table for storing performance data.
 */
void performance_init(){
    if(MyRank == ROOT){
        EmitLog(MyRank, SCHEDULER_THREAD, "Calibrating performance timers.", -1, PRINT_ALWAYS);
    }
    ORB_calibrate();

    #ifdef HAVE_PAPI
    /* Initialize PAPI and PAPI threads */
    //TEST_PAPI(PAPI_library_init, PAPI_VER_CURRENT, MyRank, SCHEDULER_THREAD, PRINT_ALWAYS, PAPI_VER_CURRENT);
    TEST_PAPI(PAPI_library_init(PAPI_VER_CURRENT), PAPI_VER_CURRENT, MyRank, SCHEDULER_THREAD, PRINT_ALWAYS);

    //TEST_PAPI(PAPI_thread_init, PAPI_OK, MyRank, SCHEDULER_THREAD, PRINT_ALWAYS, pthread_self);
    TEST_PAPI(PAPI_thread_init(pthread_self), PAPI_OK, MyRank, SCHEDULER_THREAD, PRINT_ALWAYS);
    #endif //HAVE_PAPI

    /* Initialize performance tables */
    perf_table_init();
} /* performance_init */
コード例 #18
0
ファイル: ompscrCommon.cpp プロジェクト: shayany/openmp
void init_PAPI(int numTimers)
{
    int i;

    /* Initialize the library */
    if (PAPI_library_init(PAPI_VER_CURRENT) != PAPI_VER_CURRENT)
        OSCR_error ("PAPI library init error!\n", -1);

    oscrData_timerPAPIEventSet = (int *)calloc(numTimers, sizeof(int));

    if (oscrData_timerPAPIEventSet == NULL)
        OSCR_error("Not enough memory for timers!!", -1);

    if (PAPI_thread_init((unsigned long (*)(void))(omp_get_thread_num()),0) != PAPI_OK)
        OSCR_error("PAPI Thread init error!", -1);

    for (i=0; i<numTimers; i++)
        oscrData_timerPAPIEventSet[i] = 0;
}
コード例 #19
0
ファイル: eventLib.c プロジェクト: ManuelSelva/orcc
void event_init(void) {

    int retval;

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

    // multiplex initialization
    init_multiplex(  );

    // place for initialization in case one makes use of threads
    retval = PAPI_thread_init((unsigned long (*)(void))(pthread_self));
    if ( retval != PAPI_OK )
        test_fail( __FILE__, __LINE__, "PAPI_thread_init", retval );

    printf("event_init done \n");


}
コード例 #20
0
ファイル: kufrin.c プロジェクト: pyrovski/papi-rapl
int
main( int argc, char **argv )
{
	int nthreads = 8, ret, i;
	PAPI_event_info_t info;
	pthread_t *threads;
	const PAPI_hw_info_t *hw_info;

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

	if ( !TESTS_QUIET ) {
		if ( argc > 1 ) {
			int tmp = atoi( argv[1] );
			if ( tmp >= 1 )
				nthreads = tmp;
		}
	}

	ret = PAPI_library_init( PAPI_VER_CURRENT );
	if ( ret != PAPI_VER_CURRENT ) {
		test_fail( __FILE__, __LINE__, "PAPI_library_init", ret );
	}

	hw_info = PAPI_get_hardware_info(  );
	if ( hw_info == NULL )
		test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 2 );

	if ( strcmp( hw_info->model_string, "POWER6" ) == 0 ) {
		ret = PAPI_set_domain( PAPI_DOM_ALL );
		if ( ret != PAPI_OK ) {
			test_fail( __FILE__, __LINE__, "PAPI_set_domain", ret );
		}
	}

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

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

	/* Fill up the event set with as many non-derived events as we can */

	i = PAPI_PRESET_MASK;
	do {
		if ( PAPI_get_event_info( i, &info ) == PAPI_OK ) {
			if ( info.count == 1 ) {
				events[numevents++] = ( int ) info.event_code;
				printf( "Added %s\n", info.symbol );
			} else {
				printf( "Skipping derived event %s\n", info.symbol );
			}
		}
	} while ( ( PAPI_enum_event( &i, PAPI_PRESET_ENUM_AVAIL ) == PAPI_OK )
			  && ( numevents < PAPI_MPX_DEF_DEG ) );

	printf( "Found %d events\n", numevents );

	do_stuff(  );

	printf( "Creating %d threads:\n", nthreads );

	threads =
		( pthread_t * ) malloc( ( size_t ) nthreads * sizeof ( pthread_t ) );
	if ( threads == NULL ) {
		test_fail( __FILE__, __LINE__, "malloc", PAPI_ENOMEM );
	}

	/* Create the threads */
	for ( i = 0; i < nthreads; i++ ) {
		ret = pthread_create( &threads[i], NULL, thread, NULL );
		if ( ret != 0 ) {
			test_fail( __FILE__, __LINE__, "pthread_create", PAPI_ESYS );
		}
	}

	/* Wait for thread completion */
	for ( i = 0; i < nthreads; i++ ) {
		ret = pthread_join( threads[i], NULL );
		if ( ret != 0 ) {
			test_fail( __FILE__, __LINE__, "pthread_join", PAPI_ESYS );
		}
	}

	printf( "Done." );
	test_pass( __FILE__, NULL, 0 );
	pthread_exit( NULL );
	exit( 0 );
}
コード例 #21
0
ファイル: DumpReader.c プロジェクト: DeepnessLab/mca2
void inspectDumpFile(const char *path, int repeat, StateMachine *machine, TableStateMachine *tableMachine, int isTableMachine,
		int verbose, int timing, int threads, int packets_to_steal, int dedicated_use_compressed,
		int work_group_size, int max_wgs, double *thresholds, int drop) {
#else
void inspectDumpFile(const char *path, int repeat, StateMachine *machine, int isTableMachine, int verbose, int timing, int threads) {
#endif
	double /*rate,*/ combinedRate, threadRate;//, rateWithHeaders;
	Timer t;
	long size;//, sizeWithHeaders;
	int i, cpuid;
#ifdef GLOBAL_TIMING
	GlobalTimerResult gtimer_result;
	int j;
#ifdef PRINT_GLOBAL_TIMER_EVENTS
	GlobalTimerEvent **events;
#endif
#endif
	ScannerData *scanners;
	PacketReaderData reader;
	LinkedList *packet_queues;

	MulticoreManager manager;

#ifdef COUNT_FAIL_PERCENT
	long totalFailures, totalGotos;
#endif

#ifdef PAPI
	if (PAPI_library_init(PAPI_VER_CURRENT) != PAPI_VER_CURRENT) {
		fprintf(stderr, "Cannot init PAPI\n");
		exit(1);
	}

	if (PAPI_thread_init((unsigned long (*)(void))pthread_self) != PAPI_OK) {
		fprintf(stderr, "Cannot init PAPI for threads\n");
		exit(1);
	}
#endif

	packet_queues = (LinkedList*)malloc(sizeof(LinkedList) * threads);
	scanners = (ScannerData*)malloc(sizeof(ScannerData) * threads);

	for (i = 0; i < threads; i++) {
		list_init(&packet_queues[i]);
	}

	packetreader_init(&reader, path, repeat, packet_queues, threads);
	for (i = 0; i < threads; i++) {
#ifdef HYBRID_SCANNER
		scanner_init(&(scanners[i]), i, &manager, machine, tableMachine, isTableMachine, &packet_queues[i], verbose, drop);
#else
		scanner_init(&(scanners[i]), i, &manager, machine, isTableMachine, &packet_queues[i], verbose);
#endif
	}

#ifdef HYBRID_SCANNER
	multicore_manager_init(&manager, scanners, threads, work_group_size, max_wgs, packets_to_steal, dedicated_use_compressed);
	multicore_manager_set_thresholds(&manager, thresholds);
#else
	multicore_manager_init(&manager, scanners, threads, 1, threads, 0, 0);
#endif
	packetreader_start(&reader);

	packetreader_join(&reader);

#ifdef HYBRID_SCANNER
	multicore_manager_start(&manager);
#endif
#ifdef GLOBAL_TIMING
#ifdef PRINT_GLOBAL_TIMER_EVENTS
	events = NULL;
#endif
	global_timer_start(&(manager.gtimer));
#endif
	if (timing) {
		startTiming(&t);
	}

	for (i = 0; i < threads; i++) {
		// If CPUs are ordered [core0,core0,...,core0,core1,core1,...,core1,...]
		//cpuid = i;
		// If CPUs are ordered [core0,core1,...,coreN,core0,core1,...,coreN,...]
		cpuid = (i % 2 == 0) ? i / 2 : (threads + i) / 2;
		scanner_start_with_affinity(&(scanners[i]), cpuid);

		// If you use the next line, comment out the pthread_attr_destroy call in scanner_join!!!
		//scanner_start(&(scanners[i]));
	}

	for (i = 0; i < threads; i++) {
		scanner_join(&(scanners[i]));
	}

//	scanner_start(&(scanners[0]));
//	scanner_start(&(scanners[1]));
//	scanner_join(&(scanners[0]));
//	scanner_join(&(scanners[1]));

	if (timing) {
		endTiming(&t);
	}

#ifdef GLOBAL_TIMING
	global_timer_end(&(manager.gtimer));
#endif

#ifdef HYBRID_SCANNER
	multicore_manager_stop(&manager);

	multicore_manager_join(&manager);
#endif

#ifdef GLOBAL_TIMING
	global_timer_join(&(manager.gtimer));
	global_timer_get_results(&(manager.gtimer), &gtimer_result);
#endif

	if (timing) {
		//endTiming(&t);
		size = reader.size;
		//sizeWithHeaders = reader.sizeWithHeaders;
		//rate = GET_TRANSFER_RATE(size, &t);
		//rateWithHeaders = GET_TRANSFER_RATE(sizeWithHeaders, &t);

//		printf("Time(micros)\tData(No H)\tData(w/ H)\tRate(No H) Mb/s\tRate (w/ H) Mb/s\n");
		//printf("%8ld\t%9ld\t%9ld\t%5.4f\t%5.4f\n", t.micros, size, sizeWithHeaders, rate, rateWithHeaders);

		printf("TOTAL_BYTES\tTotal data scanned: %ld bytes\n", size);
		//printf("TOTAL_TIME\tTotal time: %ld ms\n", t.micros);
		//printf("TOTAL_THRPT\tTotal throughput: %5.4f Mbps\n", rate);

		combinedRate = 0;
		printf("Alert mode timer: %ld us\n", manager.alert_mode_timer.micros);
		for (i = 0; i < threads; i++) {
			if (0 && manager.alert_mode_used) {
				threadRate = GET_TRANSFER_RATE(scanners[i].bytes_scanned_since_alert, &(manager.alert_mode_timer));
			} else {
				threadRate = GET_SCANNER_TRANSFER_RATE(&(scanners[i]));
			}
			combinedRate += threadRate;
			printf("T_%2d_THRPT\t%5.4f\tMbps\t%lu\tB\t%lu\tB\t%ld\tus\n", i, threadRate, scanners[i].bytes_scanned, scanners[i].bytes_scanned_since_alert, scanners[i].timer.micros);
		}
		printf("COMB_THRPT\t%5.4f\tMbps\n", combinedRate);

#ifdef GLOBAL_TIMING
		//printf("\nGlobal timing:\n");
/*
		printf("Time\t");
		for (j = 0; j < manager.gtimer.intervals; j++) {
			printf("%6ld\t", gtimer_result.times[j]);
		}
		printf("\n");
*/
		for (i = 0; i < manager.gtimer.num_scanners; i++) {
			printf("T_%2d_GTIME\t", i);
			for (j = 0; j < manager.gtimer.intervals; j++) {
				printf("%5.3f\t", gtimer_result.results[gtimer_result.intervals * i + j]);
			}
			printf("\n");
		}

#ifdef PRINT_GLOBAL_TIMER_EVENTS
		j = global_timer_get_events(&(manager.gtimer), &events);
		if (j > 0) {
			printf("\nEvents:\n");
			for (i = 0; i < j; i++) {
				printf("Event %d: %s [Time: %d, Source: %s]\n", i, events[i]->text, events[i]->interval, events[i]->source);
			}
		}
#endif
#endif
	}
#ifdef COUNT_FAIL_PERCENT
	totalFailures = totalGotos = 0;
        for (i = 0; i < threads; i++) {
                totalFailures += scanners[i].stats.totalFailures;
		totalGotos += scanners[i].stats.totalGotos;
        }

        printf("Fail percent: %f\n", ((double)totalFailures) / (totalFailures + totalGotos));
        printf("Total failures: %ld, Total gotos: %ld\n", totalFailures, totalGotos);
#endif

    multicore_manager_destroy(&manager);
#ifdef GLOBAL_TIMING
    global_timer_destroy(&(manager.gtimer));
    global_timer_destroy_result(&gtimer_result);
#endif

    free(scanners);
    for (i = 0; i < threads; i++) {
    	//printf("Status of input-queue of thread %d: in=%d, out=%d\n", i, packet_queues[i].in, packet_queues[i].out);
    	list_destroy(&(packet_queues[i]), 1);
    }
    free(packet_queues);
}
コード例 #22
0
int main(int argc, char **argv)
{
   pthread_t id[NUM_THREADS];
   int flops[NUM_THREADS];
   int i, rc, retval;
   pthread_attr_t attr;
   float ratio;

   memset(total,0x0,NUM_THREADS*sizeof(*total));
   memset(expected,0x0,NUM_THREADS*sizeof(*expected));
   memset(myid,0x0,NUM_THREADS*sizeof(*myid));
   tests_quiet(argc, argv);     /* Set TESTS_QUIET variable */

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

   hw_info = PAPI_get_hardware_info();
   if (hw_info == NULL)
     test_fail(__FILE__, __LINE__, "PAPI_get_hardware_info", 2);

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

#if defined(linux)
      mythreshold = hw_info->mhz*10000*2;
#else
      mythreshold = THRESHOLD*2;
#endif

   pthread_attr_init(&attr);
#ifdef PTHREAD_CREATE_UNDETACHED
   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_UNDETACHED);
#endif
#ifdef PTHREAD_SCOPE_SYSTEM
   retval = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
   if (retval != 0)
      test_skip(__FILE__, __LINE__, "pthread_attr_setscope", retval);
#endif

   for (i = 0; i < NUM_THREADS; i++) {
     flops[i] = NUM_FLOPS * (i+1);
      rc = pthread_create(&id[i], &attr, Thread, (void *)&flops[i]);
      if (rc)
         test_fail(__FILE__, __LINE__, "pthread_create", PAPI_ESYS);
   }
   for (i = 0; i < NUM_THREADS; i++)
      pthread_join(id[i], NULL);

   pthread_attr_destroy(&attr);

   {
       long long t = 0, r = 0;
       for (i=0;i<NUM_THREADS;i++) {
	   t += (NUM_FLOPS * (i+1))/mythreshold;
	   r += total[i];
       }
       printf("Expected total overflows: %lld\n",t);
       printf("Received total overflows: %lld\n",r);
   }

/*   ratio = (float)total[0] / (float)expected[0]; */
/*   printf("Ratio of total to expected: %f\n",ratio); */
   ratio = 1.0;
   for (i=0;i<NUM_THREADS;i++)
   {
       printf("Overflows thread %d: %d, expected %d\n",
	      i,total[i],(int)(ratio*(float)expected[i]));
   }

   for (i=0;i<NUM_THREADS;i++)
   {
     if (total[i] < (int)((ratio*(float)expected[i])/2.0)) 
       test_fail(__FILE__,__LINE__,"not enough overflows",PAPI_EMISC);
   }
     
   test_pass(__FILE__, NULL, 0);
   pthread_exit(NULL);
   exit(1);
}
コード例 #23
0
int
main( int argc, char **argv )
{
	pthread_t id[NUM_THREADS];
	int flops[NUM_THREADS];
	int i, rc, retval;
	pthread_attr_t attr;
	long long elapsed_us, elapsed_cyc;
	const PAPI_exe_info_t *prginfo = NULL;

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

	if ( ( retval =
		   PAPI_library_init( PAPI_VER_CURRENT ) ) != PAPI_VER_CURRENT )
		test_fail( __FILE__, __LINE__, "PAPI_library_init", 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 );
	}
	if ( ( prginfo = PAPI_get_executable_info(  ) ) == NULL ) {
		retval = 1;
		test_fail( __FILE__, __LINE__, "PAPI_get_executable_info", retval );
	}
	my_start = prginfo->address_info.text_start;
	my_end = prginfo->address_info.text_end;
	length = ( unsigned int ) ( my_end - my_start );

	elapsed_us = PAPI_get_real_usec(  );

	elapsed_cyc = PAPI_get_real_cyc(  );

	pthread_attr_init( &attr );
#ifdef PTHREAD_CREATE_UNDETACHED
	pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_UNDETACHED );
#endif
#ifdef PTHREAD_SCOPE_SYSTEM
	retval = pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );
	if ( retval != 0 )
		test_skip( __FILE__, __LINE__, "pthread_attr_setscope", retval );
#endif

	for ( i = 0; i < NUM_THREADS; i++ ) {
		flops[i] = FLOPS * ( i + 1 );
		rc = pthread_create( &id[i], &attr, Thread, ( void * ) &flops[i] );
		if ( rc )
			return ( FAILURE );
	}
	for ( i = 0; i < NUM_THREADS; i++ )
		pthread_join( id[i], NULL );

	pthread_attr_destroy( &attr );

	elapsed_cyc = PAPI_get_real_cyc(  ) - elapsed_cyc;

	elapsed_us = PAPI_get_real_usec(  ) - elapsed_us;

	if ( !TESTS_QUIET ) {
		printf( "Master real usec   : \t%lld\n", elapsed_us );
		printf( "Master real cycles : \t%lld\n", elapsed_cyc );
	}

	test_pass( __FILE__, NULL, 0 );
	pthread_exit( NULL );
	exit( 1 );
}
コード例 #24
0
ファイル: thrspecific.c プロジェクト: FMCalisto/SMP
int
main( int argc, char **argv )
{
	pthread_t e_th, f_th, g_th, h_th;
	int flops1, flops2, flops3, flops4, flops5;
	int retval, rc;
	pthread_attr_t attr;

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

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

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

	pthread_attr_init( &attr );
#ifdef PTHREAD_CREATE_UNDETACHED
	pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_UNDETACHED );
#endif
#ifdef PTHREAD_SCOPE_SYSTEM
	retval = pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );
	if ( retval != 0 )
		test_skip( __FILE__, __LINE__, "pthread_attr_setscope", retval );
#endif

	flops1 = 1000000;
	rc = pthread_create( &e_th, &attr, Thread, ( void * ) &flops1 );
	if ( rc ) {
		retval = PAPI_ESYS;
		test_fail( __FILE__, __LINE__, "pthread_create", retval );
	}
	flops2 = 2000000;
	rc = pthread_create( &f_th, &attr, Thread, ( void * ) &flops2 );
	if ( rc ) {
		retval = PAPI_ESYS;
		test_fail( __FILE__, __LINE__, "pthread_create", retval );
	}

	flops3 = 4000000;
	rc = pthread_create( &g_th, &attr, Thread, ( void * ) &flops3 );
	if ( rc ) {
		retval = PAPI_ESYS;
		test_fail( __FILE__, __LINE__, "pthread_create", retval );
	}

	flops4 = 8000000;
	rc = pthread_create( &h_th, &attr, Thread, ( void * ) &flops4 );
	if ( rc ) {
		retval = PAPI_ESYS;
		test_fail( __FILE__, __LINE__, "pthread_create", retval );
	}

	pthread_attr_destroy( &attr );
	flops5 = 500000;
	Thread( &flops5 );
	pthread_join( h_th, NULL );
	pthread_join( g_th, NULL );
	pthread_join( f_th, NULL );
	pthread_join( e_th, NULL );

	test_pass( __FILE__, NULL, 0 );
	pthread_exit( NULL );
	exit( 1 );
}
コード例 #25
0
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;
}
コード例 #26
0
ファイル: test2.c プロジェクト: Bootz/multicore-opimization
int main (int argc, char *argv[])
{
  int i, j;

  //papi init code begin
  long long s;
  long long e;
  int retval;
  int eventcode;
  PAPI_option_t opt;

  if(PAPI_VER_CURRENT != PAPI_library_init(PAPI_VER_CURRENT)){
    printf("Can't initiate PAPI library!\n");
    exit(-1);
  }

  if (PAPI_thread_init(pthread_self) != PAPI_OK) {
    printf("Can't thread init!\n");
    exit(-1);
  }

  eventset = PAPI_NULL;
  if(PAPI_create_eventset(&eventset) != PAPI_OK){
    printf("Can't create eventset!\n");
    exit(-3);
  }

  if ( ( retval = PAPI_assign_eventset_component( eventset, 0 ) ) != PAPI_OK ) {
    printf("Can't assign_event_component!\n");
    exit(-3);
  }                

  memset( &opt, 0x0, sizeof ( PAPI_option_t ) );
  opt.inherit.inherit = PAPI_INHERIT_ALL;
  opt.inherit.eventset = eventset;
  if ( ( retval = PAPI_set_opt( PAPI_INHERIT, &opt ) ) != PAPI_OK ) {
    printf("Can't set inherit!\n");
    exit(-3);
  }

  PAPI_event_name_to_code("MEM_LOAD_MISC_RETIRED:LLC_MISS",&eventcode);
  events[1] = eventcode;
  PAPI_event_name_to_code("CPU_CLK_UNHALTED:THREAD_P",&eventcode);
  events[2] = eventcode;
  for(i=0;i<3;i++) {
    retval = PAPI_add_event(eventset, events[i]);
    if(retval != PAPI_OK){
      printf("error %d\n",retval);
      exit(-4);
    }
  }
  //papi init code end


  /* create output file in gnuplot format */
  gnuplotfile = fopen("test2_tcm.txt", "w");
  fprintf(gnuplotfile,"#time(ms)\tMEM_LOAD_MISC_RETIRED:LLC_MISS\tCPU_CLK_UNHALTED:THREAD_P\n");
  s = PAPI_get_real_usec();
  PAPI_start(eventset);
  test();
  PAPI_stop(eventset, values);
  e = PAPI_get_real_usec();
  fclose(gnuplotfile);


  /*Print out PAPI reading*/
  printf("Wallclock time: %lld usec\n",e-s);
  printf("Total Cycles\t%lld\n", values[0]);
  printf("MEM_LOAD_MISC_RETIRED:LLC_MISS\t%lld\n", values[1]);
  printf("CPU_CLK_UNHALTED:THREAD_P\t%lld\n", values[2]);

  return 0;
}
コード例 #27
0
ファイル: papi.c プロジェクト: shamouda/ocr-apps
void counter_init( int *eventset, int *num_papi_events )
{
	char error_str[PAPI_MAX_STR_LEN];
	int stat;

	/////////////////////////////////////////////////////////////////////////
	//                        PAPI EVENT SELECTION
	/////////////////////////////////////////////////////////////////////////
	// User can comment/uncomment blocks as they see fit within this seciton

	// Some Standard Events
	//int events[] = {PAPI_TOT_INS,PAPI_LD_INS,PAPI_FP_INS};

	// Bandwidth Used
	// ((PAPI_Lx_TCM * Lx_linesize) / PAPI_TOT_CYC) * Clock(MHz)
	int events[] = {PAPI_L3_TCM, PAPI_TOT_CYC};

	// L3 Total Cache Miss Ratio
	// PAPI_L3_TCM / PAPI_L3_TCA
	// (On Xeon dual octo -  65%, not dependent on # of threads)
	//int events[] = {PAPI_L3_TCM, PAPI_L3_TCA};

	// % Cycles with no instruction use
	// PAPI_STL_ICY / PAPI_TOT_CYC
	//int events[] = { PAPI_STL_ICY, PAPI_TOT_CYC };

	// % Branch instructions Mispredicted
	// PAPI_BR_MSP / PAPI_BR_CN
	//int events[] = { PAPI_BR_MSP, PAPI_BR_CN, PAPI_BR_PRC };

	// TLB Misses
	//int events[] = { PAPI_TLB_DM };

	// MFlops
	// (PAPI_FP_INS/PAPI_TOT_CYC) * Clock(MHz)
	//int events[] = { PAPI_FP_INS, PAPI_TOT_CYC };

	// MFlops (Alternate?)
	// (PAPI_FP_INS/PAPI_TOT_CYC) * Clock(MHz)
	//int events[] = { PAPI_DP_OPS, PAPI_TOT_CYC };


	// TLB misses (Using native counters)
	/*
	int events[2];
	int EventCode;
	char * event1 = "perf::DTLB-LOADS";
	char * event2 = "perf::DTLB-LOAD-MISSES";
	PAPI_event_name_to_code( event1, &EventCode );
	events[0] = EventCode;
	PAPI_event_name_to_code( event2, &EventCode );
	events[1] = EventCode;
	*/

	/*
	// Stalled Cycles, front v back (Using native counters)
	int events[3];
	int EventCode;
	char * event1 = "perf::STALLED-CYCLES-FRONTEND";
	char * event2 = "perf::STALLED-CYCLES-BACKEND";
	char * event3 = "perf::PERF_COUNT_HW_CPU_CYCLES";
	PAPI_event_name_to_code( event1, &EventCode );
	events[0] = EventCode;
	PAPI_event_name_to_code( event2, &EventCode );
	events[1] = EventCode;
	PAPI_event_name_to_code( event3, &EventCode );
	events[2] = EventCode;
	*/
	/*
	// LLC Cache Misses (Using native counters)
	int events[2];
	int EventCode;
	char * event1 = "ix86arch::LLC_REFERENCES";
	char * event2 = "ix86arch::LLC_MISSES";
	PAPI_event_name_to_code( event1, &EventCode );
	events[0] = EventCode;
	PAPI_event_name_to_code( event2, &EventCode );
	events[1] = EventCode;
	*/

	/*
	// Node Prefetch Misses (Using native counters)
	int events[1];
	int EventCode;
	//char * event1 = "perf::NODE-PREFETCHES";
	//char * event2 = "perf::NODE-PREFETCH-MISSES";
	char * event1 = "perf::NODE-PREFETCHES";
	char * event2 = "perf::NODE-LOAD-MISSES:COUNT";
	//PAPI_event_name_to_code( event1, &EventCode );
	//events[0] = EventCode;
	PAPI_event_name_to_code( event2, &EventCode );
	events[0] = EventCode;
	*/

	/*
	// CPU Stalls Due to lack of Load Buffers (Using native counters)
	int events[2];
	int EventCode;
	char * event1 = "RESOURCE_STALLS:LB";
	char * event2 = "perf::PERF_COUNT_HW_CPU_CYCLES";
	PAPI_event_name_to_code( event1, &EventCode );
	events[0] = EventCode;
	PAPI_event_name_to_code( event2, &EventCode );
	events[1] = EventCode;
	*/
	/*
	// CPU Stalls Due to ANY Resource (Using native counters)
	int events[2];
	int EventCode;
	char * event1 = "RESOURCE_STALLS:ANY";
	char * event2 = "PAPI_TOT_CYC";
	PAPI_event_name_to_code( event1, &EventCode );
	events[0] = EventCode;
	PAPI_event_name_to_code( event2, &EventCode );
	events[1] = EventCode;
	*/

	/*
	// CPU Stalls at Reservation Station (Using native counters)
	int events[2];
	int EventCode;
	char * event1 = "RESOURCE_STALLS:RS";
	char * event2 = "perf::PERF_COUNT_HW_CPU_CYCLES";
	PAPI_event_name_to_code( event1, &EventCode );
	events[0] = EventCode;
	PAPI_event_name_to_code( event2, &EventCode );
	events[1] = EventCode;
	*/

	/*
	// CPU Stall Reason Breakdown (Using native counters)
	int events[4];
	int EventCode;
	char * event1 = "RESOURCE_STALLS:ANY";
	char * event2 = "RESOURCE_STALLS:LB";
	char * event3 = "RESOURCE_STALLS:RS";
	char * event4 = "RESOURCE_STALLS:SB";
	// Set 1
	// Set 2
	char * event1 = "RESOURCE_STALLS:ANY";
	char * event2 = "RESOURCE_STALLS:ROB";
	char * event3 = "RESOURCE_STALLS:MEM_RS";
	char * event4 = "RESOURCE_STALLS2:ALL_FL_EMPTY";
	// Set 2
	// Set 3
	char * event1 = "RESOURCE_STALLS:ANY";
	char * event2 = "RESOURCE_STALLS2:ALL_PRF_CONTROL";
	char * event3 = "RESOURCE_STALLS2:ANY_PRF_CONTROL"; // duplicate
	char * event4 = "RESOURCE_STALLS2:OOO_RSRC";
	// Set 3


	// Events that don't need to be counted
	// Don't bother measuring these
	//char * event1 = "RESOURCE_STALLS:FCSW"; // Always 0, don't measure
	//char * event1 = "RESOURCE_STALLS:MXCSR"; // Always 0, don't measure
	//char * event3 = "RESOURCE_STALLS2:BOB_FULL"; // Always trivial
	//char * event3 = "RESOURCE_STALLS2:ANY_PRF_CONTROL"; // duplicate


	PAPI_event_name_to_code( event1, &EventCode );
	events[0] = EventCode;
	PAPI_event_name_to_code( event2, &EventCode );
	events[1] = EventCode;
	PAPI_event_name_to_code( event3, &EventCode );
	events[2] = EventCode;
	PAPI_event_name_to_code( event4, &EventCode );
	events[3] = EventCode;
	*/

	/////////////////////////////////////////////////////////////////////////
	//                        PAPI EVENT LOADING
	/////////////////////////////////////////////////////////////////////////
	// Users should not need to alter anything within this section

	int thread = omp_get_thread_num();
	if( thread == 0 )
		printf("Initializing PAPI counters...\n");

	*num_papi_events = sizeof(events) / sizeof(int);

	if ((stat = PAPI_thread_init((long unsigned int (*)(void)) omp_get_thread_num)) != PAPI_OK){
		PAPI_perror("PAPI_thread_init");
		exit(1);
	}

	if ( (stat= PAPI_create_eventset(eventset)) != PAPI_OK){
		PAPI_perror("PAPI_create_eventset");
		exit(1);
	}

	for( int i = 0; i < *num_papi_events; i++ ){
		if ((stat=PAPI_add_event(*eventset,events[i])) != PAPI_OK){
			PAPI_perror("PAPI_add_event");
			exit(1);
		}
	}

	if ((stat=PAPI_start(*eventset)) != PAPI_OK){
		PAPI_perror("PAPI_start");
		exit(1);
	}
}
コード例 #28
0
ファイル: papi_hw_counter.c プロジェクト: LaminarIR/framework
void PAPI_HW_COUNTER_thread_init(int tid, int _numtasks){
  int retval;
  int i,j;

  retval = PAPI_thread_init(pthread_self);
  if(retval != PAPI_OK){
    //handle_error(retval);
    printf("ERROR: PAPI failed to initialized threads.(%d)\n", retval);
    exit(1);
  }

  unsigned long int pid = PAPI_HW_COUNTER_thread_pid();
  printf("Thread %6d: PAPI initialized.\n", tid);

  // init PAPI_VARS
  thr_vars[tid].pid = pid;
  thr_vars[tid].EventSet = PAPI_NULL;
  thr_vars[tid].num_events = 0;
 
#ifdef MEASURE_ENERGY
  thr_vars[tid].num_energy_events = 0;
  thr_vars[tid].EnergyEventSet = PAPI_NULL;
  thr_vars[tid].rapl_cid = -1;
  thr_vars[tid].cmpinfo = NULL;
#endif

  // task level memory allocation
  thr_vars[tid].numtasks = _numtasks;

  if(posix_memalign((void**)&(thr_vars[tid].ids), CACHELINE_SIZE, sizeof(char*)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d].ids\n",tid); exit(1);
  }

  for(i=0; i<_numtasks; i++){
    if(posix_memalign((void**)&(thr_vars[tid].ids[i]), CACHELINE_SIZE, sizeof(char)*(MAX_TASK_ID_LENGTH+1))){
      printf("ERROR: failed to allocate memory to thr_vars[%d].ids[%d]\n",tid,i); exit(1);
    }
  }
   
  #ifdef MEASURE_TIME
  if(posix_memalign((void**)&(thr_vars[tid]._tmp_time), CACHELINE_SIZE, sizeof(struct TS)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]._tmp_time\n",tid); exit(1);
  }
  if(posix_memalign((void**)&(thr_vars[tid]._time), CACHELINE_SIZE, sizeof(long long*)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]._time\n",tid); exit(1);
  }
  #endif

  #ifdef MEASURE_CPI
  if(posix_memalign((void**)&(thr_vars[tid]._tmp_inst), CACHELINE_SIZE, sizeof(struct TS)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]._tmp_inst\n",tid); exit(1);
  }
  if(posix_memalign((void**)&(thr_vars[tid]._tmp_cyc), CACHELINE_SIZE, sizeof(struct TS)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]._tmp_cyc\n",tid); exit(1);
  }
  if(posix_memalign((void**)&(thr_vars[tid]._inst), CACHELINE_SIZE, sizeof(long long*)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]._inst\n",tid); exit(1);
  }
  if(posix_memalign((void**)&(thr_vars[tid]._cyc), CACHELINE_SIZE, sizeof(long long*)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]_cyc\n",tid); exit(1);
  }
  if(posix_memalign((void**)&(thr_vars[tid]._cpi), CACHELINE_SIZE, sizeof(long long*)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]_cpi\n",tid); exit(1);
  }
  #endif

  #ifdef MEASURE_MEMACC
  if(posix_memalign((void**)&(thr_vars[tid]._tmp_load), CACHELINE_SIZE, sizeof(struct TS)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]._tmp_load\n",tid); exit(1);
  }
  if(posix_memalign((void**)&(thr_vars[tid]._tmp_store), CACHELINE_SIZE, sizeof(struct TS)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]._tmp_store\n",tid); exit(1);
  }
  if(posix_memalign((void**)&(thr_vars[tid]._load), CACHELINE_SIZE, sizeof(long long*)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]._load\n",tid); exit(1);
  }
  if(posix_memalign((void**)&(thr_vars[tid]._store), CACHELINE_SIZE, sizeof(long long*)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]._store\n",tid); exit(1);
  }
  #endif

  #ifdef MEASURE_LLCMISS
  if(posix_memalign((void**)&(thr_vars[tid]._tmp_llcmiss), CACHELINE_SIZE, sizeof(struct TS)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]._tmp_llcmiss\n",tid); exit(1);
  }
  if(posix_memalign((void**)&(thr_vars[tid]._llcmiss), CACHELINE_SIZE,sizeof(long long*)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]._llcmiss\n",tid); exit(1);
  }
  #endif

  #ifdef MEASURE_ICACHEMISS
  if(posix_memalign((void**)&(thr_vars[tid]._tmp_icachemiss), CACHELINE_SIZE, sizeof(struct TS)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]._tmp_icachemiss\n",tid); exit(1);
  }
  if(posix_memalign((void**)&(thr_vars[tid]._icachemiss),CACHELINE_SIZE,sizeof(long long*)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]._icachemiss\n",tid); exit(1);
  }
  #endif

  #ifdef MEASURE_DCACHEMISS
  if(posix_memalign((void**)&(thr_vars[tid]._tmp_l1dcm), CACHELINE_SIZE, sizeof(struct TS)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]._tmp_l1dcm\n",tid); exit(1);
  }
  if(posix_memalign((void**)&(thr_vars[tid]._tmp_l1dca), CACHELINE_SIZE, sizeof(struct TS)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]._tmp_l1dca\n",tid); exit(1);
  }
  if (posix_memalign((void**)&(thr_vars[tid]._l1dcm), CACHELINE_SIZE, sizeof(long long*)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]._l1dcm\n",tid); exit(1);
  }
  if (posix_memalign((void**)&(thr_vars[tid]._l1dca), CACHELINE_SIZE, sizeof(long long*)*_numtasks)){
    printf("ERROR: failed to allocate memory to thr_vars[%d]._l1dca\n",tid); exit(1);
  }
  #endif

  #ifdef MEASURE_ENERGY
  for( j = 0; j < MAX_RAPL_EVENTS; j++){
    if(posix_memalign((void**)&(thr_vars[tid]._tmp_energies[j]),CACHELINE_SIZE, sizeof(struct TS)*_numtasks)){
      printf("ERROR: failed to allocate memory to thr_vars[%d]._tmp_energies[%d]\n",tid,j); exit(1);
    }
    if (posix_memalign((void**)&(thr_vars[tid]._energies[j]), CACHELINE_SIZE, 
          sizeof(long long*)*_numtasks)){
      printf("ERROR: failed to allocate memory to thr_vars[%d]._energies[%d]\n",tid,j); exit(1);
    }
  }
  #endif

  // iteration level memory allocation
  for (i = 0; i < _numtasks; i++){
  #ifdef MEASURE_TIME
    if (posix_memalign((void**)&thr_vars[tid]._time[i], CACHELINE_SIZE, sizeof(long long)*numiters)){
      printf("ERROR: failed to allocate memory to thr_vars[%d]._time[]\n",tid); exit(1);
    }
  #endif

  #ifdef MEASURE_CPI
    if (posix_memalign((void**)&thr_vars[tid]._inst[i], CACHELINE_SIZE, sizeof(long long)*numiters)){
      printf("ERROR: failed to allocate memory to thr_vars[%d]._inst[]\n",tid); exit(1);
    }
    if (posix_memalign((void**)&thr_vars[tid]._cyc[i], CACHELINE_SIZE, sizeof(long long)*numiters)){
      printf("ERROR: failed to allocate memory to thr_vars[%d]_cyc[]\n",tid); exit(1);
    }
    if (posix_memalign((void**)&thr_vars[tid]._cpi[i], CACHELINE_SIZE, sizeof(long long)*numiters)){
      printf("ERROR: failed to allocate memory to thr_vars[%d]_cpi[]\n",tid); exit(1);
    }
  #endif

  #ifdef MEASURE_MEMACC
    if (posix_memalign((void**)&thr_vars[tid]._load[i], CACHELINE_SIZE, sizeof(long long)*numiters)){
      printf("ERROR: failed to allocate memory to thr_vars[%d]._load[]\n",tid); exit(1);
    }
    if (posix_memalign((void**)&thr_vars[tid]._store[i], CACHELINE_SIZE, sizeof(long long)*numiters)){
      printf("ERROR: failed to allocate memory to thr_vars[%d]._store[]\n",tid); exit(1);
    }
  #endif

  #ifdef MEASURE_LLCMISS
    if (posix_memalign((void**)&thr_vars[tid]._llcmiss[i], CACHELINE_SIZE, sizeof(long long)*numiters)){
      printf("ERROR: failed to allocate memory to thr_vars[%d]._llcmiss[]\n",tid); exit(1);
    }
  #endif

  #ifdef MEASURE_ICACHEMISS
    if (posix_memalign((void**)&thr_vars[tid]._icachemiss[i], CACHELINE_SIZE, sizeof(long long)*numiters)){
      printf("ERROR: failed to allocate memory to thr_vars[%d]._icachemiss[]\n",tid); exit(1);
    }
  #endif

  #ifdef MEASURE_DCACHEMISS
    if (posix_memalign((void**)&thr_vars[tid]._l1dcm[i], CACHELINE_SIZE, sizeof(long long)*numiters)){
      printf("ERROR: failed to allocate memory to thr_vars[%d]._l1dcm[]\n",tid); exit(1);
    }
    if (posix_memalign((void**)&thr_vars[tid]._l1dca[i], CACHELINE_SIZE, sizeof(long long)*numiters)){
      printf("ERROR: failed to allocate memory to thr_vars[%d]._l1dca[]\n",tid); exit(1);
    }
  #endif

  #ifdef MEASURE_ENERGY
    for( j = 0; j < MAX_RAPL_EVENTS; j++){
      if (posix_memalign((void**)&(thr_vars[tid]._energies[j][i]), CACHELINE_SIZE, 
            sizeof(long long)*numiters)){
        printf("ERROR: failed to allocate memory to thr_vars[%d]._energies[%d][]\n",tid,j); exit(1);
      }
    }
  #endif
  }
}
コード例 #29
0
int main(int argc, char *argv[]) {
  pthread_t *threads;
  pthread_attr_t attr;
  uint32_t **ranks;
  void *status;

#if defined(PAPI_ENABLED) && !defined(DEBUG)
  int num_sets;
  PAPI_event_set_wrapper_t* event_sets;
#endif
  int rc;
  uint32_t t;

  printf("Optimized Stream benchmark (using SSE intrinsics)\n");

  init_flush_cache_array();
  malloc_arrays(argv);
  print_array_parameters();
  select_code_variant(argv);
  print_code_variant_parameters();

  threads = (pthread_t *) malloc(numThreads * sizeof(pthread_t));
  ranks = (uint32_t **) malloc(numThreads * sizeof(uint32_t *));

#if !defined(DEBUG)
#if defined(PAPI_ENABLED)
  papi_init(desired_events, num_desired, &event_sets, &num_sets);

  // initialize threaded PAPI
  if (PAPI_thread_init((unsigned long (*)(void)) (pthread_self)) != PAPI_OK) {
    printf("Error with PAPI_thread_init().\n");
    exit(EXIT_FAILURE);
  }

  results = (double *) malloc(num_sets * numThreads * NUM_TRIALS * sizeof(double));
  if (results==NULL) {
    printf("Error on array results malloc.\n");
    exit(EXIT_FAILURE);
  }
#else
  results = (double *) malloc(numThreads * NUM_TRIALS * sizeof(double));
  if (results==NULL) {
    printf("Error on array results malloc.\n");
    exit(EXIT_FAILURE);
  }
#if defined(CYCLE_TIME)
  // calculate clock rate
  GET_CLOCK_RATE(results, NUM_TRIALS);
  median_counts_per_sec = find_median(results, NUM_TRIALS);
  //printf("Median ticks per second = %e\n", median_counts_per_sec);

#else
  timer_init();
  median_counts_per_sec = 1.0;
#endif
#endif
#endif

  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  barrier_init(&my_barrier, numThreads);
#if defined(AFFINITY_ENABLED)
  Affinity_Init();
#endif

  // run stream tests
  for (t=0; t < numThreads; t++) {
    ranks[t] = (uint32_t *) malloc(sizeof(uint32_t));
    *ranks[t] = t;
  }

  for (t=1; t < numThreads; t++) {
#if defined(DEBUG)
    printf("Creating thread %u\n", t);
#endif
    rc = pthread_create(&threads[t], &attr, pthreads_each, (void *) ranks[t]);
    if (rc) {
      printf("ERROR; return code from pthread_create() is %d\n", rc);
      exit(EXIT_FAILURE);
    }
  }
  pthreads_each((void *) ranks[0]);

  // join the other threads
  for (t=1; t < numThreads; t++) {
    pthread_join(threads[t], &status);
  }

#if defined(PAPI_ENABLED) && !defined(DEBUG)
  papi_cleanup(event_sets, num_sets);
#endif
  pthread_attr_destroy(&attr);
  pthread_exit(NULL);
  barrier_destroy(&my_barrier);
  free_arrays();

  return EXIT_SUCCESS;
}
コード例 #30
0
ファイル: Knn_multihilos_papic.c プロジェクト: Cristofher/Knn
int main(int argc, char *argv[]) {


	float rtime1, rtime2, ptime1, ptime2, mflops;
	long long flpops;

	unsigned long int tid;
	int num_hwcntrs = 0;
	int fip = 0, retval;
	float real_time, proc_time;
	long long flpins;

	int i;
	unsigned int EventSet = PAPI_NULL; 
    int count = 0, err_count = 0;


    PAPI_event_info_t info;

    long long ( values2[2] )[2];
    long long min, max;
    int PAPI_event, mythreshold = THRESHOLD;
    char event_name1[PAPI_MAX_STR_LEN];
    const PAPI_hw_info_t *hw_info = NULL;
    int num_events, mask;
    int num_flops = NUM_FLOPS;
    long long elapsed_us, elapsed_cyc;



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



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

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

	/* Get hardware info */
  hw_info = PAPI_get_hardware_info(  );
  if ( hw_info == NULL )
      test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 2 );

  EventSet = 	add_two_nonderived_events( &num_events, &PAPI_event, &mask );

  printf("Using %#x for the overflow event\n",PAPI_event);

  if ( PAPI_event == PAPI_FP_INS ) {
      mythreshold = THRESHOLD;
  }
  else {
		#if defined(linux)
      mythreshold = ( int ) hw_info->cpu_max_mhz * 20000;
		#else
      mythreshold = THRESHOLD * 2;
		#endif
  }

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

  do_flops( NUM_FLOPS );

	/* stop the calibration run */
  retval = PAPI_stop( EventSet, values2[0] );
  if ( retval != PAPI_OK )
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );


	/* set up overflow handler */
  retval = PAPI_overflow( EventSet, PAPI_event, mythreshold, 0, handler );
  if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_overflow", retval );
  }

	/* Start overflow run */
  retval = PAPI_start( EventSet );
  if ( retval != PAPI_OK ) {
      test_fail( __FILE__, __LINE__, "PAPI_start", retval );
  }

  do_flops( num_flops );

	/* stop overflow run */
  retval = PAPI_stop( EventSet, values2[1] );
  if ( retval != PAPI_OK )
      test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
  retval = PAPI_overflow( EventSet, PAPI_event, 0, 0, handler );
  if ( retval != PAPI_OK )
      test_fail( __FILE__, __LINE__, "PAPI_overflow", retval );

  if ( !TESTS_QUIET ) {
      if ( ( retval =
         PAPI_event_code_to_name( PAPI_event, event_name1 ) ) != PAPI_OK )
         test_fail( __FILE__, __LINE__, "PAPI_event_code_to_name", retval );

     printf( "Test case: Overflow dispatch of 2nd event in set with 2 events.\n" );
     printf( "---------------------------------------------------------------\n" );
     printf( "Threshold for overflow is: %d\n", mythreshold );
     printf( "Using %d iterations\n", num_flops );
     printf( "-----------------------------------------------\n" );

     printf( "Test type    : %16d%16d\n", 1, 2 );
     printf( OUT_FMT, event_name1, ( values2[0] )[1], ( values2[1] )[1] );
     printf( OUT_FMT, "PAPI_TOT_CYC", ( values2[0] )[0], ( values2[1] )[0] );
     printf( "Overflows    : %16s%16d\n", "", total );
     printf( "-----------------------------------------------\n" );
 }

 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 );

if ( !TESTS_QUIET ) {
  printf( "Verification:\n" );
#if defined(linux) || defined(__ia64__) || defined(_POWER4)
  num_flops *= 2;
#endif
  if ( PAPI_event == PAPI_FP_INS || PAPI_event == PAPI_FP_OPS ) {
     printf( "Row 1 approximately equals %d %d\n", num_flops, num_flops );
 }
 printf( "Column 1 approximately equals column 2\n" );
 printf( "Row 3 approximately equals %u +- %u %%\n",( unsigned ) ( ( values2[0] )[1] / ( long long ) mythreshold ),( unsigned ) ( OVR_TOLERANCE * 100.0 ) );
}

min =
( long long ) ( ( ( double ) values2[0][1] * ( 1.0 - OVR_TOLERANCE ) ) /
  ( double ) mythreshold );
max =
( long long ) ( ( ( double ) values2[0][1] * ( 1.0 + OVR_TOLERANCE ) ) /
  ( double ) mythreshold );
printf( "Overflows: total(%d) > max(%lld) || total(%d) < min(%lld) \n", total,
  max, total, min );
if ( total > max || total < min )
  test_fail( __FILE__, __LINE__, "Overflows", 1 );



printf("Initial thread id is: %lu\n",tid);

	/* Initialize the PAPI library and get the number of counters available */

if ((num_hwcntrs = PAPI_num_counters()) <= 0)  
  handle_error(1);



  /*  The installation supports PAPI, but has no counters */
if ((num_hwcntrs = PAPI_num_counters()) == 0 )
    fprintf(stderr,"Info:: This machine does not provide hardware counters.");

printf("This system has %d available counters.\n", num_hwcntrs);

if (num_hwcntrs > 2)
  num_hwcntrs = 2;

	 /* Start counting events */




if (PAPI_start_counters(Events, num_hwcntrs) != PAPI_OK)
  handle_error(1);

if (argc != 8) {
  printf("\nError :: Ejecutar como : a.out archivo_BD Num_elem archivo_queries Num_queries N_THREADS numero_K Dimension_objetos\n");
  return 0;
}
TOPK = atoi(argv[6]);
DIM = atoi(argv[7]);
double **DB;
	double **Consultas; //Cola de consultas
	int N_QUERIES, N_DB;
	char str_f[256];
	double dato[DIM];
	int j;
	FILE *f_dist, *fquery;
	Elem *heap, e_temp,*answer;
	int *acum, N_THREADS;


	//N_THREADS es el nro. de threads con el que se lanzará la región paralela
	N_THREADS = atoi(argv[5]);
	//N_QUERIES es el nro. de consultas
	N_QUERIES = atoi(argv[4]);
	N_DB = atoi(argv[2]);

	printf("\nN_QUERIES = %d\nN_THREADS = %d\n", N_QUERIES, N_THREADS);
	fflush(stdout);

	acum = (int *) malloc(sizeof (int)*N_THREADS);
	for (i = 0; i < N_THREADS; i++)
		acum[i] = 0;

	sprintf(str_f, "%s", argv[1]);
	printf("\nAbriendo %s... ", argv[1]);
	fflush(stdout);
	f_dist = fopen(str_f, "r");
	printf("OK\n");
	fflush(stdout);


	Consultas = (double **) malloc(sizeof (double *)*N_QUERIES);
	for (i = 0; i < N_QUERIES; i++)
		Consultas[i] = (double *) malloc(sizeof (double)*DIM);

	DB = (double **) malloc(sizeof (double *)*N_DB);
	for (i = 0; i < N_DB; i++)
		DB[i] = (double *) malloc(sizeof (double)*DIM);

	answer = (Elem *)malloc(sizeof(Elem)*N_QUERIES*TOPK);

	printf("\nCargando DB... ");
	fflush(stdout);
	for (i = 0; i < N_DB; i++) {
		//Usar leedato_cophir() cuando se utilice la BD Cophir para no tener problemas con las ","
		//if (leedato_cophir(dato, f_dist) == ERROR || feof(f_dist))
		if (leedato(dato, f_dist) == ERROR || feof(f_dist)) {
			printf("\n\nERROR :: N_DB mal establecido\n\n");
			fflush(stdout);
			fclose(f_dist);
			break;
		}
		copiavalor(DB[i], dato);
	}
	fclose(f_dist);
	printf("OK\n");
	fflush(stdout);

	if ((fquery = fopen(argv[3], "r")) == NULL)
		printf("Error al abrir para lectura el archivo de qeuries: %s\n", argv[3]);
	else
		printf("Abriendo  para lectura %s\n", argv[3]);
	printf("\nCargando Consultas... ");
	fflush(stdout);
	for (i = 0; i < N_QUERIES; i++) {
		//Usar leedato_cophir() cuando se utilice la BD Cophir para no tener problemas con las ","
		//if (leedato_cophir(dato, fquery) == ERROR || feof(fquery))
		if (leedato(dato, fquery) == ERROR || feof(fquery)) {
			printf("\n\nERROR :: N_QUERIES mal establecido, Menos queries que las indicadas\n\n");
			fflush(stdout);
			fclose(fquery);
			break;
		}
		copiavalor(Consultas[i], dato);
	}
	fclose(fquery);
	printf("OK\n");
	fflush(stdout);

	PAPI_start_counters((int*) Events, NUM_EVENTS);
	omp_set_num_threads(N_THREADS);

	elapsed_us = PAPI_get_real_usec(  );

	elapsed_cyc = PAPI_get_real_cyc(  );

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

#pragma omp parallel shared(Consultas, DB, N_QUERIES, N_DB, N_THREADS, acum, DIM)
	{
		float real_time;
		struct timeval t1, t2;
		int i, j;
		Elem *heap, e_temp;
		double d;
		int n_elem = 0;
		int trid = omp_get_thread_num(); //ID del thread
		int procs = omp_get_num_threads(); //Nro. total de threads
		double suma = 0;

		suma = 0;
		heap = (Elem *) malloc(sizeof (Elem) * TOPK);

#pragma omp barrier

#pragma omp master
		{
			gettimeofday(&t1, 0);
		}

		//Cada hilo accede a un subconjunto de las consultas. Cada hio accede de manera circular al arreglo de consultas.
        for (i = trid; i < N_QUERIES; i += procs) {
         n_elem = 0;
         for (j = 0; j < N_DB; j++) {

            d = distancia(Consultas[i], DB[j]);
				//Si la distancia del objeto a la consulta es menor que la raíz del heap, entonces se inserta en el heap. La raíz siempre mantiene la mayor de las distancias

            if(n_elem<TOPK){
               e_temp.dist = d;
               e_temp.ind = j;
               inserta2(heap, &e_temp, &n_elem);
           }
           if (n_elem==TOPK){
               if (d < topH(heap, &n_elem)) {
                  e_temp.dist = d;
                  e_temp.ind = j;
					//Si el heap no está lleno, se inserta el elemento
                  if (n_elem < TOPK)
                     inserta2(heap, &e_temp, &n_elem);
						//Si el heap está lleno, se inserta el elemento nuevo y se saca el que era antes de mayor de distancia. popush2() hace las operaciones de sacar el elemento mayor e insertar el nuevo.
                 else
                     popush2(heap, &n_elem, &e_temp);
             }}
         }

			//En este punto del código se tienen los K elemntos más cercanos a la consulta en 'heap'. Se pueden extraer con extraer2()
         for (j = 0; j < TOPK ; j++) {
           extrae2(heap, &n_elem, &e_temp);
           answer[i*TOPK+j].ind = e_temp.ind;
           answer[i*TOPK+j].dist = e_temp.dist;
       }
			//Realizamos una operación con los resultados para que el compilador no evite hacer instrucciones que considere que el usuario no utiliza. Simplemente cada hilo suma las distancias de los elementos mas cercanos a la consulta 
   }
   Thread( 1000000 * ( tid + 1 ) );



   fflush(stdout);

#pragma omp barrier

#pragma omp master
   {   

    if ( fip > 0 ) {
		/* Setup PAPI library and begin collecting data from the counters */
       if ( fip == 1 ) {
          if ( ( retval =
             PAPI_flips( &real_time, &proc_time, &flpins,
                &mflops ) ) < PAPI_OK )
             test_fail( __FILE__, __LINE__, "PAPI_flips", retval );
     } else {
      if ( ( retval =
         PAPI_flops( &real_time, &proc_time, &flpins,
            &mflops ) ) < PAPI_OK )
         test_fail( __FILE__, __LINE__, "PAPI_flops", retval );
 }

 gettimeofday(&t2, 0);
 real_time = (t2.tv_sec - t1.tv_sec) + (float) (t2.tv_usec - t1.tv_usec) / 1000000;

 Salida_Multihilo = fopen("Salida_Multihilo.txt", "w");
 for (i = 0; i < N_QUERIES; ++i){
  fprintf(Salida_Multihilo, "Consulta id:: %d\n",i);
  for (j = 0; j < TOPK; ++j){
     fprintf(Salida_Multihilo,"ind = %d :: dist = %f\n",answer[(i*TOPK)+j].ind,answer[(i*TOPK)+j].dist);
 }
 fprintf(Salida_Multihilo, "---------------------------------\n");
}
fclose(Salida_Multihilo);

printf("\n\nK = %d", TOPK);
printf("\nReal Time = %f segundos.\n", real_time);
fflush(stdout);


if ( fip == 1 ) {
  if ( ( retval =
     PAPI_flips( &real_time, &proc_time, &flpins,
        &mflops ) ) < PAPI_OK )
     test_fail( __FILE__, __LINE__, "PAPI_flips", retval );
} else {
  if ( ( retval =
     PAPI_flops( &real_time, &proc_time, &flpins,
        &mflops ) ) < PAPI_OK )
     test_fail( __FILE__, __LINE__, "PAPI_flops", retval );
}

if ( !TESTS_QUIET ) {
  if ( fip == 1 ) {
     printf( "Real_time: %f Proc_time: %f Total flpins: ", real_time,
        proc_time );
 } else {
     printf( "Real_time: %f Proc_time: %f Total flpops: ", real_time,
        proc_time );
 }
 printf( LLDFMT, flpins );
 printf( " MFLOPS: %f\n", mflops );
}
}

}
free(heap);



	}//end pragma omp parallel

	elapsed_cyc = PAPI_get_real_cyc(  ) - elapsed_cyc;
	elapsed_us = PAPI_get_real_usec(  ) - elapsed_us;

	if ( !TESTS_QUIET ) {
		printf( "Master real usec   : \t%lld\n", elapsed_us );
		printf( "Master real cycles : \t%lld\n", elapsed_cyc );
	}

	const PAPI_hw_info_t *hwinfo = NULL;
	const PAPI_mh_tlb_info_t *mhinfo = NULL;
	const  PAPI_mh_cache_info_t *mhcacheinfo = NULL;
	const PAPI_mh_level_t *mhlevel = NULL;


	if (PAPI_library_init(PAPI_VER_CURRENT) != PAPI_VER_CURRENT)
		exit(1);
	if ((hwinfo = PAPI_get_hardware_info()) == NULL)
		exit(1);	
	if ((mhinfo = PAPI_get_hardware_info()) == NULL)
		exit(1);
	if ((mhcacheinfo = PAPI_get_hardware_info()) == NULL)
		exit(1);
	if ((mhlevel = PAPI_get_hardware_info()) == NULL)
		exit(1);

	printf("\n\nA continuación información actual del equipo\n\n");

	printf("MH Type %d - Num entries %d  - Associativity %d \n",mhinfo->type, mhinfo->num_entries, mhinfo->associativity);
	printf("Cache MH type %d size %d line size %d num_lines %d Associativity %d\n\n",mhcacheinfo->type, mhcacheinfo->size,mhcacheinfo->line_size, mhcacheinfo->num_lines, mhcacheinfo->associativity);



    retval=papi_print_header("Available PAPI preset and user defined events plus hardware information.\n",&hwinfo );


    printf("Total hardware flops = %lld\n",(float)values[1]);
    printf("L2 data cache misses is %lld\n", values[0]);






    retval = PAPI_stop_counters(values, NUM_EVENTS);
    return 0;
}