From 8ca11d941d99df42664b32c101020283cc83054e Mon Sep 17 00:00:00 2001 From: Ulrich Germann Date: Sat, 21 Mar 2015 16:12:52 +0000 Subject: 1. Lifetime of tasks in ThreadPool is now managed via shared pointers. 2. Code cleanup in IOWrapper and a bit elsewhere. --- moses-cmd/LatticeMBRGrid.cpp | 77 +++++++++++++++++++++----------------------- moses-cmd/MainVW.cpp | 35 +++++++++----------- 2 files changed, 52 insertions(+), 60 deletions(-) (limited to 'moses-cmd') diff --git a/moses-cmd/LatticeMBRGrid.cpp b/moses-cmd/LatticeMBRGrid.cpp index 631c717f4..edb4176c7 100644 --- a/moses-cmd/LatticeMBRGrid.cpp +++ b/moses-cmd/LatticeMBRGrid.cpp @@ -52,6 +52,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "moses/StaticData.h" #include "util/exception.hh" +#include using namespace std; using namespace Moses; @@ -156,59 +157,55 @@ int main(int argc, char* argv[]) exit(1); } - StaticData& staticData = const_cast(StaticData::Instance()); - staticData.SetUseLatticeMBR(true); + StaticData& SD = const_cast(StaticData::Instance()); + SD.SetUseLatticeMBR(true); - IOWrapper* ioWrapper = new IOWrapper(); + boost::shared_ptr ioWrapper(new IOWrapper); if (!ioWrapper) { throw runtime_error("Failed to initialise IOWrapper"); } - size_t nBestSize = staticData.GetMBRSize(); + size_t nBestSize = SD.GetMBRSize(); if (nBestSize <= 0) { throw new runtime_error("Non-positive size specified for n-best list"); } - size_t lineCount = 0; - InputType* source = NULL; - const vector& pgrid = grid.getGrid(lmbr_p); const vector& rgrid = grid.getGrid(lmbr_r); const vector& prune_grid = grid.getGrid(lmbr_prune); const vector& scale_grid = grid.getGrid(lmbr_scale); - while(ioWrapper->ReadInput(staticData.GetInputType(),source)) { - ++lineCount; - source->SetTranslationId(lineCount); - - Manager manager(*source); - manager.Decode(); - TrellisPathList nBestList; - manager.CalcNBest(nBestSize, nBestList,true); - //grid search - for (vector::const_iterator pi = pgrid.begin(); pi != pgrid.end(); ++pi) { - float p = *pi; - staticData.SetLatticeMBRPrecision(p); - for (vector::const_iterator ri = rgrid.begin(); ri != rgrid.end(); ++ri) { - float r = *ri; - staticData.SetLatticeMBRPRatio(r); - for (vector::const_iterator prune_i = prune_grid.begin(); prune_i != prune_grid.end(); ++prune_i) { - size_t prune = (size_t)(*prune_i); - staticData.SetLatticeMBRPruningFactor(prune); - for (vector::const_iterator scale_i = scale_grid.begin(); scale_i != scale_grid.end(); ++scale_i) { - float scale = *scale_i; - staticData.SetMBRScale(scale); - cout << lineCount << " ||| " << p << " " << r << " " << prune << " " << scale << " ||| "; - vector mbrBestHypo = doLatticeMBR(manager,nBestList); - manager.OutputBestHypo(mbrBestHypo, lineCount, staticData.GetReportSegmentation(), - staticData.GetReportAllFactors(),cout); - } - } - - } + for (boost::shared_ptr source = ioWrapper->ReadInput(); + source != NULL; source = ioWrapper->ReadInput()) + { + Manager manager(*source); + manager.Decode(); + TrellisPathList nBestList; + manager.CalcNBest(nBestSize, nBestList,true); + //grid search + BOOST_FOREACH(float const& p, pgrid) + { + SD.SetLatticeMBRPrecision(p); + BOOST_FOREACH(float const& r, rgrid) + { + SD.SetLatticeMBRPRatio(r); + BOOST_FOREACH(size_t const prune_i, prune_grid) + { + SD.SetLatticeMBRPruningFactor(size_t(prune_i)); + BOOST_FOREACH(float const& scale_i, scale_grid) + { + SD.SetMBRScale(scale_i); + size_t lineCount = source->GetTranslationId(); + cout << lineCount << " ||| " << p << " " + << r << " " << size_t(prune_i) << " " << scale_i + << " ||| "; + vector mbrBestHypo = doLatticeMBR(manager,nBestList); + manager.OutputBestHypo(mbrBestHypo, lineCount, + SD.GetReportSegmentation(), + SD.GetReportAllFactors(),cout); + } + } + } + } } - - - } - } diff --git a/moses-cmd/MainVW.cpp b/moses-cmd/MainVW.cpp index a93ba8b18..00df3df80 100644 --- a/moses-cmd/MainVW.cpp +++ b/moses-cmd/MainVW.cpp @@ -124,7 +124,7 @@ int main(int argc, char** argv) PrintUserTime("Created input-output object"); } - IOWrapper* ioWrapper = new IOWrapper(); + boost::shared_ptr ioWrapper(new IOWrapper()); if (ioWrapper == NULL) { cerr << "Error; Failed to create IO object" << endl; exit(1); @@ -143,37 +143,32 @@ int main(int argc, char** argv) #endif // main loop over set of input sentences - InputType* source = NULL; - size_t lineCount = staticData.GetStartTranslationId(); - while(ioWrapper->ReadInput(staticData.GetInputType(),source)) { - source->SetTranslationId(lineCount); - IFVERBOSE(1) { - ResetUserTime(); - } + + boost::shared_ptr source; + while ((source = ioWrapper->ReadInput()) != NULL) + { + IFVERBOSE(1) { ResetUserTime(); } - FeatureFunction::CallChangeSource(source); + InputType* foo = source.get(); + FeatureFunction::CallChangeSource(foo); - // set up task of training one sentence - TrainingTask* task = new TrainingTask(source, *ioWrapper); + // set up task of training one sentence + boost::shared_ptr + task(new TrainingTask(source.get(), *ioWrapper)); - // execute task + // execute task #ifdef WITH_THREADS - pool.Submit(task); + pool.Submit(task); #else - task->Run(); - delete task; + task->Run(); #endif - - source = NULL; //make sure it doesn't get deleted - ++lineCount; - } + } // we are done, finishing up #ifdef WITH_THREADS pool.Stop(true); //flush remaining jobs #endif - delete ioWrapper; FeatureFunction::Destroy(); } catch (const std::exception &e) { -- cgit v1.2.3