Skip to content

Commit 17887a2

Browse files
author
Hieu Hoang
committed
replace nth_element() with macro that execute sort() instead for gcc 4.8.1 & 4.8.2
1 parent 3d37a8f commit 17887a2

12 files changed

+31
-11
lines changed

mert/Util.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ namespace MosesTuning
3131
#define TRACE_ERR(str) { }
3232
#endif
3333

34+
#if __GNUC__ == 4 && __GNUC_MINOR__ == 8 && (__GNUC_PATCHLEVEL__ == 1 || __GNUC_PATCHLEVEL__ == 2)
35+
// gcc nth_element() bug
36+
#define NTH_ELEMENT3(begin, middle, end) std::sort(begin, end)
37+
#define NTH_ELEMENT4(begin, middle, end, orderer) std::sort(begin, end, orderer)
38+
#else
39+
#define NTH_ELEMENT3(begin, middle, end) std::nth_element(begin, middle, end)
40+
#define NTH_ELEMENT4(begin, middle, end, orderer) std::nth_element(begin, middle, end, orderer)
41+
#endif
42+
3443
const char kDefaultDelimiterSymbol[] = " ";
3544

3645
int verboselevel();

mert/pro.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4242
#include "FeatureDataIterator.h"
4343
#include "ScoreDataIterator.h"
4444
#include "BleuScorer.h"
45+
#include "Util.h"
4546

4647
using namespace std;
4748
using namespace MosesTuning;
@@ -232,7 +233,7 @@ int main(int argc, char** argv)
232233

233234
float sample_threshold = -1.0;
234235
if (samples.size() > n_samples) {
235-
nth_element(scores.begin(), scores.begin() + (n_samples-1), scores.end());
236+
NTH_ELEMENT3(scores.begin(), scores.begin() + (n_samples-1), scores.end());
236237
sample_threshold = 0.99999-scores[n_samples-1];
237238
}
238239

moses/ChartHypothesis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ void ChartHypothesis::CleanupArcList()
245245

246246
if (!distinctNBest && m_arcList->size() > nBestSize) {
247247
// prune arc list only if there too many arcs
248-
nth_element(m_arcList->begin()
248+
NTH_ELEMENT4(m_arcList->begin()
249249
, m_arcList->begin() + nBestSize - 1
250250
, m_arcList->end()
251251
, CompareChartChartHypothesisTotalScore());

moses/ChartTranslationOptionList.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void ChartTranslationOptionList::Add(const TargetPhraseCollection &tpc,
102102

103103
// Prune if bursting
104104
if (m_size == m_ruleLimit * 2) {
105-
std::nth_element(m_collection.begin(),
105+
NTH_ELEMENT4(m_collection.begin(),
106106
m_collection.begin() + m_ruleLimit - 1,
107107
m_collection.begin() + m_size,
108108
ChartTranslationOptionOrderer());
@@ -128,7 +128,7 @@ void ChartTranslationOptionList::ApplyThreshold()
128128
assert(m_size < m_ruleLimit * 2);
129129
// Reduce the list to the best m_ruleLimit options. The remaining
130130
// options can be overwritten on subsequent calls to Add().
131-
std::nth_element(m_collection.begin(),
131+
NTH_ELEMENT4(m_collection.begin(),
132132
m_collection.begin()+m_ruleLimit,
133133
m_collection.begin()+m_size,
134134
ChartTranslationOptionOrderer());

moses/Hypothesis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ void Hypothesis::CleanupArcList()
335335

336336
if (!distinctNBest && m_arcList->size() > nBestSize * 5) {
337337
// prune arc list only if there too many arcs
338-
nth_element(m_arcList->begin()
338+
NTH_ELEMENT4(m_arcList->begin()
339339
, m_arcList->begin() + nBestSize - 1
340340
, m_arcList->end()
341341
, CompareHypothesisTotalScore());

moses/PDTAimp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ class PDTAimp
324324
m_obj->m_tableLimit : costs.size());
325325

326326
// find the nth phrase according to future cost
327-
std::nth_element(costs.begin(),nth ,costs.end());
327+
NTH_ELEMENT3(costs.begin(),nth ,costs.end());
328328

329329
// add n top phrases to the return list
330330
for(std::vector<std::pair<float,size_t> >::iterator

moses/PartialTranslOptColl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void PartialTranslOptColl::Prune()
8282
// TRACE_ERR( "pruning partial translation options from size " << m_list.size() << std::endl);
8383

8484
// find nth element
85-
nth_element(m_list.begin(),
85+
NTH_ELEMENT4(m_list.begin(),
8686
m_list.begin() + m_maxSize,
8787
m_list.end(),
8888
ComparePartialTranslationOption);

moses/TargetPhraseCollection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void TargetPhraseCollection::NthElement(size_t tableLimit)
5858
nth = (tableLimit && tableLimit <= m_collection.size()
5959
? m_collection.begin() + tableLimit
6060
: m_collection.end());
61-
std::nth_element(m_collection.begin(), nth, m_collection.end(), CompareTargetPhrase());
61+
NTH_ELEMENT4(m_collection.begin(), nth, m_collection.end(), CompareTargetPhrase());
6262
}
6363

6464
void TargetPhraseCollection::Prune(bool adhereTableLimit, size_t tableLimit)

moses/TranslationModel/CompactPT/PhraseDictionaryCompact.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ PhraseDictionaryCompact::GetTargetPhraseCollectionNonCacheLEGACY(const Phrase &s
129129
TargetPhraseVector::iterator nth =
130130
(m_tableLimit == 0 || tpv->size() < m_tableLimit) ?
131131
tpv->end() : tpv->begin() + m_tableLimit;
132-
std::nth_element(tpv->begin(), nth, tpv->end(), CompareTargetPhrase());
132+
NTH_ELEMENT4(tpv->begin(), nth, tpv->end(), CompareTargetPhrase());
133133
for(TargetPhraseVector::iterator it = tpv->begin(); it != nth; it++) {
134134
TargetPhrase *tp = new TargetPhrase(*it);
135135
phraseColl->Add(tp);

moses/TranslationOptionCollection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void TranslationOptionCollection::Prune()
107107
if (m_maxNoTransOptPerCoverage > 0 &&
108108
fullList.size() > m_maxNoTransOptPerCoverage) {
109109
// sort in vector
110-
nth_element(fullList.begin(), fullList.begin() + m_maxNoTransOptPerCoverage, fullList.end(), CompareTranslationOption);
110+
NTH_ELEMENT4(fullList.begin(), fullList.begin() + m_maxNoTransOptPerCoverage, fullList.end(), CompareTranslationOption);
111111
totalPruned += fullList.size() - m_maxNoTransOptPerCoverage;
112112

113113
// delete the rest

moses/Util.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ namespace Moses
5858
#define VERBOSE(level,str) { if (StaticData::Instance().GetVerboseLevel() >= level) { TRACE_ERR(str); } }
5959
#define IFVERBOSE(level) if (StaticData::Instance().GetVerboseLevel() >= level)
6060

61+
#if __GNUC__ == 4 && __GNUC_MINOR__ == 8 && (__GNUC_PATCHLEVEL__ == 1 || __GNUC_PATCHLEVEL__ == 2)
62+
// gcc nth_element() bug
63+
#define NTH_ELEMENT3(begin, middle, end) std::sort(begin, end)
64+
#define NTH_ELEMENT4(begin, middle, end, orderer) std::sort(begin, end, orderer)
65+
#else
66+
#define NTH_ELEMENT3(begin, middle, end) std::nth_element(begin, middle, end)
67+
#define NTH_ELEMENT4(begin, middle, end, orderer) std::nth_element(begin, middle, end, orderer)
68+
#endif
69+
6170
//! delete white spaces at beginning and end of string
6271
const std::string Trim(const std::string& str, const std::string dropChars = " \t\n\r");
6372
const std::string ToLower(const std::string& str);

search/nbest.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "search/nbest.hh"
22

33
#include "util/pool.hh"
4+
#include "moses/Util.h"
45

56
#include <algorithm>
67
#include <functional>
@@ -16,7 +17,7 @@ NBestList::NBestList(std::vector<PartialEdge> &partials, util::Pool &entry_pool,
1617
std::vector<PartialEdge>::iterator end;
1718
if (partials.size() > keep) {
1819
end = partials.begin() + keep;
19-
std::nth_element(partials.begin(), end, partials.end(), std::greater<PartialEdge>());
20+
NTH_ELEMENT4(partials.begin(), end, partials.end(), std::greater<PartialEdge>());
2021
} else {
2122
end = partials.end();
2223
}

0 commit comments

Comments
 (0)