-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add NVTX support via DrHook #23
Open
pmarguinaud
wants to merge
8
commits into
ecmwf-ifs:develop
Choose a base branch
from
pmarguinaud:drhooknvtx
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3c879e7
Add NVTX support
pmarguinaud ab4c080
Try nvhpc-24.3 instead of nvhpc-21.9
pmarguinaud dfad60a
Rename mynvtx -> dr_nvtx + cleaning
pmarguinaud 453b148
Make DR_NVTX optional, use HAVE_DR_NVTX instead of __PGI in Fortran/C
pmarguinaud 6a9e9c1
Try freeing some disk space
pmarguinaud 8b0d4eb
Merge branch 'develop' of https://github.com/ecmwf-ifs/fiat into drho…
pmarguinaud 5a22f69
Add test for DR_NVTX
pmarguinaud 4a0656f
Add test for DR_NVTX
pmarguinaud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @pmarguinaud, I've been doing some work on this patch to integrate it into the core of drhook so that it's faster, works with all support drhook languages, and is easier for us to maintain.
This condition is giving me a bit of confusion though. My understanding is that it's meant to eliminate high call but low cumulative runtime drhook regions from the nvtx trace, so as to reduce noise in the trace. However, the way it's implemented (particularly lines 72-75) means that once you go over 10 calls, it will always be skipped. This also adds a fun situation where the following recursive code won't close any nvtx regions it opens:
This gave the following output:
Am I correct in thinking this is not the intended behaviour and I should attempt to fix it in my patch?