<commit_before>/* 
* File:    papi_hl.c
* CVS:     $Id$
* Author:  <NAME>
*          <EMAIL>
* Mods:    <your name here>
*          <your email address>
* Mods:	   <NAME>
* 	   <EMAIL>
*/  

/* This file contains the 'high level' interface to PAPI. 
   BASIC is a high level language. ;-) */

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <string.h>
#include <strings.h>
#include <errno.h>

#include "papi.h"
#include "papi_internal.h"
#include "papiStdEventDefs.h"

/* high level papi functions*/

static int PAPI_EVENTSET_INUSE = PAPI_NULL;
static int initialized = 0;
static int MAX_COUNTERS = 0;

int PAPI_flops(float *real_time, float *proc_time, long long *flpins, float *mflops)
{
   static float total_proc_time=0.0; 
   static int EventSet = PAPI_NULL;
   static float mhz;
   static long long start_us=0, total_flpins=-4;
   const PAPI_hw_info_t *hwinfo = NULL;
   long long values[2] = {0,0};
   char buf[500];
   int retval;

   if ( !initialized ) {
	mhz = 0.0;
	*mflops = 0.0;
 	*real_time = 0.0;
 	*proc_time = 0.0;
	*flpins = 0;
	retval = PAPI_library_init( PAPI_VER_CURRENT );
	if ( retval != PAPI_VER_CURRENT )
	   return(retval);
	if ( (hwinfo = PAPI_get_hardware_info()) == NULL ) {
	   printf("Error getting hw_info\n");
	   return -1;
        } 
	mhz = hwinfo->mhz;
	PAPI_create_eventset( &EventSet );
	retval = PAPI_add_event(&EventSet, PAPI_FP_INS);
	PAPI_perror( retval, buf, 500);
	if ( retval < PAPI_OK ) {
	     PAPI_shutdown();
	     return retval;
	}
	retval = PAPI_add_event(&EventSet, PAPI_TOT_CYC);
	PAPI_perror(retval, buf, 500);
	if ( retval < PAPI_OK ) {
	     PAPI_shutdown();
	     return retval;
	}
	retval = PAPI_start(EventSet);
	PAPI_perror(retval, buf, 500);
	if ( retval < PAPI_OK ) {
	     PAPI_shutdown();
	     return retval;
	}
	start_us = PAPI_get_real_usec();
	initialized = 1;
   }
   else {
	*real_time = (PAPI_get_real_usec()-start_us)/1000000.0;
	retval = PAPI_accum( EventSet, values );
	PAPI_perror( retval, buf, 500);
	if ( retval < PAPI_OK ) {
	     PAPI_shutdown();
	     initialized = 0;
	     return retval;
	}
	*proc_time = (float)(values[1]/(mhz*1000000.0));
	*mflops = (float)(values[0]/(*proc_time*1000000.0));
	total_proc_time += *proc_time;
	total_flpins += values[0];
	*proc_time = total_proc_time;
	*flpins = total_flpins;
	total_flpins-=19;
   }
   return PAPI_OK;
}

int PAPI_num_counters(void) 
{
  int retval;

  if (!initialized)
    {
      retval = PAPI_library_init(PAPI_VER_CURRENT);
      if (retval != PAPI_VER_CURRENT)
	return(retval);

      retval = PAPI_create_eventset(&PAPI_EVENTSET_INUSE);
      if (retval)
	return(retval);
      
      MAX_COUNTERS = PAPI_get_opt(PAPI_GET_MAX_HWCTRS,NULL);
      
      initialized = 1;
    }

  return(MAX_COUNTERS);
}

/*========================================================================*/
/* int PAPI_start_counters(int *events, int array_len)                    */
/* from draft standard:                                                   */ 
/* Start counting the events named in the events array. This function     */
/* implicitly stops and initializes any counters running as the result    */
/* of a previous call to PAPI_start_counters(). It is the user's          */
/* responsibility to choose events that can be counted simultaneously     */
/* by reading the vendor's documentation. The length of this array        */
/* should be no longer than PAPI_MAX_EVNTS.                               */ 
/*========================================================================*/

int PAPI_start_counters(int *events, int array_len) 
{
  int i,retval;

  if (!initialized)
    {
      PAPI_num_counters();<commit_msg>PAPI_num_counters now returns PAPI_EINVAL on library version mis-match as described in the PAPI_num_counters(3) man page.<commit_after>/* 
* File:    papi_hl.c
* CVS:     $Id$
* Author:  <NAME>
*          <EMAIL>
* Mods:    <your name here>
*          <your email address>
* Mods:	   <NAME>
* 	   <EMAIL>
*/  

/* This file contains the 'high level' interface to PAPI. 
   BASIC is a high level language. ;-) */

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <string.h>
#include <strings.h>
#include <errno.h>

#include "papi.h"
#include "papi_internal.h"
#include "papiStdEventDefs.h"

/* high level papi functions*/

static int PAPI_EVENTSET_INUSE = PAPI_NULL;
static int initialized = 0;
static int MAX_COUNTERS = 0;

int PAPI_flops(float *real_time, float *proc_time, long long *flpins, float *mflops)
{
   static float total_proc_time=0.0; 
   static int EventSet = PAPI_NULL;
   static float mhz;
   static long long start_us=0, total_flpins=-4;
   const PAPI_hw_info_t *hwinfo = NULL;
   long long values[2] = {0,0};
   char buf[500];
   int retval;

   if ( !initialized ) {
	mhz = 0.0;
	*mflops = 0.0;
 	*real_time = 0.0;
 	*proc_time = 0.0;
	*flpins = 0;
	retval = PAPI_library_init( PAPI_VER_CURRENT );
	if ( retval != PAPI_VER_CURRENT )
	   return(retval);
	if ( (hwinfo = PAPI_get_hardware_info()) == NULL ) {
	   printf("Error getting hw_info\n");
	   return -1;
        } 
	mhz = hwinfo->mhz;
	PAPI_create_eventset( &EventSet );
	retval = PAPI_add_event(&EventSet, PAPI_FP_INS);
	PAPI_perror( retval, buf, 500);
	if ( retval < PAPI_OK ) {
	     PAPI_shutdown();
	     return retval;
	}
	retval = PAPI_add_event(&EventSet, PAPI_TOT_CYC);
	PAPI_perror(retval, buf, 500);
	if ( retval < PAPI_OK ) {
	     PAPI_shutdown();
	     return retval;
	}
	retval = PAPI_start(EventSet);
	PAPI_perror(retval, buf, 500);
	if ( retval < PAPI_OK ) {
	     PAPI_shutdown();
	     return retval;
	}
	start_us = PAPI_get_real_usec();
	initialized = 1;
   }
   else {
	*real_time = (PAPI_get_real_usec()-start_us)/1000000.0;
	retval = PAPI_accum( EventSet, values );
	PAPI_perror( retval, buf, 500);
	if ( retval < PAPI_OK ) {
	     PAPI_shutdown();
	     initialized = 0;
	     return retval;
	}
	*proc_time = (float)(values[1]/(mhz*1000000.0));
	*mflops = (float)(values[0]/(*proc_time*1000000.0));
	total_proc_time += *proc_time;
	total_flpins += values[0];
	*proc_time = total_proc_time;
	*flpins = total_flpins;
	total_flpins-=19;
   }
   return PAPI_OK;
}

int PAPI_num_counters(void) 
{
  int retval;

  if (!initialized)
    {
      retval = PAPI_library_init(PAPI_VER_CURRENT);
      if (retval != PAPI_VER_CURRENT)
	return(PAPI_EINVAL);

      retval = PAPI_create_eventset(&PAPI_EVENTSET_INUSE);
      if (retval)
	return(retval);
      
      MAX_COUNTERS = PAPI_get_opt(PAPI_GET_MAX_HWCTRS,NULL);
      
      initialized = 1;
    }

  return(MAX_COUNTERS);
}

/*========================================================================*/
/* int PAPI_start_counters(int *events, int array_len)                    */
/* from draft standard:                                                   */ 
/* Start counting the events named in the events array. This function     */
/* implicitly stops and initializes any counters running as the result    */
/* of a previous call to PAPI_start_counters(). It is the user's          */
/* responsibility to choose events that can be counted simultaneously     */
/* by reading the vendor's documentation. The length of this array        */
/* should be no longer than PAPI_MAX_EVNTS.                               */ 
/*========================================================================*/

int PAPI_start_counters(int *events, int array_len) 
{
  int i,retval;

  if (!initialized)
    {
      PAPI_num_counters();
{'fraction_non_alphanumeric': 0.15816664458868723, 'fraction_numerical': 0.017618227579811897, 'mean_word_length': 3.1506322155030237, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3655736', 'n_tokens_mistral': 2854, 'n_tokens_neox': 2717, 'n_words': 633}