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

fix flyweight iterator and enhance test #2105

Merged
merged 1 commit into from
Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions src/include/souffle/datastructure/ConcurrentFlyweight.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ class ConcurrentFlyweight {
NextMaybeUnassignedSlot = END;
for (lane_id I = 0; I < This->Lanes.lanes(); ++I) {
const auto Lane = This->Lanes.guard(I);
if (This->Handles[I].NextSlot > Slot && This->Handles[I].NextSlot < NextMaybeUnassignedSlot) {
if ((Slot == NONE || This->Handles[I].NextSlot > Slot) &&
This->Handles[I].NextSlot < NextMaybeUnassignedSlot) {
NextMaybeUnassignedSlot = This->Handles[I].NextSlot;
NextMaybeUnassignedHandle = I;
}
Expand All @@ -188,6 +189,7 @@ class ConcurrentFlyweight {
bool MoveToNextAssignedSlot() {
static_assert(NONE == std::numeric_limits<slot_type>::max(),
"required for wrap around to 0 for begin-iterator-scan");
static_assert(NONE + 1 == 0, "required for wrap around to 0 for begin-iterator-scan");
while (Slot != END) {
assert(Slot + 1 < SLOT_MAX);
if (Slot + 1 < NextMaybeUnassignedSlot) { // next unassigned slot not reached
Expand All @@ -206,9 +208,7 @@ class ConcurrentFlyweight {
This->Lanes.lock(NextMaybeUnassignedHandle);
const bool IsAssigned = (Slot + 1 < This->Handles[NextMaybeUnassignedHandle].NextSlot);
This->Lanes.unlock(NextMaybeUnassignedHandle);
if (IsAssigned) {
Slot = Slot + 1;
}
Slot = Slot + 1;
FindNextMaybeUnassignedSlot();
if (IsAssigned) {
return true;
Expand Down
81 changes: 47 additions & 34 deletions src/tests/flyweight_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

// How many times to repeat each randomized test
#define RANDOM_TESTS 32
#define RANDOM_TEST_SIZE 32
#define RANDOM_TEST_SIZE 64

// For some reason, just using `assert` doesn't work, even if cassert is
// included.
Expand All @@ -53,7 +53,7 @@ static char random_char() {
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
static constexpr size_t max_index = sizeof(charset) - 1;
static constexpr size_t max_index = sizeof(charset) - 2;
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<std::size_t> dist(0, max_index);
Expand Down Expand Up @@ -111,30 +111,37 @@ static std::vector<std::pair<FlyweightImpl<std::string>::index_type, std::string
values.emplace_back(key, s);
};
#ifdef _OPENMP
#pragma omp parallel for
#pragma omp parallel
{
srand(omp_get_thread_num());
#pragma omp for
#endif
for (std::size_t i = 0; i < max_size; ++i) {
if (random() % 2) {
add_new();
} else {
if (random() % 2 && values.size() > 0) {
FlyweightImpl<std::string>::index_type key;
std::string val;
{
const std::lock_guard<std::mutex> lock(mu);
std::tie(key, val) = values[random() % values.size()];
for (std::size_t i = 0; i < max_size; ++i) {
if (rand() % 2) {
add_new();
} else {
if (rand() % 2 && values.size() > 0) {
FlyweightImpl<std::string>::index_type key;
std::string val;
{
const std::lock_guard<std::mutex> lock(mu);
std::tie(key, val) = values[rand() % values.size()];
}
LOCAL_ASSERT_EQ(val, flyweight.fetch(key));
} else if (values.size() > 0) {
std::string val;
{
const std::lock_guard<std::mutex> lock(mu);
val = values[rand() % values.size()].second;
}
LOCAL_ASSERT_TRUE(flyweight.weakContains(val));
}
LOCAL_ASSERT_EQ(val, flyweight.fetch(key));
} else if (values.size() > 0) {
std::string val;
{
const std::lock_guard<std::mutex> lock(mu);
val = values[random() % values.size()].second;
}
LOCAL_ASSERT_TRUE(flyweight.weakContains(val));
}
}
#ifdef _OPENMP
}
#endif

while (values.size() < min_size) {
add_new();
}
Expand All @@ -144,23 +151,29 @@ static std::vector<std::pair<FlyweightImpl<std::string>::index_type, std::string

TEST(Flyweight, FindOrInsertCopy) {
for (int i = 0; i < RANDOM_TESTS; ++i) {
FlyweightImpl<std::string> flyweight{1};
FlyweightImpl<std::string> flyweight{8};
random_flyweight(flyweight, 0, RANDOM_TEST_SIZE);
#ifdef _OPENMP
#pragma omp parallel for
#pragma omp parallel
{
srand(omp_get_thread_num());
#pragma omp for
#endif
for (int j = 0; j < RANDOM_TEST_SIZE; ++j) {
// Guarantee uniqueness by appending something not in the character set
// and then the index.
std::string s = random_string() + "~" + std::to_string(j);
FlyweightImpl<std::string>::index_type data1, data2;
bool was_new1, was_new2;
std::tie(data1, was_new1) = flyweight.findOrInsert(s);
std::tie(data2, was_new2) = flyweight.findOrInsert(std::string(s));
EXPECT_EQ(data1, data2);
EXPECT_TRUE(was_new1);
EXPECT_FALSE(was_new2);
for (int j = 0; j < RANDOM_TEST_SIZE; ++j) {
// Guarantee uniqueness by appending something not in the character set
// and then the index.
std::string s = random_string() + "~" + std::to_string(j);
FlyweightImpl<std::string>::index_type data1, data2;
bool was_new1, was_new2;
std::tie(data1, was_new1) = flyweight.findOrInsert(s);
std::tie(data2, was_new2) = flyweight.findOrInsert(std::string(s));
EXPECT_EQ(data1, data2);
EXPECT_TRUE(was_new1);
EXPECT_FALSE(was_new2);
}
#ifdef _OPENMP
}
#endif
}
}

Expand Down