From 17887a27969e83f4100bd0f4af98986e33999fbe Mon Sep 17 00:00:00 2001 From: Hieu Hoang Date: Fri, 15 Nov 2013 10:55:38 +0000 Subject: [PATCH] replace nth_element() with macro that execute sort() instead for gcc 4.8.1 & 4.8.2 --- mert/Util.h | 9 +++++++++ mert/pro.cpp | 3 ++- moses/ChartHypothesis.cpp | 2 +- moses/ChartTranslationOptionList.cpp | 4 ++-- moses/Hypothesis.cpp | 2 +- moses/PDTAimp.h | 2 +- moses/PartialTranslOptColl.cpp | 2 +- moses/TargetPhraseCollection.cpp | 2 +- .../CompactPT/PhraseDictionaryCompact.cpp | 2 +- moses/TranslationOptionCollection.cpp | 2 +- moses/Util.h | 9 +++++++++ search/nbest.cc | 3 ++- 12 files changed, 31 insertions(+), 11 deletions(-) diff --git a/mert/Util.h b/mert/Util.h index 5c9c635ab..7e6926d19 100644 --- a/mert/Util.h +++ b/mert/Util.h @@ -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(); diff --git a/mert/pro.cpp b/mert/pro.cpp index b8cf81ca3..7660fe7d0 100644 --- a/mert/pro.cpp +++ b/mert/pro.cpp @@ -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]; } diff --git a/moses/ChartHypothesis.cpp b/moses/ChartHypothesis.cpp index 1cff9046d..034905158 100644 --- a/moses/ChartHypothesis.cpp +++ b/moses/ChartHypothesis.cpp @@ -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()); diff --git a/moses/ChartTranslationOptionList.cpp b/moses/ChartTranslationOptionList.cpp index c12035f18..1297e5f37 100644 --- a/moses/ChartTranslationOptionList.cpp +++ b/moses/ChartTranslationOptionList.cpp @@ -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()); diff --git a/moses/Hypothesis.cpp b/moses/Hypothesis.cpp index ba7953e62..e3140948e 100644 --- a/moses/Hypothesis.cpp +++ b/moses/Hypothesis.cpp @@ -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()); diff --git a/moses/PDTAimp.h b/moses/PDTAimp.h index bb077945f..cc4e96cd1 100644 --- a/moses/PDTAimp.h +++ b/moses/PDTAimp.h @@ -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 >::iterator diff --git a/moses/PartialTranslOptColl.cpp b/moses/PartialTranslOptColl.cpp index 7accf3a76..709075c66 100644 --- a/moses/PartialTranslOptColl.cpp +++ b/moses/PartialTranslOptColl.cpp @@ -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); diff --git a/moses/TargetPhraseCollection.cpp b/moses/TargetPhraseCollection.cpp index 31dc98be8..c899e241d 100644 --- a/moses/TargetPhraseCollection.cpp +++ b/moses/TargetPhraseCollection.cpp @@ -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) diff --git a/moses/TranslationModel/CompactPT/PhraseDictionaryCompact.cpp b/moses/TranslationModel/CompactPT/PhraseDictionaryCompact.cpp index 876e334eb..e3c931589 100644 --- a/moses/TranslationModel/CompactPT/PhraseDictionaryCompact.cpp +++ b/moses/TranslationModel/CompactPT/PhraseDictionaryCompact.cpp @@ -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); diff --git a/moses/TranslationOptionCollection.cpp b/moses/TranslationOptionCollection.cpp index 80c0d12dd..e51f3f450 100644 --- a/moses/TranslationOptionCollection.cpp +++ b/moses/TranslationOptionCollection.cpp @@ -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 diff --git a/moses/Util.h b/moses/Util.h index 5517598f1..422449d43 100644 --- a/moses/Util.h +++ b/moses/Util.h @@ -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); diff --git a/search/nbest.cc b/search/nbest.cc index ec3322c97..acfc08049 100644 --- a/search/nbest.cc +++ b/search/nbest.cc @@ -1,6 +1,7 @@ #include "search/nbest.hh" #include "util/pool.hh" +#include "moses/Util.h" #include #include @@ -16,7 +17,7 @@ NBestList::NBestList(std::vector &partials, util::Pool &entry_pool, std::vector::iterator end; if (partials.size() > keep) { end = partials.begin() + keep; - std::nth_element(partials.begin(), end, partials.end(), std::greater()); + NTH_ELEMENT4(partials.begin(), end, partials.end(), std::greater()); } else { end = partials.end(); }