Skip to content

Commit

Permalink
Fixing the crash for large M
Browse files Browse the repository at this point in the history
  • Loading branch information
searchivairus committed Jan 7, 2018
1 parent 8f3d178 commit aa6816c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion python_bindings/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import setuptools

__version__ = '1.6.3'
__version__ = '1.7'

libdir = os.path.join(".", "nmslib", "similarity_search")
library_file = os.path.join(libdir, "release", "libNonMetricSpaceLib.a")
Expand Down
11 changes: 10 additions & 1 deletion similarity_search/src/method/hnsw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ namespace similarity {

size_t total_memory_allocated = (memoryPerObject_ * ElList_.size());
data_level0_memory_ = (char *)malloc(memoryPerObject_ * ElList_.size());
CHECK(data_level0_memory_);

offsetLevel0_ = dataSectionSize;
offsetData_ = 0;
Expand Down Expand Up @@ -414,6 +415,7 @@ namespace similarity {
/////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
linkLists_ = (char **)malloc(sizeof(void *) * ElList_.size());
CHECK(linkLists_);
for (long i = 0; i < ElList_.size(); i++) {
if (ElList_[i]->level < 1) {
linkLists_[i] = nullptr;
Expand All @@ -423,6 +425,7 @@ namespace similarity {
SIZEMASS_TYPE sizemass = ((ElList_[i]->level) * (maxM_ + 1)) * sizeof(int);
total_memory_allocated += sizemass;
char *linkList = (char *)malloc(sizemass);
CHECK(linkList);
linkLists_[i] = linkList;
ElList_[i]->copyHigherLevelLinksToOptIndex(linkList, 0);
};
Expand Down Expand Up @@ -1016,8 +1019,10 @@ namespace similarity {
LOG(LIB_INFO) << "Total: " << totalElementsStored_ << ", Memory per object: " << memoryPerObject_;
size_t data_plus_links0_size = memoryPerObject_ * totalElementsStored_;
data_level0_memory_ = (char *)malloc(data_plus_links0_size);
CHECK(data_level0_memory_);
input.read(data_level0_memory_, data_plus_links0_size);
linkLists_ = (char **)malloc(sizeof(void *) * totalElementsStored_);
CHECK(linkLists_);

data_rearranged_.resize(totalElementsStored_);

Expand All @@ -1029,6 +1034,7 @@ namespace similarity {
linkLists_[i] = nullptr;
} else {
linkLists_[i] = (char *)malloc(linkListSize);
CHECK(linkLists_[i]);
input.read(linkLists_[i], linkListSize);
}
data_rearranged_[i] = new Object(data_level0_memory_ + (i)*memoryPerObject_ + offsetData_);
Expand Down Expand Up @@ -1179,7 +1185,7 @@ namespace similarity {

typedef typename SortArrBI<dist_t, HnswNode *>::Item QueueItem;
vector<QueueItem> &queueData = sortedArr.get_data();
vector<QueueItem> itemBuff(16 * M_);
vector<QueueItem> itemBuff(1 + max(M_, max(maxM_, maxM0_)));

massVisited[curNode->getId()] = currentV;
// visitedQueue.insert(curNode->getId());
Expand Down Expand Up @@ -1217,6 +1223,9 @@ namespace similarity {
d = query->DistanceObjLeft(currObj);

if (d < topKey || sortedArr.size() < ef_) {
CHECK_MSG(itemBuff.size() > itemQty,
"Perhaps a bug: buffer size is not enough " +
ConvertToString(itemQty) + " >= " + ConvertToString(itemBuff.size()));
itemBuff[itemQty++] = QueueItem(d, *iter);
}
}
Expand Down
10 changes: 8 additions & 2 deletions similarity_search/src/method/hnsw_distfunc_opt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ namespace similarity {

typedef typename SortArrBI<dist_t, int>::Item QueueItem;
vector<QueueItem> &queueData = sortedArr.get_data();
vector<QueueItem> itemBuff(8 * 30);
vector<QueueItem> itemBuff(1 + max(M_, max(maxM_, maxM0_)));

massVisited[curNodeNum] = currentV;

Expand Down Expand Up @@ -566,6 +566,9 @@ namespace similarity {
dist_t d = (fstdistfunc_(pVectq, (float *)(currObj1 + 16), qty, TmpRes));

if (d < topKey || sortedArr.size() < ef_) {
CHECK_MSG(itemBuff.size() > itemQty,
"Perhaps a bug: buffer size is not enough " +
ConvertToString(itemQty) + " >= " + ConvertToString(itemBuff.size()));
itemBuff[itemQty++] = QueueItem(d, tnum);
}
}
Expand Down Expand Up @@ -788,7 +791,7 @@ namespace similarity {

typedef typename SortArrBI<dist_t, int>::Item QueueItem;
vector<QueueItem> &queueData = sortedArr.get_data();
vector<QueueItem> itemBuff(8 * 30);
vector<QueueItem> itemBuff(1 + max(M_, max(maxM_, maxM0_)));

massVisited[curNodeNum] = currentV;

Expand Down Expand Up @@ -822,6 +825,9 @@ namespace similarity {
dist_t d = (ScalarProductSIMD(pVectq, (float *)(currObj1 + 16), qty, TmpRes));

if (d < topKey || sortedArr.size() < ef_) {
CHECK_MSG(itemBuff.size() > itemQty,
"Perhaps a bug: buffer size is not enough " +
ConvertToString(itemQty) + " >= " + ConvertToString(itemBuff.size()));
itemBuff[itemQty++] = QueueItem(d, tnum);
}
}
Expand Down

0 comments on commit aa6816c

Please sign in to comment.