Skip to content

Commit

Permalink
Initial import into the Kokkos tools repository. This is a public rep…
Browse files Browse the repository at this point in the history
…ository of tools (currently mainly profilers or connectors) which work with the Kokkos programming model/runtime
  • Loading branch information
Simon David Hammond (-EXP) committed Feb 11, 2016
0 parents commit b9f8c2f
Show file tree
Hide file tree
Showing 19 changed files with 1,398 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# KokkosP Profiling Tools
Performance profiling tools that can be optionally integrated into Kokkos-based applications and libraries.
11 changes: 11 additions & 0 deletions src/tools/kernel-filter/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CXX=icpc
CXXFLAGS=-O3 -std=c++11 -g
SHARED_CXXFLAGS=-shared -fPIC

all: kp_kernel_filter.so

kp_kernel_filter.so: kp_kernel_filter.cpp
$(CXX) $(SHARED_CXXFLAGS) $(CXXFLAGS) -o $@ kp_kernel_filter.cpp

clean:
rm *.so
284 changes: 284 additions & 0 deletions src/tools/kernel-filter/kp_kernel_filter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@

#include <stdio.h>
#include <inttypes.h>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <unordered_set>
#include <string>
#include <regex>
#include <cxxabi.h>
#include <dlfcn.h>

bool filterKernels;
uint64_t nextKernelID;
std::vector<std::regex> kernelNames;
std::unordered_set<uint64_t> activeKernels;

typedef void (*initFunction)(const int, const uint64_t, const uint32_t, void*);
typedef void (*finalizeFunction)();
typedef void (*beginFunction)(const char*, const uint32_t, uint64_t*);
typedef void (*endFunction)(uint64_t);

static initFunction initProfileLibrary = NULL;
static finalizeFunction finalizeProfileLibrary = NULL;
static beginFunction beginForCallee = NULL;
static beginFunction beginScanCallee = NULL;
static beginFunction beginReduceCallee = NULL;
static endFunction endForCallee = NULL;
static endFunction endScanCallee = NULL;
static endFunction endReduceCallee = NULL;

bool kokkospFilterMatch(const char* name) {
bool matched = false;
std::string nameStr(name);

for(auto nextRegex : kernelNames) {
if(std::regex_match(nameStr, nextRegex)) {
matched = true;
break;
}
}

return matched;
}

bool kokkospReadLine(FILE* kernelFile, char* lineBuffer) {
bool readData = false;
bool continueReading = !feof(kernelFile);
int nextIndex = 0;

while(continueReading) {
const int nextChar = fgetc(kernelFile);

if(EOF == nextChar) {
continueReading = false;
} else {
readData = true;

if(nextChar == '\n' || nextChar == '\f' || nextChar == '\r') {
continueReading = false;
} else {
lineBuffer[nextIndex++] = (char) nextChar;
}
}
}

lineBuffer[nextIndex] = '\0';
return readData;
}

extern "C" void kokkosp_init_library(const int loadSeq,
const uint64_t interfaceVer, const uint32_t devInfoCount, void* deviceInfo) {

const char* kernelFilterPath = getenv("KOKKOSP_KERNEL_FILTER");
nextKernelID = 0;

if(NULL == kernelFilterPath) {
filterKernels = false;
printf("============================================================\n");
printf("KokkosP: No kernel filtering data provided, disabled from this tool onwards\n");
printf("============================================================\n");
} else {
printf("============================================================\n");
printf("KokkosP: Filter File: %s\n", kernelFilterPath);
printf("============================================================\n");

FILE* kernelFilterFile = fopen(kernelFilterPath, "rt");

if(NULL == kernelFilterFile) {
fprintf(stderr, "Unable to open kernel filter: %s\n",
kernelFilterPath);
exit(-1);
} else {
char* lineBuffer = (char*) malloc( sizeof(char) * 65536 );

while(kokkospReadLine(kernelFilterFile, lineBuffer)) {
printf("KokkosP: Filter [%s]\n", lineBuffer);

std::regex nextRegEx(lineBuffer, std::regex::optimize);
kernelNames.push_back(nextRegEx);
}

free(lineBuffer);
}

filterKernels = (kernelNames.size() > 0);

printf("KokkosP: Kernel Filtering is %s\n", (filterKernels ?
"enabled" : "disabled"));

if(filterKernels) {
char* profileLibrary = getenv("KOKKOS_PROFILE_LIBRARY");
char* envBuffer = (char*) malloc( sizeof(char) * (strlen(profileLibrary) + 1 ));
sprintf(envBuffer, "%s", profileLibrary);

char* nextLibrary = strtok(envBuffer, ";");

for(int i = 0; i < loadSeq; i++) {
nextLibrary = strtok(NULL, ";");
}

nextLibrary = strtok(NULL, ";");

if(NULL == nextLibrary) {
printf("KokkosP: No child library to call in %s\n", profileLibrary);
} else {
printf("KokkosP: Next library to call: %s\n", nextLibrary);
printf("KokkosP: Loading child library ..\n");

void* childLibrary = dlopen(nextLibrary, RTLD_NOW | RTLD_GLOBAL);

if(NULL == childLibrary) {
fprintf(stderr, "KokkosP: Error: Unable to load: %s (Error=%s)\n",
nextLibrary, dlerror());
} else {
beginForCallee = (beginFunction) dlsym(childLibrary, "kokkosp_begin_parallel_for");
beginScanCallee = (beginFunction) dlsym(childLibrary, "kokkosp_begin_parallel_scan");
beginReduceCallee = (beginFunction) dlsym(childLibrary, "kokkosp_begin_parallel_reduce");

endScanCallee = (endFunction) dlsym(childLibrary, "kokkosp_end_parallel_scan");
endForCallee = (endFunction) dlsym(childLibrary, "kokkosp_end_parallel_for");
endReduceCallee = (endFunction) dlsym(childLibrary, "kokkosp_end_parallel_reduce");

initProfileLibrary = (initFunction) dlsym(childLibrary, "kokkosp_init_library");
finalizeProfileLibrary = (finalizeFunction) dlsym(childLibrary, "kokkosp_finalize_library");

if(NULL != initProfileLibrary) {
(*initProfileLibrary)(loadSeq + 1, interfaceVer, devInfoCount, deviceInfo);
}
}

}

free(envBuffer);
}

printf("============================================================\n");
}
}

extern "C" void kokkosp_finalize_library() {
if(NULL != finalizeProfileLibrary) {
(*finalizeProfileLibrary)();
}

// Set all profile hooks to NULL to prevent
// any additional calls. Once we are told to
// finalize, we mean it
beginForCallee = NULL;
beginScanCallee = NULL;
beginReduceCallee = NULL;
endScanCallee = NULL;
endForCallee = NULL;
endReduceCallee = NULL;
initProfileLibrary = NULL;
finalizeProfileLibrary = NULL;

printf("============================================================\n");
printf("KokkosP: Kernel filtering library, finalized.\n");
printf("============================================================\n");
}

extern "C" void kokkosp_begin_parallel_for(const char* name, const uint32_t devID, uint64_t* kID) {
if(filterKernels) {
if(kokkospFilterMatch(name)) {
if(NULL != beginForCallee) {
(*beginForCallee)(name, devID, kID);
activeKernels.insert(*kID);
} else {
*kID = nextKernelID++;
}
} else {
*kID = nextKernelID++;
}
} else {
if(NULL != beginForCallee) {
(*beginForCallee)(name, devID, kID);
activeKernels.insert(*kID);
} else {
*kID = nextKernelID++;
}
}
}

extern "C" void kokkosp_end_parallel_for(const uint64_t kID) {
auto findKernel = activeKernels.find(kID);

if(activeKernels.end() != findKernel) {
if(NULL != endForCallee) {
(*endForCallee)(kID);
}

activeKernels.erase(findKernel);
}
}

extern "C" void kokkosp_begin_parallel_scan(const char* name, const uint32_t devID, uint64_t* kID) {
if(filterKernels) {
if(kokkospFilterMatch(name)) {
if(NULL != beginScanCallee) {
(*beginScanCallee)(name, devID, kID);
activeKernels.insert(*kID);
} else {
*kID = nextKernelID++;
}
} else {
*kID = nextKernelID++;
}
} else {
if(NULL != beginScanCallee) {
(*beginScanCallee)(name, devID, kID);
activeKernels.insert(*kID);
} else {
*kID = nextKernelID++;
}
}
}

extern "C" void kokkosp_end_parallel_scan(const uint64_t kID) {
auto findKernel = activeKernels.find(kID);

if(activeKernels.end() != findKernel) {
if(NULL != endScanCallee) {
(*endScanCallee)(kID);
}

activeKernels.erase(findKernel);
}
}

extern "C" void kokkosp_begin_parallel_reduce(const char* name, const uint32_t devID, uint64_t* kID) {
if(filterKernels) {
if(kokkospFilterMatch(name)) {
if(NULL != beginScanCallee) {
(*beginReduceCallee)(name, devID, kID);
activeKernels.insert(*kID);
} else {
*kID = nextKernelID++;
}
} else {
*kID = nextKernelID++;
}
} else {
if(NULL != beginScanCallee) {
(*beginReduceCallee)(name, devID, kID);
activeKernels.insert(*kID);
} else {
*kID = nextKernelID++;
}
}
}

extern "C" void kokkosp_end_parallel_reduce(const uint64_t kID) {
auto findKernel = activeKernels.find(kID);

if(activeKernels.end() != findKernel) {
if(NULL != endReduceCallee) {
(*endReduceCallee)(kID);
}

activeKernels.erase(findKernel);
}
}

8 changes: 8 additions & 0 deletions src/tools/memory-hwm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CXX=g++
CFLAGS=-shared -O3 -fPIC -std=c++11

kp_hwm.so: kp_hwm.cpp
$(CXX) $(CFLAGS) -o $@ $<

clean:
rm *.so
36 changes: 36 additions & 0 deletions src/tools/memory-hwm/kp_hwm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

#include <stdio.h>
#include <inttypes.h>
#include <execinfo.h>
#include <stdlib.h>
#include <string.h>
#include <map>
#include <vector>
#include <algorithm>
#include <string>
#include <sys/time.h>
#include <cxxabi.h>
#include <sys/time.h>
#include <sys/resource.h>

static uint64_t uniqID = 0;

extern "C" void kokkosp_init_library(const int loadSeq,
const uint64_t interfaceVer,
const uint32_t devInfoCount,
void* deviceInfo) {

printf("KokkosP: Example Library Initialized (sequence is %d, version: %llu)\n", loadSeq, interfaceVer);
}

extern "C" void kokkosp_finalize_library() {
printf("\n");
printf("KokkosP: Finalization of profiling library.\n");

struct rusage sys_resources;
getrusage(RUSAGE_SELF, &sys_resources);

printf("KokkosP: High water mark memory consumption: %d kB\n",
sys_resources.ru_maxrss);
printf("\n");
}
14 changes: 14 additions & 0 deletions src/tools/simple-kernel-timer/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CXX=g++
CXXFLAGS=-O3 -std=c++11 -g
SHARED_CXXFLAGS=-shared -fPIC

all: kp_kernel_timer.so kp_reader

kp_reader: kp_reader.cpp kp_kernel_timer.so
$(CXX) $(CXXFLAGS) -o kp_reader kp_reader.cpp

kp_kernel_timer.so: kp_kernel_timer.cpp kp_kernel_info.h
$(CXX) $(SHARED_CXXFLAGS) $(CXXFLAGS) -o $@ kp_kernel_timer.cpp

clean:
rm *.so kp_reader
Loading

0 comments on commit b9f8c2f

Please sign in to comment.