replace nth_element() with macro that execute sort() instead for gcc 4.8.1 & 4.8.2

This commit is contained in:
Hieu Hoang 2013-11-15 10:55:38 +00:00
parent 3d37a8ffda
commit 17887a2796
12 changed files with 31 additions and 11 deletions

View File

@ -31,6 +31,15 @@ namespace MosesTuning
#define TRACE_ERR(str) { }
#endif
#if __GNUC__ == 4 && __GNUC_MINOR__ == 8 && (__GNUC_PATCHLEVEL__ == 1 || __GNUC_PATCHLEVEL__ == 2)
// gcc nth_element() bug
#define NTH_ELEMENT3(begin, middle, end) std::sort(begin, end)
#define NTH_ELEMENT4(begin, middle, end, orderer) std::sort(begin, end, orderer)
#else
#define NTH_ELEMENT3(begin, middle, end) std::nth_element(begin, middle, end)
#define NTH_ELEMENT4(begin, middle, end, orderer) std::nth_element(begin, middle, end, orderer)
#endif
const char kDefaultDelimiterSymbol[] = " ";
int verboselevel();

View File

@ -42,6 +42,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include "FeatureDataIterator.h"
#include "ScoreDataIterator.h"
#include "BleuScorer.h"
#include "Util.h"
using namespace std;
using namespace MosesTuning;
@ -232,7 +233,7 @@ int main(int argc, char** argv)
float sample_threshold = -1.0;
if (samples.size() > n_samples) {
nth_element(scores.begin(), scores.begin() + (n_samples-1), scores.end());
NTH_ELEMENT3(scores.begin(), scores.begin() + (n_samples-1), scores.end());
sample_threshold = 0.99999-scores[n_samples-1];
}

View File

@ -245,7 +245,7 @@ void ChartHypothesis::CleanupArcList()
if (!distinctNBest && m_arcList->size() > nBestSize) {
// prune arc list only if there too many arcs
nth_element(m_arcList->begin()
NTH_ELEMENT4(m_arcList->begin()
, m_arcList->begin() + nBestSize - 1
, m_arcList->end()
, CompareChartChartHypothesisTotalScore());

View File

@ -102,7 +102,7 @@ void ChartTranslationOptionList::Add(const TargetPhraseCollection &tpc,
// Prune if bursting
if (m_size == m_ruleLimit * 2) {
std::nth_element(m_collection.begin(),
NTH_ELEMENT4(m_collection.begin(),
m_collection.begin() + m_ruleLimit - 1,
m_collection.begin() + m_size,
ChartTranslationOptionOrderer());
@ -128,7 +128,7 @@ void ChartTranslationOptionList::ApplyThreshold()
assert(m_size < m_ruleLimit * 2);
// Reduce the list to the best m_ruleLimit options. The remaining
// options can be overwritten on subsequent calls to Add().
std::nth_element(m_collection.begin(),
NTH_ELEMENT4(m_collection.begin(),
m_collection.begin()+m_ruleLimit,
m_collection.begin()+m_size,
ChartTranslationOptionOrderer());

View File

@ -335,7 +335,7 @@ void Hypothesis::CleanupArcList()
if (!distinctNBest && m_arcList->size() > nBestSize * 5) {
// prune arc list only if there too many arcs
nth_element(m_arcList->begin()
NTH_ELEMENT4(m_arcList->begin()
, m_arcList->begin() + nBestSize - 1
, m_arcList->end()
, CompareHypothesisTotalScore());

View File

@ -324,7 +324,7 @@ public:
m_obj->m_tableLimit : costs.size());
// find the nth phrase according to future cost
std::nth_element(costs.begin(),nth ,costs.end());
NTH_ELEMENT3(costs.begin(),nth ,costs.end());
// add n top phrases to the return list
for(std::vector<std::pair<float,size_t> >::iterator

View File

@ -82,7 +82,7 @@ void PartialTranslOptColl::Prune()
// TRACE_ERR( "pruning partial translation options from size " << m_list.size() << std::endl);
// find nth element
nth_element(m_list.begin(),
NTH_ELEMENT4(m_list.begin(),
m_list.begin() + m_maxSize,
m_list.end(),
ComparePartialTranslationOption);

View File

@ -58,7 +58,7 @@ void TargetPhraseCollection::NthElement(size_t tableLimit)
nth = (tableLimit && tableLimit <= m_collection.size()
? m_collection.begin() + tableLimit
: m_collection.end());
std::nth_element(m_collection.begin(), nth, m_collection.end(), CompareTargetPhrase());
NTH_ELEMENT4(m_collection.begin(), nth, m_collection.end(), CompareTargetPhrase());
}
void TargetPhraseCollection::Prune(bool adhereTableLimit, size_t tableLimit)

View File

@ -129,7 +129,7 @@ PhraseDictionaryCompact::GetTargetPhraseCollectionNonCacheLEGACY(const Phrase &s
TargetPhraseVector::iterator nth =
(m_tableLimit == 0 || tpv->size() < m_tableLimit) ?
tpv->end() : tpv->begin() + m_tableLimit;
std::nth_element(tpv->begin(), nth, tpv->end(), CompareTargetPhrase());
NTH_ELEMENT4(tpv->begin(), nth, tpv->end(), CompareTargetPhrase());
for(TargetPhraseVector::iterator it = tpv->begin(); it != nth; it++) {
TargetPhrase *tp = new TargetPhrase(*it);
phraseColl->Add(tp);

View File

@ -107,7 +107,7 @@ void TranslationOptionCollection::Prune()
if (m_maxNoTransOptPerCoverage > 0 &&
fullList.size() > m_maxNoTransOptPerCoverage) {
// sort in vector
nth_element(fullList.begin(), fullList.begin() + m_maxNoTransOptPerCoverage, fullList.end(), CompareTranslationOption);
NTH_ELEMENT4(fullList.begin(), fullList.begin() + m_maxNoTransOptPerCoverage, fullList.end(), CompareTranslationOption);
totalPruned += fullList.size() - m_maxNoTransOptPerCoverage;
// delete the rest

View File

@ -58,6 +58,15 @@ namespace Moses
#define VERBOSE(level,str) { if (StaticData::Instance().GetVerboseLevel() >= level) { TRACE_ERR(str); } }
#define IFVERBOSE(level) if (StaticData::Instance().GetVerboseLevel() >= level)
#if __GNUC__ == 4 && __GNUC_MINOR__ == 8 && (__GNUC_PATCHLEVEL__ == 1 || __GNUC_PATCHLEVEL__ == 2)
// gcc nth_element() bug
#define NTH_ELEMENT3(begin, middle, end) std::sort(begin, end)
#define NTH_ELEMENT4(begin, middle, end, orderer) std::sort(begin, end, orderer)
#else
#define NTH_ELEMENT3(begin, middle, end) std::nth_element(begin, middle, end)
#define NTH_ELEMENT4(begin, middle, end, orderer) std::nth_element(begin, middle, end, orderer)
#endif
//! delete white spaces at beginning and end of string
const std::string Trim(const std::string& str, const std::string dropChars = " \t\n\r");
const std::string ToLower(const std::string& str);

View File

@ -1,6 +1,7 @@
#include "search/nbest.hh"
#include "util/pool.hh"
#include "moses/Util.h"
#include <algorithm>
#include <functional>
@ -16,7 +17,7 @@ NBestList::NBestList(std::vector<PartialEdge> &partials, util::Pool &entry_pool,
std::vector<PartialEdge>::iterator end;
if (partials.size() > keep) {
end = partials.begin() + keep;
std::nth_element(partials.begin(), end, partials.end(), std::greater<PartialEdge>());
NTH_ELEMENT4(partials.begin(), end, partials.end(), std::greater<PartialEdge>());
} else {
end = partials.end();
}