Skip to content

Commit

Permalink
Rename mynvtx -> dr_nvtx + cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarguinaud committed Jun 7, 2024
1 parent ab4c080 commit dfad60a
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 234 deletions.
4 changes: 2 additions & 2 deletions src/fiat/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/library/version.c.in ${CMAKE_CURRENT

ecbuild_list_add_pattern( LIST fiat_src GLOB *.c *.F* *.cc )
if(NOT (CMAKE_C_COMPILER_ID STREQUAL "PGI" OR CMAKE_C_COMPILER_ID STREQUAL "NVHPC" ))
# The files in the mynvtx directory are only intended to work with NVHPC
# The files in the dr_nvtx directory are only intended to work with NVHPC
# So don't try to compile them when using another compiler
ecbuild_list_exclude_pattern( LIST fiat_src REGEX mynvtx/* )
ecbuild_list_exclude_pattern( LIST fiat_src REGEX dr_nvtx/* )
endif()

set( fiat_src ${fiat_src} PARENT_SCOPE )
Expand Down
52 changes: 52 additions & 0 deletions src/fiat/dr_nvtx/dr_nvtx.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module dr_nvtx

use iso_c_binding
implicit none

interface

subroutine dr_nvtx_start (name)
use iso_c_binding
character(kind=c_char,len=*) :: name
end subroutine

subroutine dr_nvtx_end (name)
use iso_c_binding
character(kind=c_char,len=*) :: name
end subroutine

end interface

public :: dr_nvtx_push_range
public :: dr_nvtx_pop_range

contains

subroutine dr_nvtx_push_range (fstr)
character(kind=c_char,len=*), intent(in) :: fstr
character(kind=c_char,len=1024) :: cstr

!$omp master

cstr=trim(fstr)//c_null_char
call dr_nvtx_start (cstr)

!$omp end master

end subroutine

subroutine dr_nvtx_pop_range (fstr)
character(kind=c_char,len=*), intent(in) :: fstr
character(kind=c_char,len=1024) :: cstr

!$omp master

cstr=trim(fstr)//c_null_char
call dr_nvtx_end (cstr)

!$omp end master

end subroutine

end module dr_nvtx

116 changes: 116 additions & 0 deletions src/fiat/dr_nvtx/dr_nvtx.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include <nvToolsExt.h>
#include <string.h>
#include <stdlib.h>

#include "dr_nvtx_map.h"

#define INDENT(n) \
do { \
int __i; \
for (int __i = 0; __i < (n); __i++) \
printf (" "); \
} while (1)

static uint32_t myadler32 (const unsigned char *data)
{
const uint32_t MOD_ADLER = 65521;
uint32_t a = 1, b = 0;
size_t index;

for (index = 0; data[index] != 0; ++index)
{
a = (a + data[index]*2) % MOD_ADLER;
b = (b + a) % MOD_ADLER;
}

return (b << 16) | a;
}

#ifdef NVTX_VERYVERBOSE
static const char namestack[256][256];
static int istack=0;
#endif

void dr_nvtx_start_ (const char * name)
{
if (! dr_nvtx_map_start (name))
{
#ifdef NVTX_VERYVERBOSE
INDENT (istack);
printf ("Skipped open --- %s\n", name);
#endif
return;
}

int hash = 0;
int color_id = myadler32 ((const unsigned char*)name);
int r,g,b;

r=color_id & 0x000000ff;
g=(color_id & 0x000ff000) >> 12;
b=(color_id & 0x0ff00000) >> 20;

if (r<64 & g<64 & b<64)
{
r=r*3;
g=g*3+64;
b=b*4;
}

color_id = 0xff000000 | (r << 16) | (g << 8) | (b);

nvtxEventAttributes_t eventAttrib = {0};
eventAttrib.version = NVTX_VERSION;
eventAttrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE;
eventAttrib.colorType = NVTX_COLOR_ARGB;
eventAttrib.color = color_id;
eventAttrib.messageType = NVTX_MESSAGE_TYPE_ASCII;
eventAttrib.message.ascii = name;

#ifdef NVTX_VERYVERBOSE
INDENT (istack);
printf ("Opening %s\n", name);
#endif

nvtxRangePushEx (&eventAttrib);

#ifdef NVTX_VERYVERBOSE
strncpy (namestack[istack], name, 128);
istack++;
#endif

}

void dr_nvtx_end_ (const char * name)
{

if (! dr_nvtx_map_stop ())
{
#ifdef NVTX_VERYVERBOSE
INDENT (istack);
printf ("Skipped end --- %s\n",name);
#endif
return;
}

#ifdef NVTX_VERYVERBOSE
istack--;
if (istack < 0)
{
printf ("NVTX error negative stack\n");
abort ();
}

INDENT (istack);

printf ("Closing %s\n",name);

if (strcmp (name,namestack[istack]))
{
printf ("Error just closed the wrong marker: %s expected: %s\n",name, namestack[istack]);
abort ();
}
#endif

nvtxRangePop ();
}
78 changes: 78 additions & 0 deletions src/fiat/dr_nvtx/dr_nvtx_map.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <unordered_map>
#include <cstring>
#include <iostream>

#include "dr_nvtx_map.h"

using namespace std;

extern "C" double MPI_Wtime ();
#pragma weak MPI_Wtime

namespace
{
struct counter
{
int calls = 0;
double elapsed = 0;
double t0 = 0;
};

template <class _Tp>
struct equal_to : public binary_function<_Tp, _Tp, bool>
{
bool operator()(const _Tp& __x, const _Tp& __y) const
{
return strcmp( __x, __y ) == 0;
}
};


struct hash
{
//BKDR hash algorithm
int operator() (const char * str) const
{
int seed = 131;//31 131 1313 13131131313 etc//
int hash = 0;
while(*str)
{
hash = (hash * seed) + (*str);
str ++;
}

return hash & (0x7FFFFFFF);
}
};

counter * stack[128];

std::unordered_map<char const*, counter, hash, equal_to<const char*>> map;

int ilast = 0;
};

extern "C" int dr_nvtx_map_start (const char * str)
{
counter * elem = &(map[str]);
ilast++;
stack[ilast] = elem;
elem->calls ++;
if (elem->calls >= 11 && elem->elapsed < 0.0001)
return 0;
if (elem->calls > 1)
elem->t0 = MPI_Wtime();
return 1;
}

extern "C" int dr_nvtx_map_stop ()
{
counter * last = stack[ilast];
ilast--;
if (last->calls >= 11 && last->elapsed < 0.0001)
return 0;
if (last->calls > 1)
last->elapsed += MPI_Wtime() - last->t0;
return 1;
}

18 changes: 18 additions & 0 deletions src/fiat/dr_nvtx/dr_nvtx_map.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef _DR_NVTX_MAP_START
#define _DR_NVTX_MAP_START

#ifdef __cplusplus
extern "C"
{
#endif

int dr_nvtx_map_start (const char * str);
int dr_nvtx_map_stop ();

#ifdef __cplusplus
}
#endif


#endif

6 changes: 3 additions & 3 deletions src/fiat/drhook/internal/dr_hook_util.F90
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SUBROUTINE DR_HOOK_UTIL(LDHOOK,CDNAME,KCASE,PKEY,CDFILENAME,KSIZEINFO)
USE DR_HACK_MOD, ONLY : LL_DRHACK, DR_HACK_INIT, DR_HACK
#ifdef __PGI
USE NVTX
USE MYNVTX
USE DR_NVTX
#endif

IMPLICIT NONE
Expand Down Expand Up @@ -55,9 +55,9 @@ SUBROUTINE DR_HOOK_UTIL(LDHOOK,CDNAME,KCASE,PKEY,CDFILENAME,KSIZEINFO)

IF (II_DRNVTX == 1) THEN
IF (KCASE == 0) THEN
CALL PUSH_RANGE(CDNAME)
CALL DR_NVTX_PUSH_RANGE (CDNAME)
ELSEIF (KCASE==1) THEN
CALL POP_RANGE(CDNAME)
CALL DR_NVTX_POP_RANGE (CDNAME)
ENDIF
ENDIF
#endif
Expand Down
75 changes: 0 additions & 75 deletions src/fiat/mynvtx/map.cc

This file was deleted.

Loading

0 comments on commit dfad60a

Please sign in to comment.