Skip to content

Commit

Permalink
Merge pull request #9 from masterleinad/use-probability-sampling
Browse files Browse the repository at this point in the history
Fix kokkos#213 - Merge printf conversion to cout in sampler for the sake of Gtest.  We may want to provide an option of std::cout or printf for the user in the future. I am keeping this as is for now.
  • Loading branch information
vlkale authored Mar 28, 2024
2 parents 44f1eb7 + 7f0cd8a commit d1b2036
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 163 deletions.
219 changes: 93 additions & 126 deletions common/kokkos-sampler/kp_sampler_skip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "kp_config.hpp"
#include <ctime>
#include <limits>
#include <iostream>

namespace KokkosTools {
namespace Sampler {
Expand Down Expand Up @@ -53,32 +54,28 @@ uint32_t getDeviceID(uint32_t devid_in) {
void invoke_ktools_fence(uint32_t devID) {
if (tpi_funcs.fence != nullptr) {
if (tool_verbosity > 1) {
printf(
"KokkosP: Sampler attempting to invoke"
" tool-induced fence on device %d.\n",
getDeviceID(devID));
std::cout << "KokkosP: Sampler attempting to invoke tool-induced fence "
"on device "
<< getDeviceID(devID) << '\n';
}
(*(tpi_funcs.fence))(devID);
if (tool_verbosity > 1) {
printf(
"KokkosP: Sampler sucessfully invoked"
" tool-induced fence on device %d.\n",
getDeviceID(devID));
std::cout << "KokkosP: Sampler sucessfully invoked tool-induced fence on "
"device "
<< getDeviceID(devID) << '\n';
}
} else {
printf(
"KokkosP: FATAL: Kokkos Tools Programming Interface's tool-invoked "
"Fence is NULL!\n");
std::cout << "KokkosP: FATAL: Kokkos Tools Programming Interface's "
"tool-invoked Fence is NULL!\n";
}
}

void kokkosp_provide_tool_programming_interface(
uint32_t num_funcs, Kokkos_Tools_ToolProgrammingInterface funcsFromTPI) {
if (!num_funcs) {
if (tool_verbosity > 0)
printf(
"KokkosP: Note: Number of functions in Tools Programming Interface "
"is 0!\n");
std::cout << "KokkosP: Note: Number of functions in Tools Programming "
"Interface is 0!\n";
}
tpi_funcs = funcsFromTPI;
}
Expand All @@ -104,12 +101,11 @@ void kokkosp_init_library(const int loadSeq, const uint64_t interfaceVer,

char* profileLibrary = getenv("KOKKOS_TOOLS_LIBS");
if (NULL == profileLibrary) {
printf(
"Checking KOKKOS_PROFILE_LIBRARY. WARNING: This is a deprecated "
"variable. Please use KOKKOS_TOOLS_LIBS\n");
std::cout << "Checking KOKKOS_PROFILE_LIBRARY. WARNING: This is a "
"deprecated variable. Please use KOKKOS_TOOLS_LIBS\n";
profileLibrary = getenv("KOKKOS_PROFILE_LIBRARY");
if (NULL == profileLibrary) {
printf("KokkosP: No library to call in %s\n", profileLibrary);
std::cout << "KokkosP: No library to call in " << profileLibrary << '\n';
exit(-1);
}
}
Expand All @@ -126,19 +122,20 @@ void kokkosp_init_library(const int loadSeq, const uint64_t interfaceVer,
nextLibrary = strtok(NULL, ";");

if (NULL == nextLibrary) {
printf("KokkosP: No child library to call in %s\n", profileLibrary);
std::cout << "KokkosP: No child library to call in " << profileLibrary
<< '\n';
exit(-1);
} else {
if (tool_verbosity > 0) {
printf("KokkosP: Next library to call: %s\n", nextLibrary);
printf("KokkosP: Loading child library ..\n");
std::cout << "KokkosP: Next library to call: " << nextLibrary << '\n';
std::cout << "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());
std::cerr << "KokkosP: Error: Unable to load: " << nextLibrary
<< " (Error=" << dlerror() << ")\n";
exit(-1);
} else {
beginForCallee =
Expand Down Expand Up @@ -166,19 +163,19 @@ void kokkosp_init_library(const int loadSeq, const uint64_t interfaceVer,
}

if (tool_verbosity > 0) {
printf("KokkosP: Function Status:\n");
printf("KokkosP: begin-parallel-for: %s\n",
(beginForCallee == NULL) ? "no" : "yes");
printf("KokkosP: begin-parallel-scan: %s\n",
(beginScanCallee == NULL) ? "no" : "yes");
printf("KokkosP: begin-parallel-reduce: %s\n",
(beginReduceCallee == NULL) ? "no" : "yes");
printf("KokkosP: end-parallel-for: %s\n",
(endForCallee == NULL) ? "no" : "yes");
printf("KokkosP: end-parallel-scan: %s\n",
(endScanCallee == NULL) ? "no" : "yes");
printf("KokkosP: end-parallel-reduce: %s\n",
(endReduceCallee == NULL) ? "no" : "yes");
std::cout << "KokkosP: Function Status:\n";
std::cout << "KokkosP: begin-parallel-for: "
<< ((beginForCallee == NULL) ? "no" : "yes") << '\n';
std::cout << "KokkosP: begin-parallel-scan: "
<< ((beginScanCallee == NULL) ? "no" : "yes") << '\n';
std::cout << "KokkosP: begin-parallel-reduce: "
<< ((beginReduceCallee == NULL) ? "no" : "yes") << '\n';
std::cout << "KokkosP: end-parallel-for: "
<< ((endForCallee == NULL) ? "no" : "yes") << '\n';
std::cout << "KokkosP: end-parallel-scan: "
<< ((endScanCallee == NULL) ? "no" : "yes") << '\n';
std::cout << "KokkosP: end-parallel-reduce: "
<< ((endReduceCallee == NULL) ? "no" : "yes") << '\n';
}
}
}
Expand All @@ -187,14 +184,20 @@ void kokkosp_init_library(const int loadSeq, const uint64_t interfaceVer,

uniqID = 1;

const char* tool_sample = getenv("KOKKOS_TOOLS_SAMPLER_SKIP");
if (NULL != tool_sample) {
kernelSampleSkip = atoi(tool_sample) + 1;
if (0 > tool_seed) {
srand(time(NULL));
if (tool_verbosity > 0) {
std::cout << "KokkosP: Seeding random number generator using clock for "
"random sampling.\n";
}
} else {
srand(tool_seed);
if (tool_verbosity > 0) {
std::cout << "KokkosP: Seeding random number generator using seed "
<< tool_seed << " for random sampling.\n";
}
}

if (tool_verbosity > 0) {
printf("KokkosP: Sampling rate set to: %s\n", tool_sample);
}
const char* tool_probability = getenv("KOKKOS_TOOLS_SAMPLER_PROB");

if (NULL != tool_probability) {
Expand All @@ -203,85 +206,49 @@ void kokkosp_init_library(const int loadSeq, const uint64_t interfaceVer,
// Connector reasons about probability as a double between 0.0 and 1.0.
tool_prob_num = atof(tool_probability);
if (tool_prob_num > 100.0) {
printf(
"KokkosP: The sampling probability value is set to be greater than "
"100.0. "
"The probability for the sampler will be set to 100 percent; all of "
"the "
"invocations of a Kokkos kernel will be profiled.\n");
std::cout << "KokkosP: The sampling probability value is set to be "
"greater than 100.0. The probability for the sampler will "
"be set to 100 percent; all of the invocations of a Kokkos "
"kernel will be profiled.\n";
tool_prob_num = 100.0;
} else if (tool_prob_num < 0.0) {
printf(
"KokkosP: The sampling probability value is set to be a negative "
"number. The "
"sampler's probability will be set to 0 percent; none of the "
"invocations of "
"a Kokkos kernel will be profiled.\n");
std::cout
<< "KokkosP: The sampling probability value is set to be a negative "
"number. The sampler's probability will be set to 0 percent; none "
"of the invocations of a Kokkos kernel will be profiled.\n";
tool_prob_num = 0.0;
}
}
if ((tool_prob_num < 0.0) &&
(kernelSampleSkip == std::numeric_limits<uint64_t>::max())) {
if (tool_verbosity > 0) {
printf(
"KokkosP: Neither the probability "
"nor the skip rate for sampling were set...\n");
}
tool_prob_num = 10.0;
if (tool_verbosity > 0) {
printf(
"KokkosP: The probability "
"for the sampler is set to the default of %f percent. The skip rate "
"for sampler"
"will not be used.\n",
tool_prob_num);
}
}

if (tool_verbosity > 0) {
if (tool_verbosity > 1) {
printf("KokkosP: Sampling skip rate provided as input is: %s\n",
tool_sample);
printf("KokkosP: Sampling probability provided as input is: %s\n",
tool_probability);
std::cout << "KokkosP: Probability for the sampler set to: "
<< tool_prob_num << '\n';
}
printf("KokkosP: Sampling skip rate is set to: %llu\n",
(unsigned long long)(kernelSampleSkip));
printf("KokkosP: Sampling probability is set to %f\n", tool_prob_num);
kernelSampleSkip = 1;
return;
}

if (0 > tool_seed) {
srand(time(NULL));
if (tool_verbosity > 0) {
printf(
"KokkosP: Seeding random number generator using clock for "
"random sampling.\n");
}
} else {
srand(tool_seed);
const char* tool_sample = getenv("KOKKOS_TOOLS_SAMPLER_SKIP");
if (NULL != tool_sample) {
tool_prob_num = 100.0;
kernelSampleSkip = atoi(tool_sample) + 1;
if (tool_verbosity > 0) {
printf(
"KokkosP: Seeding random number generator using seed %u for "
"random sampling.\n",
tool_seed);
std::cout << "KokkosP: Sampling rate set to: " << tool_sample << '\n';
}
return;
}

if ((NULL != tool_probability) && (NULL != tool_sample)) {
printf(
"KokkosP: You set both the probability and skip rate for the sampler. "
"Only random sampling "
"will be done, using the probabability you set; "
"The skip rate you set will be ignored.\n");

if (tool_verbosity > 1) {
printf(
"KokkosP: Note: The skip rate will be set to 1. Sampling will not be "
"based "
" on a pre-defined periodicity.\n");
}
kernelSampleSkip = 1;
if (tool_verbosity > 0) {
std::cout << "KokkosP: Neither the probability nor the skip rate for "
"sampling were set...\n";
}
tool_prob_num = 10.0;
kernelSampleSkip = 1;
if (tool_verbosity > 0) {
std::cout
<< "KokkosP: The probability for the sampler is set to the default of "
<< tool_prob_num
<< " percent. The skip rate for sampler will not be used.\n";
}
kernelSampleSkip = 1;
}

void kokkosp_finalize_library() {
Expand All @@ -296,8 +263,8 @@ void kokkosp_begin_parallel_for(const char* name, const uint32_t devID,
if ((invocationNum % kernelSampleSkip) == 0) {
if ((rand() / (1.0 * RAND_MAX)) < (tool_prob_num / 100.0)) {
if (tool_verbosity > 0) {
printf("KokkosP: sample %llu calling child-begin function...\n",
(unsigned long long)(*kID));
std::cout << "KokkosP: sample " << *kID
<< " calling child-begin function...\n";
}
if (NULL != beginForCallee) {
if (tool_globFence) {
Expand All @@ -306,8 +273,8 @@ void kokkosp_begin_parallel_for(const char* name, const uint32_t devID,
uint64_t nestedkID = 0;
(*beginForCallee)(name, devID, &nestedkID);
if (tool_verbosity > 0) {
printf("KokkosP: sample %llu finished with child-begin function.\n",
(unsigned long long)(*kID));
std::cout << "KokkosP: sample " << *kID
<< " finished with child-begin function.\n";
}
infokIDSample.insert({*kID, nestedkID});
}
Expand All @@ -320,8 +287,8 @@ void kokkosp_end_parallel_for(const uint64_t kID) {
if (!(infokIDSample.find(kID) == infokIDSample.end())) {
uint64_t retrievedNestedkID = infokIDSample[kID];
if (tool_verbosity > 0) {
printf("KokkosP: sample %llu calling child-end function...\n",
(unsigned long long)(kID));
std::cout << "KokkosP: sample " << kID
<< " calling child-end function...\n";
}
if (tool_globFence) {
invoke_ktools_fence(0);
Expand All @@ -340,8 +307,8 @@ void kokkosp_begin_parallel_scan(const char* name, const uint32_t devID,
if ((invocationNum % kernelSampleSkip) == 0) {
if ((rand() / (1.0 * RAND_MAX)) < (tool_prob_num / 100.0)) {
if (tool_verbosity > 0) {
printf("KokkosP: sample %llu calling child-begin function...\n",
(unsigned long long)(*kID));
std::cout << "KokkosP: sample " << *kID
<< " calling child-begin function...\n";
}
if (NULL != beginScanCallee) {
uint64_t nestedkID = 0;
Expand All @@ -350,8 +317,8 @@ void kokkosp_begin_parallel_scan(const char* name, const uint32_t devID,
}
(*beginScanCallee)(name, devID, &nestedkID);
if (tool_verbosity > 0) {
printf("KokkosP: sample %llu finished with child-begin function.\n",
(unsigned long long)(*kID));
std::cout << "KokkosP: sample " << *kID
<< " finished with child-begin function.\n";
}
infokIDSample.insert({*kID, nestedkID});
}
Expand All @@ -364,8 +331,8 @@ void kokkosp_end_parallel_scan(const uint64_t kID) {
if (!(infokIDSample.find(kID) == infokIDSample.end())) {
uint64_t retrievedNestedkID = infokIDSample[kID];
if (tool_verbosity > 0) {
printf("KokkosP: sample %llu calling child-end function...\n",
(unsigned long long)(kID));
std::cout << "KokkosP: sample " << kID
<< " calling child-end function...\n";
}
if (tool_globFence) {
invoke_ktools_fence(0);
Expand All @@ -384,8 +351,8 @@ void kokkosp_begin_parallel_reduce(const char* name, const uint32_t devID,
if ((invocationNum % kernelSampleSkip) == 0) {
if ((rand() / (1.0 * RAND_MAX)) < (tool_prob_num / 100.0)) {
if (tool_verbosity > 0) {
printf("KokkosP: sample %llu calling child-begin function...\n",
(unsigned long long)(*kID));
std::cout << "KokkosP: sample " << *kID
<< " calling child-begin function...\n";
}
if (NULL != beginReduceCallee) {
uint64_t nestedkID = 0;
Expand All @@ -394,8 +361,8 @@ void kokkosp_begin_parallel_reduce(const char* name, const uint32_t devID,
}
(*beginReduceCallee)(name, devID, &nestedkID);
if (tool_verbosity > 0) {
printf("KokkosP: sample %llu finished with child-begin function.\n",
(unsigned long long)(*kID));
std::cout << "KokkosP: sample " << *kID
<< " finished with child-begin function.\n";
}
infokIDSample.insert({*kID, nestedkID});
}
Expand All @@ -408,8 +375,8 @@ void kokkosp_end_parallel_reduce(const uint64_t kID) {
if (!(infokIDSample.find(kID) == infokIDSample.end())) {
uint64_t retrievedNestedkID = infokIDSample[kID];
if (tool_verbosity > 0) {
printf("KokkosP: sample %llu calling child-end function...\n",
(unsigned long long)(kID));
std::cout << "KokkosP: sample " << kID
<< " calling child-end function...\n";
}
if (tool_globFence) {
invoke_ktools_fence(0);
Expand Down
Loading

0 comments on commit d1b2036

Please sign in to comment.