fix hnsw code format (#1987)

Signed-off-by: Zhonglei Song <szl_84@sina.com>
pull/1979/head
smiling849 2020-04-19 12:00:45 +08:00 committed by GitHub
parent e8f759c22b
commit 50ddc49332
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 562 additions and 581 deletions

View File

@ -5,8 +5,9 @@
#include <algorithm>
namespace hnswlib {
template<typename dist_t>
class BruteforceSearch : public AlgorithmInterface<dist_t> {
template<typename dist_t>
class BruteforceSearch : public AlgorithmInterface<dist_t> {
public:
BruteforceSearch(SpaceInterface <dist_t> *s) {
@ -83,10 +84,8 @@ namespace hnswlib {
data_ + size_per_element_ * (cur_element_count-1),
data_size_+sizeof(labeltype));
cur_element_count--;
}
std::priority_queue<std::pair<dist_t, labeltype >>
searchKnn(const void *query_data, size_t k) const {
std::priority_queue<std::pair<dist_t, labeltype >> topResults;
@ -106,7 +105,6 @@ namespace hnswlib {
topResults.pop();
lastdist = topResults.top().first;
}
}
return topResults;
};
@ -143,8 +141,6 @@ namespace hnswlib {
}
void loadIndex(const std::string &location, SpaceInterface<dist_t> *s) {
std::ifstream input(location, std::ios::binary);
std::streampos position;
@ -163,8 +159,6 @@ namespace hnswlib {
input.read(data_, maxelements_ * size_per_element_);
input.close();
}
};
};
}

View File

@ -16,8 +16,7 @@ typedef unsigned int linklistsizeint;
template<typename dist_t>
class HierarchicalNSW : public AlgorithmInterface<dist_t> {
public:
public:
HierarchicalNSW(SpaceInterface<dist_t> *s) {
}

View File

@ -89,8 +89,6 @@ namespace hnswlib {
virtual ~AlgorithmInterface(){
}
};
}
#include "space_l2.h"

View File

@ -3,8 +3,8 @@
namespace hnswlib {
static float
InnerProduct(const void *pVect1, const void *pVect2, const void *qty_ptr) {
static float
InnerProduct(const void *pVect1, const void *pVect2, const void *qty_ptr) {
size_t qty = *((size_t *) qty_ptr);
float res = 0;
for (unsigned i = 0; i < qty; i++) {
@ -12,13 +12,13 @@ namespace hnswlib {
}
return (1.0f - res);
}
}
#if defined(USE_AVX)
// Favor using AVX if available.
static float
InnerProductSIMD4Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
static float
InnerProductSIMD4Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
float PORTABLE_ALIGN32 TmpRes[8];
float *pVect1 = (float *) pVect1v;
float *pVect2 = (float *) pVect2v;
@ -66,8 +66,8 @@ namespace hnswlib {
#elif defined(USE_SSE)
static float
InnerProductSIMD4Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
static float
InnerProductSIMD4Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
float PORTABLE_ALIGN32 TmpRes[8];
float *pVect1 = (float *) pVect1v;
float *pVect2 = (float *) pVect2v;
@ -120,14 +120,14 @@ namespace hnswlib {
float sum = TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3];
return 1.0f - sum;
}
}
#endif
#if defined(USE_AVX)
static float
InnerProductSIMD16Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
static float
InnerProductSIMD16Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
float PORTABLE_ALIGN32 TmpRes[8];
float *pVect1 = (float *) pVect1v;
float *pVect2 = (float *) pVect2v;
@ -160,12 +160,12 @@ namespace hnswlib {
float sum = TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3] + TmpRes[4] + TmpRes[5] + TmpRes[6] + TmpRes[7];
return 1.0f - sum;
}
}
#elif defined(USE_SSE)
static float
InnerProductSIMD16Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
static float
InnerProductSIMD16Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
float PORTABLE_ALIGN32 TmpRes[8];
float *pVect1 = (float *) pVect1v;
float *pVect2 = (float *) pVect2v;
@ -207,19 +207,18 @@ namespace hnswlib {
float sum = TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3];
return 1.0f - sum;
}
}
#endif
class InnerProductSpace : public SpaceInterface<float> {
class InnerProductSpace : public SpaceInterface<float> {
DISTFUNC<float> fstdistfunc_;
size_t data_size_;
size_t dim_;
public:
InnerProductSpace(size_t dim) {
fstdistfunc_ = InnerProduct;
#if defined(USE_AVX) || defined(USE_SSE)
#if defined(USE_AVX) || defined(USE_SSE)
if (dim % 4 == 0)
fstdistfunc_ = InnerProductSIMD4Ext;
if (dim % 16 == 0)
@ -242,7 +241,5 @@ namespace hnswlib {
}
~InnerProductSpace() {}
};
};
}

View File

@ -3,8 +3,8 @@
namespace hnswlib {
static float
L2Sqr(const void *pVect1, const void *pVect2, const void *qty_ptr) {
static float
L2Sqr(const void *pVect1, const void *pVect2, const void *qty_ptr) {
//return *((float *)pVect2);
size_t qty = *((size_t *) qty_ptr);
float res = 0;
@ -13,14 +13,13 @@ namespace hnswlib {
res += t * t;
}
return (res);
}
}
#if defined(USE_AVX)
// Favor using AVX if available.
static float
L2SqrSIMD16Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
// Favor using AVX if available.
static float
L2SqrSIMD16Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
float *pVect1 = (float *) pVect1v;
float *pVect2 = (float *) pVect2v;
size_t qty = *((size_t *) qty_ptr);
@ -56,8 +55,8 @@ namespace hnswlib {
#elif defined(USE_SSE)
static float
L2SqrSIMD16Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
static float
L2SqrSIMD16Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
float *pVect1 = (float *) pVect1v;
float *pVect2 = (float *) pVect2v;
size_t qty = *((size_t *) qty_ptr);
@ -106,13 +105,13 @@ namespace hnswlib {
float res = TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3];
return (res);
}
}
#endif
#ifdef USE_SSE
static float
L2SqrSIMD4Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
static float
L2SqrSIMD4Ext(const void *pVect1v, const void *pVect2v, const void *qty_ptr) {
float PORTABLE_ALIGN32 TmpRes[8];
float *pVect1 = (float *) pVect1v;
float *pVect2 = (float *) pVect2v;
@ -139,18 +138,17 @@ namespace hnswlib {
float res = TmpRes[0] + TmpRes[1] + TmpRes[2] + TmpRes[3];
return (res);
}
}
#endif
class L2Space : public SpaceInterface<float> {
class L2Space : public SpaceInterface<float> {
DISTFUNC<float> fstdistfunc_;
size_t data_size_;
size_t dim_;
public:
L2Space(size_t dim) {
fstdistfunc_ = L2Sqr;
#if defined(USE_SSE) || defined(USE_AVX)
#if defined(USE_SSE) || defined(USE_AVX)
if (dim % 4 == 0)
fstdistfunc_ = L2SqrSIMD4Ext;
if (dim % 16 == 0)
@ -158,7 +156,7 @@ namespace hnswlib {
/*else{
throw runtime_error("Data type not supported!");
}*/
#endif
#endif
dim_ = dim;
data_size_ = dim * sizeof(float);
}
@ -176,11 +174,10 @@ namespace hnswlib {
}
~L2Space() {}
};
static int
L2SqrI(const void *__restrict pVect1, const void *__restrict pVect2, const void *__restrict qty_ptr) {
};
static int
L2SqrI(const void *__restrict pVect1, const void *__restrict pVect2, const void *__restrict qty_ptr) {
size_t qty = *((size_t *) qty_ptr);
int res = 0;
unsigned char *a = (unsigned char *) pVect1;
@ -205,16 +202,12 @@ namespace hnswlib {
res += ((*a) - (*b)) * ((*a) - (*b));
a++;
b++;
}
return (res);
}
}
class L2SpaceI : public SpaceInterface<int> {
class L2SpaceI : public SpaceInterface<int> {
DISTFUNC<int> fstdistfunc_;
size_t data_size_;
size_t dim_;
@ -238,7 +231,6 @@ namespace hnswlib {
}
~L2SpaceI() {}
};
};
}

View File

@ -4,9 +4,9 @@
#include <string.h>
namespace hnswlib {
typedef unsigned short int vl_type;
typedef unsigned short int vl_type;
class VisitedList {
class VisitedList {
public:
vl_type curV;
vl_type *mass;
@ -27,14 +27,15 @@ namespace hnswlib {
};
~VisitedList() { delete[] mass; }
};
};
///////////////////////////////////////////////////////////
//
// Class for multi-threaded pool-management of VisitedLists
//
/////////////////////////////////////////////////////////
class VisitedListPool {
class VisitedListPool {
std::deque<VisitedList *> pool;
std::mutex poolguard;
int numelements;
@ -73,6 +74,6 @@ namespace hnswlib {
delete rez;
}
};
};
};
}