Skip to content

Commit

Permalink
branch completed
Browse files Browse the repository at this point in the history
  • Loading branch information
brj0 committed Jul 7, 2023
1 parent fbfebbb commit b13b5fd
Show file tree
Hide file tree
Showing 14 changed files with 328 additions and 268 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# Distribution / packaging
build/
dist/
*.egg-info/

# ctags
Expand Down
25 changes: 19 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
cmake_minimum_required(VERSION 3.10)

project(nndescent
VERSION 0.0.0
VERSION 1.0.3
LANGUAGES CXX
DESCRIPTION "A C++ implementation of the nearest neighbour descent algorithm")

set(CMAKE_CXX_STANDARD 11)

# Standard mode including all distance functions (slow)
set(CMAKE_CXX_FLAGS

# Enable the fast mode option that uses only euclidean and cosine metrics
option(FAST_MODE "Enable fast compile mode" OFF)

# Standard mode includes all distance functions (slow)
set(STANDARD_CXX_FLAGS
"-Wall -g -pg -Ofast -DALL_METRICS -march=native -flto -fno-math-errno -fopenmp -pg")

# Debug compile mode (only euclidean/cosine)
# set(CMAKE_CXX_FLAGS
# "-Wall -g -pg -Ofast -march=native -flto -fno-math-errno -fopenmp -pg")
# Fast mode (only euclidean/cosine distance)
set(FAST_CXX_FLAGS
"-Wall -g -pg -Ofast -march=native -flto -fno-math-errno -fopenmp -pg")

# Select the appropriate compiler flags
if(FAST_MODE)
set(CMAKE_CXX_FLAGS "${FAST_CXX_FLAGS}")
else()
set(CMAKE_CXX_FLAGS "${STANDARD_CXX_FLAGS}")
endif()


# Collect source files
file(GLOB_RECURSE SRC_FILES src/*.cpp)
Expand All @@ -24,6 +36,7 @@ add_library(nndescent STATIC ${SRC_FILES})
# Source code
target_include_directories(nndescent PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)


# Tests
add_executable(test_distances tests/test_distances.cpp)
target_link_libraries(test_distances PRIVATE nndescent)
Expand Down
9 changes: 9 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# To upload this project to PyPI, follow these steps:
#
# python setup.py sdist
# twine upload dist/*
#
# When prompted for the username, provide __token__

# Includes the header files in the resulting .gz file for PyPI
include src/*.h
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,30 @@ Please note that not all distances have undergone thorough testing. Therefore, i

## Installation

### From PyPI

You can install nndescent directly from PyPI using pip:

```sh
pip install nndescent
```

If you want to run the examples in `tests`, additional packages are needed. You can install them manually or install nndescent with the full option:

```sh
pip install nndescent[full]
```

### From Source

1. Clone the repository:

```sh
git clone https://github.com/brj0/nndescent.git
cd nndescent
```

2. The project can by build with:
2. Build and install the package:

```sh
pip install .
Expand All @@ -71,7 +87,6 @@ pip install .[full]
python tests/make_test_data.py
```


## Usage

In Python you can utilize the nndescent library in the following way:
Expand Down
4 changes: 2 additions & 2 deletions pybindings/pybind11ings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using namespace nndescent;
const Parms DEFAULT_PARMS;


/**
/*
* @brief Convert a nndescent::Matrix<T> object to a NumPy array in Python.
*/
template<class T>
Expand Down Expand Up @@ -71,7 +71,7 @@ CSRMatrix<T> to_csr_matrix(py::object &py_obj)
}


/**
/**
* @brief Wrapper class for binding the NND (Nearest Neighbor Descent)
* algorithm in Python.
*/
Expand Down
69 changes: 46 additions & 23 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from setuptools import Extension, setup
import glob
import numpy as np
import os
import re
import pybind11

Expand All @@ -18,43 +20,49 @@ def get_version_string():
np.get_include(),
]

module = Extension(
name="nndescent",
sources=[
"pybindings/pybind11ings.cpp",
"src/dtypes.cpp",
"src/nnd.cpp",
"src/rp_trees.cpp",
"src/utils.cpp",
"src/distances.cpp",
],
include_dirs=include_dirs,
extra_compile_args=[
# Get the value of the NND_DEBUG environment variable
debug = os.getenv("NND_DEBUG") == "1"

# Set the compiler flags based on the debug flag
if debug:
print("\n*** Debugging compiler flags are used. ***\n")
compile_args = [
"-O0",
"-Wall",
"-Wextra",
"-fno-stack-protector",
"-g",
"-pg",
]
else:
compile_args = [
"-DALL_METRICS",
"-Ofast",
"-flto",
"-DALL_METRICS",
"-fno-math-errno",
"-fopenmp",
"-g",
"-march=native",
],
# CXXFLAGS for debugging
# extra_compile_args=[
# "-O0",
# "-Wall",
# "-Wextra",
# "-fno-stack-protector",
# "-g",
# "-pg",
# ],
]

module = Extension(
name="nndescent",
sources=glob.glob("pybindings/*.cpp") + glob.glob("src/*.cpp"),
include_dirs=include_dirs,
extra_compile_args=compile_args,
extra_link_args=["-fopenmp"],
language="c++",
)

with open("README.md", "r") as f:
long_description = f.read()

setup(
name="nndescent",
version=get_version_string(),
description="C++ extension implementing nearest neighbour descent",
long_description=long_description,
long_description_content_type="text/markdown",
install_requires=[
"numpy",
],
Expand All @@ -69,4 +77,19 @@ def get_version_string():
],
},
ext_modules=[module],
keywords="nearest neighbor, knn, ANN",
license="BSD 2-Clause License",
author="Jon Brugger",
url="https://github.com/brj0/nndescent",
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: BSD License",
"Operating System :: POSIX",
"Programming Language :: C++",
"Programming Language :: Python :: 3",
"Topic :: Scientific/Engineering",
"Topic :: Software Development",
],
)
1 change: 0 additions & 1 deletion src/distances.h
Original file line number Diff line number Diff line change
Expand Up @@ -2201,7 +2201,6 @@ float dot(Iter0 first0, Iter0 last0, Iter1 first1)
}



/*
* @brief Sparse dot.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/dtypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -1001,8 +1001,8 @@ class HeapList
int checked_push(size_t i, int idx, KeyType key, char flag);

/*
* Pushes a node with the specified index, key, and flag into the
* specified heap if its key is smaller and it is not already in the heap.
* Pushes a node with the specified index and key into the specified heap
* if its key is smaller and it is not already in the heap.
*
* @param i The index of the heap.
* @param idx The index of the node.
Expand Down
Loading

0 comments on commit b13b5fd

Please sign in to comment.