Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions gtwrap/matlab_wrapper/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class WrapperTemplate:
'''),
prefix=' ')

tbb_headers = textwrap.dedent("""
#ifdef GTSAM_USE_TBB
#include <tbb/version.h>
#if TBB_INTERFACE_VERSION >= 12060
#include <tbb/global_control.h>
#define GTSAM_WRAP_TBB_FINALIZE 1
#endif
#endif
""")

delete_all_objects = textwrap.dedent('''
void _deleteAllObjects()
{{
Expand All @@ -39,6 +49,16 @@ class WrapperTemplate:
"calling destructors, call \'clear all\' again if you plan to now recompile a wrap\\n"
"module, so that your recompiled module is used instead of the old one." << endl;
std::cout.rdbuf(outbuf);
#ifdef GTSAM_WRAP_TBB_FINALIZE
// Terminate TBB worker threads deterministically before MATLAB unloads
// this MEX file. Otherwise the workers may still be winding down when
// the image is unmapped and touch freed code, which crashes on exit.
// See oneTBB issue #977. finalize() is idempotent, returns false rather
// than throwing if the scheduler is still referenced elsewhere, and TBB
// re-initializes transparently if the module is used again.
static tbb::task_scheduler_handle tbbHandle{{tbb::attach{{}}}};
tbb::finalize(tbbHandle, std::nothrow);
#endif
}}
''')

Expand Down
2 changes: 2 additions & 0 deletions gtwrap/matlab_wrapper/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1952,9 +1952,11 @@ def generate_wrapper(self, namespace):
includes = textwrap.dedent("""\
{wrapper_file_headers}
{boost_headers}
{tbb_headers}
{includes_list}
""").format(wrapper_file_headers=self.wrapper_file_headers.strip(),
boost_headers=boost_headers,
tbb_headers=WrapperTemplate.tbb_headers,
includes_list='\n'.join(map(str, includes_list)))

preamble = self.generate_preamble()
Expand Down
19 changes: 19 additions & 0 deletions tests/expected/matlab/class_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#include <gtwrap/matlab.h>
#include <map>


#ifdef GTSAM_USE_TBB
#include <tbb/version.h>
#if TBB_INTERFACE_VERSION >= 12060
#include <tbb/global_control.h>
#define GTSAM_WRAP_TBB_FINALIZE 1
#endif
#endif

#include <folder/path/to/Test.h>

typedef Fun<double> FunDouble;
Expand Down Expand Up @@ -139,6 +148,16 @@ void _deleteAllObjects()
"calling destructors, call 'clear all' again if you plan to now recompile a wrap\n"
"module, so that your recompiled module is used instead of the old one." << endl;
std::cout.rdbuf(outbuf);
#ifdef GTSAM_WRAP_TBB_FINALIZE
// Terminate TBB worker threads deterministically before MATLAB unloads
// this MEX file. Otherwise the workers may still be winding down when
// the image is unmapped and touch freed code, which crashes on exit.
// See oneTBB issue #977. finalize() is idempotent, returns false rather
// than throwing if the scheduler is still referenced elsewhere, and TBB
// re-initializes transparently if the module is used again.
static tbb::task_scheduler_handle tbbHandle{tbb::attach{}};
tbb::finalize(tbbHandle, std::nothrow);
#endif
}

void _class_RTTIRegister() {
Expand Down
19 changes: 19 additions & 0 deletions tests/expected/matlab/enum_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
#include <map>


#ifdef GTSAM_USE_TBB
#include <tbb/version.h>
#if TBB_INTERFACE_VERSION >= 12060
#include <tbb/global_control.h>
#define GTSAM_WRAP_TBB_FINALIZE 1
#endif
#endif



typedef gtsam::Optimizer<gtsam::GaussNewtonParams> OptimizerGaussNewtonParams;

Expand Down Expand Up @@ -44,6 +53,16 @@ void _deleteAllObjects()
"calling destructors, call 'clear all' again if you plan to now recompile a wrap\n"
"module, so that your recompiled module is used instead of the old one." << endl;
std::cout.rdbuf(outbuf);
#ifdef GTSAM_WRAP_TBB_FINALIZE
// Terminate TBB worker threads deterministically before MATLAB unloads
// this MEX file. Otherwise the workers may still be winding down when
// the image is unmapped and touch freed code, which crashes on exit.
// See oneTBB issue #977. finalize() is idempotent, returns false rather
// than throwing if the scheduler is still referenced elsewhere, and TBB
// re-initializes transparently if the module is used again.
static tbb::task_scheduler_handle tbbHandle{tbb::attach{}};
tbb::finalize(tbbHandle, std::nothrow);
#endif
}

void _enum_RTTIRegister() {
Expand Down
19 changes: 19 additions & 0 deletions tests/expected/matlab/functions_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
#include <map>


#ifdef GTSAM_USE_TBB
#include <tbb/version.h>
#if TBB_INTERFACE_VERSION >= 12060
#include <tbb/global_control.h>
#define GTSAM_WRAP_TBB_FINALIZE 1
#endif
#endif





Expand All @@ -20,6 +29,16 @@ void _deleteAllObjects()
"calling destructors, call 'clear all' again if you plan to now recompile a wrap\n"
"module, so that your recompiled module is used instead of the old one." << endl;
std::cout.rdbuf(outbuf);
#ifdef GTSAM_WRAP_TBB_FINALIZE
// Terminate TBB worker threads deterministically before MATLAB unloads
// this MEX file. Otherwise the workers may still be winding down when
// the image is unmapped and touch freed code, which crashes on exit.
// See oneTBB issue #977. finalize() is idempotent, returns false rather
// than throwing if the scheduler is still referenced elsewhere, and TBB
// re-initializes transparently if the module is used again.
static tbb::task_scheduler_handle tbbHandle{tbb::attach{}};
tbb::finalize(tbbHandle, std::nothrow);
#endif
}

void _functions_RTTIRegister() {
Expand Down
19 changes: 19 additions & 0 deletions tests/expected/matlab/geometry_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/export.hpp>


#ifdef GTSAM_USE_TBB
#include <tbb/version.h>
#if TBB_INTERFACE_VERSION >= 12060
#include <tbb/global_control.h>
#define GTSAM_WRAP_TBB_FINALIZE 1
#endif
#endif

#include <gtsam/geometry/Point2.h>
#include <gtsam/geometry/Point3.h>

Expand Down Expand Up @@ -43,6 +52,16 @@ void _deleteAllObjects()
"calling destructors, call 'clear all' again if you plan to now recompile a wrap\n"
"module, so that your recompiled module is used instead of the old one." << endl;
std::cout.rdbuf(outbuf);
#ifdef GTSAM_WRAP_TBB_FINALIZE
// Terminate TBB worker threads deterministically before MATLAB unloads
// this MEX file. Otherwise the workers may still be winding down when
// the image is unmapped and touch freed code, which crashes on exit.
// See oneTBB issue #977. finalize() is idempotent, returns false rather
// than throwing if the scheduler is still referenced elsewhere, and TBB
// re-initializes transparently if the module is used again.
static tbb::task_scheduler_handle tbbHandle{tbb::attach{}};
tbb::finalize(tbbHandle, std::nothrow);
#endif
}

void _geometry_RTTIRegister() {
Expand Down
19 changes: 19 additions & 0 deletions tests/expected/matlab/inheritance_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
#include <map>


#ifdef GTSAM_USE_TBB
#include <tbb/version.h>
#if TBB_INTERFACE_VERSION >= 12060
#include <tbb/global_control.h>
#define GTSAM_WRAP_TBB_FINALIZE 1
#endif
#endif



typedef MyTemplate<gtsam::Point2> MyTemplatePoint2;
typedef MyTemplate<gtsam::Matrix> MyTemplateMatrix;
Expand Down Expand Up @@ -87,6 +96,16 @@ void _deleteAllObjects()
"calling destructors, call 'clear all' again if you plan to now recompile a wrap\n"
"module, so that your recompiled module is used instead of the old one." << endl;
std::cout.rdbuf(outbuf);
#ifdef GTSAM_WRAP_TBB_FINALIZE
// Terminate TBB worker threads deterministically before MATLAB unloads
// this MEX file. Otherwise the workers may still be winding down when
// the image is unmapped and touch freed code, which crashes on exit.
// See oneTBB issue #977. finalize() is idempotent, returns false rather
// than throwing if the scheduler is still referenced elsewhere, and TBB
// re-initializes transparently if the module is used again.
static tbb::task_scheduler_handle tbbHandle{tbb::attach{}};
tbb::finalize(tbbHandle, std::nothrow);
#endif
}

void _inheritance_RTTIRegister() {
Expand Down
19 changes: 19 additions & 0 deletions tests/expected/matlab/multiple_files_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
#include <map>


#ifdef GTSAM_USE_TBB
#include <tbb/version.h>
#if TBB_INTERFACE_VERSION >= 12060
#include <tbb/global_control.h>
#define GTSAM_WRAP_TBB_FINALIZE 1
#endif
#endif





Expand Down Expand Up @@ -44,6 +53,16 @@ void _deleteAllObjects()
"calling destructors, call 'clear all' again if you plan to now recompile a wrap\n"
"module, so that your recompiled module is used instead of the old one." << endl;
std::cout.rdbuf(outbuf);
#ifdef GTSAM_WRAP_TBB_FINALIZE
// Terminate TBB worker threads deterministically before MATLAB unloads
// this MEX file. Otherwise the workers may still be winding down when
// the image is unmapped and touch freed code, which crashes on exit.
// See oneTBB issue #977. finalize() is idempotent, returns false rather
// than throwing if the scheduler is still referenced elsewhere, and TBB
// re-initializes transparently if the module is used again.
static tbb::task_scheduler_handle tbbHandle{tbb::attach{}};
tbb::finalize(tbbHandle, std::nothrow);
#endif
}

void _multiple_files_RTTIRegister() {
Expand Down
19 changes: 19 additions & 0 deletions tests/expected/matlab/namespaces_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#include <gtwrap/matlab.h>
#include <map>


#ifdef GTSAM_USE_TBB
#include <tbb/version.h>
#if TBB_INTERFACE_VERSION >= 12060
#include <tbb/global_control.h>
#define GTSAM_WRAP_TBB_FINALIZE 1
#endif
#endif

#include <gtsam/nonlinear/Values.h>
#include <path/to/ns1.h>
#include <path/to/ns1/ClassB.h>
Expand Down Expand Up @@ -81,6 +90,16 @@ void _deleteAllObjects()
"calling destructors, call 'clear all' again if you plan to now recompile a wrap\n"
"module, so that your recompiled module is used instead of the old one." << endl;
std::cout.rdbuf(outbuf);
#ifdef GTSAM_WRAP_TBB_FINALIZE
// Terminate TBB worker threads deterministically before MATLAB unloads
// this MEX file. Otherwise the workers may still be winding down when
// the image is unmapped and touch freed code, which crashes on exit.
// See oneTBB issue #977. finalize() is idempotent, returns false rather
// than throwing if the scheduler is still referenced elsewhere, and TBB
// re-initializes transparently if the module is used again.
static tbb::task_scheduler_handle tbbHandle{tbb::attach{}};
tbb::finalize(tbbHandle, std::nothrow);
#endif
}

void _namespaces_RTTIRegister() {
Expand Down
19 changes: 19 additions & 0 deletions tests/expected/matlab/special_cases_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#include <gtwrap/matlab.h>
#include <map>


#ifdef GTSAM_USE_TBB
#include <tbb/version.h>
#if TBB_INTERFACE_VERSION >= 12060
#include <tbb/global_control.h>
#define GTSAM_WRAP_TBB_FINALIZE 1
#endif
#endif

#include <gtsam/geometry/Cal3Bundler.h>

typedef gtsam::PinholeCamera<gtsam::Cal3Bundler> PinholeCameraCal3Bundler;
Expand Down Expand Up @@ -53,6 +62,16 @@ void _deleteAllObjects()
"calling destructors, call 'clear all' again if you plan to now recompile a wrap\n"
"module, so that your recompiled module is used instead of the old one." << endl;
std::cout.rdbuf(outbuf);
#ifdef GTSAM_WRAP_TBB_FINALIZE
// Terminate TBB worker threads deterministically before MATLAB unloads
// this MEX file. Otherwise the workers may still be winding down when
// the image is unmapped and touch freed code, which crashes on exit.
// See oneTBB issue #977. finalize() is idempotent, returns false rather
// than throwing if the scheduler is still referenced elsewhere, and TBB
// re-initializes transparently if the module is used again.
static tbb::task_scheduler_handle tbbHandle{tbb::attach{}};
tbb::finalize(tbbHandle, std::nothrow);
#endif
}

void _special_cases_RTTIRegister() {
Expand Down
19 changes: 19 additions & 0 deletions tests/expected/matlab/template_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
#include <map>


#ifdef GTSAM_USE_TBB
#include <tbb/version.h>
#if TBB_INTERFACE_VERSION >= 12060
#include <tbb/global_control.h>
#define GTSAM_WRAP_TBB_FINALIZE 1
#endif
#endif



typedef ScopedTemplate<Result> ScopedTemplateResult;

Expand Down Expand Up @@ -36,6 +45,16 @@ void _deleteAllObjects()
"calling destructors, call 'clear all' again if you plan to now recompile a wrap\n"
"module, so that your recompiled module is used instead of the old one." << endl;
std::cout.rdbuf(outbuf);
#ifdef GTSAM_WRAP_TBB_FINALIZE
// Terminate TBB worker threads deterministically before MATLAB unloads
// this MEX file. Otherwise the workers may still be winding down when
// the image is unmapped and touch freed code, which crashes on exit.
// See oneTBB issue #977. finalize() is idempotent, returns false rather
// than throwing if the scheduler is still referenced elsewhere, and TBB
// re-initializes transparently if the module is used again.
static tbb::task_scheduler_handle tbbHandle{tbb::attach{}};
tbb::finalize(tbbHandle, std::nothrow);
#endif
}

void _template_RTTIRegister() {
Expand Down
Loading