Skip to content

Commit

Permalink
Refs #10456. parallelize another algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
quantumsteve committed Nov 5, 2014
1 parent c358971 commit 1a81bdb
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 76 deletions.
Expand Up @@ -109,6 +109,7 @@ class DLLExport He3TubeEfficiency: public API::Algorithm
std::vector<specid_t> spectraSkipped;
/// Algorithm progress keeper
API::Progress *progress;
Kernel::Mutex deteff_shapecachea,deteff_shapecacheb,deteff_shapecachec;
};

} // namespace Algorithms
Expand Down
17 changes: 3 additions & 14 deletions Code/Mantid/Framework/Algorithms/src/BinaryOperation.cpp
Expand Up @@ -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));
Expand All @@ -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));
Expand Down Expand Up @@ -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];
Expand All @@ -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];
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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));
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;

Expand Down
91 changes: 44 additions & 47 deletions Code/Mantid/Framework/Algorithms/src/He3TubeEfficiency.cpp
Expand Up @@ -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<int>(numHists); ++i )
Kernel::Mutex deteff_invalid;
BEGIN_PARALLEL_FOR(THREADSAFE(inputWS,outputWS),0,numHists,i)
{
PARALLEL_START_INTERUPT_REGION

Expand All @@ -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<double>(), 0));
PARALLEL_CRITICAL(deteff_invalid)
{
Kernel::Mutex::ScopedLock _lock(deteff_invalid);
this->spectraSkipped.push_back(this->inputWS->getAxis(1)->spectraNo(i));
}
}
Expand All @@ -141,6 +140,7 @@ void He3TubeEfficiency::exec()

PARALLEL_END_INTERUPT_REGION
}
END_PARALLEL_FOR
PARALLEL_CHECK_INTERUPT_REGION

this->logErrors();
Expand Down Expand Up @@ -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<const Geometry::Object *,
std::pair<double, Kernel::V3D> >(shape_sptr.get(),
std::pair<double, Kernel::V3D>(detRadius, detAxis)));
Expand All @@ -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<const Geometry::Object *,
std::pair<double, Kernel::V3D> >(shape_sptr.get(),
std::pair<double, Kernel::V3D>(detRadius, detAxis)));
Expand All @@ -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<const Geometry::Object *,
std::pair<double, Kernel::V3D> >(shape_sptr.get(),
std::pair<double, Kernel::V3D>(detRadius, detAxis)));
Expand Down Expand Up @@ -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<int>(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();
Expand Down
30 changes: 15 additions & 15 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/MultiThreaded.h
Expand Up @@ -248,25 +248,25 @@ namespace Kernel
template <class func>
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<std::size_t>(i));
PRAGMA(omp parallel for)
for (long i = start; i < end; ++i)
body(static_cast<std::size_t>(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) \
Expand Down

0 comments on commit 1a81bdb

Please sign in to comment.