Commit 26859603e1ed3522e4c5fe56594fb0693b4eef47

Authored by Charles Otto
1 parent 07d5f49d

Simplify handling of parallelism

Default parallelism to IdealThreadCount+1, remove checks for input parallelism
> IdealThreadCount, since we find that a higher thread count can help in some
circumstances.

Don't use release/reserve thread to increase the size of the thread pool in
init, instead just set the max pool size directly.
openbr/openbr_plugin.cpp
... ... @@ -801,10 +801,8 @@ void br::Context::setProperty(const QString &key, const QString &value)
801 801 qDebug("Set %s%s", qPrintable(key), value.isEmpty() ? "" : qPrintable(" to " + value));
802 802  
803 803 if (key == "parallelism") {
804   - const int maxThreads = std::max(1, QThread::idealThreadCount());
805   - if (parallelism == 0) parallelism = 1;
806   -
807   - QThreadPool::globalInstance()->setMaxThreadCount(parallelism ? std::min(maxThreads, abs(parallelism)) : maxThreads);
  804 + if (parallelism <= 0) parallelism = 1;
  805 + QThreadPool::globalInstance()->setMaxThreadCount(parallelism);
808 806 } else if (key == "log") {
809 807 logFile.close();
810 808 if (log.isEmpty()) return;
... ... @@ -887,8 +885,7 @@ void br::Context::initialize(int &amp;argc, char *argv[], QString sdkPath)
887 885 }
888 886 Globals->sdkPath = sdkPath;
889 887  
890   - // Empirical evidence suggests an extra thread helps achieve full CPU utilization
891   - QThreadPool::globalInstance()->releaseThread();
  888 + QThreadPool::globalInstance()->setMaxThreadCount(Globals->parallelism);
892 889  
893 890 // Trigger registered initializers
894 891 QList< QSharedPointer<Initializer> > initializers = Factory<Initializer>::makeAll();
... ... @@ -898,9 +895,6 @@ void br::Context::initialize(int &amp;argc, char *argv[], QString sdkPath)
898 895  
899 896 void br::Context::finalize()
900 897 {
901   - // Undo the 'releaseThread()' in 'initialize()'
902   - QThreadPool::globalInstance()->reserveThread();
903   -
904 898 // Trigger registered finalizers
905 899 QList< QSharedPointer<Initializer> > initializers = Factory<Initializer>::makeAll();
906 900 foreach (const QSharedPointer<Initializer> &initializer, initializers)
... ... @@ -1194,9 +1188,8 @@ private:
1194 1188 templatesList[i] = Downsample(templatesList[i], transforms[i]);
1195 1189  
1196 1190 QFutureSynchronizer<void> futures;
1197   - for (int i=0; i<templatesList.size(); i++) {
  1191 + for (int i=0; i<templatesList.size(); i++)
1198 1192 futures.addFuture(QtConcurrent::run(_train, transforms[i], &templatesList[i]));
1199   - }
1200 1193 futures.waitForFinished();
1201 1194 }
1202 1195  
... ...
openbr/openbr_plugin.h
... ... @@ -547,7 +547,7 @@ public:
547 547 * \brief The number of threads to use.
548 548 */
549 549 Q_PROPERTY(int parallelism READ get_parallelism WRITE set_parallelism RESET reset_parallelism)
550   - BR_PROPERTY(int, parallelism, std::max(1, QThread::idealThreadCount()))
  550 + BR_PROPERTY(int, parallelism, std::max(1, QThread::idealThreadCount()+1))
551 551  
552 552 /*!
553 553 * \brief The maximum number of templates to process in parallel.
... ...