Skip to content

Commit

Permalink
abstract away container
Browse files Browse the repository at this point in the history
  • Loading branch information
dalg24 committed Aug 5, 2024
1 parent 523eafc commit 38ec5a6
Showing 1 changed file with 58 additions and 46 deletions.
104 changes: 58 additions & 46 deletions debugging/vov-bug-finder/kp_view_of_views_bug_finder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,41 @@

#include <kp_core.hpp>

#include <atomic>
#include <cassert>
#include <cstdint>
#include <exception>
#include <cstdlib>
#include <iostream>
#include <map>
#include <mutex>
#include <optional>
#include <set>
#include <string>
#include <vector>

namespace {

bool verbose = false;
bool verbose = false;
bool abort_on_error = false;

std::atomic<uint64_t> events_count;
size_t const n = 128;
std::vector<std::string> events_description(n);
std::string get_event(uint64_t i) { return events_description[i % n]; }
void set_event(uint64_t i, std::string lbl) {
events_description[i % n] = std::move(lbl);
}
class {
uint64_t count_;
std::map<uint64_t, std::string> map_;

std::mutex current_mutex;
std::set<uint64_t> current;
public:
std::mutex mutex;
uint64_t push(std::string s) {
map_.emplace_hint(map_.end(), count_, std::move(s));
return count_++;
}
void pop(uint64_t x) {
auto it = map_.find(x);
assert(it != map_.end());
map_.erase(it);
}
std::string const &top() {
assert(!map_.empty());
return map_.begin()->second;
}
bool is_empty() noexcept { return map_.empty(); }
} current;

std::optional<std::string> get_substr(std::string const &str,
std::string_view prefix,
Expand All @@ -61,19 +72,18 @@ extern "C" void kokkosp_request_tool_settings(
extern "C" void kokkosp_begin_parallel_for(char const *kernelName,
uint32_t deviceID,
uint64_t *kernelID) {
*kernelID = events_count++;
set_event(*kernelID, kernelName);

std::lock_guard lock(current_mutex);
if (!current.empty()) {
std::lock_guard lock(current.mutex);
if (!current.is_empty()) {
if (auto lbl =
get_substr(kernelName, "Kokkos::View::initialization [", "]")) {
std::cerr << "constructing view \"" << *lbl
<< "\" within a parallel region \""
<< get_event(*current.begin()) << "\"\n";
<< "\" within a parallel region \"" << current.top() << "\"\n";
if (abort_on_error) {
std::abort();
}
}
}
current.emplace(*kernelID);
*kernelID = current.push(kernelName);

if (verbose) {
std::cout << "begin kernel " << *kernelID << " " << kernelName
Expand All @@ -82,13 +92,8 @@ extern "C" void kokkosp_begin_parallel_for(char const *kernelName,
}

extern "C" void kokkosp_end_parallel_for(uint64_t kernelID) {
std::lock_guard lock(current_mutex);
auto it = current.find(kernelID);
if (it == current.end()) {
throw std::runtime_error(
"Bug: attempting to end a parallel_for that is not \"running\"");
}
current.erase(it);
std::lock_guard lock(current.mutex);
current.pop(kernelID);

if (verbose) {
std::cout << "end kernel " << kernelID << '\n';
Expand All @@ -97,20 +102,21 @@ extern "C" void kokkosp_end_parallel_for(uint64_t kernelID) {

extern "C" void kokkosp_begin_fence(char const *fenceName, uint32_t deviceID,
uint64_t *fenceID) {
*fenceID = events_count++;
set_event(*fenceID, fenceName);

std::lock_guard lock(current_mutex);
if (!current.empty() && (get_event(*fenceID) != "Foo") &&
(get_event(*fenceID) !=
std::lock_guard lock(current.mutex);
if (!current.is_empty() &&
(std::string(fenceName) !=
"Kokkos::Impl::ViewValueFunctor: View init/destroy fence")) {
if (auto lbl = get_substr(get_event(*current.begin()),
"Kokkos::View::destruction [", "]")) {
if (auto lbl =
get_substr(current.top(), "Kokkos::View::destruction [", "]")) {
std::cerr << "view of views \"" << *lbl
<< "\" not properly cleared this fence labelled \"" << fenceName
<< "\" will hang\n";
if (abort_on_error) {
std::abort();
}
}
}
*fenceID = -1;

if (verbose) {
std::cout << "begin fence " << *fenceID << " " << fenceName << " on device "
Expand All @@ -126,10 +132,13 @@ extern "C" void kokkosp_end_fence(uint64_t fenceID) {

extern "C" void kokkosp_allocate_data(SpaceHandle handle, const char *name,
void *ptr, uint64_t size) {
std::lock_guard lock(current_mutex);
if (!current.empty()) {
std::lock_guard lock(current.mutex);
if (!current.is_empty()) {
std::cerr << "allocating \"" << name << "\" within parallel region \""
<< get_event(*current.begin()) << "\"\n";
<< current.top() << "\"\n";
if (abort_on_error) {
std::abort();
}
}

if (verbose) {
Expand All @@ -140,14 +149,17 @@ extern "C" void kokkosp_allocate_data(SpaceHandle handle, const char *name,

extern "C" void kokkosp_deallocate_data(SpaceHandle handle, const char *name,
void *ptr, uint64_t size) {
std::lock_guard lock(current.mutex);
if (!current.is_empty()) {
std::cerr << "deallocating \"" << name << "\" within parallel region \""
<< current.top() << "\"\n";
if (abort_on_error) {
std::abort();
}
}

if (verbose) {
std::cout << "dealloc (" << handle.name << ") " << name << " pointer "
<< ptr << "size " << size << '\n';
}

std::lock_guard lock(current_mutex);
if (!current.empty()) {
std::cerr << "deallocating \"" << name << "\" within parallel region \""
<< get_event(*current.begin()) << "\"\n";
}
}

0 comments on commit 38ec5a6

Please sign in to comment.