Blame src/genpapifdef.c

Packit Service a1973e
/* This file generates the #defines needed for Fortran examples of PAPI. 
Packit Service a1973e
   Its output is usually directed to fpapi.h. See Makefile.inc for details. 
Packit Service a1973e
*/
Packit Service a1973e
Packit Service a1973e
/* Modified to produce any of cpp, f77, or f90-style include files.
Packit Service a1973e
   Accepts an optional command-line argument, one of -c, -f77, or -f90 
Packit Service a1973e
      (-c default, as in original version of the program).
Packit Service a1973e
   The Fortran versions are fixed-format (source starts in column 7)
Packit Service a1973e
   Note: No check is made to ensure that lines don't extend past 72 columns.
Packit Service a1973e
   Date: 1/26/02 
Packit Service a1973e
   Rick Kufrin, NCSA/Univ of Illinois <rkufrin@ncsa.uiuc.edu> 
Packit Service a1973e
*/
Packit Service a1973e
Packit Service a1973e
/* Modified to eliminate reliance on libpapi.a.
Packit Service a1973e
   Now it relies only on a small collection of papi header files.
Packit Service a1973e
*/
Packit Service a1973e
Packit Service a1973e
/* Modified to always generate a symbolic representation for the
Packit Service a1973e
   maximum negative number. This is a work-around for a compiler
Packit Service a1973e
   limitation that first showed up on Cray X1 and then spread to
Packit Service a1973e
   GNU Fortran 4.3.2. Thanks to Jim Rosinski (ORNL) for
Packit Service a1973e
   identification and testing on this issue.
Packit Service a1973e
   Date: 06/03/08
Packit Service a1973e
*/
Packit Service a1973e
Packit Service a1973e
#include <stdlib.h>
Packit Service a1973e
#include <stdio.h>
Packit Service a1973e
Packit Service a1973e
#include <unistd.h>
Packit Service a1973e
#include <errno.h>
Packit Service a1973e
#include <sys/types.h>
Packit Service a1973e
#include <memory.h>
Packit Service a1973e
Packit Service a1973e
#include "papi.h"
Packit Service a1973e
Packit Service a1973e
#undef NDEBUG
Packit Service a1973e
#include <assert.h>
Packit Service a1973e
Packit Service a1973e
#define PAPI_EVENTS_IN_DERIVED_EVENT 8	/* to satisfy papi_preset.h */
Packit Service a1973e
#include "papi_preset.h"
Packit Service a1973e
#include "papi_common_strings.h"
Packit Service a1973e
/*
Packit Service a1973e
	The following array is used to create a series of defines
Packit Service a1973e
	for use with PAPI in Fortran programs.
Packit Service a1973e
	The value/name pairs come straight from papi.h. 
Packit Service a1973e
   They should be manually synchronized with papi.h when changes are made.
Packit Service a1973e
   The definition of hwi_describe_t is in papi_preset.h
Packit Service a1973e
*/
Packit Service a1973e
Packit Service a1973e
const hwi_describe_t _papi_def[] = {
Packit Service a1973e
	{PAPI_NULL, "PAPI_NULL", NULL},
Packit Service a1973e
	{PAPI_VER_CURRENT, "PAPI_VER_CURRENT", NULL},
Packit Service a1973e
	{PAPI_VERSION, "PAPI_VERSION", NULL},
Packit Service a1973e
	{PAPI_MAX_PRESET_EVENTS, "PAPI_MAX_PRESET_EVENTS", NULL},
Packit Service a1973e
Packit Service a1973e
	{PAPI_NOT_INITED, "PAPI_NOT_INITED", NULL},
Packit Service a1973e
	{PAPI_LOW_LEVEL_INITED, "PAPI_LOW_LEVEL_INITED", NULL},
Packit Service a1973e
	{PAPI_HIGH_LEVEL_INITED, "PAPI_HIGH_LEVEL_INITED", NULL},
Packit Service a1973e
	{PAPI_THREAD_LEVEL_INITED, "PAPI_THREAD_LEVEL_INITED", NULL},
Packit Service a1973e
Packit Service a1973e
	{PAPI_DOM_USER, "PAPI_DOM_USER", NULL},
Packit Service a1973e
	{PAPI_DOM_KERNEL, "PAPI_DOM_KERNEL", NULL},
Packit Service a1973e
	{PAPI_DOM_OTHER, "PAPI_DOM_OTHER", NULL},
Packit Service a1973e
	{PAPI_DOM_SUPERVISOR, "PAPI_DOM_SUPERVISOR", NULL},
Packit Service a1973e
	{PAPI_DOM_ALL, "PAPI_DOM_ALL", NULL},
Packit Service a1973e
	{PAPI_DOM_MIN, "PAPI_DOM_MIN", NULL},
Packit Service a1973e
	{PAPI_DOM_MAX, "PAPI_DOM_MAX", NULL},
Packit Service a1973e
	{PAPI_DOM_HWSPEC, "PAPI_DOM_HWSPEC", NULL},
Packit Service a1973e
Packit Service a1973e
	{PAPI_STOPPED, "PAPI_STOPPED", NULL},
Packit Service a1973e
	{PAPI_RUNNING, "PAPI_RUNNING", NULL},
Packit Service a1973e
	{PAPI_PAUSED, "PAPI_PAUSED", NULL},
Packit Service a1973e
	{PAPI_NOT_INIT, "PAPI_NOT_INIT", NULL},
Packit Service a1973e
	{PAPI_OVERFLOWING, "PAPI_OVERFLOWING", NULL},
Packit Service a1973e
	{PAPI_PROFILING, "PAPI_PROFILING", NULL},
Packit Service a1973e
	{PAPI_MULTIPLEXING, "PAPI_MULTIPLEXING", NULL},
Packit Service a1973e
	{PAPI_ATTACHED, "PAPI_ATTACHED", NULL},
Packit Service a1973e
	{PAPI_CPU_ATTACHED, "PAPI_CPU_ATTACHED", NULL},
Packit Service a1973e
Packit Service a1973e
	{PAPI_QUIET, "PAPI_QUIET", NULL},
Packit Service a1973e
	{PAPI_VERB_ECONT, "PAPI_VERB_ECONT", NULL},
Packit Service a1973e
	{PAPI_VERB_ESTOP, "PAPI_VERB_ESTOP", NULL},
Packit Service a1973e
Packit Service a1973e
	{PAPI_MIN_STR_LEN, "PAPI_MIN_STR_LEN", NULL},
Packit Service a1973e
	{PAPI_HUGE_STR_LEN, "PAPI_HUGE_STR_LEN", NULL},
Packit Service a1973e
	{PAPI_MAX_STR_LEN, "PAPI_MAX_STR_LEN", NULL},
Packit Service a1973e
	{PAPI_NUM_ERRORS, "PAPI_NUM_ERRORS", NULL},
Packit Service a1973e
Packit Service a1973e
	{PAPI_MULTIPLEX_DEFAULT, "PAPI_MULTIPLEX_DEFAULT", NULL},
Packit Service a1973e
	{PAPI_MULTIPLEX_FORCE_SW, "PAPI_MULTIPLEX_FORCE_SW", NULL},
Packit Service a1973e
Packit Service a1973e
	{PAPI_DEBUG, "PAPI_DEBUG", NULL},
Packit Service a1973e
	{PAPI_MULTIPLEX, "PAPI_MULTIPLEX", NULL},
Packit Service a1973e
	{PAPI_DEFDOM, "PAPI_DEFDOM", NULL},
Packit Service a1973e
	{PAPI_DOMAIN, "PAPI_DOMAIN", NULL},
Packit Service a1973e
	{PAPI_DEFGRN, "PAPI_DEFGRN", NULL},
Packit Service a1973e
	{PAPI_GRANUL, "PAPI_GRANUL", NULL},
Packit Service a1973e
	{PAPI_DEF_MPX_NS, "PAPI_DEF_MPX_NS", NULL},
Packit Service a1973e
	//	{PAPI_EDGE_DETECT, "PAPI_EDGE_DETECT", NULL},
Packit Service a1973e
	//{PAPI_INVERT, "PAPI_INVERT", NULL},
Packit Service a1973e
	{PAPI_MAX_MPX_CTRS, "PAPI_MAX_MPX_CTRS", NULL},
Packit Service a1973e
	{PAPI_PROFIL, "PAPI_PROFIL", NULL},
Packit Service a1973e
	{PAPI_PRELOAD, "PAPI_PRELOAD", NULL},
Packit Service a1973e
	{PAPI_CLOCKRATE, "PAPI_CLOCKRATE", NULL},
Packit Service a1973e
	{PAPI_MAX_HWCTRS, "PAPI_MAX_HWCTRS", NULL},
Packit Service a1973e
	{PAPI_HWINFO, "PAPI_HWINFO", NULL},
Packit Service a1973e
	{PAPI_EXEINFO, "PAPI_EXEINFO", NULL},
Packit Service a1973e
	{PAPI_MAX_CPUS, "PAPI_MAX_CPUS", NULL},
Packit Service a1973e
	{PAPI_ATTACH, "PAPI_ATTACH", NULL},
Packit Service a1973e
	{PAPI_SHLIBINFO, "PAPI_SHLIBINFO", NULL},
Packit Service a1973e
	{PAPI_LIB_VERSION, "PAPI_LIB_VERSION", NULL},
Packit Service a1973e
	{PAPI_COMPONENTINFO, "PAPI_COMPONENTINFO", NULL},
Packit Service a1973e
	{PAPI_DETACH, "PAPI_DETACH", NULL},
Packit Service a1973e
Packit Service a1973e
	{PAPI_GRN_THR, "PAPI_GRN_THR", NULL},
Packit Service a1973e
	{PAPI_GRN_MIN, "PAPI_GRN_MIN", NULL},
Packit Service a1973e
	{PAPI_GRN_PROC, "PAPI_GRN_PROC", NULL},
Packit Service a1973e
	{PAPI_GRN_PROCG, "PAPI_GRN_PROCG", NULL},
Packit Service a1973e
	{PAPI_GRN_SYS, "PAPI_GRN_SYS", NULL},
Packit Service a1973e
	{PAPI_GRN_SYS_CPU, "PAPI_GRN_SYS_CPU", NULL},
Packit Service a1973e
	{PAPI_GRN_MAX, "PAPI_GRN_MAX", NULL},
Packit Service a1973e
Packit Service a1973e
	{PAPI_DERIVED, "PAPI_DERIVED", NULL},
Packit Service a1973e
Packit Service a1973e
	{PAPI_PROFIL_POSIX, "PAPI_PROFIL_POSIX", NULL},
Packit Service a1973e
	{PAPI_PROFIL_RANDOM, "PAPI_PROFIL_RANDOM", NULL},
Packit Service a1973e
	{PAPI_PROFIL_WEIGHTED, "PAPI_PROFIL_WEIGHTED", NULL},
Packit Service a1973e
	{PAPI_PROFIL_COMPRESS, "PAPI_PROFIL_COMPRESS", NULL},
Packit Service a1973e
	{PAPI_PROFIL_BUCKET_16, "PAPI_PROFIL_BUCKET_16", NULL},
Packit Service a1973e
	{PAPI_PROFIL_BUCKET_32, "PAPI_PROFIL_BUCKET_32", NULL},
Packit Service a1973e
	{PAPI_PROFIL_BUCKET_64, "PAPI_PROFIL_BUCKET_64", NULL},
Packit Service a1973e
Packit Service a1973e
	{PAPI_USR1_LOCK, "PAPI_USR1_LOCK", NULL},
Packit Service a1973e
	{PAPI_USR2_LOCK, "PAPI_USR2_LOCK", NULL},
Packit Service a1973e
	{PAPI_LOCK_USR1, "PAPI_LOCK_USR1", NULL},
Packit Service a1973e
	{PAPI_LOCK_USR2, "PAPI_LOCK_USR2", NULL},
Packit Service a1973e
	{PAPI_LOCK_NUM, "PAPI_LOCK_NUM", NULL},
Packit Service a1973e
Packit Service a1973e
	{PAPI_MH_MAX_LEVELS, "PAPI_MH_MAX_LEVELS", NULL},
Packit Service a1973e
Packit Service a1973e
	{PAPI_USR1_TLS, "PAPI_USR1_TLS", NULL},
Packit Service a1973e
	{PAPI_USR2_TLS, "PAPI_USR2_TLS", NULL},
Packit Service a1973e
	{PAPI_TLS_USR1, "PAPI_TLS_USR1", NULL},
Packit Service a1973e
	{PAPI_TLS_USR2, "PAPI_TLS_USR2", NULL},
Packit Service a1973e
	{PAPI_TLS_HIGH_LEVEL, "PAPI_TLS_HIGH_LEVEL", NULL},
Packit Service a1973e
	{PAPI_TLS_NUM, "PAPI_TLS_NUM", NULL},
Packit Service a1973e
Packit Service a1973e
/* Fortran offsets into PAPI_dmem_info_t structure. */
Packit Service a1973e
	{PAPIF_DMEM_VMSIZE, "PAPIF_DMEM_VMSIZE", NULL},
Packit Service a1973e
	{PAPIF_DMEM_RESIDENT, "PAPIF_DMEM_RESIDENT", NULL},
Packit Service a1973e
	{PAPIF_DMEM_HIGH_WATER, "PAPIF_DMEM_HIGH_WATER", NULL},
Packit Service a1973e
	{PAPIF_DMEM_SHARED, "PAPIF_DMEM_SHARED", NULL},
Packit Service a1973e
	{PAPIF_DMEM_TEXT, "PAPIF_DMEM_TEXT", NULL},
Packit Service a1973e
	{PAPIF_DMEM_LIBRARY, "PAPIF_DMEM_LIBRARY", NULL},
Packit Service a1973e
	{PAPIF_DMEM_HEAP, "PAPIF_DMEM_HEAP", NULL},
Packit Service a1973e
	{PAPIF_DMEM_LOCKED, "PAPIF_DMEM_LOCKED", NULL},
Packit Service a1973e
	{PAPIF_DMEM_STACK, "PAPIF_DMEM_STACK", NULL},
Packit Service a1973e
	{PAPIF_DMEM_PAGESIZE, "PAPIF_DMEM_PAGESIZE", NULL},
Packit Service a1973e
	{PAPIF_DMEM_MAXVAL, "PAPIF_DMEM_MAXVAL", NULL},
Packit Service a1973e
Packit Service a1973e
/* PAPI error defines */
Packit Service a1973e
 /* 0 */ {PAPI_OK, "PAPI_OK", "No error"},
Packit Service a1973e
    /* 1 */ {PAPI_EINVAL, "PAPI_EINVAL", "Invalid argument"},
Packit Service a1973e
    /* 2 */ {PAPI_ENOMEM, "PAPI_ENOMEM", "Insufficient memory"},
Packit Service a1973e
    /* 3 */ {PAPI_ESYS, "PAPI_ESYS", "A System/C library call failed"},
Packit Service a1973e
    /* 4 */ {PAPI_ECMP, "PAPI_ECMP", "Not supported by component"},
Packit Service a1973e
    /* 5 */ {PAPI_ECLOST, "PAPI_ECLOST", "Access to the counters was lost or interrupted"},
Packit Service a1973e
    /* 6 */ {PAPI_EBUG, "PAPI_EBUG", "Internal error, please send mail to the developers"},
Packit Service a1973e
    /* 7 */ {PAPI_ENOEVNT, "PAPI_ENOEVNT", "Event does not exist"},
Packit Service a1973e
    /* 8 */ {PAPI_ECNFLCT, "PAPI_ECNFLCT", "Event exists, but cannot be counted due to hardware resource limits"},
Packit Service a1973e
    /* 9 */ {PAPI_ENOTRUN, "PAPI_ENOTRUN", "EventSet is currently not running"},
Packit Service a1973e
    /*10 */ {PAPI_EISRUN, "PAPI_EISRUN", "EventSet is currently counting"},
Packit Service a1973e
    /*11 */ {PAPI_ENOEVST, "PAPI_ENOEVST", "No such EventSet available"},
Packit Service a1973e
    /*12 */ {PAPI_ENOTPRESET, "PAPI_ENOTPRESET", "Event in argument is not a valid preset"},
Packit Service a1973e
    /*13 */ {PAPI_ENOCNTR, "PAPI_ENOCNTR", "Hardware does not support performance counters"},
Packit Service a1973e
    /*14 */ {PAPI_EMISC, "PAPI_EMISC", "Unknown error code"},
Packit Service a1973e
    /*15 */ {PAPI_EPERM, "PAPI_EPERM", "Permission level does not permit operation"},
Packit Service a1973e
    /*16 */ {PAPI_ENOINIT, "PAPI_ENOINIT", "PAPI hasn't been initialized yet"},
Packit Service a1973e
    /*17 */ {PAPI_ENOCMP, "PAPI_ENOCMP", "Component Index isn't set"},
Packit Service a1973e
    /*18 */ {PAPI_ENOSUPP, "PAPI_ENOSUPP", "Not supported"},
Packit Service a1973e
    /*19 */ {PAPI_ENOIMPL, "PAPI_ENOIMPL", "Not implemented"},
Packit Service a1973e
    /*20 */ {PAPI_EBUF, "PAPI_EBUF", "Buffer size exceeded"},
Packit Service a1973e
    /*21 */ {PAPI_EINVAL_DOM, "PAPI_EINVAL_DOM", "EventSet domain is not supported for the operation"},
Packit Service a1973e
    /*22 */ {PAPI_EATTR, "PAPI_EATTR", "Invalid or missing event attributes"},
Packit Service a1973e
    /*23 */ {PAPI_ECOUNT, "PAPI_ECOUNT", "Too many events or attributes"},
Packit Service a1973e
    /*24 */ {PAPI_ECOMBO, "PAPI_ECOMBO", "Bad combination of features"}
Packit Service a1973e
Packit Service a1973e
};
Packit Service a1973e
Packit Service a1973e
Packit Service a1973e
enum deftype_t
Packit Service a1973e
{ CDEFINE, F77DEFINE, F90DEFINE };
Packit Service a1973e
static char comment_char = 'C';
Packit Service a1973e
Packit Service a1973e
static void
Packit Service a1973e
define_val( const char *val_string, int val, enum deftype_t deftype )
Packit Service a1973e
{
Packit Service a1973e
	char value[20];
Packit Service a1973e
	/* The Fortran spec defines negative numbers as the negation of a positive number.
Packit Service a1973e
	   Because of that definition, the largest possible 2's complement negative
Packit Service a1973e
	   number cannot be legally expressed in Fortran. Compiler behavior is undefined
Packit Service a1973e
	   and unpredictable on this issue.
Packit Service a1973e
	   Several FORTRAN compilers (GNU Fortran (GCC) > 4.2.3, others?)
Packit Service a1973e
	   will throw errors or warnings for an explicit numeric value of -2147483648,
Packit Service a1973e
	   However, they don't object to an arithmetic evaluation that produces the
Packit Service a1973e
	   desired value. This value happens to be used for the PAPI preset
Packit Service a1973e
	   PAPI_L1_DCM, and PAPI_DOM_HWSPEC. 
Packit Service a1973e
	   The hack below works around that limitation.
Packit Service a1973e
	 */
Packit Service a1973e
	if ( ( ( unsigned ) val ) == 0x80000000 ) {
Packit Service a1973e
		sprintf( value, "((-2147483647) - 1)" );
Packit Service a1973e
	} else {
Packit Service a1973e
		sprintf( value, "%d", val );
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	switch ( deftype ) {
Packit Service a1973e
	case CDEFINE:
Packit Service a1973e
		printf( "#define %-18s %s\n", val_string, value );
Packit Service a1973e
		break;
Packit Service a1973e
	case F77DEFINE:
Packit Service a1973e
		printf( "      INTEGER %-18s\n      PARAMETER (%s=%s)\n", val_string,
Packit Service a1973e
				val_string, value );
Packit Service a1973e
		break;
Packit Service a1973e
	case F90DEFINE:
Packit Service a1973e
		printf( "      INTEGER, PARAMETER :: %-18s = %s\n", val_string, value );
Packit Service a1973e
		break;
Packit Service a1973e
	}
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
static void
Packit Service a1973e
createDef( char *title, const hwi_describe_t * descr, int size,
Packit Service a1973e
		   enum deftype_t deftype )
Packit Service a1973e
{
Packit Service a1973e
	int i, j;
Packit Service a1973e
	/* compute the size of the predefined array */
Packit Service a1973e
	j = size / sizeof ( hwi_describe_t );
Packit Service a1973e
Packit Service a1973e
	/* create defines for each line in the general arrays */
Packit Service a1973e
	printf( "\n%c\n%c  %s\n%c\n\n", comment_char, comment_char, title,
Packit Service a1973e
			comment_char );
Packit Service a1973e
	for ( i = 0; i < j; i++ )
Packit Service a1973e
		define_val( descr[i].name, descr[i].value, deftype );
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
Packit Service a1973e
int
Packit Service a1973e
main( int argc, char **argv )
Packit Service a1973e
{
Packit Service a1973e
	int i;
Packit Service a1973e
	enum deftype_t deftype = CDEFINE;
Packit Service a1973e
Packit Service a1973e
	if ( argc > 1 ) {
Packit Service a1973e
		if ( strcmp( argv[1], "-f77" ) == 0 ) {
Packit Service a1973e
			deftype = F77DEFINE;
Packit Service a1973e
			comment_char = '!';
Packit Service a1973e
		} else if ( strcmp( argv[1], "-f90" ) == 0 ) {
Packit Service a1973e
			deftype = F90DEFINE;
Packit Service a1973e
			comment_char = '!';
Packit Service a1973e
		} else if ( strcmp( argv[1], "-c" ) == 0 ) {
Packit Service a1973e
			deftype = CDEFINE;
Packit Service a1973e
			comment_char = 'C';
Packit Service a1973e
		} else {
Packit Service a1973e
			fprintf( stderr, "Usage: %s [ -c | -f77 | -f90 ]\n", argv[0] );
Packit Service a1973e
			exit( 1 );
Packit Service a1973e
		}
Packit Service a1973e
	}
Packit Service a1973e
Packit Service a1973e
	/* print a file header block */
Packit Service a1973e
	printf
Packit Service a1973e
		( "%c\n%c  This file contains defines required by the PAPI Fortran interface.\n",
Packit Service a1973e
		  comment_char, comment_char );
Packit Service a1973e
	printf( "%c  It is automagically generated by genpapifdef.c\n",
Packit Service a1973e
			comment_char );
Packit Service a1973e
	printf( "%c  DO NOT modify its contents and expect the changes to stick.\n",
Packit Service a1973e
			comment_char );
Packit Service a1973e
	printf( "%c  Changes MUST be made in genpapifdef.c instead.\n%c\n\n",
Packit Service a1973e
			comment_char, comment_char );
Packit Service a1973e
Packit Service a1973e
	/* create defines for the internal array pairs */
Packit Service a1973e
	createDef( "General purpose defines.", _papi_def, sizeof ( _papi_def ),
Packit Service a1973e
			   deftype );
Packit Service a1973e
	/* create defines for each member of the PRESET array */
Packit Service a1973e
	printf( "\n%c\n%c  PAPI preset event values.\n%c\n\n", comment_char,
Packit Service a1973e
			comment_char, comment_char );
Packit Service a1973e
Packit Service a1973e
	for ( i = 0; i < PAPI_MAX_PRESET_EVENTS; i++ ) {
Packit Service a1973e
		if ( _papi_hwi_presets[i].symbol ) {	/* if the event is in the preset table */
Packit Service a1973e
			define_val( _papi_hwi_presets[i].symbol,
Packit Service a1973e
						( i | PAPI_PRESET_MASK ), deftype );
Packit Service a1973e
		}
Packit Service a1973e
	}
Packit Service a1973e
	exit( 0 );
Packit Service a1973e
}