From b7d6fc7dba2a69a1797a15e10e9a7521e018acb2 Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Sun, 30 Sep 2012 22:46:12 -0400 Subject: [PATCH] omp_get_max_threads still occasionally causes crashes even in binaries compiled with icl.. committing a workaround --- include/mitsuba/core/thread.h | 3 +++ src/integrators/photonmapper/bre.cpp | 2 +- src/libcore/thread.cpp | 21 ++++++++++++++++++--- src/subsurface/bluenoise.cpp | 2 +- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/include/mitsuba/core/thread.h b/include/mitsuba/core/thread.h index 3157ef6f..136fcfa4 100644 --- a/include/mitsuba/core/thread.h +++ b/include/mitsuba/core/thread.h @@ -174,6 +174,9 @@ private: boost::scoped_ptr d; }; +/// Variant of \c omp_get_max_threads that works on all platforms +extern MTS_EXPORT_CORE int mts_omp_get_max_threads(); + MTS_NAMESPACE_END #endif /* __MITSUBA_CORE_THREAD_H_ */ diff --git a/src/integrators/photonmapper/bre.cpp b/src/integrators/photonmapper/bre.cpp index 7eef2865..de9b3e5f 100644 --- a/src/integrators/photonmapper/bre.cpp +++ b/src/integrators/photonmapper/bre.cpp @@ -43,7 +43,7 @@ BeamRadianceEstimator::BeamRadianceEstimator(const PhotonMap *pmap, size_t looku Log(EInfo, "Computing photon radii .."); #if defined(MTS_OPENMP) - int tcount = omp_get_max_threads(); + int tcount = mts_omp_get_max_threads(); #else int tcount = 1; #endif diff --git a/src/libcore/thread.cpp b/src/libcore/thread.cpp index 5fbe4860..5caa52ad 100644 --- a/src/libcore/thread.cpp +++ b/src/libcore/thread.cpp @@ -390,9 +390,26 @@ std::string Thread::toString() const { return oss.str(); } +#if defined(MTS_OPENMP) +static int __omp_threadCount = 0; +#endif +static std::vector __unmanagedThreads; +static boost::mutex __unmanagedMutex; + +#if defined(MTS_OPENMP) +int mts_omp_get_max_threads() { + /* This function exists to sidestep an annoying + implementation bug that causes crashes on OSX */ + return __omp_threadCount; +} +#endif + void Thread::staticInitialization() { #if defined(__OSX__) __mts_autorelease_init(); +#endif +#if defined(MTS_OPENMP) + __omp_threadCount = omp_get_max_threads(); #endif detail::initializeGlobalTLS(); detail::initializeLocalTLS(); @@ -405,9 +422,6 @@ void Thread::staticInitialization() { ThreadPrivate::self->set(mainThread); } -static std::vector __unmanagedThreads; -static boost::mutex __unmanagedMutex; - Thread *Thread::registerUnmanagedThread(const std::string &name) { Thread *thread = getThread(); if (!thread) { @@ -443,6 +457,7 @@ void Thread::initializeOpenMP(size_t threadCount) { ref logger = Thread::getThread()->getLogger(); ref fResolver = Thread::getThread()->getFileResolver(); + __omp_threadCount = threadCount; omp_set_num_threads((int) threadCount); omp_set_dynamic(false); diff --git a/src/subsurface/bluenoise.cpp b/src/subsurface/bluenoise.cpp index 8c7499f9..20aecbb3 100644 --- a/src/subsurface/bluenoise.cpp +++ b/src/subsurface/bluenoise.cpp @@ -68,7 +68,7 @@ void blueNoisePointSet(const Scene *scene, const std::vector &shapes, int kmax = 8; /* Perform 8 trial runs */ #if defined(MTS_OPENMP) - int nproc = omp_get_max_threads(); + int nproc = mts_omp_get_max_threads(); #else int nproc = 1; #endif