From: Enblend <enb...@li...> - 2012-12-15 18:34:28
|
branch: details: http://enblend.hg.sourceforge.net/hgweb/enblend/enblend/hgroot/enblend/enblend/rev/7086b2520916 changeset: 856:7086b2520916 user: Chris <cs...@us...> date: Sat Dec 15 09:25:58 2012 +0100 description: [mq]: prefer-omp-locks-to-critical diffstat: VERSION | 2 +- src/anneal.h | 40 ++++++++++++++++++++-------------------- src/openmp.h | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 21 deletions(-) diffs (129 lines): diff -r ccbb668d3762 -r 7086b2520916 VERSION --- a/VERSION Wed Dec 12 10:26:55 2012 +0100 +++ b/VERSION Sat Dec 15 09:25:58 2012 +0100 @@ -1,1 +1,1 @@ -4.2-3b832be40e80 +4.2-ccbb668d3762 diff -r ccbb668d3762 -r 7086b2520916 src/anneal.h --- a/src/anneal.h Wed Dec 12 10:26:55 2012 +0100 +++ b/src/anneal.h Sat Dec 15 09:25:58 2012 +0100 @@ -506,6 +506,9 @@ kMax = 1; size_t kmax_local = 1; + ::omp::lock lock_cerr; + ::omp::lock lock_update; + #ifdef OPENMP #pragma omp parallel firstprivate(kmax_local) #endif @@ -546,24 +549,21 @@ // Sanity check if (!costImage->isInside(newEstimate)) { -#ifdef OPENMP -#pragma omp critical(write_to_cerr) -#endif - { + lock_cerr.set(); + std::cerr << command + << ": warning: new mean field estimate outside cost image" + << std::endl; + for (unsigned int state = 0; state < localK; ++state) { std::cerr << command - << ": warning: new mean field estimate outside cost image" - << std::endl; - for (unsigned int state = 0; state < localK; ++state) { - std::cerr << command - << ": info: state " << (*stateSpace)[state] - << " weight = " - << (*stateProbabilities)[state] - << std::endl; - } - std::cerr << command - << ": info: new estimate = " << newEstimate - << std::endl; - } // omp critical + << ": info: state " << (*stateSpace)[state] + << " weight = " + << (*stateProbabilities)[state] + << std::endl; + } + std::cerr << command + << ": info: new estimate = " << newEstimate + << std::endl; + lock_cerr.unset(); // Skip this point from now on. convergedPoints[index] = true; @@ -605,10 +605,10 @@ kmax_local = std::max(kmax_local, stateProbabilities->size()); } -#ifdef OPENMP -#pragma omp critical(update_kMax) -#endif + + lock_update.set(); kMax = std::max(kMax, static_cast<unsigned int>(kmax_local)); + lock_update.unset(); } // omp parallel } diff -r ccbb668d3762 -r 7086b2520916 src/openmp.h --- a/src/openmp.h Wed Dec 12 10:26:55 2012 +0100 +++ b/src/openmp.h Sat Dec 15 09:25:58 2012 +0100 @@ -47,6 +47,27 @@ #define OPENMP_MONTH (_OPENMP % 100) +namespace omp +{ + class lock + { + public: + lock() {omp_init_lock(&lock_);} + ~lock() {omp_destroy_lock(&lock_);} + + void set() {omp_set_lock(&lock_);} + void unset() {omp_unset_lock(&lock_);} + bool test() {return omp_test_lock(&lock_);} + + private: + lock(const lock&); // not implemented + lock& operator=(const lock&); // not implemented + + omp_lock_t lock_; + }; +} + + template <class SrcImageIterator1, class SrcAccessor1, class SrcImageIterator2, class SrcAccessor2, class DestImageIterator, class DestAccessor, @@ -439,6 +460,25 @@ inline int omp_get_nested() {return 0;} +namespace omp +{ + class lock + { + public: + lock() {} + ~lock() {} + + void set() {} + void unset() {} + bool test() {return false;} + + private: + lock(const lock&); // not implemented + lock& operator=(const lock&); // not implemented + }; +} + + template <class SrcImageIterator1, class SrcAccessor1, class SrcImageIterator2, class SrcAccessor2, class DestImageIterator, class DestAccessor, |