Skip to content

Commit

Permalink
Fixed superglue deadlock on standalone (#896). Fixed some elemSize op…
Browse files Browse the repository at this point in the history
…encv asserts in debug build (876)
  • Loading branch information
matlabbe committed Sep 17, 2023
1 parent fb466a6 commit f1cd819
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 129 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ IF(WITH_PYTHON)
FIND_PACKAGE(Python3 COMPONENTS Interpreter Development NumPy)
IF(Python3_FOUND)
MESSAGE(STATUS "Found Python3")
FIND_PACKAGE(pybind11 REQUIRED)
ENDIF(Python3_FOUND)
ENDIF(WITH_PYTHON)

Expand Down
26 changes: 13 additions & 13 deletions corelib/include/rtabmap/core/PythonInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@

#include <string>
#include <rtabmap/utilite/UMutex.h>
#include <Python.h>

namespace pybind11 {
class scoped_interpreter;
class gil_scoped_release;
}

namespace rtabmap {

/**
* Create a single PythonInterface on main thread at
* global scope before any Python classes.
*/
class PythonInterface
{
public:
PythonInterface();
virtual ~PythonInterface();

protected:
std::string getTraceback(); // should be called between lock() and unlock()
void lock();
void unlock();

private:
static UMutex mutex_;
static int refCount_;

protected:
static PyThreadState * mainThreadState_;
static unsigned long mainThreadID_;
PyThreadState * threadState_;
pybind11::scoped_interpreter* guard_;
pybind11::gil_scoped_release* release_;
};

std::string getPythonTraceback();

}

#endif /* CORELIB_SRC_PYTHON_PYTHONINTERFACE_H_ */
1 change: 1 addition & 0 deletions corelib/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ IF(WITH_PYTHON AND Python3_FOUND)
${PUBLIC_LIBRARIES}
Python3::Python
Python3::NumPy
pybind11::embed
)
SET(SRC_FILES
${SRC_FILES}
Expand Down
6 changes: 3 additions & 3 deletions corelib/src/DBDriverSqlite3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4298,9 +4298,9 @@ void DBDriverSqlite3::saveQuery(const std::list<Signature *> & signatures)
{
_memoryUsedEstimate += (*i)->getMemoryUsed();
// raw data are not kept in database
_memoryUsedEstimate -= (*i)->sensorData().imageRaw().total() * (*i)->sensorData().imageRaw().elemSize();
_memoryUsedEstimate -= (*i)->sensorData().depthOrRightRaw().total() * (*i)->sensorData().depthOrRightRaw().elemSize();
_memoryUsedEstimate -= (*i)->sensorData().laserScanRaw().data().total() * (*i)->sensorData().laserScanRaw().data().elemSize();
_memoryUsedEstimate -= (*i)->sensorData().imageRaw().empty()?0:(*i)->sensorData().imageRaw().total() * (*i)->sensorData().imageRaw().elemSize();
_memoryUsedEstimate -= (*i)->sensorData().depthOrRightRaw().empty()?0:(*i)->sensorData().depthOrRightRaw().total() * (*i)->sensorData().depthOrRightRaw().elemSize();
_memoryUsedEstimate -= (*i)->sensorData().laserScanRaw().empty()?0:(*i)->sensorData().laserScanRaw().data().total() * (*i)->sensorData().laserScanRaw().data().elemSize();

stepNode(ppStmt, *i);
}
Expand Down
30 changes: 15 additions & 15 deletions corelib/src/SensorData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,23 +810,23 @@ void SensorData::setFeatures(const std::vector<cv::KeyPoint> & keypoints, const
unsigned long SensorData::getMemoryUsed() const // Return memory usage in Bytes
{
return sizeof(SensorData) +
_imageCompressed.total()*_imageCompressed.elemSize() +
_imageRaw.total()*_imageRaw.elemSize() +
_depthOrRightCompressed.total()*_depthOrRightCompressed.elemSize() +
_depthOrRightRaw.total()*_depthOrRightRaw.elemSize() +
_userDataCompressed.total()*_userDataCompressed.elemSize() +
_userDataRaw.total()*_userDataRaw.elemSize() +
_laserScanCompressed.data().total()*_laserScanCompressed.data().elemSize() +
_laserScanRaw.data().total()*_laserScanRaw.data().elemSize() +
_groundCellsCompressed.total()*_groundCellsCompressed.elemSize() +
_groundCellsRaw.total()*_groundCellsRaw.elemSize() +
_obstacleCellsCompressed.total()*_obstacleCellsCompressed.elemSize() +
_obstacleCellsRaw.total()*_obstacleCellsRaw.elemSize()+
_emptyCellsCompressed.total()*_emptyCellsCompressed.elemSize() +
_emptyCellsRaw.total()*_emptyCellsRaw.elemSize()+
(_imageCompressed.empty()?0:_imageCompressed.total()*_imageCompressed.elemSize()) +
(_imageRaw.empty()?0:_imageRaw.total()*_imageRaw.elemSize()) +
(_depthOrRightCompressed.empty()?0:_depthOrRightCompressed.total()*_depthOrRightCompressed.elemSize()) +
(_depthOrRightRaw.empty()?0:_depthOrRightRaw.total()*_depthOrRightRaw.elemSize()) +
(_userDataCompressed.empty()?0:_userDataCompressed.total()*_userDataCompressed.elemSize()) +
(_userDataRaw.empty()?0:_userDataRaw.total()*_userDataRaw.elemSize()) +
(_laserScanCompressed.empty()?0:_laserScanCompressed.data().total()*_laserScanCompressed.data().elemSize()) +
(_laserScanRaw.empty()?0:_laserScanRaw.data().total()*_laserScanRaw.data().elemSize()) +
(_groundCellsCompressed.empty()?0:_groundCellsCompressed.total()*_groundCellsCompressed.elemSize()) +
(_groundCellsRaw.empty()?0:_groundCellsRaw.total()*_groundCellsRaw.elemSize()) +
(_obstacleCellsCompressed.empty()?0:_obstacleCellsCompressed.total()*_obstacleCellsCompressed.elemSize()) +
(_obstacleCellsRaw.empty()?0:_obstacleCellsRaw.total()*_obstacleCellsRaw.elemSize())+
(_emptyCellsCompressed.empty()?0:_emptyCellsCompressed.total()*_emptyCellsCompressed.elemSize()) +
(_emptyCellsRaw.empty()?0:_emptyCellsRaw.total()*_emptyCellsRaw.elemSize())+
_keypoints.size() * sizeof(cv::KeyPoint) +
_keypoints3D.size() * sizeof(cv::Point3f) +
_descriptors.total()*_descriptors.elemSize();
(_descriptors.empty()?0:_descriptors.total()*_descriptors.elemSize());
}

void SensorData::clearCompressedData(bool images, bool scan, bool userData)
Expand Down
2 changes: 1 addition & 1 deletion corelib/src/Signature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ unsigned long Signature::getMemoryUsed(bool withSensorData) const // Return memo
total += _words.size() * (sizeof(int)*2+sizeof(std::multimap<int, cv::KeyPoint>::iterator)) + sizeof(std::multimap<int, cv::KeyPoint>);
total += _wordsKpts.size() * sizeof(cv::KeyPoint) + sizeof(std::vector<cv::KeyPoint>);
total += _words3.size() * sizeof(cv::Point3f) + sizeof(std::vector<cv::Point3f>);
total += _wordsDescriptors.total() * _wordsDescriptors.elemSize() + sizeof(cv::Mat);
total += _wordsDescriptors.empty()?0:_wordsDescriptors.total() * _wordsDescriptors.elemSize() + sizeof(cv::Mat);
total += _wordsChanged.size() * (sizeof(int)*2+sizeof(std::map<int, int>::iterator)) + sizeof(std::map<int, int>);
if(withSensorData)
{
Expand Down
26 changes: 11 additions & 15 deletions corelib/src/python/PyDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <rtabmap/utilite/UConversion.h>
#include <rtabmap/utilite/UTimer.h>

#include <pybind11/embed.h>

#define NPY_NO_DEPRECATED_API NPY_API_VERSION
#include <numpy/arrayobject.h>

Expand All @@ -32,7 +34,7 @@ PyDetector::PyDetector(const ParametersMap & parameters) :
return;
}

lock();
pybind11::gil_scoped_acquire acquire;

std::string matcherPythonDir = UDirectory::getDir(path_);
if(!matcherPythonDir.empty())
Expand All @@ -54,15 +56,13 @@ PyDetector::PyDetector(const ParametersMap & parameters) :
if(!pModule_)
{
UERROR("Module \"%s\" could not be imported! (File=\"%s\")", scriptName.c_str(), path_.c_str());
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
}

unlock();
}

PyDetector::~PyDetector()
{
lock();
pybind11::gil_scoped_acquire acquire;

if(pFunc_)
{
Expand All @@ -72,8 +72,6 @@ PyDetector::~PyDetector()
{
Py_DECREF(pModule_);
}

unlock();
}

void PyDetector::parseParameters(const ParametersMap & parameters)
Expand Down Expand Up @@ -102,7 +100,7 @@ std::vector<cv::KeyPoint> PyDetector::generateKeypointsImpl(const cv::Mat & imag
return keypoints;
}

lock();
pybind11::gil_scoped_acquire acquire;

if(!pFunc_)
{
Expand All @@ -116,7 +114,7 @@ std::vector<cv::KeyPoint> PyDetector::generateKeypointsImpl(const cv::Mat & imag
if(result == NULL)
{
UERROR("Call to \"init(...)\" in \"%s\" failed!", path_.c_str());
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
return keypoints;
}
Py_DECREF(result);
Expand All @@ -129,7 +127,7 @@ std::vector<cv::KeyPoint> PyDetector::generateKeypointsImpl(const cv::Mat & imag
else
{
UERROR("Cannot find method \"detect(...)\" in %s", path_.c_str());
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
if(pFunc_)
{
Py_DECREF(pFunc_);
Expand All @@ -141,15 +139,15 @@ std::vector<cv::KeyPoint> PyDetector::generateKeypointsImpl(const cv::Mat & imag
else
{
UERROR("Cannot call method \"init(...)\" in %s", path_.c_str());
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
return keypoints;
}
Py_DECREF(pFunc);
}
else
{
UERROR("Cannot find method \"init(...)\"");
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
return keypoints;
}
UDEBUG("init time = %fs", timer.ticks());
Expand All @@ -167,7 +165,7 @@ std::vector<cv::KeyPoint> PyDetector::generateKeypointsImpl(const cv::Mat & imag
if(pReturn == NULL)
{
UERROR("Failed to call match() function!");
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
}
else
{
Expand Down Expand Up @@ -220,8 +218,6 @@ std::vector<cv::KeyPoint> PyDetector::generateKeypointsImpl(const cv::Mat & imag
Py_DECREF(pImageBuffer);
}

unlock();

return keypoints;
}

Expand Down
2 changes: 1 addition & 1 deletion corelib/src/python/PyDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
namespace rtabmap
{

class PyDetector : public Feature2D, public PythonInterface
class PyDetector : public Feature2D
{
public:
PyDetector(const ParametersMap & parameters = ParametersMap());
Expand Down
24 changes: 11 additions & 13 deletions corelib/src/python/PyMatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <rtabmap/utilite/UConversion.h>
#include <rtabmap/utilite/UTimer.h>

#include <pybind11/embed.h>

#define NPY_NO_DEPRECATED_API NPY_API_VERSION
#include <numpy/arrayobject.h>

Expand Down Expand Up @@ -39,7 +41,7 @@ PyMatcher::PyMatcher(
return;
}

lock();
pybind11::gil_scoped_acquire acquire;

std::string matcherPythonDir = UDirectory::getDir(path_);
if(!matcherPythonDir.empty())
Expand All @@ -59,15 +61,13 @@ PyMatcher::PyMatcher(
if(!pModule_)
{
UERROR("Module \"%s\" could not be imported! (File=\"%s\")", scriptName.c_str(), path_.c_str());
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
}

unlock();
}

PyMatcher::~PyMatcher()
{
lock();
pybind11::gil_scoped_acquire acquire;
if(pFunc_)
{
Py_DECREF(pFunc_);
Expand All @@ -76,7 +76,6 @@ PyMatcher::~PyMatcher()
{
Py_DECREF(pModule_);
}
unlock();
}

std::vector<cv::DMatch> PyMatcher::match(
Expand Down Expand Up @@ -104,7 +103,7 @@ std::vector<cv::DMatch> PyMatcher::match(
imageSize.width>0 && imageSize.height>0)
{

lock();
pybind11::gil_scoped_acquire acquire;

UDEBUG("matchThreshold=%f, iterations=%d, cuda=%d", matchThreshold_, iterations_, cuda_?1:0);

Expand All @@ -120,7 +119,7 @@ std::vector<cv::DMatch> PyMatcher::match(
if(result == NULL)
{
UERROR("Call to \"init(...)\" in \"%s\" failed!", path_.c_str());
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
return matches;
}
Py_DECREF(result);
Expand All @@ -133,7 +132,7 @@ std::vector<cv::DMatch> PyMatcher::match(
else
{
UERROR("Cannot find method \"match(...)\" in %s", path_.c_str());
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
if(pFunc_)
{
Py_DECREF(pFunc_);
Expand All @@ -145,15 +144,15 @@ std::vector<cv::DMatch> PyMatcher::match(
else
{
UERROR("Cannot call method \"init(...)\" in %s", path_.c_str());
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
return matches;
}
Py_DECREF(pFunc);
}
else
{
UERROR("Cannot find method \"init(...)\"");
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
return matches;
}
UDEBUG("init time = %fs", timer.ticks());
Expand Down Expand Up @@ -216,7 +215,7 @@ std::vector<cv::DMatch> PyMatcher::match(
if(pReturn == NULL)
{
UERROR("Failed to call match() function!");
UERROR("%s", getTraceback().c_str());
UERROR("%s", getPythonTraceback().c_str());
}
else
{
Expand Down Expand Up @@ -260,7 +259,6 @@ std::vector<cv::DMatch> PyMatcher::match(

UDEBUG("Fill matches (%d/%d) and cleanup time = %fs", matches.size(), std::min(descriptorsQuery.rows, descriptorsTrain.rows), timer.ticks());
}
unlock();
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion corelib/src/python/PyMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace rtabmap
{

class PyMatcher : public PythonInterface
class PyMatcher
{
public:
PyMatcher(const std::string & pythonMatcherPath,
Expand Down
Loading

0 comments on commit f1cd819

Please sign in to comment.