Skip to content
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 function for manually writing marker file #584

Merged
merged 11 commits into from
Dec 18, 2023
Merged
336 changes: 179 additions & 157 deletions examples/C-internalMarkerAPI.c

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/applications/likwid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ likwid.sockstr_to_socklist = likwid_sockstr_to_socklist
likwid.markerInit = likwid_markerInit
likwid.markerThreadInit = likwid_markerThreadInit
likwid.markerClose = likwid_markerClose
likwid.markerWriteFile = likwid_markerWriteFile
likwid.markerNextGroup = likwid_markerNextGroup
likwid.registerRegion = likwid_registerRegion
likwid.startRegion = likwid_startRegion
Expand Down Expand Up @@ -505,7 +506,7 @@ local function printtable(tab)
if mtab and mtab["align"] and mtab["align"] == "left" then
str = str .. tostring(tab[i][j]) .. back .. front
elseif mtab and mtab["align"] and mtab["align"] == "right" then
str = str .. front .. back .. tostring(tab[i][j])
str = str .. front .. back .. tostring(tab[i][j])
else
str = str .. front.. tostring(tab[i][j]) ..back
end
Expand Down
6 changes: 6 additions & 0 deletions src/includes/likwid-marker.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ Shortcut for likwid_markerResetRegion() if compiled with -DLIKWID_PERFMON. Other
\def LIKWID_MARKER_CLOSE
Shortcut for likwid_markerClose() if compiled with -DLIKWID_PERFMON. Otherwise no operation is performed
*/
/*!
\def LIKWID_MARKER_WRITE_FILE(markerfile)
Shortcut for likwid_markerWriteFile() if compiled with -DLIKWID_PERFMON. Otherwise no operation is performed
*/
/** @}*/

#ifdef LIKWID_PERFMON
Expand All @@ -82,6 +86,7 @@ Shortcut for likwid_markerClose() if compiled with -DLIKWID_PERFMON. Otherwise n
#define LIKWID_MARKER_START(regionTag) likwid_markerStartRegion(regionTag)
#define LIKWID_MARKER_STOP(regionTag) likwid_markerStopRegion(regionTag)
#define LIKWID_MARKER_CLOSE likwid_markerClose()
#define LIKWID_MARKER_WRITE_FILE(markerfile) likwid_markerWriteFile(markerfile)
#define LIKWID_MARKER_RESET(regionTag) likwid_markerResetRegion(regionTag)
#define LIKWID_MARKER_GET(regionTag, nevents, events, time, count) likwid_markerGetRegion(regionTag, nevents, events, time, count)
#else /* LIKWID_PERFMON */
Expand All @@ -92,6 +97,7 @@ Shortcut for likwid_markerClose() if compiled with -DLIKWID_PERFMON. Otherwise n
#define LIKWID_MARKER_START(regionTag)
#define LIKWID_MARKER_STOP(regionTag)
#define LIKWID_MARKER_CLOSE
#define LIKWID_MARKER_WRITE_FILE(markerfile)
#define LIKWID_MARKER_GET(regionTag, nevents, events, time, count)
#define LIKWID_MARKER_RESET(regionTag)
#endif /* LIKWID_PERFMON */
Expand Down
11 changes: 10 additions & 1 deletion src/includes/likwid.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ regions and writes them out to a file (filepath in env variable
LIKWID_FILEPATH).
*/
extern void likwid_markerClose(void) __attribute__((visibility("default")));
/*! \brief Write marker API results to a file

Must be called in serial region of the application. It gathers all data of
regions and writes them out to file.
@param markerfile [in] The file to write to
@return Error
*/
extern int likwid_markerWriteFile(const char *markerfile)
__attribute__((visibility("default")));
/*! \brief Register a measurement region

Initializes the hashTable entry in order to reduce execution time of
Expand Down Expand Up @@ -3031,7 +3040,7 @@ int rocmon_returnGroups(int nrgroups, char **groups, char **shortinfos,
__attribute__((visibility("default")));

/** @}*/

/** \addtogroup RocmonMarkerAPI Marker API module for GPUs
* @{
*/
Expand Down
125 changes: 125 additions & 0 deletions src/libperfctr.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,131 @@ likwid_markerClose(void)
likwid_init = 0;
}

int
likwid_markerWriteFile(const char* markerfile)
{
if (markerfile == NULL)
{
fprintf(stderr, "File can not be NULL.\n");
return -EFAULT;
}

FILE *file = NULL;
int* validRegions = NULL;

int numberOfThreads = perfmon_getNumberOfThreads();
int numberOfRegions = perfmon_getNumberOfRegions();

if ( ! likwid_init )
{
return -EFAULT;;
}
if ((numberOfThreads == 0)||(numberOfRegions == 0))
{
fprintf(stderr, "No threads or regions defined in hash table\n");
return -EFAULT;
}

file = fopen(markerfile,"w");
if (file != NULL)
{
int newNumberOfRegions = 0;
int newRegionID = 0;
validRegions = (int*)malloc(numberOfRegions*sizeof(int));
if (!validRegions)
{
return -EFAULT;
}
for (int i=0; i<numberOfRegions; i++)
{
validRegions[i] = 0;
}
for (int i=0; i<numberOfRegions; i++)
{
for (int j=0; j<numberOfThreads; j++)
{
validRegions[i] += perfmon_getCountOfRegion(i, j);
}
if (validRegions[i] > 0)
newNumberOfRegions++;
else
fprintf(stderr, "WARN: Skipping region %s for evaluation.\n", perfmon_getTagOfRegion(i));
}
if (newNumberOfRegions < numberOfRegions)
{
fprintf(stderr, "WARN: Regions are skipped because:\n");
fprintf(stderr, " - The region was only registered\n");
fprintf(stderr, " - The region was started but never stopped\n");
fprintf(stderr, " - The region was never started but stopped\n");
}
DEBUG_PRINT(DEBUGLEV_DEVELOP,
Creating Marker file %s with %d regions %d groups and %d threads,
markerfile, newNumberOfRegions, numberOfGroups, numberOfThreads);
bstring thread_regs_grps = bformat("%d %d %d", numberOfThreads, newNumberOfRegions, numberOfGroups);
fprintf(file,"%s\n", bdata(thread_regs_grps));
DEBUG_PRINT(DEBUGLEV_DEVELOP, %s, bdata(thread_regs_grps));
bdestroy(thread_regs_grps);

for (int i=0; i<numberOfRegions; i++)
{
if (validRegions[i] == 0)
continue;
bstring tmp = bformat("%d:%s", newRegionID, perfmon_getTagOfRegion(i));
fprintf(file,"%s\n", bdata(tmp));
DEBUG_PRINT(DEBUGLEV_DEVELOP, %s, bdata(tmp));
bdestroy(tmp);
newRegionID++;
}
int *cpulist = (int*) malloc(numberOfThreads * sizeof(int));
if (cpulist == NULL)
{
fprintf(stderr, "Failed to allocate %lu bytes for the cpulist storage\n",
numberOfThreads * sizeof(int));
free(validRegions);
return -EFAULT;
}
newRegionID = 0;
for (int i=0; i<numberOfRegions; i++)
{
if (validRegions[i] == 0)
continue;
int groupID = perfmon_getGroupOfRegion(i);
int nevents = groupSet->groups[groupID].numberOfEvents;
int cpulist_size = perfmon_getCpulistOfRegion(i, numberOfThreads, cpulist);
for (int j=0; j<numberOfThreads; j++)
{
int count = perfmon_getCountOfRegion(i, j);
double time = perfmon_getTimeOfRegion(i, j);
bstring l = bformat("%d %d %d %u %e %d ", newRegionID,
groupID,
cpulist[j],
count,
time,
nevents);

for (int k=0; k < MIN(nevents, NUM_PMC); k++)
{
bstring tmp = bformat("%e ", perfmon_getResultOfRegionThread(i, k, j));
bconcat(l, tmp);
bdestroy(tmp);
}
fprintf(file,"%s\n", bdata(l));
DEBUG_PRINT(DEBUGLEV_DEVELOP, %s, bdata(l));
bdestroy(l);
}
newRegionID++;
}
fclose(file);
free(validRegions);
free(cpulist);
}
else
{
fprintf(stderr, "Cannot open file %s\n", markerfile);
fprintf(stderr, "%s", strerror(errno));
}
}

int
likwid_markerRegisterRegion(const char* regionTag)
{
Expand Down
11 changes: 10 additions & 1 deletion src/likwid.F90
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module likwid

!> \ingroup Fortran_Interface
!! \brief Initialize the Likwid Marker API
!! This routine initializes the Marker API for Fortran. It reads some
!! This routine initializes the Marker API for Fortran. It reads some
!! environment commonly set by likwid-perfctr.
!! \note Must be called once in a serial region.
subroutine likwid_markerInit()
Expand Down Expand Up @@ -71,6 +71,15 @@ end subroutine likwid_markerNextGroup
subroutine likwid_markerClose()
end subroutine likwid_markerClose

!> \ingroup Fortran_Interface
!! \brief Write marker API results to a file
!! Gathers all data of regions and writes them out to file.
!! \note Must be called in serial region of the application.
subroutine likwid_markerWriteFile( markerfile )
!> \param markerfile [in] The file to write to
character(*) :: markerfile
end subroutine likwid_markerWriteFile

!> \ingroup Fortran_Interface
!! \brief Register a code region
!! Initializes the hash table with an empty entry to reduce the overhead
Expand Down
18 changes: 18 additions & 0 deletions src/likwid_f90_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ likwid_markerclose_(void)
likwid_markerClose();
}

void __attribute__ ((visibility ("default") ))
likwid_markerwritefile_(char* markerfile, int len)
{
char* tmp = (char*) malloc((len+1) * sizeof(char));
strncpy(tmp, markerfile, len * sizeof(char));

for (int i = (len - 1); len > 0; len--)
{
if (tmp[i] != ' ') {
tmp[i + 1] = 0;
break;
}
}

likwid_markerWriteFile(tmp);
free(tmp);
}

void __attribute__ ((visibility ("default") ))
likwid_markernextgroup_(void)
{
Expand Down
7 changes: 7 additions & 0 deletions src/luawid.c
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,12 @@ static int lua_likwid_markerClose(lua_State *L) {
return 0;
}

static int lua_likwid_markerWriteFile(lua_State *L) {
const char *markerfile = (const char *)luaL_checkstring(L, -1);
lua_pushinteger(L, likwid_markerWriteFile(markerfile));
return 1;
}

static int lua_likwid_markerNext(lua_State *L) {
likwid_markerNextGroup();
return 0;
Expand Down Expand Up @@ -3991,6 +3997,7 @@ int __attribute__((visibility("default"))) luaopen_liblikwid(lua_State *L) {
lua_register(L, "likwid_markerThreadInit", lua_likwid_markerThreadInit);
lua_register(L, "likwid_markerNextGroup", lua_likwid_markerNext);
lua_register(L, "likwid_markerClose", lua_likwid_markerClose);
lua_register(L, "likwid_markerWriteFile", lua_likwid_markerWriteFile);
lua_register(L, "likwid_registerRegion", lua_likwid_registerRegion);
lua_register(L, "likwid_startRegion", lua_likwid_startRegion);
lua_register(L, "likwid_stopRegion", lua_likwid_stopRegion);
Expand Down
Loading