From 1a81bdbfa714ffb9c0dd1f592bf1ceb14188f684 Mon Sep 17 00:00:00 2001 From: Steven Hahn Date: Wed, 5 Nov 2014 12:02:28 -0500 Subject: [PATCH] Refs #10456. parallelize another algorithm --- .../inc/MantidAlgorithms/He3TubeEfficiency.h | 1 + .../Algorithms/src/BinaryOperation.cpp | 17 +--- .../Algorithms/src/He3TubeEfficiency.cpp | 91 +++++++++---------- .../Kernel/inc/MantidKernel/MultiThreaded.h | 30 +++--- 4 files changed, 63 insertions(+), 76 deletions(-) diff --git a/Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/He3TubeEfficiency.h b/Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/He3TubeEfficiency.h index 436944e8ad83..2b983e95d753 100644 --- a/Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/He3TubeEfficiency.h +++ b/Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/He3TubeEfficiency.h @@ -109,6 +109,7 @@ class DLLExport He3TubeEfficiency: public API::Algorithm std::vector spectraSkipped; /// Algorithm progress keeper API::Progress *progress; + Kernel::Mutex deteff_shapecachea,deteff_shapecacheb,deteff_shapecachec; }; } // namespace Algorithms diff --git a/Code/Mantid/Framework/Algorithms/src/BinaryOperation.cpp b/Code/Mantid/Framework/Algorithms/src/BinaryOperation.cpp index 8f0b7e08a619..897c4532ac2d 100644 --- a/Code/Mantid/Framework/Algorithms/src/BinaryOperation.cpp +++ b/Code/Mantid/Framework/Algorithms/src/BinaryOperation.cpp @@ -480,8 +480,7 @@ namespace Mantid if (m_eout) { // ---- The output is an EventWorkspace ------ - //BEGIN_PARALLEL_FOR(THREADSAFE(m_lhs,m_rhs,m_out),0,numHists,i) - BEGIN_PARALLEL_FOR(false,0,numHists,i) + BEGIN_PARALLEL_FOR(THREADSAFE(m_lhs,m_rhs,m_out),0,numHists,i) { PARALLEL_START_INTERUPT_REGION m_out->setX(i, m_lhs->refX(i)); @@ -495,8 +494,7 @@ namespace Mantid else { // ---- Histogram Output ----- - //BEGIN_PARALLEL_FOR(THREADSAFE(m_lhs,m_rhs,m_out),0,numHists,i) - BEGIN_PARALLEL_FOR(false,0,numHists,i) + BEGIN_PARALLEL_FOR(THREADSAFE(m_lhs,m_rhs,m_out),0,numHists,i) { PARALLEL_START_INTERUPT_REGION m_out->setX(i,m_lhs->refX(i)); @@ -529,8 +527,7 @@ namespace Mantid if (m_eout) { // ---- The output is an EventWorkspace ------ - //BEGIN_PARALLEL_FOR(THREADSAFE(m_lhs,m_rhs,m_out),0,numHists,i) - BEGIN_PARALLEL_FOR(false,0,numHists,i) + BEGIN_PARALLEL_FOR(THREADSAFE(m_lhs,m_rhs,m_out),0,numHists,i) { PARALLEL_START_INTERUPT_REGION const double rhsY = m_rhs->readY(i)[0]; @@ -551,7 +548,6 @@ namespace Mantid { // ---- Histogram Output ----- BEGIN_PARALLEL_FOR(THREADSAFE(m_lhs,m_rhs,m_out),0,numHists,i) - //BEGIN_PARALLEL_FOR(false,0,numHists,i) { PARALLEL_START_INTERUPT_REGION const double rhsY = m_rhs->readY(i)[0]; @@ -604,7 +600,6 @@ namespace Mantid // Now loop over the spectra of the left hand side calling the virtual function const std::size_t numHists = m_lhs->getNumberHistograms(); BEGIN_PARALLEL_FOR(THREADSAFE(m_lhs,m_rhs,m_out),0,numHists,i) - //BEGIN_PARALLEL_FOR(false,0,numHists,i) { PARALLEL_START_INTERUPT_REGION //m_out->setX(i,m_lhs->refX(i)); //unnecessary - that was copied before. @@ -628,7 +623,6 @@ namespace Mantid // Now loop over the spectra of the left hand side calling the virtual function const std::size_t numHists = m_lhs->getNumberHistograms(); BEGIN_PARALLEL_FOR(THREADSAFE(m_lhs,m_rhs,m_out),0,numHists,i) - //BEGIN_PARALLEL_FOR(false,0,numHists,i) { PARALLEL_START_INTERUPT_REGION //m_out->setX(i,m_lhs->refX(i)); //unnecessary - that was copied before. @@ -655,7 +649,6 @@ namespace Mantid // Now loop over the spectra of the left hand side calling the virtual function const std::size_t numHists = m_lhs->getNumberHistograms(); BEGIN_PARALLEL_FOR(THREADSAFE(m_lhs,m_rhs,m_out),0,numHists,i) - //BEGIN_PARALLEL_FOR(false,0,numHists,i) { PARALLEL_START_INTERUPT_REGION m_out->setX(i,m_lhs->refX(i)); @@ -703,7 +696,6 @@ namespace Mantid // Now loop over the spectra of each one calling the virtual function const std::size_t numHists = m_lhs->getNumberHistograms(); BEGIN_PARALLEL_FOR(THREADSAFE(m_lhs,m_rhs,m_out),0,numHists,i) - //BEGIN_PARALLEL_FOR(false,0,numHists,i) { PARALLEL_START_INTERUPT_REGION m_progress->report(this->name()); @@ -786,7 +778,6 @@ namespace Mantid // Now loop over the spectra of each one calling the virtual function const std::size_t numHists = m_lhs->getNumberHistograms(); BEGIN_PARALLEL_FOR(THREADSAFE(m_lhs,m_rhs,m_out),0,numHists,i) - //BEGIN_PARALLEL_FOR(false,0,numHists,i) { PARALLEL_START_INTERUPT_REGION m_progress->report(this->name()); @@ -865,7 +856,6 @@ namespace Mantid ParameterMap &pmap = out->instrumentParameters(); Mutex BinaryOperation_masking; BEGIN_PARALLEL_FOR(THREADSAFE(out),0,m_indicesToMask.size(),i) - //BEGIN_PARALLEL_FOR(false,0,m_indicesToMask.size(),i) { if (!m_parallelException && !m_cancel) { @@ -1035,7 +1025,6 @@ namespace Mantid const detid2index_map rhs_det_to_wi = rhs->getDetectorIDToWorkspaceIndexMap(); BEGIN_PARALLEL_FOR(true,0,lhs_nhist,lhsWI) - //BEGIN_PARALLEL_FOR(false,0,lhs_nhist,lhsWI) { bool done=false; diff --git a/Code/Mantid/Framework/Algorithms/src/He3TubeEfficiency.cpp b/Code/Mantid/Framework/Algorithms/src/He3TubeEfficiency.cpp index fd0a4739594f..af3153b7ff3e 100644 --- a/Code/Mantid/Framework/Algorithms/src/He3TubeEfficiency.cpp +++ b/Code/Mantid/Framework/Algorithms/src/He3TubeEfficiency.cpp @@ -107,9 +107,8 @@ void He3TubeEfficiency::exec() std::size_t numHists = this->inputWS->getNumberHistograms(); this->progress = new API::Progress(this, 0.0, 1.0, numHists); - - PARALLEL_FOR2(inputWS, outputWS) - for (int i = 0; i < static_cast(numHists); ++i ) + Kernel::Mutex deteff_invalid; + BEGIN_PARALLEL_FOR(THREADSAFE(inputWS,outputWS),0,numHists,i) { PARALLEL_START_INTERUPT_REGION @@ -125,8 +124,8 @@ void He3TubeEfficiency::exec() Mantid::MantidVec& dud = this->outputWS->dataY(i); std::transform(dud.begin(), dud.end(), dud.begin(), std::bind2nd(std::multiplies(), 0)); - PARALLEL_CRITICAL(deteff_invalid) { + Kernel::Mutex::ScopedLock _lock(deteff_invalid); this->spectraSkipped.push_back(this->inputWS->getAxis(1)->spectraNo(i)); } } @@ -141,6 +140,7 @@ void He3TubeEfficiency::exec() PARALLEL_END_INTERUPT_REGION } + END_PARALLEL_FOR PARALLEL_CHECK_INTERUPT_REGION this->logErrors(); @@ -266,8 +266,8 @@ void He3TubeEfficiency::getDetectorGeometry(\ detRadius = zDist / 2.0; detAxis = Kernel::V3D(0, 1, 0); // assume radii in z and x and the axis is in the y - PARALLEL_CRITICAL(deteff_shapecachea) { + Kernel::Mutex::ScopedLock _lock(deteff_shapecachea); this->shapeCache.insert(std::pair >(shape_sptr.get(), std::pair(detRadius, detAxis))); @@ -282,8 +282,8 @@ void He3TubeEfficiency::getDetectorGeometry(\ detAxis = Kernel::V3D(1, 0, 0); // assume that y and z are radii of the cylinder's circular cross-section // and the axis is perpendicular, in the x direction - PARALLEL_CRITICAL(deteff_shapecacheb) { + Kernel::Mutex::ScopedLock _lock(deteff_shapecacheb); this->shapeCache.insert(std::pair >(shape_sptr.get(), std::pair(detRadius, detAxis))); @@ -295,8 +295,8 @@ void He3TubeEfficiency::getDetectorGeometry(\ { detRadius = xDist / 2.0; detAxis = Kernel::V3D(0, 0, 1); - PARALLEL_CRITICAL(deteff_shapecachec) { + Kernel::Mutex::ScopedLock _lock(deteff_shapecachec); this->shapeCache.insert(std::pair >(shape_sptr.get(), std::pair(detRadius, detAxis))); @@ -445,58 +445,55 @@ void He3TubeEfficiency::execEvent() std::size_t numHistograms = inputWS->getNumberHistograms(); this->progress = new API::Progress(this, 0.0, 1.0, numHistograms); - PARALLEL_FOR1(outputWS) - for (int i=0; i < static_cast(numHistograms); ++i) + Kernel::Mutex deteff_invalid; + BEGIN_PARALLEL_FOR(THREADSAFE(outputWS),0,numHistograms,i) { PARALLEL_START_INTERUPT_REGION - Geometry::IDetector_const_sptr det = inputWS->getDetector(i); - if( det->isMonitor() || det->isMasked() ) - { - continue; - } - - double exp_constant = 0.0; - try + if( !(det->isMonitor() || det->isMasked()) ) { - exp_constant = this->calculateExponential(i, det); - } - catch (std::out_of_range &) - { - // Parameters are bad so skip correction - PARALLEL_CRITICAL(deteff_invalid) + double exp_constant = 0.0; + try { - this->spectraSkipped.push_back(inputWS->getAxis(1)->spectraNo(i)); - outputWS->maskWorkspaceIndex(i); + exp_constant = this->calculateExponential(i, det); + } + catch (std::out_of_range &) + { + // Parameters are bad so skip correction + { + Kernel::Mutex::ScopedLock _lock(deteff_invalid); + this->spectraSkipped.push_back(inputWS->getAxis(1)->spectraNo(i)); + outputWS->maskWorkspaceIndex(i); + } } - } - // Do the correction - DataObjects::EventList *evlist = outputWS->getEventListPtr(i); - switch (evlist->getEventType()) - { - case API::TOF: - // Switch to weights if needed. - evlist->switchTo(API::WEIGHTED); - // Fall through - case API::WEIGHTED: - eventHelper(evlist->getWeightedEvents(), exp_constant); - break; - case API::WEIGHTED_NOTIME: - eventHelper(evlist->getWeightedEventsNoTime(), exp_constant); - break; - } + // Do the correction + DataObjects::EventList *evlist = outputWS->getEventListPtr(i); + switch (evlist->getEventType()) + { + case API::TOF: + // Switch to weights if needed. + evlist->switchTo(API::WEIGHTED); + // Fall through + case API::WEIGHTED: + eventHelper(evlist->getWeightedEvents(), exp_constant); + break; + case API::WEIGHTED_NOTIME: + eventHelper(evlist->getWeightedEventsNoTime(), exp_constant); + break; + } - this->progress->report(); + this->progress->report(); - // check for canceling the algorithm - if ( i % 1000 == 0 ) - { - interruption_point(); + // check for canceling the algorithm + if ( i % 1000 == 0 ) + { + interruption_point(); + } } - PARALLEL_END_INTERUPT_REGION } + END_PARALLEL_FOR PARALLEL_CHECK_INTERUPT_REGION outputWS->clearMRU(); diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/MultiThreaded.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/MultiThreaded.h index 53c7316770b0..b06900145772 100644 --- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/MultiThreaded.h +++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/MultiThreaded.h @@ -248,25 +248,25 @@ namespace Kernel template void parallel_for(bool parallel, std::size_t start, std::size_t end, const func& body) { - if( parallel ) - { + if( parallel ) + { #ifdef HAVE_TBB - tbb::parallel_for(start, end, body); + tbb::parallel_for(start, end, body); #elif (defined(HAVE_OPENMP) && defined(_MSC_VER)) - PRAGMA(omp parallel for) - for (long i = start; i < end; ++i) - body(static_cast(i)); + PRAGMA(omp parallel for) + for (long i = start; i < end; ++i) + body(static_cast(i)); #else - PRAGMA(omp parallel for) - for (std::size_t i = start; i < end; ++i) - body(i); + PRAGMA(omp parallel for) + for (std::size_t i = start; i < end; ++i) + body(i); #endif - } - else - { - for (std::size_t i = start; i < end; ++i) - body(i); - } + } + else + { + for (std::size_t i = start; i < end; ++i) + body(i); + } } #define BEGIN_PARALLEL_FOR(parallel,start,end,variable) \