From 2b1948515e31c58aef05dd6938415e57aed5a550 Mon Sep 17 00:00:00 2001 From: CascadingRadium Date: Mon, 3 Feb 2025 17:26:40 +0530 Subject: [PATCH 1/7] MB-63430: Add jemalloc option --- CMakeLists.txt | 1 + c_api/CMakeLists.txt | 8 ++ c_api/jemalloc_override_c.cpp | 6 ++ cmake/FindJemalloc.cmake | 40 ++++++++++ faiss/CMakeLists.txt | 12 +++ faiss/jemalloc_override.cpp | 135 ++++++++++++++++++++++++++++++++++ 6 files changed, 202 insertions(+) create mode 100644 c_api/jemalloc_override_c.cpp create mode 100644 cmake/FindJemalloc.cmake create mode 100644 faiss/jemalloc_override.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fa5ab4d1f5..1693bb59f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,7 @@ option(FAISS_ENABLE_GPU "Enable support for GPU indexes." ON) option(FAISS_ENABLE_RAFT "Enable RAFT for GPU indexes." OFF) option(FAISS_ENABLE_PYTHON "Build Python extension." ON) option(FAISS_ENABLE_C_API "Build C API." OFF) +option(FAISS_USE_JEMALLOC "Use jemalloc for memory allocation." OFF) # Force FAISS_OPT_LEVEL to "generic" if not building on x86_64. if(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64") diff --git a/c_api/CMakeLists.txt b/c_api/CMakeLists.txt index 8af3ee8e4a..d4d7fa04a9 100644 --- a/c_api/CMakeLists.txt +++ b/c_api/CMakeLists.txt @@ -28,6 +28,7 @@ set(FAISS_C_SRC MetaIndexes_c.cpp clone_index_c.cpp error_impl.cpp + jemalloc_override_c.cpp index_factory_c.cpp index_io_c.cpp index_io_c_ex.cpp @@ -42,6 +43,13 @@ elseif(FAISS_OPT_LEVEL STREQUAL "avx2") elseif(FAISS_OPT_LEVEL STREQUAL "avx512") target_link_libraries(faiss_c PRIVATE faiss_avx512) endif() + +if(FAISS_USE_JEMALLOC) + find_package(Jemalloc REQUIRED) + target_link_libraries(faiss_c PRIVATE ${JEMALLOC_LIBRARIES}) + target_include_directories(faiss_c PRIVATE ${JEMALLOC_INCLUDE_DIRS}) +endif() + install(TARGETS faiss_c EXPORT faiss-targets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} diff --git a/c_api/jemalloc_override_c.cpp b/c_api/jemalloc_override_c.cpp new file mode 100644 index 0000000000..0b0eae2db9 --- /dev/null +++ b/c_api/jemalloc_override_c.cpp @@ -0,0 +1,6 @@ +#ifdef FAISS_USE_JEMALLOC + +#define JEMALLOC_MANGLE +#include + +#endif /* FAISS_USE_JEMALLOC */ diff --git a/cmake/FindJemalloc.cmake b/cmake/FindJemalloc.cmake new file mode 100644 index 0000000000..2e6f4a44af --- /dev/null +++ b/cmake/FindJemalloc.cmake @@ -0,0 +1,40 @@ +# - Try to find jemalloc +# Once done this will define +# JEMALLOC_FOUND - System has jemalloc +# JEMALLOC_INCLUDE_DIRS - The jemalloc include directories +# JEMALLOC_LIBRARIES - The libraries needed to use jemalloc + +find_package(PkgConfig QUIET) +pkg_check_modules(PC_JEMALLOC QUIET jemalloc) + +find_path(JEMALLOC_INCLUDE_DIR + NAMES jemalloc/jemalloc.h + HINTS ${PC_JEMALLOC_INCLUDE_DIRS} +) +find_library(JEMALLOC_LIBRARY + NAMES jemalloc + HINTS ${PC_JEMALLOC_LIBRARY_DIRS} +) + +if(JEMALLOC_INCLUDE_DIR) + set(_version_regex "^#define[ \t]+JEMALLOC_VERSION[ \t]+\"([^\"]+)\".*") + file(STRINGS "${JEMALLOC_INCLUDE_DIR}/jemalloc/jemalloc.h" + JEMALLOC_VERSION REGEX "${_version_regex}") + string(REGEX REPLACE "${_version_regex}" "\\1" + JEMALLOC_VERSION "${JEMALLOC_VERSION}") + unset(_version_regex) +endif() + +include(FindPackageHandleStandardArgs) +# handle the QUIETLY and REQUIRED arguments and set JEMALLOC_FOUND to TRUE +# if all listed variables are TRUE and the requested version matches. +find_package_handle_standard_args(Jemalloc REQUIRED_VARS + JEMALLOC_LIBRARY JEMALLOC_INCLUDE_DIR + VERSION_VAR JEMALLOC_VERSION) + +if(JEMALLOC_FOUND) + set(JEMALLOC_LIBRARIES ${JEMALLOC_LIBRARY}) + set(JEMALLOC_INCLUDE_DIRS ${JEMALLOC_INCLUDE_DIR}) +endif() + +mark_as_advanced(JEMALLOC_INCLUDE_DIR JEMALLOC_LIBRARY) \ No newline at end of file diff --git a/faiss/CMakeLists.txt b/faiss/CMakeLists.txt index 1584023086..f3bac3d8eb 100644 --- a/faiss/CMakeLists.txt +++ b/faiss/CMakeLists.txt @@ -46,6 +46,7 @@ set(FAISS_SRC IndexScalarQuantizer.cpp IndexShards.cpp IndexShardsIVF.cpp + jemalloc_override.cpp MatrixStats.cpp MetaIndexes.cpp OMPConfig.cpp @@ -332,6 +333,17 @@ else() target_link_libraries(faiss_avx512 PRIVATE ${LAPACK_LIBRARIES}) endif() +if(FAISS_USE_JEMALLOC) + find_package(Jemalloc REQUIRED) + target_link_libraries(faiss PRIVATE ${JEMALLOC_LIBRARIES}) + target_link_libraries(faiss_avx2 PRIVATE ${JEMALLOC_LIBRARIES}) + target_link_libraries(faiss_avx512 PRIVATE ${JEMALLOC_LIBRARIES}) + + target_include_directories(faiss PRIVATE ${JEMALLOC_INCLUDE_DIRS}) + target_include_directories(faiss_avx2 PRIVATE ${JEMALLOC_INCLUDE_DIRS}) + target_include_directories(faiss_avx512 PRIVATE ${JEMALLOC_INCLUDE_DIRS}) +endif() + if(FAISS_OPT_LEVEL STREQUAL "generic") install(TARGETS faiss diff --git a/faiss/jemalloc_override.cpp b/faiss/jemalloc_override.cpp new file mode 100644 index 0000000000..5f5dda507c --- /dev/null +++ b/faiss/jemalloc_override.cpp @@ -0,0 +1,135 @@ +#ifdef FAISS_USE_JEMALLOC +// ---------------------------------------------------------------------------- +// This header provides convenient overrides for the new and +// delete operations in C++. +// +// This header should be included in only one source file! +// +// See +// --------------------------------------------------------------------------- +#define JEMALLOC_MANGLE +#include +#include + +#if __cplusplus > 202002L +#warning Only operator new/delete up to C++20 overridden. If later standards add additional overrides they should be added here. +#endif + + +// (new 1) +[[nodiscard]] void* operator new(std::size_t count) { + void* result = malloc(count); + if (result == nullptr) { + throw std::bad_alloc(); + } + return result; +} + +// (new 2) +[[nodiscard]] void* operator new[](std::size_t count) { + void* result = malloc(count); + if (result == nullptr) { + throw std::bad_alloc(); + } + return result; +} + +// (new 3) +[[nodiscard]] void* operator new(std::size_t count, std::align_val_t al) { + void* result = aligned_alloc(static_cast(al), count); + if (result == nullptr) { + throw std::bad_alloc(); + } + return result; +} + +// (new 4) +[[nodiscard]] void* operator new[](std::size_t count, std::align_val_t al) { + void* result = aligned_alloc(static_cast(al), count); + if (result == nullptr) { + throw std::bad_alloc(); + } + return result; +} + +// (new 5) +[[nodiscard]] void* operator new(std::size_t count, + const std::nothrow_t& tag) noexcept { + void* result = malloc(count); + return result; +} + +// (new 6) +[[nodiscard]] void* operator new[](std::size_t count, + const std::nothrow_t& tag) noexcept { + void* result = malloc(count); + return result; +} + +// (new 7) +[[nodiscard]] void* operator new(std::size_t count, + std::align_val_t al, + const std::nothrow_t& tag) noexcept { + void* result = aligned_alloc(static_cast(al), count); + return result; +} + +// (new 8) +[[nodiscard]] void* operator new[](std::size_t count, + std::align_val_t al, + const std::nothrow_t& tag) noexcept { + void* result = aligned_alloc(static_cast(al), count); + return result; +} + +// (del 1) +void operator delete(void* ptr) noexcept { + free(ptr); +} + +// (del 2) +void operator delete[](void* ptr) noexcept { + free(ptr); +} + +// (del 3) +void operator delete(void* ptr, std::align_val_t) noexcept { + free(ptr); +} + +// (del 4) +void operator delete[](void* ptr, std::align_val_t) noexcept { + free(ptr); +} + +// (del 6) +void operator delete[](void* ptr, std::size_t size) noexcept { + free(ptr); +} + +// (del 5) +void operator delete(void* ptr, std::size_t size) noexcept { + free(ptr); +} + +// (del 7) +void operator delete(void* ptr, std::size_t size, std::align_val_t) noexcept { + free(ptr); +} + +// (del 8) +void operator delete[](void* ptr, std::size_t size, std::align_val_t) noexcept { + free(ptr); +} + +// (del 9) +void operator delete(void* ptr, const std::nothrow_t& tag) noexcept { + free(ptr); +} + +// (del 10) +void operator delete[](void* ptr, const std::nothrow_t& tag) noexcept { + free(ptr); +} + +#endif // FAISS_USE_JEMALLOC \ No newline at end of file From 0743d6f19241c45a7f6d390867149e72ee51503a Mon Sep 17 00:00:00 2001 From: CascadingRadium Date: Tue, 4 Feb 2025 20:03:49 +0530 Subject: [PATCH 2/7] fix --- c_api/CMakeLists.txt | 1 + c_api/jemalloc_override_c.cpp | 4 ++-- faiss/CMakeLists.txt | 1 + faiss/jemalloc_override.cpp | 4 ++-- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/c_api/CMakeLists.txt b/c_api/CMakeLists.txt index d4d7fa04a9..2de84fc211 100644 --- a/c_api/CMakeLists.txt +++ b/c_api/CMakeLists.txt @@ -46,6 +46,7 @@ endif() if(FAISS_USE_JEMALLOC) find_package(Jemalloc REQUIRED) + add_definitions(-DUSE_JEMALLOC) target_link_libraries(faiss_c PRIVATE ${JEMALLOC_LIBRARIES}) target_include_directories(faiss_c PRIVATE ${JEMALLOC_INCLUDE_DIRS}) endif() diff --git a/c_api/jemalloc_override_c.cpp b/c_api/jemalloc_override_c.cpp index 0b0eae2db9..77292309f0 100644 --- a/c_api/jemalloc_override_c.cpp +++ b/c_api/jemalloc_override_c.cpp @@ -1,6 +1,6 @@ -#ifdef FAISS_USE_JEMALLOC +#ifdef USE_JEMALLOC #define JEMALLOC_MANGLE #include -#endif /* FAISS_USE_JEMALLOC */ +#endif /* USE_JEMALLOC */ diff --git a/faiss/CMakeLists.txt b/faiss/CMakeLists.txt index f3bac3d8eb..65980d205e 100644 --- a/faiss/CMakeLists.txt +++ b/faiss/CMakeLists.txt @@ -335,6 +335,7 @@ endif() if(FAISS_USE_JEMALLOC) find_package(Jemalloc REQUIRED) + add_definitions(-DUSE_JEMALLOC) target_link_libraries(faiss PRIVATE ${JEMALLOC_LIBRARIES}) target_link_libraries(faiss_avx2 PRIVATE ${JEMALLOC_LIBRARIES}) target_link_libraries(faiss_avx512 PRIVATE ${JEMALLOC_LIBRARIES}) diff --git a/faiss/jemalloc_override.cpp b/faiss/jemalloc_override.cpp index 5f5dda507c..602d40c082 100644 --- a/faiss/jemalloc_override.cpp +++ b/faiss/jemalloc_override.cpp @@ -1,4 +1,4 @@ -#ifdef FAISS_USE_JEMALLOC +#ifdef USE_JEMALLOC // ---------------------------------------------------------------------------- // This header provides convenient overrides for the new and // delete operations in C++. @@ -132,4 +132,4 @@ void operator delete[](void* ptr, const std::nothrow_t& tag) noexcept { free(ptr); } -#endif // FAISS_USE_JEMALLOC \ No newline at end of file +#endif // USE_JEMALLOC \ No newline at end of file From bf4b3f7ddaa7f74a456fc75d3c1c892126a9fdfb Mon Sep 17 00:00:00 2001 From: CascadingRadium Date: Tue, 4 Feb 2025 20:29:30 +0530 Subject: [PATCH 3/7] fix 2 --- c_api/index_io_c_ex.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/c_api/index_io_c_ex.h b/c_api/index_io_c_ex.h index b0217bdab8..85ee409379 100644 --- a/c_api/index_io_c_ex.h +++ b/c_api/index_io_c_ex.h @@ -17,6 +17,13 @@ #include "Index_c.h" #include "faiss_c.h" +#ifdef USE_JEMALLOC + +#define JEMALLOC_MANGLE +#include + +#endif + #ifdef __cplusplus extern "C" { #endif From 594c42a6550684c1e0c3058fae5e1b79fb547991 Mon Sep 17 00:00:00 2001 From: Chris Hillery Date: Wed, 5 Feb 2025 02:17:40 -0800 Subject: [PATCH 4/7] Updates for 'Modern Cmake' and our jemalloc package --- c_api/CMakeLists.txt | 3 +- cmake/FindJemalloc.cmake | 82 ++++++++++++++++++++++++---------------- faiss/CMakeLists.txt | 10 ++--- 3 files changed, 54 insertions(+), 41 deletions(-) diff --git a/c_api/CMakeLists.txt b/c_api/CMakeLists.txt index 2de84fc211..41b361a90f 100644 --- a/c_api/CMakeLists.txt +++ b/c_api/CMakeLists.txt @@ -47,8 +47,7 @@ endif() if(FAISS_USE_JEMALLOC) find_package(Jemalloc REQUIRED) add_definitions(-DUSE_JEMALLOC) - target_link_libraries(faiss_c PRIVATE ${JEMALLOC_LIBRARIES}) - target_include_directories(faiss_c PRIVATE ${JEMALLOC_INCLUDE_DIRS}) + target_link_libraries(faiss_c PRIVATE Jemalloc::jemalloc) endif() install(TARGETS faiss_c diff --git a/cmake/FindJemalloc.cmake b/cmake/FindJemalloc.cmake index 2e6f4a44af..998a475058 100644 --- a/cmake/FindJemalloc.cmake +++ b/cmake/FindJemalloc.cmake @@ -1,40 +1,58 @@ # - Try to find jemalloc -# Once done this will define -# JEMALLOC_FOUND - System has jemalloc -# JEMALLOC_INCLUDE_DIRS - The jemalloc include directories -# JEMALLOC_LIBRARIES - The libraries needed to use jemalloc - -find_package(PkgConfig QUIET) -pkg_check_modules(PC_JEMALLOC QUIET jemalloc) - -find_path(JEMALLOC_INCLUDE_DIR - NAMES jemalloc/jemalloc.h - HINTS ${PC_JEMALLOC_INCLUDE_DIRS} -) -find_library(JEMALLOC_LIBRARY - NAMES jemalloc - HINTS ${PC_JEMALLOC_LIBRARY_DIRS} -) - -if(JEMALLOC_INCLUDE_DIR) +# Once done this will define a target Jemalloc::jemalloc which will include all +# required definitions and libraries. + +if (NOT FindJemalloc_included) + + # First try finding Jemalloc using pkg-config. + find_package(PkgConfig QUIET) + if (PKG_CONFIG_FOUND) + pkg_check_modules(PC_JEMALLOC QUIET IMPORTED_TARGET GLOBAL jemalloc) + + if (PC_JEMALLOC_FOUND) + # Ensure it created the PkgConfig target + if(NOT TARGET PkgConfig::PC_JEMALLOC) + message(FATAL_ERROR + "Found Jemalloc via pkg-config, but it did not create the PkgConfig::PC_JEMALLOC target.") + endif() + + # Make the discovered target available as Jemalloc::jemalloc + add_library(Jemalloc::jemalloc ALIAS PkgConfig::PC_JEMALLOC) + set(_jemalloc_found TRUE) + endif() + endif() + + # If that didn't find it, try finding Jemalloc using CMake's config + # mode. + if(NOT _jemalloc_found) + find_package(Jemalloc CONFIG) + + if(Jemalloc_FOUND) + if(NOT TARGET Jemalloc::jemalloc) + # Ensure this found package created the standard target. + message(FATAL_ERROR + "Found Jemalloc, but it did not create the Jemalloc::jemalloc target.") + endif() + set(_jemalloc_found TRUE) + endif() + endif() + + # Determine the version of the found jemalloc. + get_target_property(_jemalloc_include_dirs Jemalloc::jemalloc INTERFACE_INCLUDE_DIRECTORIES) + list (GET _jemalloc_include_dirs 0 _jemalloc_include_dir) set(_version_regex "^#define[ \t]+JEMALLOC_VERSION[ \t]+\"([^\"]+)\".*") - file(STRINGS "${JEMALLOC_INCLUDE_DIR}/jemalloc/jemalloc.h" + file(STRINGS "${_jemalloc_include_dir}/jemalloc/jemalloc.h" JEMALLOC_VERSION REGEX "${_version_regex}") string(REGEX REPLACE "${_version_regex}" "\\1" JEMALLOC_VERSION "${JEMALLOC_VERSION}") unset(_version_regex) -endif() - -include(FindPackageHandleStandardArgs) -# handle the QUIETLY and REQUIRED arguments and set JEMALLOC_FOUND to TRUE -# if all listed variables are TRUE and the requested version matches. -find_package_handle_standard_args(Jemalloc REQUIRED_VARS - JEMALLOC_LIBRARY JEMALLOC_INCLUDE_DIR - VERSION_VAR JEMALLOC_VERSION) -if(JEMALLOC_FOUND) - set(JEMALLOC_LIBRARIES ${JEMALLOC_LIBRARY}) - set(JEMALLOC_INCLUDE_DIRS ${JEMALLOC_INCLUDE_DIR}) -endif() + # handle the QUIET and REQUIRED arguments, verify version (if + # necessary), report found status, and set JEMALLOC_FOUND to TRUE. + include(FindPackageHandleStandardArgs) + find_package_handle_standard_args( + Jemalloc VERSION_VAR JEMALLOC_VERSION + REQUIRED_VARS _jemalloc_found) + set(FindJemalloc_included TRUE) -mark_as_advanced(JEMALLOC_INCLUDE_DIR JEMALLOC_LIBRARY) \ No newline at end of file +endif (NOT FindJemalloc_included) \ No newline at end of file diff --git a/faiss/CMakeLists.txt b/faiss/CMakeLists.txt index 65980d205e..145659879e 100644 --- a/faiss/CMakeLists.txt +++ b/faiss/CMakeLists.txt @@ -336,13 +336,9 @@ endif() if(FAISS_USE_JEMALLOC) find_package(Jemalloc REQUIRED) add_definitions(-DUSE_JEMALLOC) - target_link_libraries(faiss PRIVATE ${JEMALLOC_LIBRARIES}) - target_link_libraries(faiss_avx2 PRIVATE ${JEMALLOC_LIBRARIES}) - target_link_libraries(faiss_avx512 PRIVATE ${JEMALLOC_LIBRARIES}) - - target_include_directories(faiss PRIVATE ${JEMALLOC_INCLUDE_DIRS}) - target_include_directories(faiss_avx2 PRIVATE ${JEMALLOC_INCLUDE_DIRS}) - target_include_directories(faiss_avx512 PRIVATE ${JEMALLOC_INCLUDE_DIRS}) + target_link_libraries(faiss PRIVATE Jemalloc::jemalloc) + target_link_libraries(faiss_avx2 PRIVATE Jemalloc::jemalloc) + target_link_libraries(faiss_avx512 PRIVATE Jemalloc::jemalloc) endif() From 21a81a9d30e310ad4d6df619a4ebe35424b4a6e7 Mon Sep 17 00:00:00 2001 From: CascadingRadium Date: Thu, 6 Feb 2025 12:11:49 +0530 Subject: [PATCH 5/7] windows fix --- cmake/FindJemalloc.cmake | 5 +++++ faiss/impl/platform_macros.h | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/cmake/FindJemalloc.cmake b/cmake/FindJemalloc.cmake index 998a475058..a22f85f05d 100644 --- a/cmake/FindJemalloc.cmake +++ b/cmake/FindJemalloc.cmake @@ -37,6 +37,11 @@ if (NOT FindJemalloc_included) endif() endif() + # If still not found, report an error + if(NOT _jemalloc_found) + message(FATAL_ERROR "Could not find Jemalloc. Install Jemalloc or provide -DJemalloc_ROOT=.") + endif() + # Determine the version of the found jemalloc. get_target_property(_jemalloc_include_dirs Jemalloc::jemalloc INTERFACE_INCLUDE_DIRECTORIES) list (GET _jemalloc_include_dirs 0 _jemalloc_include_dir) diff --git a/faiss/impl/platform_macros.h b/faiss/impl/platform_macros.h index a6c9cf27ce..14a3b46270 100644 --- a/faiss/impl/platform_macros.h +++ b/faiss/impl/platform_macros.h @@ -12,6 +12,11 @@ #include #include +#ifdef USE_JEMALLOC +#define JEMALLOC_MANGLE +#include +#endif + #ifdef _MSC_VER /******************************************************* @@ -30,9 +35,18 @@ #define __PRETTY_FUNCTION__ __FUNCSIG__ +#ifndef USE_JEMALLOC +// no jemalloc on windows, and windows does not have posix_memalign +// _aligned_malloc is the equivalent #define posix_memalign(p, a, s) \ (((*(p)) = _aligned_malloc((s), (a))), *(p) ? 0 : errno) +// _aligned_free is the equivalent of free for _aligned_malloc #define posix_memalign_free _aligned_free +#else +// je_posix_memalign is available on jemalloc +// posix_memalign_free is defined as je_free +#define posix_memalign_free free +#endif // aligned should be in front of the declaration #define ALIGNED(x) __declspec(align(x)) From aba40f18c65577f8ac47bd7d25afcf1285d2dc43 Mon Sep 17 00:00:00 2001 From: CascadingRadium Date: Thu, 6 Feb 2025 15:16:42 +0530 Subject: [PATCH 6/7] fix stuff --- .gitignore | 1 + CMakeLists.txt | 4 ++-- c_api/example_c.c | 1 + c_api/index_io_c_ex.h | 7 ------- c_api/jemalloc_override_c.cpp | 13 +++++++++++-- c_api/macros_impl.h | 1 + faiss/impl/FaissException.cpp | 1 + faiss/impl/platform_macros.h | 8 +++----- faiss/jemalloc_override.cpp | 9 +++++++-- faiss/jemalloc_override.h | 20 ++++++++++++++++++++ faiss/utils/AlignedTable.h | 1 + 11 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 faiss/jemalloc_override.h diff --git a/.gitignore b/.gitignore index caab1304c8..158c2cfc1e 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ /autom4te.cache/ /makefile.inc /bin/ +/build/ /c_api/bin/ /c_api/gpu/bin/ /tests/test diff --git a/CMakeLists.txt b/CMakeLists.txt index 1693bb59f3..39d562a8c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -90,9 +90,9 @@ add_subdirectory(demos) add_subdirectory(benchs) add_subdirectory(tutorial/cpp) -# CTest must be included in the top level to enable `make test` target. -include(CTest) if(BUILD_TESTING) + # CTest must be included in the top level to enable `make test` target. + include(CTest) add_subdirectory(tests) if(FAISS_ENABLE_GPU) diff --git a/c_api/example_c.c b/c_api/example_c.c index 299305cf3f..59bd957c4f 100644 --- a/c_api/example_c.c +++ b/c_api/example_c.c @@ -20,6 +20,7 @@ #include "impl/AuxIndexStructures_c.h" #include "index_factory_c.h" #include "index_io_c.h" +#include "macros_impl.h" #define FAISS_TRY(C) \ { \ diff --git a/c_api/index_io_c_ex.h b/c_api/index_io_c_ex.h index 85ee409379..b0217bdab8 100644 --- a/c_api/index_io_c_ex.h +++ b/c_api/index_io_c_ex.h @@ -17,13 +17,6 @@ #include "Index_c.h" #include "faiss_c.h" -#ifdef USE_JEMALLOC - -#define JEMALLOC_MANGLE -#include - -#endif - #ifdef __cplusplus extern "C" { #endif diff --git a/c_api/jemalloc_override_c.cpp b/c_api/jemalloc_override_c.cpp index 77292309f0..b89c2fc7b0 100644 --- a/c_api/jemalloc_override_c.cpp +++ b/c_api/jemalloc_override_c.cpp @@ -1,6 +1,15 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +// Copyright 2004-present Facebook. All Rights Reserved. +// -*- c++ -*- + #ifdef USE_JEMALLOC -#define JEMALLOC_MANGLE -#include +#include #endif /* USE_JEMALLOC */ diff --git a/c_api/macros_impl.h b/c_api/macros_impl.h index 48c5efffbc..623047a648 100644 --- a/c_api/macros_impl.h +++ b/c_api/macros_impl.h @@ -18,6 +18,7 @@ #include #include "error_impl.h" #include "faiss_c.h" +#include #ifdef NDEBUG #define CATCH_AND_HANDLE \ diff --git a/faiss/impl/FaissException.cpp b/faiss/impl/FaissException.cpp index 3dcf47a5ea..fb0cb61671 100644 --- a/faiss/impl/FaissException.cpp +++ b/faiss/impl/FaissException.cpp @@ -14,6 +14,7 @@ #include #endif +#include namespace faiss { FaissException::FaissException(const std::string& m) : msg(m) {} diff --git a/faiss/impl/platform_macros.h b/faiss/impl/platform_macros.h index 14a3b46270..e3f2817275 100644 --- a/faiss/impl/platform_macros.h +++ b/faiss/impl/platform_macros.h @@ -12,11 +12,6 @@ #include #include -#ifdef USE_JEMALLOC -#define JEMALLOC_MANGLE -#include -#endif - #ifdef _MSC_VER /******************************************************* @@ -45,6 +40,9 @@ #else // je_posix_memalign is available on jemalloc // posix_memalign_free is defined as je_free +// it MUST be ensured that the jemalloc_override.h file is imported +// AFTER whichever file imported the platform_macros.h file +// (which is where posix_memalign_free -> free -> je_free is defined) #define posix_memalign_free free #endif diff --git a/faiss/jemalloc_override.cpp b/faiss/jemalloc_override.cpp index 602d40c082..2138d26049 100644 --- a/faiss/jemalloc_override.cpp +++ b/faiss/jemalloc_override.cpp @@ -1,3 +1,9 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ #ifdef USE_JEMALLOC // ---------------------------------------------------------------------------- // This header provides convenient overrides for the new and @@ -7,8 +13,7 @@ // // See // --------------------------------------------------------------------------- -#define JEMALLOC_MANGLE -#include +#include "jemalloc_override.h" #include #if __cplusplus > 202002L diff --git a/faiss/jemalloc_override.h b/faiss/jemalloc_override.h new file mode 100644 index 0000000000..3659a43e48 --- /dev/null +++ b/faiss/jemalloc_override.h @@ -0,0 +1,20 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +// -*- c++ -*- + +#ifndef JEMALLOC_OVERRIDE_H +#define JEMALLOC_OVERRIDE_H + +#ifdef USE_JEMALLOC + +#define JEMALLOC_MANGLE +#include + +#endif /* USE_JEMALLOC */ + +#endif /* JEMALLOC_OVERRIDE_H */ \ No newline at end of file diff --git a/faiss/utils/AlignedTable.h b/faiss/utils/AlignedTable.h index 05adb1c0d0..5a0e903ffc 100644 --- a/faiss/utils/AlignedTable.h +++ b/faiss/utils/AlignedTable.h @@ -15,6 +15,7 @@ #include #include +#include namespace faiss { From 4820f950c03fcc541570c658085e4742f105cb3f Mon Sep 17 00:00:00 2001 From: CascadingRadium Date: Fri, 7 Feb 2025 14:08:39 +0530 Subject: [PATCH 7/7] formatting changes --- cmake/FindJemalloc.cmake | 2 +- faiss/jemalloc_override.cpp | 2 +- faiss/jemalloc_override.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmake/FindJemalloc.cmake b/cmake/FindJemalloc.cmake index a22f85f05d..fe0a955455 100644 --- a/cmake/FindJemalloc.cmake +++ b/cmake/FindJemalloc.cmake @@ -60,4 +60,4 @@ if (NOT FindJemalloc_included) REQUIRED_VARS _jemalloc_found) set(FindJemalloc_included TRUE) -endif (NOT FindJemalloc_included) \ No newline at end of file +endif (NOT FindJemalloc_included) diff --git a/faiss/jemalloc_override.cpp b/faiss/jemalloc_override.cpp index 2138d26049..96ad1a696d 100644 --- a/faiss/jemalloc_override.cpp +++ b/faiss/jemalloc_override.cpp @@ -137,4 +137,4 @@ void operator delete[](void* ptr, const std::nothrow_t& tag) noexcept { free(ptr); } -#endif // USE_JEMALLOC \ No newline at end of file +#endif // USE_JEMALLOC diff --git a/faiss/jemalloc_override.h b/faiss/jemalloc_override.h index 3659a43e48..8796f8cdea 100644 --- a/faiss/jemalloc_override.h +++ b/faiss/jemalloc_override.h @@ -17,4 +17,4 @@ #endif /* USE_JEMALLOC */ -#endif /* JEMALLOC_OVERRIDE_H */ \ No newline at end of file +#endif /* JEMALLOC_OVERRIDE_H */