Commit 07d5f49df8e68e8cdf727431081c162d60409e9c

Authored by Charles Otto
1 parent 190b8755

Drop special casing of parallelism=0

Drop most special casing of parallelism=0, if parallelism=0 is received, set
the max number of threads to 1, and go through the same code as when more
threads are available.

Some outstanding issues, the following transforms are blocked out since they
aren't consistent with this change:
EditTransform
ElicitMetaDataTransform
StasmTransform

In stasm's case, a global mutex must be used, similarly ElicitMetaData reads
from stdin, and would also require a global lock. EditTransform can probably be
reimplemented through Qt similarly to Show
openbr/openbr_plugin.cpp
... ... @@ -802,6 +802,8 @@ void br::Context::setProperty(const QString &key, const QString &value)
802 802  
803 803 if (key == "parallelism") {
804 804 const int maxThreads = std::max(1, QThread::idealThreadCount());
  805 + if (parallelism == 0) parallelism = 1;
  806 +
805 807 QThreadPool::globalInstance()->setMaxThreadCount(parallelism ? std::min(maxThreads, abs(parallelism)) : maxThreads);
806 808 } else if (key == "log") {
807 809 logFile.close();
... ... @@ -1193,8 +1195,7 @@ private:
1193 1195  
1194 1196 QFutureSynchronizer<void> futures;
1195 1197 for (int i=0; i<templatesList.size(); i++) {
1196   - if (Globals->parallelism) futures.addFuture(QtConcurrent::run(_train, transforms[i], &templatesList[i]));
1197   - else _train (transforms[i], &templatesList[i]);
  1198 + futures.addFuture(QtConcurrent::run(_train, transforms[i], &templatesList[i]));
1198 1199 }
1199 1200 futures.waitForFinished();
1200 1201 }
... ... @@ -1308,21 +1309,12 @@ void Transform::project(const TemplateList &amp;src, TemplateList &amp;dst) const
1308 1309 {
1309 1310 dst.reserve(src.size());
1310 1311  
1311   - // There are certain conditions where we should process the templates in serial,
1312   - // but generally we'd prefer to process them in parallel.
1313   - if ((src.size() < 2) || (Globals->parallelism == 0)) {
1314   - foreach (const Template &t, src) {
1315   - dst.append(Template());
1316   - _project(this, &t, &dst.last());
1317   - }
1318   - } else {
1319   - for (int i=0; i<src.size(); i++)
1320   - dst.append(Template());
1321   - QFutureSynchronizer<void> futures;
1322   - for (int i=0; i<dst.size(); i++)
1323   - futures.addFuture(QtConcurrent::run(_project, this, &src[i], &dst[i]));
1324   - futures.waitForFinished();
1325   - }
  1312 + for (int i=0; i<src.size(); i++)
  1313 + dst.append(Template());
  1314 + QFutureSynchronizer<void> futures;
  1315 + for (int i=0; i<dst.size(); i++)
  1316 + futures.addFuture(QtConcurrent::run(_project, this, &src[i], &dst[i]));
  1317 + futures.waitForFinished();
1326 1318 }
1327 1319  
1328 1320 static void _backProject(const Transform *transform, const Template *dst, Template *src)
... ... @@ -1343,8 +1335,7 @@ void Transform::backProject(const TemplateList &amp;dst, TemplateList &amp;src) const
1343 1335  
1344 1336 QFutureSynchronizer<void> futures;
1345 1337 for (int i=0; i<dst.size(); i++)
1346   - if (Globals->parallelism) futures.addFuture(QtConcurrent::run(_backProject, this, &dst[i], &src[i]));
1347   - else _backProject (this, &dst[i], &src[i]);
  1338 + futures.addFuture(QtConcurrent::run(_backProject, this, &dst[i], &src[i]));
1348 1339 futures.waitForFinished();
1349 1340 }
1350 1341  
... ...
openbr/plugins/distance.cpp
... ... @@ -158,8 +158,7 @@ class PipeDistance : public Distance
158 158 {
159 159 QFutureSynchronizer<void> futures;
160 160 foreach (br::Distance *distance, distances)
161   - if (Globals->parallelism) futures.addFuture(QtConcurrent::run(distance, &Distance::train, data));
162   - else distance->train(data);
  161 + futures.addFuture(QtConcurrent::run(distance, &Distance::train, data));
163 162 futures.waitForFinished();
164 163 }
165 164  
... ...
openbr/plugins/draw.cpp
... ... @@ -98,6 +98,7 @@ class DrawGridTransform : public UntrainableTransform
98 98  
99 99 BR_REGISTER(Transform, DrawGridTransform)
100 100  
  101 +#if 0
101 102 /*!
102 103 * \ingroup transforms
103 104 * \brief Remove landmarks.
... ... @@ -162,6 +163,7 @@ Template EditTransform::currentTemplate;
162 163 QMutex EditTransform::currentTemplateLock;
163 164  
164 165 BR_REGISTER(Transform, EditTransform)
  166 +#endif
165 167  
166 168 } // namespace br
167 169  
... ...
openbr/plugins/gallery.cpp
... ... @@ -107,8 +107,7 @@ class EmptyGallery : public Gallery
107 107 QList< QFuture<TemplateList> > futures;
108 108 foreach (const QString &folder, NaturalStringSort(dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))) {
109 109 const QDir subdir = dir.absoluteFilePath(folder);
110   - if (Globals->parallelism) futures.append(QtConcurrent::run(&EmptyGallery::getTemplates, subdir));
111   - else templates.append(getTemplates(subdir));
  110 + futures.append(QtConcurrent::run(&EmptyGallery::getTemplates, subdir));
112 111 }
113 112 foreach (const QFuture<TemplateList> &future, futures)
114 113 templates.append(future.result());
... ...
openbr/plugins/meta.cpp
... ... @@ -109,8 +109,7 @@ class PipeTransform : public CompositeTransform
109 109 fprintf(stderr, " projecting...");
110 110 QFutureSynchronizer<void> futures;
111 111 for (int j=0; j<copy.size(); j++)
112   - if (Globals->parallelism) futures.addFuture(QtConcurrent::run(this, &PipeTransform::_projectPartial, &copy[j], i, nextTrainableTransform));
113   - else _projectPartial( &copy[j], i, nextTrainableTransform);
  112 + futures.addFuture(QtConcurrent::run(this, &PipeTransform::_projectPartial, &copy[j], i, nextTrainableTransform));
114 113 futures.waitForFinished();
115 114 i = nextTrainableTransform;
116 115 }
... ... @@ -289,8 +288,7 @@ class ForkTransform : public CompositeTransform
289 288 if (!trainable) return;
290 289 QFutureSynchronizer<void> futures;
291 290 for (int i=0; i<transforms.size(); i++) {
292   - if (Globals->parallelism) futures.addFuture(QtConcurrent::run(_train, transforms[i], &data));
293   - else _train (transforms[i], &data);
  291 + futures.addFuture(QtConcurrent::run(_train, transforms[i], &data));
294 292 }
295 293 futures.waitForFinished();
296 294 }
... ...
openbr/plugins/misc.cpp
... ... @@ -328,6 +328,7 @@ class AnonymizeTransform : public UntrainableMetaTransform
328 328  
329 329 BR_REGISTER(Transform, AnonymizeTransform)
330 330  
  331 +#if 0
331 332 /*!
332 333 * \ingroup transforms
333 334 * \brief Name a point
... ... @@ -375,6 +376,7 @@ class ElicitMetadataTransform : public UntrainableMetaTransform
375 376 };
376 377  
377 378 BR_REGISTER(Transform, ElicitMetadataTransform)
  379 +#endif
378 380  
379 381 /*!
380 382 * \ingroup transforms
... ...
openbr/plugins/normalize.cpp
... ... @@ -137,11 +137,9 @@ private:
137 137 }
138 138  
139 139 QFutureSynchronizer<void> futures;
140   - const bool parallel = (data.size() > 1000) && Globals->parallelism;
141 140 for (size_t c = 0; c < mv.size(); c++) {
142 141 for (int i=0; i<dims; i++)
143   - if (parallel) futures.addFuture(QtConcurrent::run(_train, method, mv[c].col(i), labels, &av[c].at<double>(0, i), &bv[c].at<double>(0, i)));
144   - else _train (method, mv[c].col(i), labels, &av[c].at<double>(0, i), &bv[c].at<double>(0, i));
  142 + futures.addFuture(QtConcurrent::run(_train, method, mv[c].col(i), labels, &av[c].at<double>(0, i), &bv[c].at<double>(0, i)));
145 143 av[c] = av[c].reshape(1, data.first().m().rows);
146 144 bv[c] = bv[c].reshape(1, data.first().m().rows);
147 145 }
... ...
openbr/plugins/quantize.cpp
... ... @@ -82,8 +82,7 @@ class HistEqQuantizationTransform : public Transform
82 82  
83 83 QFutureSynchronizer<void> futures;
84 84 for (int i=0; i<data.cols; i++)
85   - if (Globals->parallelism) futures.addFuture(QtConcurrent::run(&HistEqQuantizationTransform::computeThresholds, data.col(i), &thresholds.data()[i*256]));
86   - else computeThresholds( data.col(i), &thresholds.data()[i*256]);
  85 + futures.addFuture(QtConcurrent::run(&HistEqQuantizationTransform::computeThresholds, data.col(i), &thresholds.data()[i*256]));
87 86 futures.waitForFinished();
88 87 }
89 88  
... ... @@ -156,8 +155,7 @@ class BayesianQuantizationDistance : public Distance
156 155  
157 156 QFutureSynchronizer<void> futures;
158 157 for (int i=0; i<data.cols; i++)
159   - if (Globals->parallelism) futures.addFuture(QtConcurrent::run(&BayesianQuantizationDistance::computeLogLikelihood, data.col(i), templateLabels, &loglikelihoods.data()[i*256]));
160   - else computeLogLikelihood( data.col(i), templateLabels, &loglikelihoods.data()[i*256]);
  158 + futures.addFuture(QtConcurrent::run(&BayesianQuantizationDistance::computeLogLikelihood, data.col(i), templateLabels, &loglikelihoods.data()[i*256]));
161 159 futures.waitForFinished();
162 160 }
163 161  
... ...
openbr/plugins/quantize2.cpp
... ... @@ -83,8 +83,7 @@ class BayesianQuantizationTransform : public Transform
83 83  
84 84 QFutureSynchronizer<void> futures;
85 85 for (int i=0; i<data.cols; i++)
86   - if (Globals->parallelism) futures.addFuture(QtConcurrent::run(&BayesianQuantizationTransform::computeThresholds, data.col(i), labels, &thresholds.data()[i*256]));
87   - else computeThresholds( data.col(i), labels, &thresholds.data()[i*256]);
  86 + futures.addFuture(QtConcurrent::run(&BayesianQuantizationTransform::computeThresholds, data.col(i), labels, &thresholds.data()[i*256]));
88 87 futures.waitForFinished();
89 88 }
90 89  
... ...
openbr/plugins/stasm.cpp
... ... @@ -33,7 +33,7 @@ BR_REGISTER(Initializer, StasmInitializer)
33 33 * \brief Wraps STASM key point detector
34 34 * \author Scott Klum \cite sklum
35 35 */
36   -
  36 +#if 0
37 37 class StasmTransform : public UntrainableTransform
38 38 {
39 39 Q_OBJECT
... ... @@ -67,6 +67,7 @@ class StasmTransform : public UntrainableTransform
67 67 };
68 68  
69 69 BR_REGISTER(Transform, StasmTransform)
  70 +#endif
70 71  
71 72 } // namespace br
72 73  
... ...
openbr/plugins/validate.cpp
... ... @@ -46,8 +46,7 @@ class CrossValidateTransform : public MetaTransform
46 46 if (partitions[j] == i)
47 47 partitionedData.removeAt(j);
48 48 // Train on the remaining templates
49   - if (Globals->parallelism) futures.addFuture(QtConcurrent::run(transforms[i], &Transform::train, partitionedData));
50   - else transforms[i]->train(partitionedData);
  49 + futures.addFuture(QtConcurrent::run(transforms[i], &Transform::train, partitionedData));
51 50 }
52 51 futures.waitForFinished();
53 52 }
... ...