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

Allow for randomized sampling #181

Closed
wants to merge 24 commits into from
Closed
Changes from 10 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fc36b9d
Update kp_sampler_skip.cpp
vlkale Apr 21, 2023
a18b328
Merge branch 'develop' into allow-randomized-sampling
vlkale Jun 8, 2023
abafd7a
update to Makefile
vlkale Jun 8, 2023
3bede60
putting in fix to sampler skip with randomized probabilities
vlkale Jun 8, 2023
a0a1bd2
Merging kp_sampler_skip.cpp with fixes
vlkale Jun 8, 2023
7d2302d
committing sampler formatted file
vlkale Jun 8, 2023
5e4f762
putting fixes for formatting into kp_sampler skip
vlkale Jun 8, 2023
6269ab4
fixed kp_sampler making tool_prob_num an int
vlkale Jun 8, 2023
c7c724c
committed formatted sampler_skip.cpp
vlkale Jun 8, 2023
9830c58
fixing randomized samples to obtain correct invocation number.
vlkale Jun 9, 2023
4e5c4a4
Update kp_sampler_skip.cpp
vlkale Jul 20, 2023
d7aa5bc
Applied clang-format-8
Jul 21, 2023
3254951
committing kp sampler with fix to scan
vlkale Jul 31, 2023
f236b87
coommitting delete commented code
vlkale Jul 31, 2023
c05a65f
fix kp sampler skip formatting
vlkale Jul 31, 2023
79ebc63
fix to randomization float conversation and clang-formatting
vlkale Jul 31, 2023
75b23d7
Readme
vlkale Jul 31, 2023
cf105ff
Fix tool glob fence to bool
vlkale Sep 6, 2023
a9cb706
set new defaults of tool prob num and kernelSampleSkip
vlkale Sep 6, 2023
5db6e8c
Error check and handle case when both skip rate and probability set
vlkale Sep 6, 2023
278bfe0
fixing the sampler's minimum skip rate so it is zero in order fix err…
vlkale Sep 7, 2023
687bdd2
fixing the sampler's nestedkID init in parallel reduce
vlkale Sep 7, 2023
b606098
putting numeric limits for kernelSampleSkip back in
vlkale Sep 7, 2023
82b2423
applied clang format
vlkale Sep 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 109 additions & 53 deletions common/kokkos-sampler/kp_sampler_skip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
#include <dlfcn.h>
#include "../../profiling/all/kp_core.hpp"
#include "kp_config.hpp"
#include <ctime>
#include <time.h>
vlkale marked this conversation as resolved.
Show resolved Hide resolved

namespace KokkosTools {
namespace Sampler {
static uint64_t uniqID = 0;
static uint64_t kernelSampleSkip = 101;
static int tool_prob_num = 100;
static int tool_verbosity = 0;
static int tool_globFence = 0;

Expand Down Expand Up @@ -40,6 +43,7 @@ void kokkosp_init_library(const int loadSeq, const uint64_t interfaceVer,
const uint32_t devInfoCount, void* deviceInfo) {
const char* tool_verbose_str = getenv("KOKKOS_TOOLS_SAMPLER_VERBOSE");
const char* tool_globFence_str = getenv("KOKKOS_TOOLS_GLOBALFENCES");

if (NULL != tool_verbose_str) {
tool_verbosity = atoi(tool_verbose_str);
} else {
Expand All @@ -62,7 +66,6 @@ void kokkosp_init_library(const int loadSeq, const uint64_t interfaceVer,
exit(-1);
}
}

char* envBuffer = (char*)malloc(sizeof(char) * (strlen(profileLibrary) + 1));
strcpy(envBuffer, profileLibrary);

Expand Down Expand Up @@ -96,14 +99,12 @@ void kokkosp_init_library(const int loadSeq, const uint64_t interfaceVer,
(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 =
Expand Down Expand Up @@ -136,109 +137,164 @@ void kokkosp_init_library(const int loadSeq, const uint64_t interfaceVer,

uniqID = 1;

const char* tool_sample = getenv("KOKKOS_TOOLS_SAMPLER_SKIP");
const char* tool_sample = getenv("KOKKOS_TOOLS_SAMPLER_SKIP");
const char* tool_probability = getenv("KOKKOS_TOOLS_SAMPLER_PROBABILITY");
// composing the periodicity and probability parameter. Set to 1 if
// probability for each periodic sample. Set to 0 to default to probability
// even if periodicity (skip rate) is defined.

if (NULL != tool_sample) {
kernelSampleSkip = atoi(tool_sample) + 1;
}

if (NULL != tool_probability) {
// read sampling probability as an integer between 1 and 100, but
// programs reasons about probability as a double between 0.0 and 1.0.
tool_prob_num = atoi(tool_probability);
vlkale marked this conversation as resolved.
Show resolved Hide resolved
if (tool_prob_num > 100) {
printf(
"KokkosP: Tool sample probability was set to be greater than 100. "
"Setting to 100.\n");
tool_prob_num = 100;
} else if (tool_prob_num < 0) {
printf(
"KokkosP: Tool sample probability was set to be less than 0. Setting "
"to 0.\n");
tool_prob_num = 0;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If both tool_prob_num < 0 and kernelSampleSkip is max of uint64_t set tool_prob_num to 10.0

Make an error check checking that not. both of them are set

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tool_prob_num default has been assigned to the default requested. The kernelSampleSkip default is part of a new PR which focuses just on the correct matching of sampled kernels.

}
// srand48((unsigned)clock());
// seed48(0);

if (tool_verbosity > 0) {
printf("KokkosP: Sampling rate set to: %s\n", tool_sample);
printf("KokkosP: Sampling probability set to: %s\n", tool_probability);
printf(
"KokkosP: seeding Random Number Generator using clock for "
"probabilistic sampling\n");
}
}
srand(time(NULL));

if ((NULL != tool_probability) && (NULL != tool_sample)) {
printf(
"KokkosP: Note that both probability and skip rate are set. Kokkos "
"Tools Sampler utility will invoke Kokkos Tool child event with a "
"probability at the skip rate.\n");
}
} // end kokkosp_init_library

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

void kokkosp_begin_parallel_for(const char* name, const uint32_t devID,
uint64_t* kID) {
*kID = uniqID++;

if (((*kID) % kernelSampleSkip) == 0) {
if (tool_verbosity > 0) {
printf("KokkosP: sample %llu calling child-begin function...\n",
(unsigned long long)(*kID));
}

if (NULL != beginForCallee) {
(*beginForCallee)(name, devID, kID);
*kID = 0;
static uint64_t invocationNum;
++invocationNum;
if ((invocationNum % kernelSampleSkip) == 0) {
if ((rand() % 100) < tool_prob_num) {
*kID = 1; // set kernel ID to 1 so that it is matched with the end.
if (tool_verbosity > 0) {
printf(
"KokkosP: sample %llu on (a parallel_for on its invocation number "
"%d) calling child-begin function...\n",
(unsigned long long)(*kID), (int)invocationNum);
}
if (NULL != beginForCallee) {
(*beginForCallee)(name, devID, kID);
}
}
}
}
} // kokkosp_begin_parallel_for

void kokkosp_end_parallel_for(const uint64_t kID) {
if ((kID % kernelSampleSkip) == 0) {
if (kID > 0) {
if (tool_verbosity > 0) {
printf("KokkosP: sample %llu calling child-end function...\n",
(unsigned long long)(kID));
printf(
"KokkosP: sample %llu (a parallel_for) calling child-end "
"function...\n",
(unsigned long long)(kID));
}

if (NULL != endForCallee) {
(*endForCallee)(kID);
}
}
}
} // kokkosp_end_parallel_for

void kokkosp_begin_parallel_scan(const char* name, const uint32_t devID,
uint64_t* kID) {
*kID = uniqID++;

if (((*kID) % kernelSampleSkip) == 0) {
if (tool_verbosity > 0) {
printf("KokkosP: sample %llu calling child-begin function...\n",
(unsigned long long)(*kID));
}

if (NULL != beginScanCallee) {
(*beginScanCallee)(name, devID, kID);
*kID = 0;
static uint64_t invocationNum;
++invocationNum;
if ((invocationNum % kernelSampleSkip) == 0) {
if ((rand() % 100) < tool_prob_num) {
*kID = 1; // set kernel ID to 1 so that it is matched with the end.
if (tool_verbosity > 0) {
printf(
"KokkosP: sample %llu (parallel_scan on its invocation num %d) "
"calling child-begin function...\n",
(unsigned long long)(*kID), (int)invocationNum);
}
if (NULL != beginScanCallee) {
(*beginScanCallee)(name, devID, kID);
}
}
}
}
} // kokkosp_begin_parallel_scan

void kokkosp_end_parallel_scan(const uint64_t kID) {
if ((kID % kernelSampleSkip) == 0) {
if (kID > 0) {
if (tool_verbosity > 0) {
printf("KokkosP: sample %llu calling child-end function...\n",
(unsigned long long)(kID));
printf(
"KokkosP: sample %llu (a parallel_scan) calling child-end "
"function...\n",
(unsigned long long)(kID));
}

if (NULL != endScanCallee) {
(*endScanCallee)(kID);
}
}
}
} // kokkosp_end_parallel_scan

void kokkosp_begin_parallel_reduce(const char* name, const uint32_t devID,
uint64_t* kID) {
*kID = uniqID++;

if (((*kID) % kernelSampleSkip) == 0) {
if (tool_verbosity > 0) {
printf("KokkosP: sample %llu calling child-begin function...\n",
(unsigned long long)(*kID));
}

if (NULL != beginReduceCallee) {
(*beginReduceCallee)(name, devID, kID);
*kID = 0;
static uint64_t invocationNum;
++invocationNum;
if ((invocationNum % kernelSampleSkip) == 0) {
if ((rand() % 100) < tool_prob_num) {
if (tool_verbosity > 0) {
printf(
"KokkosP: sample %llu (a parallel_reduce on its invocation number "
"%d) calling child-begin function...\n",
(unsigned long long)(*kID), (int)invocationNum);
}
*kID = 1;
if (NULL != beginReduceCallee) {
(*beginReduceCallee)(name, devID, kID);
}
}
}
}
} // kokkosp_begin_parallel_reduce

void kokkosp_end_parallel_reduce(const uint64_t kID) {
if ((kID % kernelSampleSkip) == 0) {
if (kID > 0) {
if (tool_verbosity > 0) {
printf("KokkosP: sample %llu calling child-end function...\n",
(unsigned long long)(kID));
printf(
"KokkosP: sample %llu (a parallel_reduce) calling child-end "
"function...\n",
(unsigned long long)(kID));
}

if (NULL != endReduceCallee) {
(*endReduceCallee)(kID);
}
}
}
} // kokkosp_end_parallel_reduce

} // namespace Sampler
} // end namespace KokkosTools
} // namespace KokkosTools

extern "C" {

Expand Down