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

[GraphBolt] Make unique_and_compact deterministic #7217

Merged
merged 10 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 37 additions & 3 deletions graphbolt/src/concurrent_id_hash_map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ torch::Tensor ConcurrentIdHashMap<IdType>::Init(
// Fill in the first `num_seeds` ids.
torch::parallel_for(0, num_seeds, kGrainSize, [&](int64_t s, int64_t e) {
RamonZhou marked this conversation as resolved.
Show resolved Hide resolved
for (int64_t i = s; i < e; i++) {
InsertAndSet(ids_data[i], static_cast<IdType>(i));
InsertAndSetSmaller(ids_data[i], static_cast<IdType>(i));
mfbalin marked this conversation as resolved.
Show resolved Hide resolved
}
});
// Place the first `num_seeds` ids.
Expand All @@ -83,12 +83,22 @@ torch::Tensor ConcurrentIdHashMap<IdType>::Init(
const int64_t num_threads = torch::get_num_threads();
std::vector<size_t> block_offset(num_threads + 1, 0);
// Insert all elements in this loop.
torch::parallel_for(
num_seeds, num_ids, kGrainSize, [&](int64_t s, int64_t e) {
for (int64_t i = s; i < e; i++) {
InsertAndSetSmaller(ids_data[i], static_cast<IdType>(i));
}
});
RamonZhou marked this conversation as resolved.
Show resolved Hide resolved
RamonZhou marked this conversation as resolved.
Show resolved Hide resolved

// Count the valid numbers in each thread.
torch::parallel_for(
num_seeds, num_ids, kGrainSize, [&](int64_t s, int64_t e) {
size_t count = 0;
for (int64_t i = s; i < e; i++) {
valid[i] = Insert(ids_data[i]);
count += valid[i];
if (MapId(ids_data[i]) == i) {
count++;
valid[i] = 1;
}
}
auto thread_id = torch::get_thread_num();
block_offset[thread_id + 1] = count;
Expand Down Expand Up @@ -199,6 +209,30 @@ inline void ConcurrentIdHashMap<IdType>::InsertAndSet(IdType id, IdType value) {
hash_map_.data_ptr<IdType>()[getValueIndex(pos)] = value;
}

template <typename IdType>
void ConcurrentIdHashMap<IdType>::InsertAndSetSmaller(IdType id, IdType value) {
IdType pos = (id & mask_), delta = 1;
IdType* hash_map_data = hash_map_.data_ptr<IdType>();
InsertState state = AttemptInsertAt(pos, id);
while (state == InsertState::OCCUPIED) {
Next(&pos, &delta);
state = AttemptInsertAt(pos, id);
}

if (state == InsertState::INSERTED) {
hash_map_data[getValueIndex(pos)] = value;
RamonZhou marked this conversation as resolved.
Show resolved Hide resolved
return;
}
RamonZhou marked this conversation as resolved.
Show resolved Hide resolved

IdType val_pos = getValueIndex(pos);
IdType old_val = hash_map_data[val_pos];
mfbalin marked this conversation as resolved.
Show resolved Hide resolved
RamonZhou marked this conversation as resolved.
Show resolved Hide resolved
while (old_val > value) {
RamonZhou marked this conversation as resolved.
Show resolved Hide resolved
old_val = CompareAndSwap(&(hash_map_data[val_pos]), old_val, value);
}

return;
mfbalin marked this conversation as resolved.
Show resolved Hide resolved
}

template <typename IdType>
inline typename ConcurrentIdHashMap<IdType>::InsertState
ConcurrentIdHashMap<IdType>::AttemptInsertAt(int64_t pos, IdType key) {
Expand Down
10 changes: 10 additions & 0 deletions graphbolt/src/concurrent_id_hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ class ConcurrentIdHashMap {
*/
inline void InsertAndSet(IdType key, IdType value);

/**
* @brief Insert a key into the hash map. If the key exists, set the value
* with the smaller value.
*
* @param id The key to be inserted.
* @param value The value to be set for the `key`.
*
*/
inline void InsertAndSetSmaller(IdType id, IdType value);
frozenbugs marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief Attempt to insert the key into the hash map at the given position.
*
Expand Down
Loading