[英]TCLAP makes multithreaded program slower
TCLAP是C ++模板化的僅標頭庫,用於解析命令行參數。
我正在使用TCLAP處理多線程程序中的命令行參數:在主函數中讀取參數,然后啟動多個線程以處理參數定義的任務(NLP任務的某些參數)。
我已經開始顯示線程每秒處理的單詞數量,而且我發現,如果我將參數硬編碼到main中,而不是使用TCLAP從cli中讀取它們,那么吞吐量將提高6倍!
我正在將gcc與-O2參數一起使用,與在編譯過程中未進行優化時(未使用TCLAP的情況)相比,我看到的速度提高了大約10倍。因此,似乎使用TCLAP會以某種方式抵消部分優勢編譯器優化。
這是我使用TCLAP的唯一功能,主要功能如下所示:
int main(int argc, char** argv)
{
uint32_t mincount;
uint32_t dim;
uint32_t contexthalfwidth;
uint32_t negsamples;
uint32_t numthreads;
uint32_t randomseed;
string corpus_fname;
string output_basefname;
string vocab_fname;
Eigen::initParallel();
try {
TCLAP::CmdLine cmd("Driver for various word embedding models", ' ', "0.1");
TCLAP::ValueArg<uint32_t> dimArg("d","dimension","dimension of word representations",false,300,"uint32_t");
TCLAP::ValueArg<uint32_t> mincountArg("m", "mincount", "required minimum occurrence count to be added to vocabulary",false,5,"uint32_t");
TCLAP::ValueArg<uint32_t> contexthalfwidthArg("c", "contexthalfwidth", "half window size of a context frame",false,15,"uint32_t");
TCLAP::ValueArg<uint32_t> numthreadsArg("t", "numthreads", "number of threads",false,12,"uint32_t");
TCLAP::ValueArg<uint32_t> negsamplesArg("n", "negsamples", "number of negative samples for skipgram model",false,15,"uint32_t");
TCLAP::ValueArg<uint32_t> randomseedArg("s", "randomseed", "seed for random number generator",false,2014,"uint32_t");
TCLAP::UnlabeledValueArg<string> corpus_fnameArg("corpusfname", "file containing the training corpus, one paragraph or sentence per line", true, "corpus", "corpusfname");
TCLAP::UnlabeledValueArg<string> output_basefnameArg("outputbasefname", "base filename for the learnt word embeddings", true, "wordreps-", "outputbasefname");
TCLAP::ValueArg<string> vocab_fnameArg("v", "vocabfname", "filename for the vocabulary and word counts", false, "wordsandcounts.txt", "filename");
cmd.add(dimArg);
cmd.add(mincountArg);
cmd.add(contexthalfwidthArg);
cmd.add(numthreadsArg);
cmd.add(randomseedArg);
cmd.add(corpus_fnameArg);
cmd.add(output_basefnameArg);
cmd.add(vocab_fnameArg);
cmd.parse(argc, argv);
mincount = mincountArg.getValue();
dim = dimArg.getValue();
contexthalfwidth = contexthalfwidthArg.getValue();
negsamples = negsamplesArg.getValue();
numthreads = numthreadsArg.getValue();
randomseed = randomseedArg.getValue();
corpus_fname = corpus_fnameArg.getValue();
output_basefname = output_basefnameArg.getValue();
vocab_fname = vocab_fnameArg.getValue();
}
catch (TCLAP::ArgException &e) {};
/*
uint32_t mincount = 5;
uint32_t dim = 50;
uint32_t contexthalfwidth = 15;
uint32_t negsamples = 15;
uint32_t numthreads = 10;
uint32_t randomseed = 2014;
string corpus_fname = "imdbtrain.txt";
string output_basefname = "wordreps-";
string vocab_fname = "wordsandcounts.txt";
*/
string test_fname = "imdbtest.txt";
string output_fname = "parreps.txt";
string countmat_fname = "counts.hdf5";
Vocabulary * vocab;
vocab = determineVocabulary(corpus_fname, mincount);
vocab->dump(vocab_fname);
Par2VecModel p2vm = Par2VecModel(corpus_fname, vocab, dim, contexthalfwidth, negsamples, randomseed);
p2vm.learn(numthreads);
p2vm.save(output_basefname);
p2vm.learnparreps(test_fname, output_fname, numthreads);
}
使用多線程的唯一地方是在Par2VecModel :: learn函數中:
void Par2VecModel::learn(uint32_t numthreads) {
thread* workers;
workers = new thread[numthreads];
uint64_t numwords = 0;
bool killflag = 0;
uint32_t randseed;
ifstream filein(corpus_fname.c_str(), ifstream::ate | ifstream::binary);
uint64_t filesize = filein.tellg();
fprintf(stderr, "Total number of in vocab words to train over: %u\n", vocab->gettotalinvocabwords());
for(uint32_t idx = 0; idx < numthreads; idx++) {
randseed = eng();
workers[idx] = thread(skipgram_training_thread, this, numthreads, idx, filesize, randseed, std::ref(numwords));
}
thread monitor(monitor_training_thread, this, numthreads, std::ref(numwords), std::ref(killflag));
for(uint32_t idx = 0; idx < numthreads; idx++)
workers[idx].join();
killflag = true;
monitor.join();
}
本節完全不涉及TCLAP,所以怎么回事? (我也在使用c ++ 11功能,因此-std = c ++ 11標志,如果有區別的話)
因此,它已經開放了很長時間,並且該建議可能不再有用,但是我首先要檢查如果用“簡單”解析器替換TCLAP會發生什么情況(即,僅在命令行中輸入參數特定的固定順序並將其轉換為正確的類型)。 問題極不可能是由TCLAP引起的(即,我無法想象這種行為的任何機制)。 但是,可以想象的是,對於硬編碼的值,編譯器能夠執行一些編譯時優化,而這些值必須是變量時是不可能的。 但是,性能差異的程度似乎有些病態,因此我仍然懷疑沒有其他事情在發生。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.