Skip to content

Commit dbbb30e

Browse files
committed
Functional Code with CPU benchmarks
1 parent 47a7269 commit dbbb30e

File tree

9 files changed

+64
-86
lines changed

9 files changed

+64
-86
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
*.out
44
*.o
55
*.exp
6-
*.lib
6+
*.lib
7+
*.tmp
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
#define ITEMS_H
33

44
#include <vector>
5+
#include <fstream>
6+
#include <iostream>
57
#include <cmath>
8+
#include <memory>
9+
#include <string>
10+
#include <sstream>
611
#include "omp.h"
12+
using namespace std;
713

814
struct Item {
915
Item(std::vector<double> _values) : values(_values) {}

Project Document.docx

15.4 KB
Binary file not shown.

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# Approximate-Nearest-Neighbor-Framework
22

3+
# HOW TO RUN
4+
5+
bash compile.sh <br />
6+
bash run.sh [input_file] [output_file] [Integer value of k] <br />
37

48
# DEMO RUN
59

610
bash compile.sh <br />
7-
bash run.sh sample_inputs/sample4.in sample_outputs/sample4.out <br />
11+
bash run.sh sample_inputs/sample4.in sample_outputs/sample4.out 5 <br />
812

913
# DEMO CONSOLE OUTPUT
1014

hnsw_implementation/hnsw.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88
#include <unordered_set>
99
#include <vector>
1010
#include <omp.h>
11+
#include <cmath>
12+
#include <limits>
1113
using namespace std;
1214

1315
vector<int> HNSWGraph::searchLayer(Item& q, int entry_point, int ef, int lc) {
16+
if (lc < 0 || lc >= layerEdgeLists.size()) {
17+
cerr << "Error: Invalid layer index." << endl;
18+
exit(EXIT_FAILURE);
19+
}
1420
set<pair<double, int>> candidates;
1521
set<pair<double, int>> nearestNeighbors;
1622
unordered_set<int> isVisited;
@@ -49,7 +55,18 @@ vector<int> HNSWGraph::searchLayer(Item& q, int entry_point, int ef, int lc) {
4955
return results;
5056
}
5157

52-
vector<int> HNSWGraph::KNNSearch(Item& q, int K) {
58+
vector<int> HNSWGraph::KNNSearch(Item& q, int K, int N) {
59+
60+
if (K <= 0 || std::ceil(K) != K || K > std::numeric_limits<int>::max()) {
61+
cerr << "Error: Invalid value of K for KNNSearch." << endl;
62+
exit(EXIT_FAILURE);
63+
}
64+
65+
if (K > N) {
66+
std::cerr << "Error: Value of K (" << K << ") is greater than the number of data points (N = " << N << ")." << std::endl;
67+
exit(EXIT_FAILURE);
68+
}
69+
5370
int maxLyer = layerEdgeLists.size() - 1;
5471
int entry_point = enterNode;
5572
for (int l = maxLyer; l >= 1; l--) {
@@ -59,6 +76,10 @@ vector<int> HNSWGraph::KNNSearch(Item& q, int K) {
5976
}
6077

6178
void HNSWGraph::addEdge(int st, int ed, int lc) {
79+
if (lc < 0 || lc >= layerEdgeLists.size()) {
80+
cerr << "Error: Invalid layer index." << endl;
81+
return;
82+
}
6283
if (st == ed) {
6384
return;
6485
}
@@ -91,10 +112,12 @@ void HNSWGraph::Insert(Item& q) {
91112
int MM = l == 0 ? MMax0 : MMax;
92113
vector<int> neighbors = searchLayer(q, entry_point, efConstruction, i);
93114
vector<int> selectedNeighbors = vector<int>(neighbors.begin(), neighbors.begin()+min(int(neighbors.size()), M));
94-
for (int n: selectedNeighbors) {
95-
addEdge(n, nid, i);
96-
}
97-
for (int n: selectedNeighbors) {
115+
for (size_t j = 0; j < selectedNeighbors.size(); j++) {
116+
int n = selectedNeighbors[j];
117+
addEdge(n, nid, i);
118+
}
119+
for (size_t j = 0; j < selectedNeighbors.size(); j++) {
120+
int n = selectedNeighbors[j];
98121
if (layerEdgeLists[i][n].size() > MM) {
99122
vector<pair<double, int>> distPairs;
100123
for (int nn: layerEdgeLists[i][n]) {

hnsw_implementation/hnsw.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <unordered_map>
77
#include <iostream>
88

9-
#include "../CPU/vector_initialize.h"
9+
#include "../CPU/initialization.h"
1010
using namespace std;
1111

1212

@@ -28,7 +28,7 @@ struct HNSWGraph {
2828
void addEdge(int st, int ed, int lc);
2929
vector<int> searchLayer(Item& q, int ep, int ef, int lc);
3030
void Insert(Item& q);
31-
vector<int> KNNSearch(Item& q, int K);
31+
vector<int> KNNSearch(Item& q, int K, int N);
3232

3333
void printGraph() {
3434
for (int l = 0; l < layerEdgeLists.size(); l++) {

main.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "hnsw_implementation/hnsw.h"
2+
#include "CPU/initialization.h"
23
#include <algorithm>
34
#include <ctime>
45
#include <iostream>
@@ -54,17 +55,26 @@ void readInputFromFile(const string& filename, int& D, int& N, int& M, vector<It
5455
infile.close();
5556
}
5657

57-
5858
int main(int argc, char* argv[]) {
5959

60-
if (argc != 3) {
61-
cerr << "Usage: " << argv[0] << " <input_filename> <output_filename>" << endl;
60+
if (argc != 4) {
61+
cerr << "Usage: " << argv[0] << " <input_filename> <output_filename> <Integer value of k>" << endl;
6262
return EXIT_FAILURE;
6363
}
6464

6565
string filename = argv[1];
6666
string outputFilename = argv[2];
67-
int K = 5;
67+
68+
int K;
69+
try {
70+
K = std::stoi(argv[3]);
71+
} catch (std::invalid_argument const& e) {
72+
std::cerr << "Invalid argument: " << e.what() << std::endl;
73+
return EXIT_FAILURE;
74+
} catch (std::out_of_range const& e) {
75+
std::cerr << "Out of range argument: " << e.what() << std::endl;
76+
return EXIT_FAILURE;
77+
}
6878

6979
ifstream infile(filename);
7080
if (!infile.is_open()) {
@@ -78,6 +88,11 @@ int main(int argc, char* argv[]) {
7888

7989
readInputFromFile(filename, D, N, M, base, queries);
8090

91+
if (K > N) {
92+
std::cerr << "Error: Value of K (" << K << ") is greater than the number of data points (N = " << N << ")." << std::endl;
93+
return EXIT_FAILURE;
94+
}
95+
8196
HNSWGraph myHNSWGraph(10, 30, 30, 10, 2);
8297

8398
for (int i = 0; i < N; ++i) {
@@ -118,7 +133,7 @@ int main(int argc, char* argv[]) {
118133

119134
begin_time = clock();
120135

121-
vector<int> knns = myHNSWGraph.KNNSearch(query, K);
136+
vector<int> knns = myHNSWGraph.KNNSearch(query, K, N);
122137
for (size_t idx = 0; idx < knns.size(); ++idx) {
123138
outfile << knns[idx];
124139
if (idx != knns.size() - 1) {

main1.cpp

Lines changed: 0 additions & 71 deletions
This file was deleted.

run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
./my_program $1 $2
1+
./my_program $1 $2 $3

0 commit comments

Comments
 (0)