Skip to content

Commit

Permalink
Lab Swmem Prefetch 1: Prefetch next hash
Browse files Browse the repository at this point in the history
  • Loading branch information
simveit committed Dec 20, 2024
1 parent 8acd3df commit 5d6beae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion labs/memory_bound/swmem_prefetch_1/solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ static int getSumOfDigits(int n) {
int solution(const hash_map_t *hash_map, const std::vector<int> &lookups) {
int result = 0;

for (int val : lookups) {
for (int i = 0; i < lookups.size(); i++) {
int val = lookups[i];
if (i < lookups.size() - 1) {
int future_val = lookups[i + 1];
int future_bucket = future_val % hash_map->getNBuckets();
__builtin_prefetch(&((hash_map->getVector())[future_bucket]));
}
if (hash_map->find(val))
result += getSumOfDigits(val);
}
Expand Down
8 changes: 8 additions & 0 deletions labs/memory_bound/swmem_prefetch_1/solution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class hash_map_t {
int bucket = val % N_Buckets;
return m_vector[bucket] != UNUSED;
}

const std::vector<int>& getVector() const {
return m_vector;
}

const std::size_t& getNBuckets() const {
return N_Buckets;
}
};

void init(hash_map_t* hash_map, std::vector<int>& lookups);
Expand Down

0 comments on commit 5d6beae

Please sign in to comment.