Skip to content

Commit 0e6ca3f

Browse files
author
peng.li24
committed
refactor: auto-discover numpy .so path via /proc/self/maps — remove bridge_init requirement
- resolve_svml() now lazily discovers _multiarray_umath.so on first call by scanning /proc/self/maps — no explicit bridge_init() needed. - bridge_init() deprecated to no-op for backward compatibility. - module.cpp no longer includes svml_bridge.h or calls bridge_init. - Downstream code never touches detail:: — core API auto-proxies transparently. - All 475 tests pass.
1 parent d234458 commit 0e6ca3f

2 files changed

Lines changed: 38 additions & 16 deletions

File tree

numpy/svml_bridge.h

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <cmath>
2222
#include <cstdio>
2323
#include <dlfcn.h>
24+
#include <fstream>
25+
#include <string>
2426
#include "npy_math_float.h"
2527

2628
#ifdef __AVX512F__
@@ -31,19 +33,49 @@ namespace numpy {
3133
namespace detail {
3234
// Internal dispatch namespace — use numpy::exp() etc., not numpy::detail::exp().
3335
// All math functions are resolved at runtime from numpy's _multiarray_umath.so.
36+
//
37+
// The shared library handle is auto-discovered on first use by scanning
38+
// /proc/self/maps — no explicit bridge_init() call needed.
3439

3540
inline void* g_svml_handle = nullptr;
3641

42+
// Auto-discover numpy's _multiarray_umath shared library path via /proc/self/maps.
43+
// Called lazily from resolve_svml() on first use.
44+
inline const char* find_umath_path() {
45+
static std::string path;
46+
static bool tried = false;
47+
if (tried) return path.empty() ? nullptr : path.c_str();
48+
tried = true;
49+
50+
std::ifstream maps("/proc/self/maps");
51+
std::string line;
52+
while (std::getline(maps, line)) {
53+
if (line.find("_multiarray_umath") != std::string::npos &&
54+
line.find(".so") != std::string::npos) {
55+
auto pos = line.rfind('/');
56+
auto start = line.rfind(' ', pos);
57+
if (start != std::string::npos && pos != std::string::npos) {
58+
path = line.substr(start + 1);
59+
break;
60+
}
61+
}
62+
}
63+
return path.empty() ? nullptr : path.c_str();
64+
}
65+
66+
// DEPRECATED — kept for backward compatibility with code that still calls it.
67+
// resolve_svml() now auto-discovers the .so path; explicit init is unnecessary.
3768
inline void bridge_init(const char* numpy_so_path) {
38-
static bool initialized = false;
39-
if (initialized || !numpy_so_path) return;
40-
initialized = true;
41-
g_svml_handle = dlopen(numpy_so_path, RTLD_NOLOAD | RTLD_LAZY);
69+
(void)numpy_so_path;
4270
}
4371

4472
inline void* resolve_svml(const char* name) {
45-
void* h = g_svml_handle;
46-
if (h) return dlsym(h, name);
73+
// Lazy init: auto-discover numpy's shared library on first call
74+
if (!g_svml_handle) {
75+
const char* path = find_umath_path();
76+
if (path) g_svml_handle = dlopen(path, RTLD_NOLOAD | RTLD_LAZY);
77+
}
78+
if (g_svml_handle) return dlsym(g_svml_handle, name);
4779
return nullptr;
4880
}
4981

tests/module.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "core_py.h"
88
#include "linalg_py.h"
99
#include "einsum_py.h"
10-
#include "../numpy/svml_bridge.h"
1110

1211
namespace py = pybind11;
1312

@@ -43,15 +42,6 @@ namespace py = pybind11;
4342
PYBIND11_MODULE(numpycpp, m) {
4443
m.doc() = "C++ pixel-level alignment of Python numpy, powered by Eigen";
4544

46-
// Initialize SVML bridge via numpy's _multiarray_umath.so.
47-
try {
48-
py::module_ np_core = py::module_::import("numpy.core._multiarray_umath");
49-
std::string umath_path = np_core.attr("__file__").cast<std::string>();
50-
numpy::detail::bridge_init(umath_path.c_str());
51-
} catch (...) {
52-
// Fall back: SVML → libm
53-
}
54-
5545
// -- linalg submodule --------------------------------------------------
5646
py::module_ la = m.def_submodule("linalg", "numpy.linalg equivalents");
5747
la.def("norm", static_cast<float(*)(const py::array_t<float>&)>(&numpy::linalg::norm));

0 commit comments

Comments
 (0)