Skip to content
Open
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
16 changes: 16 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <atomic>
#include <thread>
#include <array>
#include <unordered_set>

#ifndef OMISSION_LARGE_BIOMES
#define OMISSION_LARGE_BIOMES 0
Expand Down Expand Up @@ -42,13 +43,28 @@ struct CpuOutput {
int32_t score;
};

inline bool operator==(const CpuOutput &a, const CpuOutput &b) {
return a.seed == b.seed && a.x == b.x && a.z == b.z && a.score == b.score;
}

struct CpuOutputHash {
size_t operator()(const CpuOutput &output) const {
size_t h = std::hash<uint64_t>{}(output.seed);
h ^= std::hash<int32_t>{}(output.x) + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2);
h ^= std::hash<int32_t>{}(output.z) + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2);
h ^= std::hash<int32_t>{}(output.score) + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2);
return h;
}
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should probably specialize std::hash<CpuOutput> instead of writing a custom hasher struct.


struct GpuOutputs {
std::queue<GpuOutput> queue;
std::mutex mutex;
};

struct CpuOutputs {
std::queue<CpuOutput> queue;
std::unordered_set<CpuOutput, CpuOutputHash> seen;
std::mutex mutex;
};

Expand Down
4 changes: 3 additions & 1 deletion src/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ void CpuThread::run() {

{
std::lock_guard lock(outputs.mutex);
outputs.queue.push(output.value());
if (outputs.seen.insert(output.value()).second) {
outputs.queue.push(output.value());
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/gpu.cu
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,15 @@ void GpuThread::run() {
h_buffer.resize(len);
TRY_CUDA(cudaMemcpy(h_buffer.data(), final_outputs.data, sizeof(*h_buffer.data()) * len, cudaMemcpyDeviceToHost));

std::sort(h_buffer.begin(), h_buffer.end(), [](const SeedPos &a, const SeedPos &b) {
if (a.seed_index != b.seed_index) return a.seed_index < b.seed_index;
if (a.x != b.x) return a.x < b.x;
return a.z < b.z;
});
h_buffer.erase(std::unique(h_buffer.begin(), h_buffer.end(), [](const SeedPos &a, const SeedPos &b) {
return a.seed_index == b.seed_index && a.x == b.x && a.z == b.z;
}), h_buffer.end());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should probably overload the comparison operators instead of using a lambda. There is a nice way to implement these by wrapping the members in a temporary tuple.


{
std::lock_guard lock(outputs.mutex);
for (const auto &result : h_buffer) {
Expand Down
Loading