コード例 #1
0
int main(int argc, char **argv)
{
   int retval;
   int preset;

   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_event_name_to_code("PAPI_FP_INS", &preset);
   if (retval != PAPI_OK)
      test_fail(__FILE__, __LINE__, "PAPI_event_name_to_code", retval);
   if (preset != PAPI_FP_INS)
      test_fail(__FILE__, __LINE__, "Wrong preset returned", retval);

   retval = PAPI_event_name_to_code("PAPI_TOT_CYC", &preset);
   if (retval != PAPI_OK)
      test_fail(__FILE__, __LINE__, "PAPI_event_name_to_code", retval);
   if (preset != PAPI_TOT_CYC)
      test_fail(__FILE__, __LINE__, "*preset returned did not equal PAPI_TOT_CYC",
                retval);

   test_pass(__FILE__, NULL, 0);
   exit(1);
}
コード例 #2
0
ファイル: gptl_papi.c プロジェクト: ACME-Climate/cime
int GPTLevent_name_to_code (const char *name, int *code)
{
  int ret;   /* return code */
  int n;     /* loop over derived entries */

  /*
  ** First check derived events 
  */

  for (n = 0; n < nderivedentries; ++n) {
    if (STRMATCH (name, derivedtable[n].namestr)) {
      *code = derivedtable[n].counter;
      return 0;
    }
  }

  /*
  ** Next check PAPI events--note that PAPI must be initialized before the
  ** name_to_code function can be invoked.
  */

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

  if ((PAPI_event_name_to_code ((char *) name, code)) != PAPI_OK)
    return GPTLerror ("GPTL_event_name_to_code: PAPI_event_name_to_code failure\n");

  return 0;
}
コード例 #3
0
int
check_papi_counter(char * counter_name)
{
  PAPI_event_info_t * info;
  int EventSet = PAPI_NULL;
  unsigned int event_code = 0x0;
  int err;

  if ((err=PAPI_create_eventset (&EventSet)) != PAPI_OK){
    fprintf(stderr,"Failed to create a PAPI eventset\n");
    exit(1);
  }
  if((err = PAPI_event_name_to_code(counter_name,&event_code)) != PAPI_OK){
    return err;
  }
  if((err = PAPI_add_event (EventSet, event_code)) != PAPI_OK) {
    return err;
  }

  PAPI_remove_named_event (EventSet, counter_name);
  if ( PAPI_destroy_eventset( &EventSet ) != PAPI_OK ){
    printf("**********  Call to destroy eventset failed when trying to validate event '%s'  **********\n", counter_name);
  }
  return err;
}
コード例 #4
0
void dump_avail(char * (*get_avail(unsigned *)))
{
  unsigned n_avail, i;
  char *ptr, ** avail = get_avail(&n_avail);
  int eventcode = PAPI_NULL, retval;
  PAPI_event_info_t info;

  for(i=0;i<n_avail;i++){
    if(get_avail == get_native_avail_papi_counters || get_avail == get_preset_avail_papi_counters){
      PAPI_event_name_to_code(avail[i],&eventcode);
      PAPI_get_event_info(eventcode, &info);
      printf("\t%s%*s, %s\n",info.symbol, (int)(40-strlen(info.symbol))," ", info.long_descr);
      
      if(get_avail == get_native_avail_papi_counters &&
	 PAPI_enum_event(&eventcode, PAPI_NTV_ENUM_UMASKS) == PAPI_OK){
	printf("\t\t%s%*s, %s\n","Masks", (int)(20-strlen("Masks"))," ", "Mask description");
	do{
	  retval = PAPI_get_event_info(eventcode, &info);
	  if (retval == PAPI_OK){
	    if(parse_unit_masks(&info)){
	      printf("\t\t%s%*s, %s\n",info.symbol, (int)(20-strlen(info.symbol))," ",info.long_descr);
	    }
	  }
	} while ( PAPI_enum_event( &eventcode, PAPI_NTV_ENUM_UMASKS) == PAPI_OK );
      }
    }
    else
      printf("%s\n",avail[i]);

    free(avail[i]);
  }
  free(avail);
}
コード例 #5
0
int PAPI_add_env_event(int *EventSet, int *EventCode, char *env_variable){
  int real_event=*EventCode;
  char *eventname;
  int retval;
 
  if ( env_variable != NULL ){
    if ( (eventname=getenv(env_variable)) ) {
        if ( eventname[0] == 'P' ) {  /* Use the PAPI name */
           retval=PAPI_event_name_to_code(eventname, &real_event );
           if ( retval != PAPI_OK ) real_event = *EventCode;
        }
        else{
           if ( strlen(eventname)>1 && eventname[1]=='x')
                sscanf(eventname, "%#x", &real_event);
           else
               real_event = atoi(eventname);
        }
    }
  }
  if ( (retval = PAPI_add_event( *EventSet, real_event))!= PAPI_OK ){
        if ( real_event != *EventCode ) {
                if ( (retval = PAPI_add_event( *EventSet, *EventCode)) == PAPI_OK
){
                        real_event = *EventCode;
                }
        }
  }
  *EventCode = real_event;
  return retval;
}
コード例 #6
0
ファイル: papi.c プロジェクト: jreybert/power
void name_to_code(int *events_code, char **events_name, int nb_events ) {
  int i, retval;
  for (i=0; i < nb_events; i++) {
    if ( (retval = PAPI_event_name_to_code(events_name[i], &events_code[i]) ) != PAPI_OK ) {
      fprintf(stderr,"Instruction counter \"%s\" does not exist\n%s\n", events_name[i], PAPI_strerror(retval));
    }
  }
}
コード例 #7
0
int main(int argc, char** argv) {
  int Events[NUM_EVENTS]; 
  const char* names[NUM_EVENTS] = {"OPEN_CALLS", "OPEN_FDS", "READ_CALLS", "READ_BYTES", "READ_USEC", "READ_ERR", "READ_INTERRUPTED", "READ_WOULD_BLOCK", "WRITE_CALLS","WRITE_BYTES","WRITE_USEC", "WRITE_WOULD_BLOCK"};
  long long values[NUM_EVENTS];

  /* 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);
  }

  if (!TESTS_QUIET) fprintf(stderr, "This program will read from stdin and echo it to stdout\n");
  int retval;
  int e;
  for (e=0; e<NUM_EVENTS; e++) {
    retval = PAPI_event_name_to_code((char*)names[e], &Events[e]);
    if (retval != PAPI_OK) {
      fprintf(stderr, "Error getting code for %s\n", names[e]);
      exit(2);
    } 
  }

  /* Start counting events */
  if (PAPI_start_counters(Events, NUM_EVENTS) != PAPI_OK) {
    fprintf(stderr, "Error in PAPI_start_counters\n");
    exit(1);
  }

  int bytes = 0;
  char buf[1024];

 
//if (PAPI_read_counters(values, NUM_EVENTS) != PAPI_OK)
//   handle_error(1);
//printf("After reading the counters: %lld\n",values[0]);

  while ((bytes = read(0, buf, 1024)) > 0) {
    write(1, buf, bytes);
  }


  /* Stop counting events */
  if (PAPI_stop_counters(values, NUM_EVENTS) != PAPI_OK) {
    fprintf(stderr, "Error in PAPI_stop_counters\n");
  }
 
  if (!TESTS_QUIET) { 
    printf("----\n");
    for (e=0; e<NUM_EVENTS; e++)  
      printf("%s: %lld\n", names[e], values[e]);
  }
  test_pass( __FILE__, NULL, 0 );
  return 0;
}
コード例 #8
0
ファイル: pointerchasing_init.c プロジェクト: nschloe/sippp
void bi_getinfo(bi_info* infostruct){
  int i, l;
  char buf[200], *s;
  int events[10];

  init_global_vars();

	
  /*infostruct->kernelstring=bi_strdup("Random Memory Access");*/
  infostruct->codesequence=bi_strdup("for i=1,N#  var=memory[random(0..size)]#");
  infostruct->xaxistext=bi_strdup("Accessed Memory in Byte");
  
  infostruct->numfunctions= 1+ NUM_COUNTERS;
  infostruct->maxproblemsize=nMeasurements;
  infostruct->outlier_direction_upwards=malloc(infostruct->numfunctions*sizeof(int));
  
  for (i=0; i< infostruct->numfunctions; i++)
  		infostruct->outlier_direction_upwards[i]=1;
		
  infostruct->log_xaxis=1;
  infostruct->base_xaxis=2.0;
  infostruct->log_yaxis=malloc(infostruct->numfunctions*sizeof(int));
  infostruct->log_yaxis[0]=0;
  infostruct->base_yaxis=malloc(infostruct->numfunctions*sizeof(double));
  infostruct->base_yaxis[0]=0.0;
  infostruct->legendtexts=malloc(infostruct->numfunctions*sizeof(char*));
  
  infostruct->legendtexts[0]=bi_strdup("Average Access Time");
	if ( PAPI_library_init(PAPI_VER_CURRENT) != PAPI_VER_CURRENT) {
		printf("PAPI library init error!\n");
		exit(127);
	}
 	for (i=0; i< NUM_COUNTERS; i++) {
		PAPI_event_name_to_code(sCounters[i],&(events[i]));
		infostruct->legendtexts[i+1] = sCounters[i];
		printf("%s -> %lx\n", sCounters[i],events[i]);
  }
	if (PAPI_start_counters(events, NUM_COUNTERS) != PAPI_OK) {
		printf("PAPI error: start_counters");
		exit(127);
	}
	  
  
  infostruct->yaxistexts=malloc(infostruct->numfunctions*sizeof(char*));
  if (infostruct->legendtexts==0){
    printf("No more core\n");
    exit(127);
  }
  infostruct->yaxistexts[0]=bi_strdup("s");
  infostruct->yaxistexts[1]=bi_strdup("");

}
コード例 #9
0
ファイル: perf.c プロジェクト: jiachengy/numa-db
perf_t*
perf_init()
{
  int retval;
  int EventSet = PAPI_NULL;

  retval = PAPI_create_eventset(&EventSet);
  assert(retval == PAPI_OK);

  retval = PAPI_assign_eventset_component(EventSet, 0);
  assert(retval == PAPI_OK);


  /* By default, the counter is inherited. */
  PAPI_option_t opt;
  memset(&opt, 0x0, sizeof(PAPI_option_t));
  
  opt.inherit.inherit = PAPI_INHERIT_ALL;
  opt.inherit.eventset = EventSet;

  retval = PAPI_set_opt(PAPI_INHERIT, &opt);
  assert(retval == PAPI_OK);

  /* Add events by names */
  for (int i = 0; i < NUM_EVENTS; i++) {
    int EventCode;
    if ((retval = PAPI_event_name_to_code(PERF_EVENT_NAMES[i], &EventCode)) != PAPI_OK) {
      fprintf(stderr, "Event name %s not found, skipped.\n", PERF_EVENT_NAMES[i]);
      continue;
    }


    if ((retval = PAPI_query_event(EventCode)) != PAPI_OK) {
      fprintf(stderr, "Event %s not supported on this hardware, skipped.\n", PERF_EVENT_NAMES[i]);
      continue;
    }

    if ((retval = PAPI_add_event(EventSet, EventCode)) != PAPI_OK) {
      assert(retval == PAPI_ECNFLCT);
      fprintf(stderr, "%s conflicts, skipped.\n", PERF_EVENT_NAMES[i]);
    }
  }

  perf_t *perf = (perf_t*)malloc(sizeof(perf_t));
  perf->EventSet = EventSet;
  /* perf->values = (counter_t*)malloc(sizeof(counter_t) * NUM_EVENTS); */
  /* memset(perf->values, 0x0, sizeof(counter_t) * NUM_EVENTS); */

  return perf;
}
コード例 #10
0
void Timing::setPapiEvents(std::vector<std::string> names){
#ifdef KRIPKE_USE_PAPI


  static bool papi_initialized = false;
  if(!papi_initialized){
    //printf("PAPI INIT\n");
    int retval = PAPI_library_init(PAPI_VER_CURRENT);
    papi_initialized = true;

    if(retval != PAPI_VER_CURRENT){
      fprintf(stderr, "ERROR INITIALIZING PAPI\n");
      exit(1);
    }
  }

  //printf("PAPI VERSION=%x\n",
  //    PAPI_VERSION);

  papi_set = PAPI_NULL;
  PAPI_create_eventset(&papi_set);


  for(int i = 0;i < names.size();++ i){
    // Convert text string to PAPI id
    int event_code;
    PAPI_event_name_to_code(
        const_cast<char*>(names[i].c_str()),
        &event_code);

    // TODO: error checking?

    // Add to our list of PAPI events
    papi_names.push_back(names[i]);
    papi_event.push_back(event_code);

    int retval = PAPI_add_event(papi_set, event_code);
    if(retval != PAPI_OK){
      fprintf(stderr, "ERROR ADDING %s, retval=%d, ID=0x%-10x\n", names[i].c_str(), retval, event_code);
    }

    //printf("EVT=%s, ID=0x%-10x\n", names[i].c_str(), event_code);
  }
  PAPI_start(papi_set);
#else
  if(names.size() > 0){
    fprintf(stderr, "WARNING: PAPI NOT ENABLED, IGNORING PAPI EVENTS\n");
  }
#endif
}
コード例 #11
0
void *ThreadIO(void *arg) {
  unsigned long tid = (unsigned long)pthread_self();
  if (!TESTS_QUIET) printf("\nThread %#lx: will read %s and write it to /dev/null\n", tid,(const char*) arg);
  int Events[NUM_EVENTS]; 
  long long values[NUM_EVENTS];
  int retval;
  int e;
  for (e=0; e<NUM_EVENTS; e++) {
    retval = PAPI_event_name_to_code((char*)names[e], &Events[e]);
    if (retval != PAPI_OK) {
      fprintf(stderr, "Error getting code for %s\n", names[e]);
      exit(2);
    } 
  }

  /* Start counting events */
  if (PAPI_start_counters(Events, NUM_EVENTS) != PAPI_OK) {
    fprintf(stderr, "Error in PAPI_start_counters\n");
    exit(1);
  }
 
//if (PAPI_read_counters(values, NUM_EVENTS) != PAPI_OK)
//   handle_error(1);
//printf("After reading the counters: %lld\n",values[0]);

  int fdin = open((const char*)arg, O_RDONLY);
  if (fdin < 0) perror("Could not open file for reading: \n");

  int bytes = 0;
  char buf[1024];

  int fdout = open("/dev/null", O_WRONLY);
  if (fdout < 0) perror("Could not open /dev/null for writing: \n");
  while ((bytes = read(fdin, buf, 1024)) > 0) {
    write(fdout, buf, bytes);
  }
  close(fdout);

  /* Stop counting events */
  if (PAPI_stop_counters(values, NUM_EVENTS) != PAPI_OK) {
    fprintf(stderr, "Error in PAPI_stop_counters\n");
  }

  if (!TESTS_QUIET) {
    for (e=0; e<NUM_EVENTS; e++)  
      printf("Thread %#lx: %s: %lld\n", tid, names[e], values[e]);
  }
  return(NULL);
}
コード例 #12
0
void papi_base::register_counter( const std::string &counter_name, const std::string &header ) {
		
	int counter;
	char c_style_string[ counter_name.length() + 1 ];
	strcpy( c_style_string, counter_name.c_str() );
	c_style_string[ counter_name.length() ] = '\0';
	int retval = PAPI_event_name_to_code( c_style_string, &counter );
	if ( retval != PAPI_OK ) {
		std::cerr << "Could not decode PAPI counter: " << counter_name << std::endl;
		return;
	}
	else { 
		values_.push_back( 0 );
		counters_.push_back( counter );
		headers_.push_back( header );
	}
}
コード例 #13
0
void *our_thread( void *arg ) {

  int retval,i;

  printf("Entering thread\n");

  retval = PAPI_register_thread(  );
  for(i=0;i<num_events;i++) {
     PAPI_event_name_to_code (event_names[i], &event_codes[i]);
  }

  printf("Done thread\n");

  retval = PAPI_unregister_thread(  );

  return NULL;
}
コード例 #14
0
ファイル: sw_multiplex.c プロジェクト: DanieleDeSensi/mammut
int
mpx_init( int interval_ns )
{
#if defined(PTHREADS) || defined(_POWER6)
	int retval;
#endif

#ifdef _POWER6
	retval = PAPI_event_name_to_code( "PM_RUN_CYC", &_PNE_PM_RUN_CYC );
	if ( retval != PAPI_OK )
		return ( retval );
#endif
	tlist = NULL;
	mpx_hold(  );
	mpx_shutdown_itimer(  );
	mpx_init_timers( interval_ns / 1000 );

	return ( PAPI_OK );
}
コード例 #15
0
void polybench_papi_init()
{
# ifdef _OPENMP
#pragma omp parallel
  {
#pragma omp master
    {
      if (omp_get_max_threads () < polybench_papi_counters_threadid)
	polybench_papi_counters_threadid = omp_get_max_threads () - 1;
    }
#pragma omp barrier

    if (omp_get_thread_num () == polybench_papi_counters_threadid)
      {
# endif
	int retval;
	polybench_papi_eventset = PAPI_NULL;
	if ((retval = PAPI_library_init (PAPI_VER_CURRENT)) != PAPI_VER_CURRENT)
	  test_fail (__FILE__, __LINE__, "PAPI_library_init", retval);
	if ((retval = PAPI_create_eventset (&polybench_papi_eventset))
	    != PAPI_OK)
	  test_fail (__FILE__, __LINE__, "PAPI_create_eventset", retval);
	int k;
	for (k = 0; _polybench_papi_eventlist[k]; ++k)
	  {
	    if ((retval =
		 PAPI_event_name_to_code (_polybench_papi_eventlist[k],
					  &(polybench_papi_eventlist[k])))
		!= PAPI_OK)
	      test_fail (__FILE__, __LINE__, "PAPI_event_name_to_code", retval);
	  }
	polybench_papi_eventlist[k] = 0;


# ifdef _OPENMP
      }
  }
#pragma omp barrier
# endif
}
コード例 #16
0
void 
check_map_event_obj(hwloc_topology_t topology, char * obj_name, char * event_name)
{
  PAPI_event_info_t info;
  int err, eventcode;
  hwloc_obj_t obj;
  unsigned depth = hwloc_get_obj_depth_by_name(topology,obj_name);
  if((err = PAPI_event_name_to_code(event_name,&eventcode))!=PAPI_OK){
    handle_error(err);
    fprintf(stderr,"could not get \"%s\" event infos\n",event_name);
  }
  
  PAPI_get_event_info(eventcode,&info);
  switch(info.location){
  case PAPI_LOCATION_UNCORE:
      if(depth >= hwloc_get_type_depth(topology, HWLOC_OBJ_CORE)){
	fprintf(stderr,"event %s is an UNCORE event and is actually mapped on %s which is under HWLOC_OBJ_CORE\n",
		event_name, obj_name);
      }
      break;      
    case PAPI_LOCATION_CPU:
      if(depth > hwloc_get_type_depth(topology, HWLOC_OBJ_MACHINE)){
	fprintf(stderr,"event %s is a CPU event and is actually mapped on %s which is strictly under HWLOC_OBJ_MACHINE\n",
		event_name, obj_name);
      }
      break;
    case PAPI_LOCATION_PACKAGE:
      if(depth > hwloc_get_type_depth(topology, HWLOC_OBJ_SOCKET)){
	fprintf(stderr,"event %s is a PACKAGE event and is actually mapped on %s which is strictly under HWLOC_OBJ_SOCKET\n",
		event_name, obj_name);
      }
      break;
    default:
      break;
    }
}
コード例 #17
0
ファイル: papi_utils.c プロジェクト: mandrews/spiral_wht_2
void
papi_set_events(char *metric)
{
  const size_t n = 1;

  int max;
  long_long *papi_tmp;
  int papi_events[1];
  int code;

  max = PAPI_num_counters();

  if (n > max)
    papi_eprintf("Too many counters requested.\n");

  papi_tmp = malloc(sizeof(*papi_tmp) * n);

  PAPI_reset(max);

  PAPI_stop_counters(papi_tmp, n);

  if (PAPI_event_name_to_code(metric, &code) != PAPI_OK)
    papi_eprintf("Unknown PAPI event %s.\n", metric);

  if (code == 0)
    papi_eprintf("Unknown PAPI event %s.\n", metric);

  papi_events[0] = code;

  PAPI_start_counters(papi_events, n);

  if (PAPI_read_counters(papi_tmp, n) != PAPI_OK)
    papi_eprintf("Problem reading counters %s:%d.\n", __FILE__, __LINE__);

  free(papi_tmp);
}
コード例 #18
0
int HWCBE_PAPI_Add_Set (int pretended_set, int rank, int ncounters, char **counters,
	char *domain, char *change_at_globalops, char *change_at_time, 
	int num_overflows, char **overflow_counters, unsigned long long *overflow_values)
{
	int i, rc, num_set = HWC_num_sets;
	PAPI_event_info_t info;

#if !defined(PAPI_SAMPLING_SUPPORT)
	UNREFERENCED_PARAMETER(num_overflows);
	UNREFERENCED_PARAMETER(overflow_counters);
	UNREFERENCED_PARAMETER(overflow_values);
#endif
	
	if (ncounters == 0 || counters == NULL)
		return 0;
	
	if (ncounters > MAX_HWC)
	{
		fprintf (stderr, PACKAGE_NAME": You cannot provide more HWC counters than %d (see set %d)\n", MAX_HWC, pretended_set);
		ncounters = MAX_HWC;
	}
	
	HWC_sets = (struct HWC_Set_t *) realloc (HWC_sets, sizeof(struct HWC_Set_t)* (HWC_num_sets+1));
	if (HWC_sets == NULL)
	{
		fprintf (stderr, PACKAGE_NAME": Cannot allocate memory for HWC_set (rank %d)\n", rank);
		return 0;
	}

	/* Initialize this set */
	HWC_sets[num_set].num_counters = 0;
	HWC_sets[num_set].eventsets = NULL;
#if defined(PAPI_SAMPLING_SUPPORT)
	HWC_sets[num_set].OverflowCounter = NULL;
	HWC_sets[num_set].OverflowValue = NULL;
	HWC_sets[num_set].NumOverflows = 0;
#endif

	for (i = 0; i < ncounters; i++)
	{
		/* counter_last_position will hold the address of the end of the 
		   counter[i] string 
		   This shall be compared with strtoul_check to know if the hex
		   is correct or not
		*/
		char *counter_last_position = &(counters[i][strlen(counters[i])]);
		char *strtoul_check;

		HWC_sets[num_set].counters[HWC_sets[num_set].num_counters] = 
			strtoul (counters[i], &strtoul_check, 16);

		if (strtoul_check != counter_last_position)
		{
			int EventCode;
			if (PAPI_event_name_to_code(counters[i], &EventCode) != PAPI_OK)
			{
				if (rank == 0)
					fprintf (stderr, PACKAGE_NAME": Cannot parse HWC %s in set %d, skipping\n", counters[i], pretended_set);
			}
			else
			{
				HWC_sets[num_set].counters[HWC_sets[num_set].num_counters] = EventCode;
			}
		}

		rc = PAPI_get_event_info (HWC_sets[num_set].counters[HWC_sets[num_set].num_counters], &info);
		if (rc != PAPI_OK)
		{
			if (rank == 0)
				fprintf (stderr, PACKAGE_NAME": Error! Cannot query information for hardware counter %s (0x%08x). Check set %d.\n", counters[i], HWC_sets[num_set].counters[HWC_sets[num_set].num_counters], pretended_set);

			HWC_sets[num_set].counters[HWC_sets[num_set].num_counters] = NO_COUNTER;
		}
		/* Native events seem that could have info.count = 0! */
		else if (rc == PAPI_OK && info.count == 0 && (HWC_sets[num_set].counters[HWC_sets[num_set].num_counters] & PAPI_NATIVE_MASK) == 0)
		{
			if (rank == 0)
				fprintf (stderr, PACKAGE_NAME": Error! Hardware counter %s (0x%08x) is not available. Check set %d.\n", counters[i], HWC_sets[num_set].counters[HWC_sets[num_set].num_counters], pretended_set);

			HWC_sets[num_set].counters[HWC_sets[num_set].num_counters] = NO_COUNTER;
		}
		else 
		{
			if (rank == 0)
				HWCBE_PAPI_AddDefinition (HWC_sets[num_set].counters[HWC_sets[num_set].num_counters],
					info.symbol, (info.event_code & PAPI_PRESET_MASK)?info.short_descr:info.long_descr);

			HWC_sets[num_set].num_counters++;
		}
	}

	if (HWC_sets[num_set].num_counters == 0)
	{
		if (rank == 0)
			fprintf (stderr, PACKAGE_NAME": Set %d of counters seems to be empty/invalid, skipping\n", pretended_set);
		return 0;
	}

	/* Just check if the user wants us to change the counters in some manner */
	if (change_at_time != NULL)
	{
		HWC_sets[num_set].change_at = getTimeFromStr (change_at_time, 
			"change-at-time", rank);
		HWC_sets[num_set].change_type = 
				(HWC_sets[num_set].change_at == 0)?CHANGE_NEVER:CHANGE_TIME;
	}
	else if (change_at_globalops != NULL)
	{
		HWC_sets[num_set].change_at = strtoul (change_at_globalops, (char **) NULL, 10);
		HWC_sets[num_set].change_type = 
			(HWC_sets[num_set].change_at == 0)?CHANGE_NEVER:CHANGE_GLOPS;
	}
	else
		HWC_sets[num_set].change_type = CHANGE_NEVER;
	
	if (domain != NULL)
	{
		if (!strcasecmp(domain, "all"))
		{
			if (rank == 0)
				fprintf (stdout, PACKAGE_NAME": PAPI domain set to ALL for HWC set %d\n",
					pretended_set);
			HWC_sets[num_set].domain = PAPI_DOM_ALL;
		}	
		else if (!strcasecmp(domain, "kernel"))
		{
			if (rank == 0)
				fprintf (stdout, PACKAGE_NAME": PAPI domain set to KERNEL for HWC set %d\n",
					pretended_set);
			HWC_sets[num_set].domain = PAPI_DOM_KERNEL;
		}	
		else if (!strcasecmp(domain, "user"))
		{
			if (rank == 0)
				fprintf (stdout, PACKAGE_NAME": PAPI domain set to USER for HWC set %d\n",
					pretended_set);
			HWC_sets[num_set].domain = PAPI_DOM_USER;
		}	
		else if (!strcasecmp(domain, "other"))
		{
			if (rank == 0)
				fprintf (stdout, PACKAGE_NAME": PAPI domain set to OTHER for HWC set %d\n",
					pretended_set);
			HWC_sets[num_set].domain = PAPI_DOM_OTHER;
		}	
		else
		{
			if (rank == 0)
				fprintf (stdout, PACKAGE_NAME": PAPI domain set to USER for HWC set %d\n",
					pretended_set);
			HWC_sets[num_set].domain = PAPI_DOM_USER;
		}
	} /* domain != NULL */
	else
	{
		if (rank == 0)
			fprintf (stdout, PACKAGE_NAME": PAPI domain set to USER for HWC set %d\n",
				pretended_set);
		HWC_sets[num_set].domain = PAPI_DOM_USER;
	}

	HWCBE_PAPI_Allocate_eventsets_per_thread (num_set, 0, Backend_getNumberOfThreads());

	/* We validate this set */
	HWC_num_sets++;

	if (rank == 0)
	{
		fprintf (stdout, PACKAGE_NAME": HWC set %d contains following counters < ", pretended_set);
		for (i = 0; i < HWC_sets[num_set].num_counters; i++)
		{
			if (HWC_sets[num_set].counters[i] != NO_COUNTER)
			{
				char counter_name[PAPI_MAX_STR_LEN];

				PAPI_event_code_to_name (HWC_sets[num_set].counters[i], counter_name);
				fprintf (stdout, "%s (0x%08x) ", counter_name, HWC_sets[num_set].counters[i]);
			}
		}
		fprintf (stdout, ">");

		if (HWC_sets[num_set].change_type == CHANGE_TIME)
			fprintf (stdout, " - changing every %lld nanoseconds\n", HWC_sets[num_set].change_at);
		else if (HWC_sets[num_set].change_type == CHANGE_GLOPS)
			fprintf (stdout, " - changing every %lld global operations\n", HWC_sets[num_set].change_at);
		else
			fprintf (stdout, " - never changes\n");

		fflush (stdout);
	}

#if defined(PAPI_SAMPLING_SUPPORT)
	if (num_overflows > 0)
		Add_Overflows_To_Set (rank, num_set, pretended_set, num_overflows,
			overflow_counters, overflow_values);
#endif

	return HWC_sets[num_set].num_counters;
}
コード例 #19
0
int vt_metric_open()
{
  int retval;
  char* env;
  char* env_sep;
  char* var;
  char* token;
  char* saveptr;
  PAPI_event_info_t info;
  metricmap_t* mapv = NULL;
  metricmap_t* map;

  /* read environment variable "VT_METRICS". Return if
     uset and no PAPI timer used. */
  env = vt_env_metrics();
  if( env == NULL )
  {
#if TIMER != TIMER_PAPI_REAL_CYC && TIMER != TIMER_PAPI_REAL_USEC
    return nmetrics;
#endif
  }

  env_sep = vt_env_metrics_sep();

  mapv = vt_metricmap_init(
    (metmap_t)(METMAP_MEASURE|METMAP_AGGROUP));
  metricmap_dump(mapv);

  /* initialize PAPI */
  retval = PAPI_library_init(PAPI_VER_CURRENT);
  if ( retval != PAPI_VER_CURRENT )
    metric_error(retval, "PAPI_library_init");

  /* return if environment variable is unset */
  if ( env == NULL )
    return nmetrics;

  var = strdup(env);
  vt_cntl_msg(2, "VT_METRICS=%s", var);

  /* read metrics from specification string */
  token = strtok_r(var, env_sep, &saveptr);
  while ( token && (nmetrics < VT_METRIC_MAXNUM) ) {
    /* set counter properties */
    uint32_t props;
    if (token[0]=='!')
    {
      props = VT_CNTR_ABS | VT_CNTR_NEXT;
      token++;
    }
    else
    {
      props = VT_CNTR_ACC;
    }
    /* search metricmap for a suitable definition */
    map = mapv;
    /*printf("Token%d: <%s>\n", nmetrics, token);*/
    while (map != NULL) {
      if ( strcmp(map->event_name, token) == 0 ) {
        /*printf("Definition %s = <%s>\n", token, map->alias_name);*/
        /* expand definition and set components */
        char* c_token = map->alias_name;
        int len = strcspn(c_token, " \t"); /* first token */
        int got_valid_match = 1; /* to be verified */
        int k = 0;
        do { /* verify each component of definition is available */
          char component[64];
          int code = -1;
          strncpy(component, c_token, len);
          component[len] = '\0';
          /*printf("Comp[%d] <%s>\n", k, component);*/
          c_token += len + strspn(c_token+len, " \t");
          len = strcspn(c_token, " \t"); /* next token */

          PAPI_event_name_to_code(component, &code);
          memset(&info, 0, sizeof(PAPI_event_info_t));
          retval = PAPI_get_event_info(code, &info);
          /*printf("v[%d] %s [0x%X] %d\n", k, component, code, info.count);*/

          if (info.count == 0) {
            /*printf("Event %s *N/A*\n", component);*/
            got_valid_match = 0;
          } else if ((k==0) && (len==0)) { /* use provided event name */
            metricv_add(token, code, props);
          } else { /* use alias component name */
            metricv_add(component, code, props);
          }
          k++;
        } while (got_valid_match && (len > 0));
        if (got_valid_match) {
          /*printf("Definition %s = <%s> OK\n", map->event_name, map->alias_name);*/
          break; /* accept this event definition */
        }
      }
      map = map->next;
    }

    if (map == NULL) { /* no map match, so try given name */
      int code = -1;
      char* component = token;
      /*printf("Comp[X] <%s>\n", component);*/
      retval = PAPI_event_name_to_code(component, &code);
      if (retval != PAPI_OK || code == -1)
        vt_error_msg("Metric <%s> not supported\n", component);

      memset(&info, 0, sizeof(PAPI_event_info_t));
      retval = PAPI_get_event_info(code, &info);
      /*printf("v[%d] %s [0x%X] %d\n", nmetrics, component, code, info.count);*/
      if (retval != PAPI_OK)
        vt_error_msg("Metric <%s> not available\n", component);

      metricv_add(component, code, props);
    }

    token = strtok_r(NULL, env_sep, &saveptr);
  }

  /*printf("nmetrics=%d\n", nmetrics);*/

  /* clean up */
  metricmap_free(mapv);
  free(var);

  /* Check whether event combination is valid. This is done here to
     avoid errors when creating the event set for each thread, which
     would multiply the error messages. */
  metric_test();

  metric_descriptions();

  return nmetrics;
}
コード例 #20
0
ファイル: command_line.c プロジェクト: FMCalisto/SMP
int
main( int argc, char **argv )
{
	int retval;
	int num_events;
	long long *values;
	char *success;
	PAPI_event_info_t info;
	int EventSet = PAPI_NULL;
	int i, j, data_type, event;
	int u_format = 0;
	int hex_format = 0;

	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_create_eventset( &EventSet ) ) != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );

	/* Automatically pass if no events, for run_tests.sh */
	if ((( TESTS_QUIET ) && ( argc == 2)) || ( argc == 1 )) {
		test_pass( __FILE__, NULL, 0 );
	}

	values =
		( long long * ) malloc( sizeof ( long long ) * ( size_t ) argc );
	success = ( char * ) malloc( ( size_t ) argc );

	if ( success == NULL || values == NULL )
		test_fail_exit( __FILE__, __LINE__, "malloc", PAPI_ESYS );

	for ( num_events = 0, i = 1; i < argc; i++ ) {
		if ( !strcmp( argv[i], "-h" ) ) {
			print_help( argv );
			exit( 1 );
		} else if ( !strcmp( argv[i], "-u" ) ) {
			u_format = 1;
		} else if ( !strcmp( argv[i], "-x" ) ) {
			hex_format = 1;
		} else {
			if ( ( retval = PAPI_add_named_event( EventSet, argv[i] ) ) != PAPI_OK ) {
				printf( "Failed adding: %s\nbecause: %s\n", argv[i], 
					PAPI_strerror(retval));
			} else {
				success[num_events++] = i;
				printf( "Successfully added: %s\n", argv[i] );
			}
		}
	}

	/* Automatically pass if no events, for run_tests.sh */
	if ( num_events == 0 ) {
		test_pass( __FILE__, NULL, 0 );
	}


	printf( "\n" );

	do_flops( 1 );
	do_flush(  );

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

	do_flops( NUM_FLOPS );
	do_misses( 1, L1_MISS_BUFFER_SIZE_INTS );

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

	for ( j = 0; j < num_events; j++ ) {
		i = success[j];
		if (! (u_format || hex_format) ) {
			retval = PAPI_event_name_to_code( argv[i], &event );
			if (retval == PAPI_OK) {
				retval = PAPI_get_event_info(event, &info);
				if (retval == PAPI_OK) data_type = info.data_type;
				else data_type = PAPI_DATATYPE_INT64;
			}
			switch (data_type) {
			  case PAPI_DATATYPE_UINT64:
				printf( "%s : \t%llu(u)", argv[i], (unsigned long long)values[j] );
				break;
			  case PAPI_DATATYPE_FP64:
				printf( "%s : \t%0.3f", argv[i], *((double *)(&values[j])) );
				j++;
				break;
			  case PAPI_DATATYPE_BIT64:
				printf( "%s : \t0x%llX", argv[i], values[j] );
				break;
			  case PAPI_DATATYPE_INT64:
			  default:
				printf( "%s : \t%lld", argv[i], values[j] );
				break;
			}
			if (retval == PAPI_OK)  printf( " %s", info.units );
			printf( "\n" );
		}
		if (u_format) printf( "%s : \t%llu(u)\n", argv[i], (unsigned long long)values[j] );
		if (hex_format) printf( "%s : \t0x%llX\n", argv[i], values[j] );
	}

	printf( "\n----------------------------------\n" );
	printf
		( "Verification: Checks for valid event name.\n This utility lets you add events from the command line interface to see if they work.\n" );
	test_pass( __FILE__, NULL, 0 );
	exit( 1 );
}
コード例 #21
0
int Add_Overflows_To_Set (int rank, int num_set, int pretended_set, 
	int num_overflows, char **counter_to_ovfs, unsigned long long *ovf_values)
{
	int cnt, i, found;
	char *strtoul_check;

	HWC_sets[num_set].OverflowCounter = (int*) malloc (sizeof(int) * num_overflows);
	if (HWC_sets[num_set].OverflowCounter == NULL)
	{
		fprintf (stderr, PACKAGE_NAME": ERROR cannot allocate memory for OverflowCounter structure at %s:%d\n",__FILE__,__LINE__);
		return FALSE;
	}
	HWC_sets[num_set].OverflowValue = (long long*) malloc (sizeof(long long) * num_overflows);
	if (HWC_sets[num_set].OverflowValue == NULL)
	{
		fprintf (stderr, PACKAGE_NAME": ERROR cannot allocate memory for OverflowValue structure at %s:%d\n",__FILE__,__LINE__);
		return FALSE;
	}
	HWC_sets[num_set].NumOverflows = num_overflows;
	for (cnt = 0; cnt < num_overflows; cnt++)
	{
		char *counter_last_position = &(counter_to_ovfs[cnt][strlen(counter_to_ovfs[cnt])]);
	
		/* Convert this counter into a code */
		HWC_sets[num_set].OverflowCounter[cnt] = strtoul (counter_to_ovfs[cnt], &strtoul_check, 16);
		if (strtoul_check != counter_last_position)
		{
			int EventCode;
			if (PAPI_event_name_to_code(counter_to_ovfs[cnt], &EventCode) != PAPI_OK)
			{
				if (rank == 0)
					fprintf (stderr, PACKAGE_NAME": Cannot parse HWC %s in set %d for sampling, skipping\n", counter_to_ovfs[cnt], pretended_set);
				HWC_sets[num_set].OverflowCounter[cnt] = NO_COUNTER;
			}
			else
				HWC_sets[num_set].OverflowCounter[cnt] = EventCode;
		}

		/* Check if this counter code is in the HWC_set */
		if (HWC_sets[num_set].OverflowCounter[cnt] != NO_COUNTER)
		{
			for (found = FALSE, i = 0; i < HWC_sets[num_set].num_counters; i++)
				found |= (HWC_sets[num_set].counters[i] == HWC_sets[num_set].OverflowCounter[cnt]);

			if (!found)
			{
				HWC_sets[num_set].OverflowCounter[cnt] = NO_COUNTER;
				if (rank == 0)
					fprintf (stderr, PACKAGE_NAME": Sampling counter %s is not in available in set\n", counter_to_ovfs[cnt]);
				/* return FALSE; */
			}
		}

		HWC_sets[num_set].OverflowValue[cnt] = ovf_values[cnt];
	
		if (rank == 0)
			fprintf (stdout, PACKAGE_NAME": HWC set %d sampling counter %s (0x%08x) every %lld events.\n", pretended_set, counter_to_ovfs[cnt], HWC_sets[num_set].OverflowCounter[cnt], ovf_values[cnt]);
	}


	return TRUE;
}
コード例 #22
0
ファイル: papi_preset.c プロジェクト: pyrovski/papi-rapl
/* The function below, _xml_start(), is a hook into expat's XML
 * parser.  _xml_start() defines how the parser handles the
 * opening tags in PAPI's XML file.  This function can be understood
 * more easily if you follow along with its logic while looking at
 * papi_events.xml.  The location variable is a global telling us
 * where we are in the XML file.  Have we found our architecture's
 * events yet?  Are we looking at an event definition?...etc.
 */
static void
_xml_start( void *data, const char *el, const char **attr )
{
	int native_encoding;

	if ( location == SPARSE_BEGIN && !strcmp( "papistdevents", el ) ) {
		location = SPARSE_EVENT_SEARCH;
	} else if ( location == SPARSE_EVENT_SEARCH && !strcmp( "papievent", el ) ) {
		_papi_hwi_presets[sparse_index].info.symbol = papi_strdup( attr[1] );
//      strcpy(_papi_hwi_presets.info[sparse_index].symbol, attr[1]);
		location = SPARSE_EVENT;
	} else if ( location == SPARSE_EVENT && !strcmp( "desc", el ) ) {
		location = SPARSE_DESC;
	} else if ( location == ARCH_SEARCH && !strcmp( "availevents", el ) &&
				!strcmp( xml_arch, attr[1] ) ) {
		location = DENSE_EVENT_SEARCH;
	} else if ( location == DENSE_EVENT_SEARCH && !strcmp( "papievent", el ) ) {
		if ( !strcmp( "PAPI_NULL", attr[1] ) ) {
			location = FINISHED;
			return;
		} else if ( PAPI_event_name_to_code( ( char * ) attr[1], &sparse_index )
					!= PAPI_OK ) {
			PAPIERROR( "Improper Preset name given in XML file for %s.",
					   attr[1] );
			error = 1;
		}
		sparse_index &= PAPI_PRESET_AND_MASK;

		/* allocate and initialize data space for this event */
		papi_valid_free( _papi_hwi_presets[sparse_index].data );
		_papi_hwi_presets[sparse_index].data =
			papi_malloc( sizeof ( hwi_preset_data_t ) );
		native_index = 0;
		_papi_hwi_presets[sparse_index].data->native[native_index] = PAPI_NULL;
		_papi_hwi_presets[sparse_index].data->operation[0] = '\0';


		if ( attr[2] ) {	 /* derived event */
			_papi_hwi_presets[sparse_index].data->derived =
				_papi_hwi_derived_type( ( char * ) attr[3] );
			/* where does DERIVED POSTSCRIPT get encoded?? */
			if ( _papi_hwi_presets[sparse_index].data->derived == -1 ) {
				PAPIERROR( "No derived type match for %s in Preset XML file.",
						   attr[3] );
				error = 1;
			}

			if ( attr[5] ) {
				_papi_hwi_presets[sparse_index].count = atoi( attr[5] );
			} else {
				PAPIERROR( "No count given for %s in Preset XML file.",
						   attr[1] );
				error = 1;
			}
		} else {
			_papi_hwi_presets[sparse_index].data->derived = NOT_DERIVED;
			_papi_hwi_presets[sparse_index].count = 1;
		}
		location = DENSE_NATIVE_SEARCH;
	} else if ( location == DENSE_NATIVE_SEARCH && !strcmp( "native", el ) ) {
		location = DENSE_NATIVE_DESC;
	} else if ( location == DENSE_NATIVE_DESC && !strcmp( "event", el ) ) {
		if ( _papi_hwi_native_name_to_code( attr[1], &native_encoding ) !=
			 PAPI_OK ) {
			printf( "Improper Native name given in XML file for %s\n",
					attr[1] );
			PAPIERROR( "Improper Native name given in XML file for %s\n",
					   attr[1] );
			error = 1;
		}
		_papi_hwi_presets[sparse_index].data->native[native_index] =
			native_encoding;
		native_index++;
		_papi_hwi_presets[sparse_index].data->native[native_index] = PAPI_NULL;
	} else if ( location && location != ARCH_SEARCH && location != FINISHED ) {
		PAPIERROR( "Poorly-formed Preset XML document." );
		error = 1;
	}
}
コード例 #23
0
ファイル: data_range.c プロジェクト: naps62/CPD_PAPI
int
main( int argc, char **argv )
{
	int retval;
	const PAPI_exe_info_t *prginfo = NULL;
	const PAPI_hw_info_t *hw_info;

#if !defined(ITANIUM2) && !defined(ITANIUM3)
	test_skip( __FILE__, __LINE__, "Currently only works on itanium2", 0 );
	exit( 1 );
#endif

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

	init_array(  );
	printf( "Malloc'd array  pointers: %p   %p   %p\n", &parray1, &parray2,
			&parray3 );
	printf( "Malloc'd array addresses: %p   %p   %p\n", parray1, parray2,
			parray3 );
	printf( "Static   array addresses: %p   %p   %p\n", &array1, &array2,
			&array3 );

	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 ( ( prginfo = PAPI_get_executable_info(  ) ) == NULL )
		test_fail( __FILE__, __LINE__, "PAPI_get_executable_info", 1 );

#if defined(linux) && defined(__ia64__)
	sprintf( event_name[0], "loads_retired" );
	sprintf( event_name[1], "stores_retired" );
	PAPI_event_name_to_code( event_name[0], &PAPI_event[0] );
	PAPI_event_name_to_code( event_name[1], &PAPI_event[1] );
#else
	test_fail( __FILE__, __LINE__, "only works for Itanium", PAPI_ESBSTR );
#endif

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

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

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

/***************************************************************************************/
	printf
		( "\n\nMeasure loads and stores on the pointers to the allocated arrays\n" );
	printf( "Expected loads: %d; Expected stores: 0\n", NUM * 2 );
	printf
		( "These loads result from accessing the pointers to compute array addresses.\n" );
	printf
		( "They will likely disappear with higher levels of optimization.\n" );

	measure_load_store( ( caddr_t ) & parray1, ( caddr_t ) ( &parray1 + 1 ) );
	measure_load_store( ( caddr_t ) & parray2, ( caddr_t ) ( &parray2 + 1 ) );
	measure_load_store( ( caddr_t ) & parray3, ( caddr_t ) ( &parray3 + 1 ) );
/***************************************************************************************/
	printf
		( "\n\nMeasure loads and stores on the allocated arrays themselves\n" );
	printf( "Expected loads: %d; Expected stores: %d\n", NUM, NUM );

	measure_load_store( ( caddr_t ) parray1, ( caddr_t ) ( parray1 + NUM ) );
	measure_load_store( ( caddr_t ) parray2, ( caddr_t ) ( parray2 + NUM ) );
	measure_load_store( ( caddr_t ) parray3, ( caddr_t ) ( parray3 + NUM ) );
/***************************************************************************************/
	printf( "\n\nMeasure loads and stores on the static arrays\n" );
	printf
		( "These values will differ from the expected values by the size of the offsets.\n" );
	printf( "Expected loads: %d; Expected stores: %d\n", NUM, NUM );

	measure_load_store( ( caddr_t ) array1, ( caddr_t ) ( array1 + NUM ) );
	measure_load_store( ( caddr_t ) array2, ( caddr_t ) ( array2 + NUM ) );
	measure_load_store( ( caddr_t ) array3, ( caddr_t ) ( array3 + NUM ) );
/***************************************************************************************/

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

	free( parray1 );
	free( parray2 );
	free( parray3 );

	test_pass( __FILE__, NULL, 0 );

	exit( 1 );
}
コード例 #24
0
ファイル: wl_lsms.cpp プロジェクト: sidjana/lsms-shmem
int main(int argc, char *argv[])
{
  int size, rank, world_rank, my_group;
  int num_lsms; // number of parallel LSMS instances
  int size_lsms; // number of atoms in a lsms instance
  int num_steps; // number of energy calculations
  int initial_steps; // number of steps before sampling starts
  int stepCount=0; // count the Monte Carlo steps executed
  double max_time; // maximum walltime for this run in seconds
  bool restrict_time = false;       // was the maximum time specified?
  bool restrict_steps = false; // or the max. numer of steps?
  int align; // alignment of lsms_instances
  
  double magnetization;
  double energy_accumulator; // accumulates the enegy to calculate the mean
  int energies_accumulated;


  int new_peid,new_root;
  static int op,flag;
  double *evec,*r_values;
  evec=(double *)shmalloc(sizeof(double)*3*size_lsms);
  r_values=(double *)shmalloc(sizeof(double)*(R_VALUE_OFFSET+3*(size_lsms+1)));




  energy_accumulator=0.0;
  energies_accumulated=0;

  double walltime_0,walltime;

  double restartWriteFrequency=30.0*60.0;
  double nextWriteTime=restartWriteFrequency;

  MPI_Comm local_comm;
  int *lsms_rank0;
  MPI_Status status;

  char prefix[40];
  char i_lsms_name[64];
  char gWL_in_name[64], gWL_out_name[64];
  char mode_name[64];
  char energy_calculation_name[64];
  char stupid[37];

  char step_out_name[64];
  char wl_step_out_name[128];
  char *wl_stepf=NULL;
  bool step_out_flag=false;
  std::ofstream step_out_file;
  typedef enum {Constant, Random, WangLandau_1d, ExhaustiveIsing, WangLandau_2d} EvecGenerationMode;
  typedef enum {MagneticMoment, MagneticMomentZ, MagneticMomentX, MagneticMomentY} SecondDimension;

  EvecGenerationMode evec_generation_mode = Constant;
  SecondDimension second_dimension = MagneticMoment;
  double ev0[3];

  bool return_moments_flag=true; // true-> return all magnetic moments from lsms run at each step.
  bool generator_needs_moment=false;

  typedef enum {OneStepEnergy, MultiStepEnergy, ScfEnergy} EnergyCalculationMode;
  EnergyCalculationMode energyCalculationMode = OneStepEnergy;
  int energyIndex=1; // index for the return value to use for the MC step (0: total energy, 1: band energy)

  ev0[0]=ev0[1]=0.0; ev0[2]=1.0;
  // size has to be align + size_lsms*num_lsms
  align=1;
  num_lsms=1;
  size_lsms=-1;
  my_group=-1;
  num_steps=1;
  initial_steps=0;

  sprintf(i_lsms_name,"i_lsms");
  gWL_in_name[0]=gWL_out_name[0]=0;
  mode_name[0]=0;
  energy_calculation_name[0]=0;

  // check command line arguments
  for(int i=0; i<argc; i++)
  {
    if(!strcmp("-num_lsms",argv[i])) num_lsms=atoi(argv[++i]);
    if(!strcmp("-size_lsms",argv[i])) size_lsms=atoi(argv[++i]);
    if(!strcmp("-align",argv[i])) align=atoi(argv[++i]);
    if(!strcmp("-num_steps",argv[i])) {num_steps=atoi(argv[++i]); restrict_steps=true;}
    if(!strcmp("-initial_steps",argv[i])) initial_steps=atoi(argv[++i]); 
    if(!strcmp("-walltime",argv[i])) {max_time=60.0*atof(argv[++i]); restrict_time=true;}
    if(!strcmp("-i",argv[i])) strncpy(i_lsms_name,argv[++i],64);
    if(!strcmp("-random_dir",argv[i])) {evec_generation_mode = Random;}
    if(!strcmp("-step_out",argv[i]))
    {strncpy(step_out_name,argv[++i],64); step_out_flag=true;
      return_moments_flag=true;}
    if(!strcmp("-wl_out", argv[i])) strncpy(gWL_out_name,argv[++i],64);
    if(!strcmp("-wl_in", argv[i])) strncpy(gWL_in_name,argv[++i],64);
    if(!strcmp("-mode", argv[i])) strncpy(mode_name,argv[++i],64);
    if(!strcmp("-energy_calculation",argv[i])) strncpy(energy_calculation_name,argv[++i],64);
  }

  if(!(restrict_steps || restrict_time)) restrict_steps=true;

  if(mode_name[0]!=0)
  {
    if(!strcmp("constant",mode_name)) evec_generation_mode = Constant;
    if(!strcmp("random",mode_name)) evec_generation_mode = Random;
    if(!strcmp("1d",mode_name)) evec_generation_mode = WangLandau_1d;
    if(!strcmp("ising",mode_name)) evec_generation_mode = ExhaustiveIsing;
    if(!strcmp("2d",mode_name)) evec_generation_mode = WangLandau_2d;
    if(!strcmp("2d-m",mode_name)) {evec_generation_mode = WangLandau_2d; second_dimension=MagneticMoment;}
    if(!strcmp("2d-x",mode_name)) {evec_generation_mode = WangLandau_2d; second_dimension=MagneticMomentX;}
    if(!strcmp("2d-y",mode_name)) {evec_generation_mode = WangLandau_2d; second_dimension=MagneticMomentY;}
    if(!strcmp("2d-z",mode_name)) {evec_generation_mode = WangLandau_2d; second_dimension=MagneticMomentZ;}
  }

  if(energy_calculation_name[0]!=0)
  {
    if(energy_calculation_name[0]=='o') { energyCalculationMode = OneStepEnergy; energyIndex=1; }
    if(energy_calculation_name[0]=='m') { energyCalculationMode = MultiStepEnergy; energyIndex=1; }
    if(energy_calculation_name[0]=='s') { energyCalculationMode = ScfEnergy; energyIndex=0; }
  }

#ifdef USE_PAPI
#define NUM_PAPI_EVENTS 4
  int hw_counters = PAPI_num_counters();
  if(hw_counters>NUM_PAPI_EVENTS) hw_counters=NUM_PAPI_EVENTS;
  int papi_events[NUM_PAPI_EVENTS]; // = {PAPI_TOT_INS,PAPI_TOT_CYC,PAPI_FP_OPS,PAPI_VEC_INS};
  char *papi_event_name[] = {"PAPI_TOT_INS","PAPI_FP_OPS",
                             "RETIRED_SSE_OPERATIONS:DOUBLE_ADD_SUB_OPS:DOUBLE_MUL_OPS:DOUBLE_DIV_OPS:OP_TYPE",
                             "RETIRED_SSE_OPERATIONS:SINGLE_ADD_SUB_OPS:SINGLE_MUL_OPS:SINGLE_DIV_OPS:OP_TYPE"};
  // "RETIRED_INSTRUCTIONS",
  // "RETIRED_MMX_AND_FP_INSTRUCTIONS:PACKED_SSE_AND_SSE2",
  // "RETIRED_SSE_OPERATIONS:DOUBLE_ADD_SUB_OPS:DOUBLE_MUL_OPS:DOUBLE_DIV_OPS:1",
  // "RETIRED_SSE_OPERATIONS:SINGLE_ADD_SUB_OPS:SINGLE_MUL_OPS:SINGLE_DIV_OPS:1"
  // get events from names:
  for(int i=0; i<NUM_PAPI_EVENTS; i++)
  {
    if(PAPI_event_name_to_code(papi_event_name[i],&papi_events[i]) != PAPI_OK)
    {
      // printline("Error in obtaining PAPI event code for: "+ttos(papi_event_name[i]),
      //           std::cerr,parameters.myrankWorld);
      // printline("Skipping all following events",
      //           std::cerr,parameters.myrankWorld);
      if(hw_counters>i) hw_counters=i;
    }
  }
  long long papi_values[NUM_PAPI_EVENTS+4];
  // printline("PAPI: "+ttos(hw_counters)+" counters available",std::cout,parameters.myrankWorld);
  if(hw_counters>NUM_PAPI_EVENTS) hw_counters=NUM_PAPI_EVENTS;
  long long papi_real_cyc_0 = PAPI_get_real_cyc();
  long long papi_real_usec_0 = PAPI_get_real_usec();
  long long papi_virt_cyc_0 = PAPI_get_virt_cyc();
  long long papi_virt_usec_0 = PAPI_get_virt_usec();
  PAPI_start_counters(papi_events,hw_counters);
#endif


  lsms_rank0=(int *)malloc(sizeof(int)*(num_lsms+1));

  // initialize MPI:
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  world_rank=rank;
  MPI_Comm_size(MPI_COMM_WORLD, &size);

  walltime_0 = get_rtc();

#ifndef SVN_REV
#define SVN_REV "unknown"
#endif

// make sure 'return_moments_flag' is set correctly
  switch(evec_generation_mode)
  {
  case Constant : break;
  case Random : break;
  case WangLandau_1d :
    return_moments_flag = true;
    generator_needs_moment = true;
    break;
  case ExhaustiveIsing : break;
  case WangLandau_2d :
    return_moments_flag = true;
    generator_needs_moment = true;
    break;
  default: std::cout<<" ERROR: UNKNOWN EVEC GENERATION MODE\n"; exit(1);
  }

  if(rank==0)
  {
    std::cout<<"LSMS_3"<<std::endl;
    std::cout<<" SVN revision "<<SVN_REV<<std::endl<<std::endl;
#ifdef USE_PAPI
    std::cout<<" Using Papi counters"<<std::endl<<std::endl; 
#endif
    std::cout<<" Size of LSMS instances = "<<size_lsms<<" atoms\n";
    std::cout<<" Number of LSMS instances = "<<num_lsms<<std::endl;
    std::cout<<" LSMS Energy calculated using ";
    switch(energyCalculationMode)
    {
    case OneStepEnergy: std::cout<<"oneStepEnergy [frozen potential band energy]"<<std::endl; break;
    case MultiStepEnergy: std::cout<<"multiStepEnergy [frozen potential band energy with converged Fermi energy]"<<std::endl; break;
    case ScfEnergy: std::cout<<"scfEnergy [self-consistent total energy]"<<std::endl; break;
    default: std::cout<<"UNKNOWN ENERGY CALCULATION METHOD"<<std::endl; exit(1);
    }
    if(restrict_steps) std::cout<<" Number of gWL steps = "<<num_steps<<std::endl;
    if(restrict_time) std::cout<<" Maximum walltime = "<<max_time<<"s\n";
    std::cout<<" Processor alignment (process allocation quantization) = "<<align<<std::endl;
    switch(evec_generation_mode)
    {
    case Constant : std::cout<<" Constant moments direction along "
                             <<ev0[0]<<" "<<ev0[1]<<" "<<ev0[2]<<std::endl;
      break;
    case Random : std::cout<<" Random distribution of moments (no Wang-Landau)"<<std::endl;
      break;
    case WangLandau_1d : std::cout<<" Wang-Landau for one continuous variable (energy)"<<std::endl;
//      return_moments_flag = true;
//      generator_needs_moment = true;
      break;
    case ExhaustiveIsing : std::cout<<" Exhaustive Ising sampling"<<std::endl; break;
    case WangLandau_2d : std::cout<<" Wang-Landau for two continuous variable (energy, ";
      switch(second_dimension)
      {
      case MagneticMoment  : std::cout<<"magnitude of magnetization)"; break;
      case MagneticMomentX : std::cout<<"x component of magnetization)"; break;
      case MagneticMomentY : std::cout<<"y component of magnetization)"; break;
      case MagneticMomentZ : std::cout<<"z component of magnetization)"; break;
      }
      std::cout<<std::endl;
//      return_moments_flag = true;
//      generator_needs_moment = true;
      break;
    default: std::cout<<" ERROR: UNKNOWN EVEC GENERATION MODE\n"; exit(1);
    }
    if(step_out_flag) std::cout<<" Step output written to: "<<step_out_name<<std::endl;
    std::cout<<std::endl;

    if(step_out_flag && (evec_generation_mode==WangLandau_1d))
    {
      // step_out_flag=false;
      snprintf(wl_step_out_name,127,"wl1d_%s",step_out_name);
      wl_stepf=wl_step_out_name;
    }

    if(step_out_flag)
    {
      step_out_file.open(step_out_name);
      step_out_file<<"#";
      for(int i=0; i<argc; i++) step_out_file<<" "<<argv[i];
      step_out_file<<std::endl<<size_lsms<<std::endl;
    }
  }

  if(generator_needs_moment) return_moments_flag=true;

  if(num_lsms==1)
  {
    SHMEM_activeset local_comm;
    local_comm.rank=shmem_my_pe();
    local_comm.size=shmem_n_pes();
    local_comm.start_pe=0;
    local_comm.logPE_stride=0;
    LSMS lsms_calc(local_comm,i_lsms_name,"1_");
      
    if(rank==0)
    {
      std::cout<<"executing LSMS(C++) for "<<lsms_calc.numSpins()<<" atoms\n";
      std::cout<<"  LSMS version = "<<lsms_calc.version()<<std::endl;
    }

    if(energyCalculationMode==OneStepEnergy)
      std::cout<<"one step Energy = "<<lsms_calc.oneStepEnergy()<<std::endl;
    else if(energyCalculationMode==MultiStepEnergy)
      std::cout<<"multi-step Energy = "<<lsms_calc.multiStepEnergy()<<std::endl;
    else if(energyCalculationMode==ScfEnergy)
      std::cout<<"self-consistent Energy = "<<lsms_calc.scfEnergy()<<std::endl;
    else
    {
      printf("ERROR: Unknown energy calculation mode for lsms_calc in wl-lsms main!\n");
     // MPI_Abort(MPI_COMM_WORLD,5);
      exit(5);
    }
  }
  else
  {
    // build the communicators
    //int color=MPI_UNDEFINED;
    //Assuming user passes a power of two while using "-align"
    int s = align;
    int comm_size=(size-align)/num_lsms;
    int world_rank;
    for(int i=0; i<num_lsms; i++)
    {
      if((world_rank>=s) && (world_rank<s+comm_size)) 
      { 
        my_group=i; 
        //color=i; 
        new_peid=world_rank-s;
        new_root=s;
      }
      lsms_rank0[i]=s;
      s+=comm_size;
    }
    if(world_rank==0){ 
      //color=num_lsms;
      new_peid=0;
      comm_size=1;
      new_root=0;
    }

    //MPI_Comm_split(MPI_COMM_WORLD, color, 0, &local_comm);
    SHMEM_activeset local_comm;
    local_comm.rank=new_peid;
    local_comm.size=comm_size;
    local_comm.start_pe=new_root;
    local_comm.logPE_stride=0;

    std::cout<<"world_rank="<<world_rank<<" -> group="<<my_group<<std::endl;

      
    snprintf(prefix,38,"Group %4d: ",my_group);

    // now we get ready to do some calculations...

    if(my_group>=0)
    {
      double energy;
      double band_energy;
      int static i_values[10];
      double static r_values[10];
      static int op;


      //MPI_Comm_rank(local_comm, &rank);
      rank = local_comm.rank;
      snprintf(prefix,38,"%d_",my_group);
      // to use the ramdisk on jaguarpf:
      // snprintf(prefix,38,"/tmp/ompi/%d_",my_group);
      LSMS lsms_calc(local_comm,i_lsms_name,prefix);
      snprintf(prefix,38,"Group %4d: ",my_group);

      if(rank==0 && my_group==0)
      {
        std::cout<<prefix<<"executing LSMS(C++) for "<<lsms_calc.numSpins()<<" atoms\n";
        std::cout<<prefix<<"  LSMS version = "<<lsms_calc.version()<<std::endl;
      }

      // wait for commands from master
      bool finished=false;
      while(!finished)
      {
        if(rank==0)
        {
          //MPI_Recv(evec,3*size_lsms,MPI_DOUBLE,0,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
          //op =status.MPI_TAG;
          if (lsms_rank0[0]==world_rank)
                shmem_barrier(0, lsms_rank0[0], 2, pSync1);

        }
        //MPI_Bcast(&op,1,MPI_INT,0,local_comm);
        shmem_broadcast32(&op, &op, 1, local_comm.start_pe, local_comm.start_pe, local_comm.logPE_stride, local_comm.size, pSync2); 

/* recognized opcodes:
   5: calculate energy

   recognized energy calculation modes:
   OneStepEnergy : calclulate frozen potential band energy in one step (don't converge Ef)
   use only if the Fermi energy will not change due to MC steps!
   The only method available in LSMS_1.9
   MultiStepEnergy : calculate frozen potential band energy after converging Fermi energy
   This should be the new default method. If the Fermi energy doesn't change
   multiStepEnergy only performs one step and should be equivalent to oneStepEnergy
   The tolerance for Ef convergence can be set with LSMS::setEfTol(Real).
   The default tolerance is set in the LSMS::LSMS constructor (currently 1.0e-6).
   The maximum number of steps is read from the LSMS input file 'nscf' parameter.
   ScfEnergy : this will calculate the selfconsistent total energy.
   The maximum number of steps is read from the LSMS input file 'nscf' parameter.
   NOT IMPLEMENTED YET!!!

   10: get number of sites
*/

        if(op==5)
        {
          lsms_calc.setEvec(evec);
          if(energyCalculationMode==OneStepEnergy)
            energy=lsms_calc.oneStepEnergy(&band_energy);
          else if(energyCalculationMode==MultiStepEnergy)
            band_energy=energy=lsms_calc.multiStepEnergy();
          else if(energyCalculationMode==ScfEnergy)
            energy=lsms_calc.scfEnergy(&band_energy);
          else
          {
            printf("ERROR: Unknown energy calculation mode for lsms_calc in wl-lsms main!\n");
            //MPI_Abort(MPI_COMM_WORLD,5);
            exit(5);
          }
          r_values[0]=energy;
          r_values[1]=band_energy;
          if(return_moments_flag)
          {
            lsms_calc.getMag(&r_values[R_VALUE_OFFSET]);
          }
          if(rank==0)
          {
            if(return_moments_flag)
            {
              //MPI_Send(r_values,R_VALUE_OFFSET+3*size_lsms,MPI_DOUBLE,0,1005,MPI_COMM_WORLD);
              shmem_double_put(r_values, r_values, R_VALUE_OFFSET+3*size_lsms, 0);

            } else {
              //MPI_Send(r_values,R_VALUE_OFFSET,MPI_DOUBLE,0,1005,MPI_COMM_WORLD);
              shmem_double_put(r_values, r_values, R_VALUE_OFFSET, 0);
            }
            shmem_fence();
            shmem_int_swap(&flag, world_rank, 0);

          }
              
        } else if(op==10) {
          i_values[0]=lsms_calc.numSpins();
          //MPI_Send(i_values,10,MPI_INT,0,1010,MPI_COMM_WORLD);
          shmem_int_put(i_values, i_values, 10, 0);
        } else {
          // printf("world rank %d: recieved exit\n",world_rank); 
          finished=true;
        }
      }

      shfree(evec);
      //shfree(r_values);
    }
    else if(world_rank==0)
    {
      int running;
      double **evecs;
      //double *r_values;
      //int i_values[10];
      int *init_steps;
      int total_init_steps;
      bool accepted;
        
      char *wl_inf=NULL;
      char *wl_outf=NULL;
      if(gWL_in_name) wl_inf=gWL_in_name;
      if(gWL_out_name) wl_outf=gWL_out_name;
        
      EvecGenerator *generator;

/*
      // get number of spins from first LSMS instance
      // temp r_values:
      r_values=(double *)malloc(sizeof(double)*10);
      MPI_Send(r_values,1,MPI_DOUBLE, lsms_rank0[0], 10, MPI_COMM_WORLD);
      free(r_values);
      MPI_Recv(i_values,10,MPI_INT,lsms_rank0[0],1010,MPI_COMM_WORLD,&status);
      if(i_values[0]!=size_lsms)
      {
        printf("Size specified for Wang-Landau and in LSMS input file don't match!\n");
        size_lsms=i_values[0];
      }
*/

      evecs=(double **)shmalloc(sizeof(double *)*num_lsms);
      init_steps=(int *)shmalloc(sizeof(int)*num_lsms);
      for(int i=0; i<num_lsms; i++)
      {
        evecs[i]=(double *)shmalloc(sizeof(double)*3*size_lsms);
        init_steps[i]=initial_steps;
      }
      total_init_steps=num_lsms*initial_steps;
        

      // Initialize the correct evec generator
      switch(evec_generation_mode)
      {
      case Random :  generator = new RandomEvecGenerator(size_lsms);
        break;
      case Constant: generator = new ConstantEvecGenerator(size_lsms, ev0, num_lsms);
        break;
     //case WangLandau_1d : generator = new WL1dEvecGenerator<std::mt19937>(size_lsms, num_lsms,
     //                                                                      evecs, wl_inf, wl_outf, wl_stepf);
     case WangLandau_1d : generator = new WL1dEvecGenerator<boost::mt19937>(size_lsms, num_lsms,
                                                                           evecs, wl_inf, wl_outf, wl_stepf);
        break;
      case ExhaustiveIsing : generator = new ExhaustiveIsing1dEvecGenerator(size_lsms, num_lsms,
                                                                            evecs, wl_inf, wl_outf);
        break;
      //case WangLandau_2d : generator = new WL2dEvecGenerator<std::mt19937>(size_lsms, num_lsms,
      //                                                                     evecs, wl_inf, wl_outf, wl_stepf);
      case WangLandau_2d : generator = new WL2dEvecGenerator<boost::mt19937>(size_lsms, num_lsms,
                                                                           evecs, wl_inf, wl_outf, wl_stepf);
        break;
      default: std::cerr<<"The code should never arrive here: UNKNOWN EVEC GENERATION MODE\n";
        exit(1);
      }

      for(int i=0; i<num_lsms; i++)
      {
        generator->initializeEvec(i,evecs[i]);
      }
      std::cout<<"This is the master node\n";
      // issue initial commands to all LSMS instances
      running=0;
      bool more_work=true;
      if(total_init_steps>0)
      {
        for(int i=0; i<num_lsms; i++)
        {
          std::cout<<"starting initial calculation in group "<<i<<std::endl;
          //MPI_Send(evecs[i], 3*size_lsms, MPI_DOUBLE, lsms_rank0[i], 5, MPI_COMM_WORLD);
          shmem_double_put(evec, evecs[i], 3*size_lsms, lsms_rank0[i]);
          shmem_int_p(&op, 5, lsms_rank0[i]);
          shmem_fence();


          num_steps--; running++; stepCount++;
          if(restrict_steps) std::cout<<"      "<<num_steps<<" steps remaining\n";
        }
        shmem_barrier(0, lsms_rank0[0], 2, pSync1);
        // first deal with the initial steps:
        while(running>0)
        {
          //if(return_moments_flag)
          //  MPI_Recv(r_values,R_VALUE_OFFSET+3*size_lsms,MPI_DOUBLE,MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
          //else
          //  MPI_Recv(r_values,R_VALUE_OFFSET,MPI_DOUBLE,MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
          
          shmem_int_wait(&flag,-1);

          running--;
          // std::cout<<"received energy E_tot ="<<r_values[0]<<std::endl;
          // std::cout<<"    band energy E_band="<<r_values[1]<<std::endl;
          if(total_init_steps>0)
          {
            //int r_group=(status.MPI_SOURCE-align)/comm_size;
            int r_group=(flag-align)/comm_size;
            std::cout<<"starting additional calculation in group "<<r_group<<std::endl;

            if(init_steps[r_group]>0)
            {
              more_work = !(generator->generateUnsampledEvec(r_group,evecs[r_group],r_values[energyIndex]));
              init_steps[r_group]--; total_init_steps--;
            }
                
            //MPI_Send(evecs[r_group], 3*size_lsms, MPI_DOUBLE, lsms_rank0[r_group], 5, MPI_COMM_WORLD);
            shmem_double_put(r_values, evecs[r_group],  3*size_lsms, lsms_rank0[r_group]); //TODO check this
            shmem_fence();
                
            num_steps--; running++; stepCount++;
            if(restrict_steps && num_steps<=0) more_work=false;
            if(restrict_steps) std::cout<<"      "<<num_steps<<" steps remaining\n";
            walltime = get_rtc() - walltime_0;
            if(restrict_time && walltime>=max_time) more_work=false;
            if(restrict_time) std::cout<<"      "<<max_time-walltime<<" seconds remaining\n";
          }
              
        }
      }
      more_work=true;
      running=0;
      for(int i=0; i<num_lsms; i++)
      {
        std::cout<<"starting main calculation in group "<<i<<std::endl;
        //MPI_Send(evecs[i], 3*size_lsms, MPI_DOUBLE, lsms_rank0[i], 5, MPI_COMM_WORLD);
        shmem_double_put(evec, evecs[i], 3*size_lsms, lsms_rank0[i]);
        shmem_int_p(&op, 5, lsms_rank0[i]);
        shmem_fence();
        num_steps--; running++; stepCount++;
        if(restrict_steps) std::cout<<"      "<<num_steps<<" steps remaining\n";
      }
      shmem_barrier(0, lsms_rank0[0], 2, pSync1);
        
      generator->startSampling();
      // wait for results and issue new commands or wind down
      while(running>0)
      {
        //MPI_Recv(r_values,R_VALUE_OFFSET+3*size_lsms,MPI_DOUBLE,MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
        shmem_int_wait(&flag,-1);

        running--;
        std::cout<<"received energy E_tot ="<<r_values[0]<<std::endl;
        std::cout<<"    band energy E_band="<<r_values[1]<<std::endl;
        // printf("from status.MPI_SOURCE=%d\n",status.MPI_SOURCE);
        energy_accumulator+=r_values[0]; energies_accumulated++;
        if(more_work)
        {
          int r_group=(status.MPI_SOURCE-align)/comm_size;
          std::cout<<"starting additional calculation in group "<<r_group<<std::endl;
              
          if(generator_needs_moment)
          {
            double m0,m1,m2;
            m0=0.0; m1=0.0; m2=0.0;
            for(int i=0; i<3*size_lsms; i+=3)
            {
              m0+=r_values[R_VALUE_OFFSET+i];
              m1+=r_values[R_VALUE_OFFSET+i+1];
              m2+=r_values[R_VALUE_OFFSET+i+2];
            }
            switch(second_dimension)
            {
            case  MagneticMoment : magnetization=std::sqrt(m0*m0+m1*m1+m2*m2); break;
            case  MagneticMomentX : magnetization=m0; break;
            case  MagneticMomentY : magnetization=m1; break;
            case  MagneticMomentZ : magnetization=m2; break;
            }
            if(generator->generateEvec(r_group,evecs[r_group],r_values[energyIndex],magnetization, &accepted))
              more_work=false;
          } else {
            if(generator->generateEvec(r_group,evecs[r_group],r_values[energyIndex], &accepted)) more_work=false;
          }

          //MPI_Send(evecs[r_group], 3*size_lsms, MPI_DOUBLE, lsms_rank0[r_group], 5, MPI_COMM_WORLD);
          shmem_double_put(r_values, evecs[r_group],  3*size_lsms, lsms_rank0[r_group]); //TODO check this
          shmem_fence();

          num_steps--; running++; stepCount++;
          if(restrict_steps && num_steps<=0) more_work=false;
          if(restrict_steps) std::cout<<"      "<<num_steps<<" steps remaining\n";
          walltime = get_rtc() - walltime_0;
          if(restrict_time && walltime>=max_time) more_work=false;
          if(restrict_time) std::cout<<"      "<<max_time-walltime<<" seconds remaining\n";
        }
        else
        {
          // send an exit message to this instance of LSMS
          int r_group=(status.MPI_SOURCE-align)/comm_size;

          MPI_Send(evecs[r_group], 3*size_lsms, MPI_DOUBLE, lsms_rank0[r_group], 2, MPI_COMM_WORLD);
        }

        if(step_out_flag && accepted)
        {
          step_out_file<<"# iteration "<<energies_accumulated<<std::endl;
          step_out_file.precision(15);
          step_out_file<<energies_accumulated<<std::endl;
          step_out_file<<r_values[0]<<"  "<<r_values[1]<<std::endl;
          for(int j=0; j<3*size_lsms; j+=3)
          {
            step_out_file<<r_values[j+R_VALUE_OFFSET]<<"  "<<r_values[j+R_VALUE_OFFSET+1]
                         <<"  "<<r_values[j+R_VALUE_OFFSET+2]<<std::endl;
          }
        }
        // write restart file every restartWriteFrequency seconds
        if(walltime>nextWriteTime)
        {
          generator->writeState("WLrestart.jsn");
          nextWriteTime+=restartWriteFrequency;
        }

      }
      generator->writeState("WLrestart.jsn");
/*
  if(evec_generation_mode==WangLandau_1d)
  (static_cast<WL1dEvecGenerator<std::mt19937> *>(generator))->writeState("WLrestart.state");
  if(evec_generation_mode==ExhaustiveIsing)
  (static_cast<ExhaustiveIsing1dEvecGenerator *>(generator))->writeState("WLrestart.state");
*/
      for(int i=0; i<num_lsms; i++) free(evecs[i]);
      shfree(evecs);
      //shfree(r_values);
    }
  }

  if(world_rank==0)
  {
    if(step_out_flag)
    {
      step_out_file<<"# end\n-1\n"
                   <<energy_accumulator/double(energies_accumulated)<<std::endl;
      step_out_file.close();
    }
    std::cout<<"Finished all scheduled calculations. Freeing resources.\n";
    std::cout<<"Energy mean = "<<energy_accumulator/double(energies_accumulated)<<"Ry\n";
  }


  if(num_lsms>1)
  {
    // make sure averyone arrives here:
    MPI_Bcast(stupid,37,MPI_CHAR,0,MPI_COMM_WORLD);

    if(world_rank==0)
    {
      MPI_Comm_free(&local_comm);
    }
    else if(my_group>=0)
    {
      MPI_Comm_free(&local_comm);
    }
  }



  if(world_rank==0)
  {
    double walltime = get_rtc() - walltime_0;
    std::cout<<" WL-LSMS finished in "<<walltime<<" seconds.\n";
    std::cout<<" Monte-Carlo steps / walltime = "
             <<double(stepCount)/walltime<<"/sec\n";
  }

#ifdef USE_PAPI
  PAPI_stop_counters(papi_values,hw_counters);
  papi_values[hw_counters  ] = PAPI_get_real_cyc()-papi_real_cyc_0;
  papi_values[hw_counters+1] = PAPI_get_real_usec()-papi_real_usec_0;
  papi_values[hw_counters+2] = PAPI_get_virt_cyc()-papi_virt_cyc_0;
  papi_values[hw_counters+3] = PAPI_get_virt_usec()-papi_virt_usec_0;
  long long accumulated_counters[NUM_PAPI_EVENTS+4];
/*
  for(int i=0; i<hw_counters; i++)
  {
  printline(ttos(papi_event_name[i])+" = "+ttos(papi_values[i]),
  std::cout,parameters.myrankWorld);
  }
  printline("PAPI real cycles : "+ttos(papi_values[hw_counters]),
  std::cout,parameters.myrankWorld);
  printline("PAPI real usecs : "+ttos(papi_values[hw_counters+1]),
  std::cout,parameters.myrankWorld);
  printline("PAPI user cycles : "+ttos(papi_values[hw_counters+2]),
  std::cout,parameters.myrankWorld);
  printline("PAPI user usecs : "+ttos(papi_values[hw_counters+3]),
  std::cout,parameters.myrankWorld);
*/
  
  //MPI_Reduce(papi_values,accumulated_counters,hw_counters+4,
  //           MPI_LONG,MPI_SUM,0,MPI_COMM_WORLD);

  shmem_long_sum_to_all(accumulated_counters, papi_values, hw_counters+4,
      comm.pestart, comm.logPE_stride, comm.size, pWrk_i, pSync2);



  if(world_rank==0)
  {
    for(int i=0; i<hw_counters; i++)
    {
      std::cout<<"Accumulated: "<<(papi_event_name[i])<<" = "<<(accumulated_counters[i])<<"\n";
    }
    std::cout<<"PAPI accumulated real cycles : "<<(accumulated_counters[hw_counters])<<"\n";
    std::cout<<"PAPI accumulated user cycles : "<<(accumulated_counters[hw_counters+2])<<"\n";
    double gflops_papi = ((double)accumulated_counters[1])/
      (1000.0*(double)papi_values[hw_counters+1]);
    double gflops_hw_double = ((double)accumulated_counters[2])/
      (1000.0*(double)papi_values[hw_counters+1]);
    double gflops_hw_single = ((double)accumulated_counters[3])/
      (1000.0*(double)papi_values[hw_counters+1]);
    double gips = ((double)accumulated_counters[0])/(1000.0*(double)papi_values[hw_counters+1]);
    std::cout<<"PAPI_FP_OPS real GFLOP/s : "<<(gflops_papi)<<"\n";
    std::cout<<"PAPI hw double real GFLOP/s : "<<(gflops_hw_double)<<"\n";
    std::cout<<"PAPI hw single real GFLOP/s : "<<(gflops_hw_single)<<"\n";
    std::cout<<"PAPI real GINST/s : "<<(gips)<<"\n";
  }
#endif


  //MPI_Finalize();
  return 0;
}
コード例 #25
0
ファイル: calibrate.c プロジェクト: naps62/CPD_PAPI
int
main( int argc, char *argv[] )
{
	extern void dummy( void * );

	float aa, *a, *b, *c, *x, *y;
	double aad, *ad, *bd, *cd, *xd, *yd;
	int i, j, n;
	int inner = 0;
	int vector = 0;
	int matrix = 0;
	int double_precision = 0;
	int retval = PAPI_OK;
	char papi_event_str[PAPI_MIN_STR_LEN] = "PAPI_FP_OPS";
	int papi_event;
	int EventSet = PAPI_NULL;

/* Parse the input arguments */
	for ( i = 0; i < argc; i++ ) {
		if ( strstr( argv[i], "-i" ) )
			inner = 1;
		else if ( strstr( argv[i], "-v" ) )
			vector = 1;
		else if ( strstr( argv[i], "-m" ) )
			matrix = 1;
		else if ( strstr( argv[i], "-e" ) ) {
			if ( ( argv[i + 1] == NULL ) || ( strlen( argv[i + 1] ) == 0 ) ) {
				print_help( argv );
				exit( 1 );
			}
			strncpy( papi_event_str, argv[i + 1], sizeof ( papi_event_str ) );
			i++;
		} else if ( strstr( argv[i], "-d" ) )
			double_precision = 1;
		else if ( strstr( argv[i], "-h" ) ) {
			print_help( argv );
			exit( 1 );
		}
	}

	/* if no options specified, set all tests to TRUE */
	if ( inner + vector + matrix == 0 )
		inner = vector = matrix = 1;


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

	if ( !TESTS_QUIET )
		printf( "Initializing..." );

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

	/* Translate name */
	retval = PAPI_event_name_to_code( papi_event_str, &papi_event );
	if ( retval != PAPI_OK )
		test_fail( __FILE__, __LINE__, "PAPI_event_name_to_code", retval );

	if ( PAPI_query_event( papi_event ) != PAPI_OK )
		test_skip( __FILE__, __LINE__, "PAPI_query_event", PAPI_ENOEVNT );

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

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

	printf( "\n" );

	retval = PAPI_OK;

	/* Inner Product test */
	if ( inner ) {
		/* Allocate the linear arrays */
	   if (double_precision) {
	        xd = malloc( INDEX5 * sizeof(double) );
	        yd = malloc( INDEX5 * sizeof(double) );
		if ( !( xd && yd ) )
			retval = PAPI_ENOMEM;
	   }
	   else {
	        x = malloc( INDEX5 * sizeof(float) );
		y = malloc( INDEX5 * sizeof(float) );
		if ( !( x && y ) )
			retval = PAPI_ENOMEM;
	   }

		if ( retval == PAPI_OK ) {
			headerlines( "Inner Product Test", TESTS_QUIET );

			/* step through the different array sizes */
			for ( n = 0; n < INDEX5; n++ ) {
				if ( n < INDEX1 || ( ( n + 1 ) % 50 ) == 0 ) {

					/* Initialize the needed arrays at this size */
					if ( double_precision ) {
						for ( i = 0; i <= n; i++ ) {
							xd[i] = ( double ) rand(  ) * ( double ) 1.1;
							yd[i] = ( double ) rand(  ) * ( double ) 1.1;
						}
					} else {
						for ( i = 0; i <= n; i++ ) {
							x[i] = ( float ) rand(  ) * ( float ) 1.1;
							y[i] = ( float ) rand(  ) * ( float ) 1.1;
						}
					}

					/* reset PAPI flops count */
					reset_flops( "Inner Product Test", EventSet );

					/* do the multiplication */
					if ( double_precision ) {
						aad = inner_double( n, xd, yd );
						dummy( ( void * ) &aad );
					} else {
						aa = inner_single( n, x, y );
						dummy( ( void * ) &aa );
					}
					resultline( n, 1, EventSet );
				}
			}
		}
		if (double_precision) {
			free( xd );
			free( yd );
		} else {
			free( x );
			free( y );
		}
	}

	/* Matrix Vector test */
	if ( vector && retval != PAPI_ENOMEM ) {
		/* Allocate the needed arrays */
	  if (double_precision) {
	        ad = malloc( INDEX5 * INDEX5 * sizeof(double) );
	        xd = malloc( INDEX5 * sizeof(double) );
	        yd = malloc( INDEX5 * sizeof(double) );
		if ( !( ad && xd && yd ) )
			retval = PAPI_ENOMEM;
	  } else {
	        a = malloc( INDEX5 * INDEX5 * sizeof(float) );
	        x = malloc( INDEX5 * sizeof(float) );
	        y = malloc( INDEX5 * sizeof(float) );
		if ( !( a && x && y ) )
			retval = PAPI_ENOMEM;
	  }

		if ( retval == PAPI_OK ) {
			headerlines( "Matrix Vector Test", TESTS_QUIET );

			/* step through the different array sizes */
			for ( n = 0; n < INDEX5; n++ ) {
				if ( n < INDEX1 || ( ( n + 1 ) % 50 ) == 0 ) {

					/* Initialize the needed arrays at this size */
					if ( double_precision ) {
						for ( i = 0; i <= n; i++ ) {
							yd[i] = 0.0;
							xd[i] = ( double ) rand(  ) * ( double ) 1.1;
							for ( j = 0; j <= n; j++ )
								ad[i * n + j] =
									( double ) rand(  ) * ( double ) 1.1;
						}
					} else {
						for ( i = 0; i <= n; i++ ) {
							y[i] = 0.0;
							x[i] = ( float ) rand(  ) * ( float ) 1.1;
							for ( j = 0; j <= n; j++ )
								a[i * n + j] =
									( float ) rand(  ) * ( float ) 1.1;
						}
					}

					/* reset PAPI flops count */
					reset_flops( "Matrix Vector Test", EventSet );

					/* compute the resultant vector */
					if ( double_precision ) {
						vector_double( n, ad, xd, yd );
						dummy( ( void * ) yd );
					} else {
						vector_single( n, a, x, y );
						dummy( ( void * ) y );
					}
					resultline( n, 2, EventSet );
				}
			}
		}
		if (double_precision) {
			free( ad );
			free( xd );
			free( yd );
		} else {
			free( a );
			free( x );
			free( y );
		}
	}

	/* Matrix Multiply test */
	if ( matrix && retval != PAPI_ENOMEM ) {
		/* Allocate the needed arrays */
	  if (double_precision) {
	        ad = malloc( INDEX5 * INDEX5 * sizeof(double) );
	        bd = malloc( INDEX5 * INDEX5 * sizeof(double) );
	        cd = malloc( INDEX5 * INDEX5 * sizeof(double) );
		if ( !( ad && bd && cd ) )
			retval = PAPI_ENOMEM;
	  } else {
	        a = malloc( INDEX5 * INDEX5 * sizeof(float) );
	        b = malloc( INDEX5 * INDEX5 * sizeof(float) );
	        c = malloc( INDEX5 * INDEX5 * sizeof(float) );
		if ( !( a && b && c ) )
			retval = PAPI_ENOMEM;
	  }


		if ( retval == PAPI_OK ) {
			headerlines( "Matrix Multiply Test", TESTS_QUIET );

			/* step through the different array sizes */
			for ( n = 0; n < INDEX5; n++ ) {
				if ( n < INDEX1 || ( ( n + 1 ) % 50 ) == 0 ) {

					/* Initialize the needed arrays at this size */
					if ( double_precision ) {
						for ( i = 0; i <= n * n + n; i++ ) {
							cd[i] = 0.0;
							ad[i] = ( double ) rand(  ) * ( double ) 1.1;
							bd[i] = ( double ) rand(  ) * ( double ) 1.1;
						}
					} else {
						for ( i = 0; i <= n * n + n; i++ ) {
							c[i] = 0.0;
							a[i] = ( float ) rand(  ) * ( float ) 1.1;
							b[i] = ( float ) rand(  ) * ( float ) 1.1;
						}
					}

					/* reset PAPI flops count */
					reset_flops( "Matrix Multiply Test", EventSet );

					/* compute the resultant matrix */
					if ( double_precision ) {
						matrix_double( n, cd, ad, bd );
						dummy( ( void * ) c );
					} else {
						matrix_single( n, c, a, b );
						dummy( ( void * ) c );
					}
					resultline( n, 3, EventSet );
				}
			}
		}
		if (double_precision) {
			free( ad );
			free( bd );
			free( cd );
		} else {
			free( a );
			free( b );
			free( c );
		}
	}

	/* exit with status code */
	if ( retval == PAPI_ENOMEM )
		test_fail( __FILE__, __LINE__, "malloc", retval );
	else
		test_pass( __FILE__, NULL, 0 );
	exit( 1 );
}
コード例 #26
0
ファイル: Perft_init.cpp プロジェクト: nedbrek/tclPapi
 /*
 *______________________________________________
 *
 *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;        
}    
コード例 #27
0
ファイル: natif.c プロジェクト: gpichon/eigenproblems
int main(int argc, char **argv){
  if (argc != 3){
    printf("\tFirst argument: matrix size\n");
    printf("\tEigenvalues only: 1 for QR, 2 for BI, 3 for DC and 4 for MRRR\n");
    printf("\tEigenvectors too: 5 for QR, 6 for BI, 7 for DC and 6 for MRRR\n");
    return EXIT_SUCCESS;
  }
  int N = atoi(argv[1]);
  int method = atoi(argv[2]);
  if (method < 1 || method > 8){
    printf("\tFirst argument: matrix size\n");
    printf("\tEigenvalues only: 1 for QR, 2 for BI, 3 for DC and 4 for MRRR\n");
    printf("\tEigenvectors too: 5 for QR, 6 for BI, 7 for DC and 6 for MRRR\n");
    return EXIT_SUCCESS;
  } 

  /* allocate space for matrices, eigenvalues and eigenvectors */
  /* double *A = malloc(N * N * sizeof(double)); assert(A); */
  /* double *E = malloc(N * sizeof(double));   assert(E); */
  /* double *Q = malloc(N * N * sizeof(double)); assert(Q); */

  /* srand(0); */
  /* int j, k; */
  /* for (j = 0; j < N; ++j){ */
  /*   for (k = 0; k <= j; ++k){ */
  /*     A[N*j+k] = A[N*k+j] = 0; //(rand()%(100000*N)); */
  /*   } */
  /* } */

  /* for (j = 0; j < N/2; j++){ */
  /*   A[N*j+j] = N/2-j; */
  /* } */
  /* for (j = N/2; j < N; j++){ */
  /*   A[N*j+j] = j-N/2; */
  /* } */

  /* for (j = 0; j < N-1; j++){ */
  /*   A[N*j+j+1] = 1; */
  /* } */
  /* for (j = 1; j < N; j++){ */
  /*   A[N*j+j-1] = 1; */
  /* } */

  /* for (j = 0; j < N; j++){ */
  /*   for (k = 0; k < N; k++){ */
  /*     printf("%f  ", A[N*j+k]); */
  /*   } */
  /*   printf("\n"); */
  /* } */

  double *D = malloc(N * sizeof(double));   assert(D);
  double *E = malloc(N * sizeof(double));   assert(E);
  int j;
  /* srand(0); */
  for (j = 0; j < N; j++){
    D[j] = rand()%(1000000*N);
    E[j] = rand()%(1000000*N);
  }

  /* for (j = 0; j < N/2; j++){ */
  /*   D[j] = N/2-j; */
  /* } */
  /* for (j = N/2; j < N; j++){ */
  /*   D[j] = j-N/2; */
  /* } */
  /* for (j = 0; j < N; j++){ */
  /*     E[j] = 1; */
  /* } */

  int i, retval, native;
  const PAPI_hw_info_t *hwinfo;
  long long values[8];
  struct timeval t1, t2;

  if ((retval = PAPI_library_init(PAPI_VER_CURRENT)) != PAPI_VER_CURRENT)
    printf("Erreur: PAPI_library_init\n");

  if ((retval = PAPI_create_eventset(&EventSet)) != PAPI_OK)
    printf("Erreur: PAPI_create_eventset\n");

  if ((hwinfo = PAPI_get_hardware_info( )) == NULL)
    printf("Erreur: PAPI_get_hardware_info\n");

  /* printf("Architecture %s, %d\n", hwinfo->model_string, hwinfo->model); */

  for (i=0; native_name[i] != NULL; i++) {
    retval = PAPI_event_name_to_code(native_name[i], &native);
    if (retval != PAPI_OK)
      printf("Erreur: PAPI_event_name_to_code\n");
    if ((retval = PAPI_add_event(EventSet, native)) != PAPI_OK)
      printf("Erreur: PAPI_add_event\n");
  }

  if ((retval = PAPI_start(EventSet)) != PAPI_OK)
    printf("Erreur: PAPI_start\n");

  gettimeofday(&t1, NULL);

  /* functions to be measured */
  switch (method){
  case 1:
    printf("\nSolve using QR: eigenvalues only\n");
    solve_tridiag_qr(N, D, E, 'N');
    /* solve_entire_qr(N, A, E, Q, 'N'); */
    break;
  case 2:
    printf("\nSolve using BI: eigenvalues only\n");
    printf("Not in tridiagonal version\n");
    return EXIT_FAILURE;
    /* solve_tridiag_bi(N, D, E, 'N'); */
    /* solve_entire_bi(N, A, E, Q, 'N'); */
    break;
  case 3:
    printf("\nSolve using DC: eigenvalues only\n");
    solve_tridiag_dc(N, D, E, 'N');
    /* solve_entire_dc(N, A, E, Q, 'N'); */
    break;
  case 4:
    printf("\nSolve using MRRR: eigenvalues only\n");
    solve_tridiag_mr(N, D, E, 'N');
    /* solve_entire_mr(N, A, E, Q, 'N'); */
    break;
  case 5:
    printf("\nSolve using QR: eigenvalues and eigenvectors\n");
    solve_tridiag_qr(N, D, E, 'I');
    /* solve_entire_qr(N, A, E, Q, 'V'); */
    break;
  case 6:
    printf("\nSolve using BI: eigenvalues and eigenvectors\n");
    printf("Not in tridiagonal version\n");
    return EXIT_FAILURE;
    /* solve_tridiag_bi(N, D, E, 'I'); */
    /* solve_entire_bi(N, A, E, Q, 'V'); */
    break;
  case 7:
    printf("\nSolve using DC: eigenvalues and eigenvectors\n");
    solve_tridiag_dc(N, D, E, 'V');
    /* solve_entire_dc(N, A, E, Q, 'V'); */
    break;
  default:
    printf("\nSolve using MRRR: eigenvalues and eigenvectors\n");
    solve_tridiag_mr(N, D, E, 'V');
    /* solve_entire_mr(N, A, E, Q, 'V'); */
    break;
  }    

  gettimeofday(&t2, NULL);

  if ((retval = PAPI_stop(EventSet, values)) != PAPI_OK)
    printf("Erreur: PAPI_stop\n");


  printf("Time %ld (us)\n", 1000000 * (t2.tv_sec-t1.tv_sec) + t2.tv_usec-t1.tv_usec);

  for (i=0; native_name[i] != NULL; i++) {
    fprintf(stderr, "%-40s: ", native_name[i]);
    fprintf(stderr, "values %lld\n", values[i]);
  }

  retval = PAPI_cleanup_eventset(EventSet);
  if (retval != PAPI_OK)
    printf("Erreur: PAPI_cleanup\n");
  retval = PAPI_destroy_eventset(&EventSet);
  if (retval != PAPI_OK)
    printf("Erreur: PAPI_destroy_eventset\n");

  free(D);
  free(E);
  exit(0);
}
コード例 #28
0
ファイル: Init.c プロジェクト: xiaodao90/wrapper
int PAPI_get_info (char *event_name, int finish, int type)
{
	int retval,i;
	PAPI_Event Event_papi;
	Event_papi.event_name = event_name;
	Event_papi.finish = finish;
	Event_papi.time = gettime();

	long long values [PAPI_MAX_EVENT_NUM];

	if (type == PAPI_PROCESS)
	{
		if ( (retval=PAPI_start(EventSet)) != PAPI_OK)
		ERROR_RETURN(retval);

		if ((retval=PAPI_stop(EventSet,values)) != PAPI_OK)
			ERROR_RETURN(retval);

//		printf_d ("%s\n", Event_papi.event_name);
		for (i = 0; i < NUM_EVENT; i++)
		{
			Event_papi.papi_event = PAPI_EVENT[i];
			Event_papi.data = values[i];
			if ((PAPI_info_record (Event_papi)) == -1)
				printf_d ("[PAPI]GET stream error!!\n");
		}
		return 0;
	}
	else if (type == PAPI_THREAD)
	{
		int EventSet1 = PAPI_NULL;

		int *event_codes;
		event_codes = (int *)calloc (sizeof (int *), NUM_EVENT);
		for (i = 0; i < NUM_EVENT; ++i)
		{
			retval = PAPI_event_name_to_code (PAPI_EVENT[i], &event_codes[i]);
			if( retval != PAPI_OK )
				printf_d("[PAPI]PAPI_event_name_to_code failed, retval = %d\n", retval );
		}
		if ((retval = PAPI_register_thread ()) != PAPI_OK)
			ERROR_RETURN (retval);

		if ((retval=PAPI_create_eventset(&EventSet1)) != PAPI_OK)
			ERROR_RETURN(retval);
		if ((retval=PAPI_add_events(EventSet1, event_codes, NUM_EVENT)) != PAPI_OK)
			ERROR_RETURN(retval);

		if ((retval=PAPI_start(EventSet1)) != PAPI_OK)
			ERROR_RETURN(retval);

		if ((retval=PAPI_stop(EventSet1,values)) != PAPI_OK)
			ERROR_RETURN(retval);

		for (i = 0; i < NUM_EVENT; i++)
		{
			Event_papi.papi_event = PAPI_EVENT[i];
			Event_papi.data = values[i];
			PAPI_info_record (Event_papi);
		}
		if ((PAPI_unregister_thread()) != PAPI_OK)
			ERROR_RETURN(retval);
		return 0;
	}
	return 1;
}
コード例 #29
0
int main (int argc, char **argv)
{
    int i, retval;
    int EventSet = PAPI_NULL;
    char *event_name[NUM_EVENTS] = {
        IFNAME ":rx:bytes",
        IFNAME ":rx:packets",
        IFNAME ":tx:bytes",
        IFNAME ":tx:packets",
    };
    int event_code[NUM_EVENTS] = { 0, 0, 0, 0};
    long long event_value[NUM_EVENTS];
    int total_events=0;

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

    /* PAPI Initialization */
    retval = PAPI_library_init( PAPI_VER_CURRENT );
    if ( retval != PAPI_VER_CURRENT ) {
        test_fail(__FILE__, __LINE__,"PAPI_library_init failed\n",retval);
    }

    if (!TESTS_QUIET) {
        printf("Net events by name\n");
    }

    /* Map names to codes */
    for ( i=0; i<NUM_EVENTS; i++ ) {
        retval = PAPI_event_name_to_code( event_name[i], &event_code[i]);
        if ( retval != PAPI_OK ) {
            test_fail( __FILE__, __LINE__, "PAPI_event_name_to_code", retval );
        }

        total_events++;
    }

    /* Create and populate the EventSet */
    EventSet = PAPI_NULL;

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

    retval = PAPI_add_events( EventSet, event_code, NUM_EVENTS);
    if (retval != PAPI_OK) {
        test_fail(__FILE__, __LINE__, "PAPI_add_events()", retval);
    }

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

    /* generate some traffic
     * the operation should take more than one second in order
     * to guarantee that the network counters are updated */
    retval = system("ping -c 4 " PINGADDR " > /dev/null");
    if (retval < 0) {
		test_fail(__FILE__, __LINE__, "Unable to start ping", retval);
	}

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

    if (!TESTS_QUIET) {
        for ( i=0; i<NUM_EVENTS; i++ ) {
            printf("%#x %-24s = %lld\n",
                event_code[i], event_name[i], event_value[i]);
        }
    }

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

    test_pass( __FILE__, NULL, 0 );

    return 0;
}
コード例 #30
0
ファイル: native_avail.c プロジェクト: multics69/danbi
int
main( int argc, char **argv )
{
	int i, j = 0, k;
	int retval;
	PAPI_event_info_t info;
	const PAPI_hw_info_t *hwinfo = NULL;
	command_flags_t flags;
	int enum_modifier;
	int numcmp, cid;

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

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

	/* Parse the command-line arguments */
	parse_args( argc, argv, &flags );

	/* Set enum modifier mask */
	if ( flags.dear )
		enum_modifier = PAPI_NTV_ENUM_DEAR;
	else if ( flags.darr )
		enum_modifier = PAPI_NTV_ENUM_DARR;
	else if ( flags.iear )
		enum_modifier = PAPI_NTV_ENUM_IEAR;
	else if ( flags.iarr )
		enum_modifier = PAPI_NTV_ENUM_IARR;
	else if ( flags.opcm )
		enum_modifier = PAPI_NTV_ENUM_OPCM;
	else
		enum_modifier = PAPI_ENUM_EVENTS;


	if ( !TESTS_QUIET ) {
	   retval = PAPI_set_debug( PAPI_VERB_ECONT );
	   if ( retval != PAPI_OK ) {
	      test_fail( __FILE__, __LINE__, "PAPI_set_debug", retval );
	   }
	}

	retval = papi_print_header( "Available native events and hardware information.\n", &hwinfo );
	if ( retval != PAPI_OK ) {
		test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 2 );
	}


	/* Do this code if an event name was specified on the commandline */
	if ( flags.named ) {
	   if ( PAPI_event_name_to_code( flags.name, &i ) == PAPI_OK ) {
	      if ( PAPI_get_event_info( i, &info ) == PAPI_OK ) {
		 printf( "%-30s%s\n%-30s%d\n",
			 "Event name:", info.symbol,
			 "Number of Register Values:", info.count );
		 printf( "%-29s|%s|\n", "Description:", info.long_descr );
		 for ( k = 0; k < ( int ) info.count; k++ )
		     printf( " Register[%2d]:               |%s|\n", 
			     k, info.name[k] );

		     /* if unit masks exist but none specified, process all */
		     if ( !strchr( flags.name, ':' ) ) {
			if ( PAPI_enum_event( &i, PAPI_NTV_ENUM_UMASKS ) == PAPI_OK ) {
			   printf( "\nUnit Masks:\n" );
			   do {
			      retval = PAPI_get_event_info( i, &info );
			      if ( retval == PAPI_OK ) {
				 if ( parse_unit_masks( &info ) ) {
				    printf( "%-29s|%s|%s|\n", " Mask Info:",
					    info.symbol, info.long_descr );
				    for ( k = 0; k < ( int ) info.count; k++ )
					printf( "  Register[%2d]:  0x%08x  |%s|\n",
					       k, info.code[k], info.name[k] );
				 }
			      }
			   } while ( PAPI_enum_event( &i, PAPI_NTV_ENUM_UMASKS ) == PAPI_OK );
			}
		     }
	      }
	   } else {
	     printf("Sorry, an event by the name '%s' could not be found.\n",
		    flags.name);
	     printf("Is it typed correctly?\n\n");
	     exit( 1 );
	   }
	}
        else {

	   /* Print *ALL* available events */


	   numcmp = PAPI_num_components(  );

	   j = 0;

	   for ( cid = 0; cid < numcmp; cid++ ) {

	       const PAPI_component_info_t *component;
	       component=PAPI_get_component_info(cid);

	       /* Skip if requested enumeration not available */
	       if (( flags.dear ) && !( component->cntr_DEAR_events )) {
		 continue;
	       }
	       if (( flags.darr ) && !( component->data_address_range)) {
		 continue;
	       }
               if (( flags.iear ) && !( component->cntr_IEAR_events )) {
		 continue;
	       }
	       if (( flags.iarr ) && !( component->instr_address_range )) {
		 continue;
	       }
	       if (( flags.opcm ) && !( component->cntr_OPCM_events )) {
		 continue;
	       }


	       printf( "===============================================================================\n" );
	       printf( " Events in Component: %s\n",component->name);
	       printf( "===============================================================================\n" );
	     
	       /* Always ASK FOR the first event */
	       /* Don't just assume it'll be the first numeric value */
	       i = 0 | PAPI_NATIVE_MASK | PAPI_COMPONENT_MASK( cid );

	       /* UGH PAPI_enum_event() really needs a component field */
	       PAPI_enum_event( &i, PAPI_ENUM_FIRST );

	       do {
		  memset( &info, 0, sizeof ( info ) );
		  retval = PAPI_get_event_info( i, &info );

		  /* This event may not exist */
		  if ( retval != PAPI_OK )
		     continue;

		  /* count only events that as supported by host cpu */
		  j++;

		  print_event( &info, 0 );

		  /* Does this matter on anything but perfctr? */
		  if ( flags.details ) {
		     for ( k = 0; k < ( int ) info.count; k++ ) {
			 if ( strlen( info.name[k] ) ) {
			    printf( "  Register[%d] Name: %-20s  Value: 0x%-28x|\n",
				    k, info.name[k], info.code[k] );
			 }
		     }
		  }

/*		modifier = PAPI_NTV_ENUM_GROUPS returns event codes with a
			groups id for each group in which this
			native event lives, in bits 16 - 23 of event code
			terminating with PAPI_ENOEVNT at the end of the list.
*/

		  /* This is an IBM Power issue */
		  if ( flags.groups ) {
		     k = i;
		     if ( PAPI_enum_event( &k, PAPI_NTV_ENUM_GROUPS ) == PAPI_OK ) {
			printf( "Groups: " );
			do {
			  printf( "%4d", ( ( k & PAPI_NTV_GROUP_AND_MASK ) >>
					     PAPI_NTV_GROUP_SHIFT ) - 1 );
			} while ( PAPI_enum_event( &k, PAPI_NTV_ENUM_GROUPS ) ==PAPI_OK );
			printf( "\n" );
		     }
		  }

		  /* Print umasks */
		  /* components that don't have them can just ignore */

	          if ( flags.umask ) { 
		     k = i;
		     if ( PAPI_enum_event( &k, PAPI_NTV_ENUM_UMASKS ) == PAPI_OK ) {
		        do {
			   retval = PAPI_get_event_info( k, &info );
			   if ( retval == PAPI_OK ) {
			      if ( parse_unit_masks( &info ) )
			         print_event( &info, 2 );
			   }
		        } while ( PAPI_enum_event( &k, PAPI_NTV_ENUM_UMASKS ) == PAPI_OK );
		     }

		  }
		  printf( "--------------------------------------------------------------------------------\n" );

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