-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
332 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
2.0.9 | ||
2.0.10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
if( ${UNIX} ) | ||
include_directories("${PROJECT_BINARY_DIR}/lib") | ||
include_directories("${PROJECT_SOURCE_DIR}/lib") | ||
link_directories("${PROJECT_SOURCE_DIR}/lib/NGT") | ||
|
||
add_executable(qbg-capi qbg-capi.cpp) | ||
add_dependencies(qbg-capi ngt) | ||
target_link_libraries(qbg-capi ngt pthread) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
|
||
#include "NGT/Index.h" | ||
#include "NGT/NGTQ/Capi.h" | ||
int | ||
main(int argc, char **argv) | ||
{ | ||
std::string indexPath = "index"; | ||
std::string objectFile = "sift-128-euclidean.tsv"; | ||
std::string queryFile = "query.tsv"; | ||
|
||
std::cerr << "run the following commands to prepare data for this sample program." << std::endl; | ||
std::cerr << " curl -L -O https://github.com/yahoojapan/NGT/raw/main/tests/datasets/ann-benchmarks/sift-128-euclidean.tsv" << std::endl; | ||
std::cerr << " curl -L -O https://github.com/yahoojapan/NGT/raw/main/tests/datasets/ann-benchmarks/sift-128-euclidean_query.tsv" << std::endl; | ||
std::cerr << " head -1 sift-128-euclidean_query.tsv > query.tsv" << std::endl; | ||
std::cerr << std::endl; | ||
std::cerr << "index path=" << indexPath << std::endl; | ||
std::cerr << "object file=" << objectFile << std::endl; | ||
std::cerr << "query file=" << queryFile << std::endl; | ||
std::cerr << std::endl; | ||
|
||
size_t dimension = 128; | ||
NGTError err = ngt_create_error_object(); | ||
|
||
std::cerr << "create an empty index..." << std::endl; | ||
QBGConstructionParameters constructionParameters; | ||
qbg_initialize_construction_parameters(&constructionParameters); | ||
constructionParameters.dimension = dimension; | ||
constructionParameters.number_of_subvectors = 64; | ||
constructionParameters.number_of_blobs = 0; | ||
if (!qbg_create(indexPath.c_str(), &constructionParameters, err)) { | ||
std::cerr << "Cannot create" << std::endl; | ||
std::cerr << ngt_get_error_string(err) << std::endl; | ||
return 1; | ||
} | ||
|
||
std::cerr << "append objects..." << std::endl; | ||
auto index = qbg_open_index(indexPath.c_str(), false, err); | ||
if (index == 0) { | ||
std::cerr << "Cannot open" << std::endl; | ||
std::cerr << ngt_get_error_string(err) << std::endl; | ||
return 1; | ||
} | ||
|
||
try { | ||
std::ifstream is(objectFile); | ||
std::string line; | ||
while (getline(is, line)) { | ||
std::vector<float> obj; | ||
std::stringstream linestream(line); | ||
while (!linestream.eof()) { | ||
float value; | ||
linestream >> value; | ||
if (linestream.fail()) { | ||
obj.clear(); | ||
break; | ||
} | ||
obj.push_back(value); | ||
} | ||
if (obj.empty()) { | ||
std::cerr << "An empty line or invalid value: " << line << std::endl; | ||
return 1; | ||
} | ||
if (qbg_append_object(index, obj.data(), dimension, err) == 0) { | ||
std::cerr << ngt_get_error_string(err) << std::endl; | ||
return 1; | ||
} | ||
} | ||
} catch (...) { | ||
std::cerr << "Error" << std::endl; | ||
return 1; | ||
} | ||
|
||
qbg_save_index(index, err); | ||
qbg_close_index(index); | ||
|
||
std::cerr << "building the index..." << std::endl; | ||
QBGBuildParameters buildParameters; | ||
qbg_initialize_build_parameters(&buildParameters); | ||
buildParameters.number_of_objects = 500; | ||
auto status = qbg_build_index(indexPath.c_str(), &buildParameters, err); | ||
if (!status) { | ||
std::cerr << "Cannot build. " << ngt_get_error_string(err) << std::endl; | ||
return 1; | ||
} | ||
|
||
index = qbg_open_index(indexPath.c_str(), true, err); | ||
if (index == 0) { | ||
std::cerr << "Cannot open. " << ngt_get_error_string(err) << std::endl; | ||
return 1; | ||
} | ||
|
||
std::ifstream is(queryFile); | ||
if (!is) { | ||
std::cerr << "Cannot open the specified file. " << queryFile << std::endl; | ||
return 1; | ||
} | ||
|
||
std::string line; | ||
float queryVector[dimension]; | ||
if (getline(is, line)) { | ||
std::vector<double> queryObject; | ||
{ | ||
std::vector<std::string> tokens; | ||
NGT::Common::tokenize(line, tokens, " \t"); | ||
tokens.resize(dimension); | ||
if (tokens.size() != dimension) { | ||
std::cerr << "dimension of the query is invalid. dimesion=" << tokens.size() << ":" << dimension << std::endl; | ||
return 1; | ||
} | ||
for (std::vector<std::string>::iterator ti = tokens.begin(); ti != tokens.end(); ++ti) { | ||
queryVector[distance(tokens.begin(), ti)] = NGT::Common::strtod(*ti); | ||
} | ||
} | ||
QBGObjectDistances result = ngt_create_empty_results(err); | ||
QBGQuery query; | ||
qbg_initialize_query(&query); | ||
query.query = &queryVector[0]; | ||
std::cerr << "search the index for the specified query..." << std::endl; | ||
auto status = qbg_search_index(index, query, result, err); | ||
if (!status) { | ||
std::cerr << "Cannot search. " << ngt_get_error_string(err) << std::endl; | ||
return 1; | ||
} | ||
auto rsize = qbg_get_result_size(result, err); | ||
std::cout << "Rank\tID\tDistance" << std::endl; | ||
for (size_t i = 0; i < rsize; i++) { | ||
NGTObjectDistance object = qbg_get_result(result, i, err); | ||
std::cout << i + 1 << "\t" << object.id << "\t" << object.distance << std::endl; | ||
} | ||
|
||
qbg_destroy_results(result); | ||
} | ||
|
||
qbg_close_index(index); | ||
ngt_destroy_error_object(err); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
if( ${UNIX} ) | ||
include_directories("${PROJECT_BINARY_DIR}/lib") | ||
include_directories("${PROJECT_SOURCE_DIR}/lib") | ||
link_directories("${PROJECT_SOURCE_DIR}/lib/NGT") | ||
|
||
add_executable(qg-capi qg-capi.cpp) | ||
add_dependencies(qg-capi ngt) | ||
target_link_libraries(qg-capi ngt pthread) | ||
endif() |
Oops, something went wrong.