Skip to content

Commit

Permalink
Make fiat-drhook-sanity a unit-test, refactor CMake and other cosmetic
Browse files Browse the repository at this point in the history
cleanup
  • Loading branch information
wdeconinck committed Jul 24, 2024
1 parent 586191a commit e3ff319
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 232 deletions.
11 changes: 3 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ ecbuild_add_option( FEATURE MPI
DESCRIPTION "Support for MPI distributed parallelism"
REQUIRED_PACKAGES "MPI COMPONENTS Fortran" )

ecbuild_add_option( FEATURE PAPI
DESCRIPTION "Support for HW counters in drhook via PAPI"
REQUIRED_PACKAGES "OpenMP COMPONENTS C" "PAPI")
ecbuild_add_option( FEATURE DR_HOOK_PAPI
DESCRIPTION "Support for HW counters in DR_HOOK via PAPI"
REQUIRED_PACKAGES "PAPI")

ecbuild_find_package( fckit QUIET )
ecbuild_add_option( FEATURE FCKIT
Expand All @@ -54,11 +54,6 @@ ecbuild_add_option( FEATURE WARNINGS
DEFAULT ON
DESCRIPTION "Add warnings to compiler" )

ecbuild_add_option( FEATURE MKL
DESCRIPTION "Use MKL for BLAS and/or FFTW"
DEFAULT ON
REQUIRED_PACKAGES "MKL" )

ecbuild_find_package( NAME Realtime QUIET )

### Sources
Expand Down
4 changes: 2 additions & 2 deletions cmake/FindPAPI.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ find_path(PAPI_ROOT
find_library(PAPI_LIBRARIES
# Pick the static library first for easier run-time linking.
NAMES libpapi.so libpapi.a papi
HINTS ${PAPI_ROOT}/lib ${HILTIDEPS}/lib
HINTS ${PAPI_ROOT}/lib
)

find_path(PAPI_INCLUDE_DIRS
NAMES papi.h
HINTS ${PAPI_ROOT}/include ${HILTIDEPS}/include
HINTS ${PAPI_ROOT}/include
)

include(FindPackageHandleStandardArgs)
Expand Down
6 changes: 2 additions & 4 deletions src/fiat/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,13 @@ else()
endif()

if( HAVE_OMP )

target_link_libraries( fiat PRIVATE OpenMP::OpenMP_Fortran )

endif()

if ( HAVE_PAPI )
if ( HAVE_DR_HOOK_PAPI )
target_link_libraries ( fiat PRIVATE ${PAPI_LIBRARIES} )
target_include_directories ( fiat PRIVATE ${PAPI_INCLUDE_DIRS} )
target_compile_definitions ( fiat PRIVATE HKPAPI )
target_compile_definitions ( fiat PRIVATE DR_HOOK_HAVE_PAPI=1 )
endif()

fiat_target_ignore_missing_symbols( TARGET fiat SYMBOLS
Expand Down
25 changes: 13 additions & 12 deletions src/fiat/drhook/drhook.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ typedef struct drhook_key_t {
long long int mem_maxhwm, mem_maxrss, mem_maxstk, mem_maxpagdelta;
long long int paging_in;

#ifdef HKPAPI
#if defined(DR_HOOK_HAVE_PAPI)
long_long counters_in[NPAPICNTRS];
long_long delta_counters_all[NPAPICNTRS];
long_long delta_counters_child[NPAPICNTRS];
Expand Down Expand Up @@ -507,7 +507,7 @@ typedef struct drhook_prof_t {
double pc;
double total;
double self;
#ifdef HKPAPI
#if defined(DR_HOOK_HAVE_PAPI)
long_long counter_tot[NPAPICNTRS];
long_long counter_self[NPAPICNTRS];
#endif
Expand Down Expand Up @@ -1060,7 +1060,7 @@ remove_calltree(int tid, drhook_key_t *keyptr,
if (treeptr->prev) {
drhook_key_t *parent_keyptr = treeptr->prev->keyptr;
if (parent_keyptr) { /* extra security */
#ifdef HKPAPI
#if defined(DR_HOOK_HAVE_PAPI)
drhook_papi_add(NULL,
parent_keyptr->delta_counters_child,
delta_counters
Expand Down Expand Up @@ -2827,7 +2827,7 @@ getkey(int tid, const char *name, int name_len,
if (opt_walltime) keyptr->wall_in = walltime ? *walltime : WALLTIME();
if (opt_cputime) keyptr->cpu_in = cputime ? *cputime : CPUTIME();
if (opt_cycles) keyptr->cycles_in = cycles ? *cycles : ec_get_cycles();
#ifdef HKPAPI
#if defined(DR_HOOK_HAVE_PAPI)
drhook_papi_readAll(keyptr->counters_in);
#endif
if (any_memstat) memstat(keyptr,&tid,1);
Expand Down Expand Up @@ -2932,7 +2932,7 @@ putkey(int tid, drhook_key_t *keyptr, const char *name, int name_len,
double delta_wall = 0;
double delta_cpu = 0;
long_long * delta_counters=NULL;
#ifdef HKPAPI
#if defined(DR_HOOK_HAVE_PAPI)
delta_counters=alloca(drhook_papi_num_counters() * sizeof(long_long) );
drhook_papi_bzero(delta_counters);
#endif
Expand Down Expand Up @@ -2965,7 +2965,7 @@ putkey(int tid, drhook_key_t *keyptr, const char *name, int name_len,
if (opt_walltime) keyptr->delta_wall_all += delta_wall;
if (opt_cputime) keyptr->delta_cpu_all += delta_cpu;
if (opt_cycles) keyptr->delta_cycles_all += delta_cycles;
#ifdef HKPAPI
#if defined(DR_HOOK_HAVE_PAPI)
drhook_papi_subtract(delta_counters, NULL , keyptr->counters_in);
drhook_papi_add(NULL, keyptr->delta_counters_all, delta_counters);
#endif
Expand Down Expand Up @@ -3095,7 +3095,7 @@ itself(drhook_key_t *keyptr_self,
if (opt == 0) {
if (opt_wallprof) keyptr->wall_in = walltime ? *walltime : WALLTIME();
else keyptr->cpu_in = cputime ? *cputime : CPUTIME();
#ifdef HKPAPI
#if defined(DR_HOOK_HAVE_PAPI)
drhook_papi_readAll(keyptr->counters_in);
#endif
keyptr->calls++;
Expand All @@ -3112,7 +3112,8 @@ itself(drhook_key_t *keyptr_self,
}
if (delta_time) *delta_time = delta;

#ifdef HKPAPI
#if defined(DR_HOOK_HAVE_PAPI)

long_long cntrs_delta[NPAPICNTRS];

/* cntrs_delta = current - counters_in */
Expand Down Expand Up @@ -3412,7 +3413,7 @@ c_drhook_check_watch_(const char *where,
}

/*** PUBLIC ***/
#ifdef HKPAPI
#if defined(DR_HOOK_HAVE_PAPI)
#define PAPIREAD \
long_long cntrs[NPAPICNTRS]; \
drhook_papi_readAll(cntrs)
Expand Down Expand Up @@ -3509,7 +3510,7 @@ c_drhook_init_(const char *progname,
tabort_delete_lockfile();
drhook_delete_lockfile();
}
#ifdef HKPAPI
#if defined(DR_HOOK_HAVE_PAPI)
drhook_papi_init(myproc -1);
#endif

Expand Down Expand Up @@ -4308,7 +4309,7 @@ c_drhook_print_(const int *ftnunitno,
drhook_key_t *keyptr = &keydata[t][j];
while (keyptr) {
if (keyptr->name && (keyptr->status == 0 || signal_handler_called)) {
#ifdef HKPAPI
#if defined(DR_HOOK_HAVE_PAPI)
drhook_papi_subtract(p->counter_self,
keyptr->delta_counters_all,
keyptr->delta_counters_child);
Expand Down Expand Up @@ -4580,7 +4581,7 @@ c_drhook_print_(const int *ftnunitno,
} /* for (j=0; j<nprof; ) */
fclose(fp);

#ifdef HKPAPI
#if defined(DR_HOOK_HAVE_PAPI)
if (opt_papi){
p=prof;
int first_counter_is_cyc=0;
Expand Down
84 changes: 45 additions & 39 deletions src/fiat/drhook/drhook_papi.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifdef HKPAPI
#if defined(DR_HOOK_HAVE_PAPI)
#include "drhook_papi.h"
#include <stdio.h>
#include <stdbool.h>
Expand All @@ -8,20 +8,22 @@

#define STD_MSG_LEN 4096

int * drhook_papi_event_set=NULL;
enum {drhook_papi_notstarted,drhook_papi_running,drhook_papi_failed};
int drhook_papi_state=0;
int* drhook_papi_event_set=NULL;
enum {
drhook_papi_notstarted,
drhook_papi_running,
drhook_papi_failed
};
int drhook_papi_state=drhook_papi_notstarted;
int drhook_papi_rank=0; /* C style! */
size_t drhook_max_counter_name=0;

/* hardwired for now */
const char * hookCounters[ NPAPICNTRS ][2]=
{
const char* hookCounters[NPAPICNTRS][2]= {
{"PAPI_TOT_CYC","Cycles"},
{"PAPI_FP_OPS","FP Operations"},
{"PAPI_L1_DCA","L1 Access"},
{"PAPI_L2_DCM","L2 Miss"}
};
};

/* function to use for thread id
- it should be better than omp_get_thread_num!
Expand All @@ -30,7 +32,7 @@ unsigned long safe_thread_num(){
return oml_my_thread()-1;
}

const char * drhook_papi_counter_name(int c,int t){
const char* drhook_papi_counter_name(int c,int t){
return hookCounters[c][t];
}

Expand All @@ -46,7 +48,7 @@ void drhook_papi_bzero(long_long* a){
}
}

void drhook_papi_print(char * s,long_long* a,int header){
void drhook_papi_print(char* s, long_long* a, int header){
char msg[STD_MSG_LEN];
if (header>0){
char fmt[STD_MSG_LEN];
Expand Down Expand Up @@ -88,8 +90,6 @@ void drhook_papi_add(long_long* a,long_long* b, long_long* c){
}
}



// number of counters available to read
int drhook_papi_num_counters(){
return NPAPICNTRS;
Expand All @@ -115,7 +115,7 @@ int drhook_papi_readAll(long_long * counterArray){
printf("DRHOOK:PAPI:PAPI_read: Error reading counters, thread=%ld es=%d %s\n",safe_thread_num(),drhook_papi_event_set[safe_thread_num()],PAPI_strerror(err));
}
#if defined(DEBUG)
drhook_papi_print("readAll:",counterArray);
drhook_papi_print("readAll:",counterArray,0);
#endif
return err;
}
Expand All @@ -142,7 +142,7 @@ int drhook_papi_init(int rank){
}

paperr=PAPI_library_init(PAPI_VER_CURRENT);
if (paperr != PAPI_VER_CURRENT){
if (paperr != PAPI_VER_CURRENT){
snprintf(pmsg,STD_MSG_LEN,"DRHOOK:PAPI:PAPI_library_init: ret code=%d version loaded =%d ",
paperr,PAPI_VER_CURRENT);
printf("%s\n",pmsg);
Expand Down Expand Up @@ -211,7 +211,7 @@ int drhook_papi_init(int rank){
return 1;
}

int dr_hook_papi_start_threads(int * events){
int dr_hook_papi_start_threads(int* events){
int thread=safe_thread_num();
int papiErr;
char pmsg[STD_MSG_LEN];
Expand All @@ -222,18 +222,23 @@ int dr_hook_papi_start_threads(int * events){
snprintf(pmsg,STD_MSG_LEN,"DRHOOK:PAPI: create event set failed (%s) \n",PAPI_strerror(papiErr));
printf("%s\n",pmsg);
return 0;
} else
printf("Event set %d created for thread %d\n",events[thread],thread);
}

printf("DRHOOK:PAPI: Event set %d created for thread %d\n",events[thread],thread);

int prof_papi_numcntrs=NPAPICNTRS;
for (int counter=0;counter < prof_papi_numcntrs ;counter ++){
for (int counter=0; counter < prof_papi_numcntrs; counter ++){
int eventCode;

snprintf(pmsg,STD_MSG_LEN,"DRHOOK:PAPI: %s (%s)",hookCounters[counter][0],hookCounters[counter][1]);
if (drhook_papi_rank==0) if (thread==0)printf("%s\n",pmsg);
if (drhook_papi_rank==0) {
if (thread==0) {
printf("%s\n",pmsg);
}
}

papiErr=PAPI_event_name_to_code(hookCounters[counter][0],&eventCode);
if (papiErr !=PAPI_OK){
if (papiErr != PAPI_OK){
snprintf(pmsg,STD_MSG_LEN,"DRHOOK:PAPI: event name to code failed (%s)",PAPI_strerror(papiErr));
printf("%s\n",pmsg);
PAPI_perror("initPapi");
Expand All @@ -245,22 +250,25 @@ int dr_hook_papi_start_threads(int * events){
snprintf(pmsg,STD_MSG_LEN,"DRHOOK:PAPI: add_event failed: %d (%s)",papiErr,PAPI_strerror(papiErr));
printf("%s\n",pmsg);
if (papiErr == PAPI_EINVAL)
printf("Invalid argumet");
printf("Invalid argumet");
else if (papiErr == PAPI_ENOMEM)
printf("Out of Mmemory");
printf("Out of Mmemory");
else if (papiErr == PAPI_ENOEVST)
printf("EventSet does not exist");
printf("EventSet does not exist");
else if (papiErr == PAPI_EISRUN)
printf("EventSet is running");
printf("EventSet is running");
else if (papiErr == PAPI_ECNFLCT)
printf("Conflict");
printf("Conflict");
else if (papiErr == PAPI_ENOEVNT)
printf("Preset not available");
printf("Preset not available");
return 0;
}else {
}
else {
#if defined(DEBUG)
snprintf(pmsg,STD_MSG_LEN,"DRHOOK:PAPI: Added code=%d to Evnt set %d",events[thread]);
if (thread==0)printf("%s\n",pmsg);
snprintf(pmsg,STD_MSG_LEN,"DRHOOK:PAPI: Added code=%d to Event set %d",eventCode, events[thread]);
if (thread==0) {
printf("%s\n",pmsg);
}
#endif
}
}
Expand All @@ -272,15 +280,13 @@ int dr_hook_papi_start_threads(int * events){
snprintf(pmsg,STD_MSG_LEN,"DRHOOK:PAPI: Error querying events - %d=%s",papiErr,PAPI_strerror(papiErr));
printf("%s\n",pmsg);
return 0;
}else {
}
#if defined(DEBUG)
for (counter=0;counter<number;counter++)
{
snprintf(pmsg,STD_MSG_LEN,"DRHOOK:PAPI: Ev: %d=%d",counter,events[counter]);
printf("%s\n",pmsg);
}
#endif
for (int counter=0;counter<number;counter++) {
snprintf(pmsg,STD_MSG_LEN,"DRHOOK:PAPI: Ev: %d=%d",counter,events[counter]);
printf("%s\n",pmsg);
}
#endif

if (number != prof_papi_numcntrs){
snprintf(pmsg,STD_MSG_LEN,"DRHOOK:PAPI: Error checking events - expected=%d got=%d",prof_papi_numcntrs,number);
Expand All @@ -293,11 +299,11 @@ int dr_hook_papi_start_threads(int * events){
snprintf(pmsg,STD_MSG_LEN,"DRHOOK:PAPI: starting counters failed (%d=%s)",papiErr,PAPI_strerror(papiErr));
printf("%s\n",pmsg);
return 0;
}else {
}

#if defined(DEBUG)
printf("DRHOOK:PAPI: Started counting for thread %d \n",thread);
printf("DRHOOK:PAPI: Started counting for thread %d \n",thread);
#endif
}

return 1;
}
Expand Down
8 changes: 2 additions & 6 deletions src/fiat/drhook/drhook_papi.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
#ifndef DRHOOK_PAPI
#define DRHOOK_PAPI
#ifdef HKPAPI

#ifdef _OPENMP
#include <omp.h>
#endif
#if defined(DR_HOOK_HAVE_PAPI)

#include <papi.h>

#define NPAPICNTRS 4

int drhook_papi_init(int rank);
int drhook_papi_num_counters();
const char * drhook_papi_counter_name(int c,int t);
const char* drhook_papi_counter_name(int c,int t);
long_long drhook_papi_read(int counterId);
int drhook_papi_readAll(long_long * counterArray);

Expand Down
Loading

0 comments on commit e3ff319

Please sign in to comment.