Skip to content

Commit b44c048

Browse files
authored
using prefetch on all architectures (#166)
* changing _mm_prefetch to __builtin_prefetch * removing #ifdef USE_SSE from HNSW * removed unnecessary cast
1 parent 2938646 commit b44c048

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

src/VecSim/algorithms/hnsw/hnsw_batch_iterator.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,18 @@ candidatesMaxHeap HNSW_BatchIterator::scanGraph(candidatesMinHeap &candidates,
106106
this->hnsw_index->get_linklist_at_level(curr_node_id, 0);
107107
unsigned short links_num = this->hnsw_index->getListCount(cur_node_links_header);
108108
auto *node_links = (unsigned int *)(cur_node_links_header + 1);
109-
#ifdef USE_SSE
110-
_mm_prefetch((char *)(visited_list->getElementsTags() + *node_links), _MM_HINT_T0);
111-
_mm_prefetch((char *)(visited_list->getElementsTags() + *node_links + 64), _MM_HINT_T0);
112-
_mm_prefetch(hnsw_index->getDataByInternalId(*node_links), _MM_HINT_T0);
113-
_mm_prefetch(hnsw_index->getDataByInternalId(*(node_links + 1)), _MM_HINT_T0);
114-
#endif
109+
110+
__builtin_prefetch(visited_list->getElementsTags() + *node_links);
111+
__builtin_prefetch(visited_list->getElementsTags() + *node_links + 64);
112+
__builtin_prefetch(hnsw_index->getDataByInternalId(*node_links));
113+
__builtin_prefetch(hnsw_index->getDataByInternalId(*(node_links + 1)));
115114

116115
for (size_t j = 0; j < links_num; j++) {
117116
unsigned int candidate_id = *(node_links + j);
118-
#ifdef USE_SSE
119-
_mm_prefetch((char *)(visited_list->getElementsTags() + *(node_links + j + 1)),
120-
_MM_HINT_T0);
121-
_mm_prefetch(hnsw_index->getDataByInternalId(*(node_links + j + 1)), _MM_HINT_T0);
122-
#endif
117+
118+
__builtin_prefetch(visited_list->getElementsTags() + *(node_links + j + 1));
119+
__builtin_prefetch(hnsw_index->getDataByInternalId(*(node_links + j + 1)));
120+
123121
if (this->hasVisitedNode(candidate_id)) {
124122
continue;
125123
}

src/VecSim/algorithms/hnsw/hnswlib.h

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "VecSim/spaces/L2_space.h"
55
#include "VecSim/spaces/IP_space.h"
66
#include "VecSim/spaces/space_interface.h"
7-
#include "VecSim/spaces/space_includes.h"
87
#include "VecSim/utils/arr_cpp.h"
98
#include "VecSim/memory/vecsim_malloc.h"
109
#include "VecSim/utils/vecsim_stl.h"
@@ -362,21 +361,18 @@ dist_t HierarchicalNSW<dist_t>::processCandidate(tableint curNodeId, const void
362361
linklistsizeint *node_ll = get_linklist_at_level(curNodeId, layer);
363362
size_t links_num = getListCount(node_ll);
364363
auto *node_links = (tableint *)(node_ll + 1);
365-
#ifdef USE_SSE
366-
_mm_prefetch((char *)(visited_nodes_handler->getElementsTags() + *(node_ll + 1)), _MM_HINT_T0);
367-
_mm_prefetch((char *)(visited_nodes_handler->getElementsTags() + *(node_ll + 1) + 64),
368-
_MM_HINT_T0);
369-
_mm_prefetch(getDataByInternalId(*node_links), _MM_HINT_T0);
370-
_mm_prefetch(getDataByInternalId(*(node_links + 1)), _MM_HINT_T0);
371-
#endif
364+
365+
__builtin_prefetch(visited_nodes_handler->getElementsTags() + *(node_ll + 1));
366+
__builtin_prefetch(visited_nodes_handler->getElementsTags() + *(node_ll + 1) + 64);
367+
__builtin_prefetch(getDataByInternalId(*node_links));
368+
__builtin_prefetch(getDataByInternalId(*(node_links + 1)));
372369

373370
for (size_t j = 0; j < links_num; j++) {
374371
tableint candidate_id = *(node_links + j);
375-
#ifdef USE_SSE
376-
_mm_prefetch((char *)(visited_nodes_handler->getElementsTags() + *(node_links + j + 1)),
377-
_MM_HINT_T0);
378-
_mm_prefetch(getDataByInternalId(*(node_links + j + 1)), _MM_HINT_T0);
379-
#endif
372+
373+
__builtin_prefetch(visited_nodes_handler->getElementsTags() + *(node_links + j + 1));
374+
__builtin_prefetch(getDataByInternalId(*(node_links + j + 1)));
375+
380376
if (this->visited_nodes_handler->getNodeTag(candidate_id) == visited_tag)
381377
continue;
382378
this->visited_nodes_handler->tagNode(candidate_id, visited_tag);
@@ -385,9 +381,9 @@ dist_t HierarchicalNSW<dist_t>::processCandidate(tableint curNodeId, const void
385381
dist_t dist1 = fstdistfunc_(data_point, currObj1, dist_func_param_);
386382
if (top_candidates.size() < ef || lowerBound > dist1) {
387383
candidate_set.emplace(-dist1, candidate_id);
388-
#ifdef USE_SSE
389-
_mm_prefetch(getDataByInternalId(candidate_set.top().second), _MM_HINT_T0);
390-
#endif
384+
385+
__builtin_prefetch(getDataByInternalId(candidate_set.top().second));
386+
391387
top_candidates.emplace(dist1, candidate_id);
392388

393389
if (top_candidates.size() > ef)

0 commit comments

Comments
 (0)