Commit f0dc2607d9d798c69382fefd5d8ca4f0251dbebc
1 parent
952f60e6
more future refactoring
Showing
6 changed files
with
37 additions
and
45 deletions
sdk/core/qtutils.h
| @@ -63,14 +63,6 @@ namespace QtUtils | @@ -63,14 +63,6 @@ namespace QtUtils | ||
| 63 | bool runRScript(const QString &file); | 63 | bool runRScript(const QString &file); |
| 64 | bool runDot(const QString &file); | 64 | bool runDot(const QString &file); |
| 65 | void showFile(const QString &file); | 65 | void showFile(const QString &file); |
| 66 | - | ||
| 67 | - /**** Thread Utilities ****/ | ||
| 68 | - template <typename T> | ||
| 69 | - static inline void waitForFinished(const QList< QFuture<T> > &futures) | ||
| 70 | - { | ||
| 71 | - foreach (QFuture<T> future, futures) | ||
| 72 | - future.waitForFinished(); | ||
| 73 | - } | ||
| 74 | } | 66 | } |
| 75 | 67 | ||
| 76 | #endif // __QTUTILS_H | 68 | #endif // __QTUTILS_H |
sdk/openbr_plugin.cpp
| @@ -14,6 +14,7 @@ | @@ -14,6 +14,7 @@ | ||
| 14 | * limitations under the License. * | 14 | * limitations under the License. * |
| 15 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | 15 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 16 | 16 | ||
| 17 | +#include <QFutureSynchronizer> | ||
| 17 | #include <QMetaProperty> | 18 | #include <QMetaProperty> |
| 18 | #include <QPointF> | 19 | #include <QPointF> |
| 19 | #include <QRect> | 20 | #include <QRect> |
| @@ -1162,12 +1163,12 @@ private: | @@ -1162,12 +1163,12 @@ private: | ||
| 1162 | for (int i=0; i<templatesList.size(); i++) | 1163 | for (int i=0; i<templatesList.size(); i++) |
| 1163 | templatesList[i] = Downsample(templatesList[i], transforms[i]); | 1164 | templatesList[i] = Downsample(templatesList[i], transforms[i]); |
| 1164 | 1165 | ||
| 1165 | - QList< QFuture<void> > futures; | 1166 | + QFutureSynchronizer<void> futures; |
| 1166 | for (int i=0; i<templatesList.size(); i++) { | 1167 | for (int i=0; i<templatesList.size(); i++) { |
| 1167 | - if (Globals->parallelism) futures.append(QtConcurrent::run(_train, transforms[i], &templatesList[i])); | ||
| 1168 | - else _train (transforms[i], &templatesList[i]); | 1168 | + if (Globals->parallelism) futures.addFuture(QtConcurrent::run(_train, transforms[i], &templatesList[i])); |
| 1169 | + else _train (transforms[i], &templatesList[i]); | ||
| 1169 | } | 1170 | } |
| 1170 | - QtUtils::waitForFinished(futures); | 1171 | + futures.waitForFinished(); |
| 1171 | } | 1172 | } |
| 1172 | 1173 | ||
| 1173 | void project(const Template &src, Template &dst) const | 1174 | void project(const Template &src, Template &dst) const |
| @@ -1306,11 +1307,11 @@ void Transform::backProject(const TemplateList &dst, TemplateList &src) const | @@ -1306,11 +1307,11 @@ void Transform::backProject(const TemplateList &dst, TemplateList &src) const | ||
| 1306 | src.reserve(dst.size()); | 1307 | src.reserve(dst.size()); |
| 1307 | for (int i=0; i<dst.size(); i++) src.append(Template()); | 1308 | for (int i=0; i<dst.size(); i++) src.append(Template()); |
| 1308 | 1309 | ||
| 1309 | - QList< QFuture<void> > futures; | 1310 | + QFutureSynchronizer<void> futures; |
| 1310 | for (int i=0; i<dst.size(); i++) | 1311 | for (int i=0; i<dst.size(); i++) |
| 1311 | - if (Globals->parallelism) futures.append(QtConcurrent::run(_backProject, this, &dst[i], &src[i])); | ||
| 1312 | - else _backProject (this, &dst[i], &src[i]); | ||
| 1313 | - QtUtils::waitForFinished(futures); | 1312 | + if (Globals->parallelism) futures.addFuture(QtConcurrent::run(_backProject, this, &dst[i], &src[i])); |
| 1313 | + else _backProject (this, &dst[i], &src[i]); | ||
| 1314 | + futures.waitForFinished(); | ||
| 1314 | } | 1315 | } |
| 1315 | 1316 | ||
| 1316 | /* Distance - public methods */ | 1317 | /* Distance - public methods */ |
| @@ -1338,16 +1339,16 @@ void Distance::compare(const TemplateList &target, const TemplateList &query, Ou | @@ -1338,16 +1339,16 @@ void Distance::compare(const TemplateList &target, const TemplateList &query, Ou | ||
| 1338 | const bool stepTarget = target.size() > query.size(); | 1339 | const bool stepTarget = target.size() > query.size(); |
| 1339 | const int totalSize = std::max(target.size(), query.size()); | 1340 | const int totalSize = std::max(target.size(), query.size()); |
| 1340 | int stepSize = ceil(float(totalSize) / float(std::max(1, abs(Globals->parallelism)))); | 1341 | int stepSize = ceil(float(totalSize) / float(std::max(1, abs(Globals->parallelism)))); |
| 1341 | - QList< QFuture<void> > futures; futures.reserve(ceil(float(totalSize)/float(stepSize))); | 1342 | + QFutureSynchronizer<void> futures; |
| 1342 | for (int i=0; i<totalSize; i+=stepSize) { | 1343 | for (int i=0; i<totalSize; i+=stepSize) { |
| 1343 | const TemplateList &targets(stepTarget ? TemplateList(target.mid(i, stepSize)) : target); | 1344 | const TemplateList &targets(stepTarget ? TemplateList(target.mid(i, stepSize)) : target); |
| 1344 | const TemplateList &queries(stepTarget ? query : TemplateList(query.mid(i, stepSize))); | 1345 | const TemplateList &queries(stepTarget ? query : TemplateList(query.mid(i, stepSize))); |
| 1345 | const int targetOffset = stepTarget ? i : 0; | 1346 | const int targetOffset = stepTarget ? i : 0; |
| 1346 | const int queryOffset = stepTarget ? 0 : i; | 1347 | const int queryOffset = stepTarget ? 0 : i; |
| 1347 | - if (Globals->parallelism) futures.append(QtConcurrent::run(this, &Distance::compareBlock, targets, queries, output, targetOffset, queryOffset)); | ||
| 1348 | - else compareBlock (targets, queries, output, targetOffset, queryOffset); | 1348 | + if (Globals->parallelism) futures.addFuture(QtConcurrent::run(this, &Distance::compareBlock, targets, queries, output, targetOffset, queryOffset)); |
| 1349 | + else compareBlock (targets, queries, output, targetOffset, queryOffset); | ||
| 1349 | } | 1350 | } |
| 1350 | - QtUtils::waitForFinished(futures); | 1351 | + futures.waitForFinished(); |
| 1351 | } | 1352 | } |
| 1352 | 1353 | ||
| 1353 | QList<float> Distance::compare(const TemplateList &targets, const Template &query) const | 1354 | QList<float> Distance::compare(const TemplateList &targets, const Template &query) const |
sdk/plugins/distance.cpp
| @@ -14,6 +14,7 @@ | @@ -14,6 +14,7 @@ | ||
| 14 | * limitations under the License. * | 14 | * limitations under the License. * |
| 15 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | 15 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 16 | 16 | ||
| 17 | +#include <QFutureSynchronizer> | ||
| 17 | #include <QtConcurrentRun> | 18 | #include <QtConcurrentRun> |
| 18 | #include <opencv2/imgproc/imgproc.hpp> | 19 | #include <opencv2/imgproc/imgproc.hpp> |
| 19 | #include <openbr_plugin.h> | 20 | #include <openbr_plugin.h> |
| @@ -155,11 +156,11 @@ class PipeDistance : public Distance | @@ -155,11 +156,11 @@ class PipeDistance : public Distance | ||
| 155 | 156 | ||
| 156 | void train(const TemplateList &data) | 157 | void train(const TemplateList &data) |
| 157 | { | 158 | { |
| 158 | - QList< QFuture<void> > futures; | 159 | + QFutureSynchronizer<void> futures; |
| 159 | foreach (br::Distance *distance, distances) | 160 | foreach (br::Distance *distance, distances) |
| 160 | - if (Globals->parallelism) futures.append(QtConcurrent::run(distance, &Distance::train, data)); | ||
| 161 | - else distance->train(data); | ||
| 162 | - QtUtils::waitForFinished(futures); | 161 | + if (Globals->parallelism) futures.addFuture(QtConcurrent::run(distance, &Distance::train, data)); |
| 162 | + else distance->train(data); | ||
| 163 | + futures.waitForFinished(); | ||
| 163 | } | 164 | } |
| 164 | 165 | ||
| 165 | float compare(const Template &a, const Template &b) const | 166 | float compare(const Template &a, const Template &b) const |
sdk/plugins/meta.cpp
| @@ -14,6 +14,7 @@ | @@ -14,6 +14,7 @@ | ||
| 14 | * limitations under the License. * | 14 | * limitations under the License. * |
| 15 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | 15 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 16 | 16 | ||
| 17 | +#include <QFutureSynchronizer> | ||
| 17 | #include <QtConcurrentRun> | 18 | #include <QtConcurrentRun> |
| 18 | #include <openbr_plugin.h> | 19 | #include <openbr_plugin.h> |
| 19 | 20 | ||
| @@ -284,12 +285,12 @@ class ForkTransform : public CompositeTransform | @@ -284,12 +285,12 @@ class ForkTransform : public CompositeTransform | ||
| 284 | 285 | ||
| 285 | void train(const TemplateList &data) | 286 | void train(const TemplateList &data) |
| 286 | { | 287 | { |
| 287 | - QList< QFuture<void> > futures; | 288 | + QFutureSynchronizer<void> futures; |
| 288 | for (int i=0; i<transforms.size(); i++) { | 289 | for (int i=0; i<transforms.size(); i++) { |
| 289 | - if (Globals->parallelism) futures.append(QtConcurrent::run(_train, transforms[i], &data)); | ||
| 290 | - else _train (transforms[i], &data); | 290 | + if (Globals->parallelism) futures.addFuture(QtConcurrent::run(_train, transforms[i], &data)); |
| 291 | + else _train (transforms[i], &data); | ||
| 291 | } | 292 | } |
| 292 | - QtUtils::waitForFinished(futures); | 293 | + futures.waitForFinished(); |
| 293 | } | 294 | } |
| 294 | 295 | ||
| 295 | void backProject(const Template &dst, Template &src) const {Transform::backProject(dst, src);} | 296 | void backProject(const Template &dst, Template &src) const {Transform::backProject(dst, src);} |
| @@ -634,16 +635,13 @@ public: | @@ -634,16 +635,13 @@ public: | ||
| 634 | output_buffer.append(TemplateList()); | 635 | output_buffer.append(TemplateList()); |
| 635 | } | 636 | } |
| 636 | 637 | ||
| 637 | - QList< QFuture<void> > futures; | ||
| 638 | - futures.reserve(src.size()); | 638 | + QFutureSynchronizer<void> futures; |
| 639 | for (int i=0; i<src.size(); i++) { | 639 | for (int i=0; i<src.size(); i++) { |
| 640 | input_buffer[i].append(src[i]); | 640 | input_buffer[i].append(src[i]); |
| 641 | - if (Globals->parallelism) | ||
| 642 | - futures.append(QtConcurrent::run(_projectList, transform, &input_buffer[i], &output_buffer[i])); | ||
| 643 | - else | ||
| 644 | - _projectList(transform, &input_buffer[i], &output_buffer[i]); | 641 | + if (Globals->parallelism) futures.addFuture(QtConcurrent::run(_projectList, transform, &input_buffer[i], &output_buffer[i])); |
| 642 | + else _projectList( transform, &input_buffer[i], &output_buffer[i]); | ||
| 645 | } | 643 | } |
| 646 | - QtUtils::waitForFinished(futures); | 644 | + futures.waitForFinished(); |
| 647 | 645 | ||
| 648 | for (int i=0; i<src.size(); i++) dst.append(output_buffer[i]); | 646 | for (int i=0; i<src.size(); i++) dst.append(output_buffer[i]); |
| 649 | } | 647 | } |
sdk/plugins/normalize.cpp
| @@ -14,6 +14,7 @@ | @@ -14,6 +14,7 @@ | ||
| 14 | * limitations under the License. * | 14 | * limitations under the License. * |
| 15 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | 15 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 16 | 16 | ||
| 17 | +#include <QFutureSynchronizer> | ||
| 17 | #include <QtConcurrentRun> | 18 | #include <QtConcurrentRun> |
| 18 | #include <opencv2/imgproc/imgproc.hpp> | 19 | #include <opencv2/imgproc/imgproc.hpp> |
| 19 | #include <opencv2/highgui/highgui.hpp> | 20 | #include <opencv2/highgui/highgui.hpp> |
| @@ -120,16 +121,16 @@ private: | @@ -120,16 +121,16 @@ private: | ||
| 120 | bv.push_back(Mat(1, dims, CV_64FC1)); | 121 | bv.push_back(Mat(1, dims, CV_64FC1)); |
| 121 | } | 122 | } |
| 122 | 123 | ||
| 123 | - QList< QFuture<void> > futures; | 124 | + QFutureSynchronizer<void> futures; |
| 124 | const bool parallel = (data.size() > 1000) && Globals->parallelism; | 125 | const bool parallel = (data.size() > 1000) && Globals->parallelism; |
| 125 | for (size_t c = 0; c < mv.size(); c++) { | 126 | for (size_t c = 0; c < mv.size(); c++) { |
| 126 | for (int i=0; i<dims; i++) | 127 | for (int i=0; i<dims; i++) |
| 127 | - if (parallel) futures.append(QtConcurrent::run(_train, method, mv[c], &av[c], &bv[c], i)); | ||
| 128 | - else _train (method, mv[c], &av[c], &bv[c], i); | 128 | + if (parallel) futures.addFuture(QtConcurrent::run(_train, method, mv[c], &av[c], &bv[c], i)); |
| 129 | + else _train (method, mv[c], &av[c], &bv[c], i); | ||
| 129 | av[c] = av[c].reshape(1, data.first().m().rows); | 130 | av[c] = av[c].reshape(1, data.first().m().rows); |
| 130 | bv[c] = bv[c].reshape(1, data.first().m().rows); | 131 | bv[c] = bv[c].reshape(1, data.first().m().rows); |
| 131 | } | 132 | } |
| 132 | - QtUtils::waitForFinished(futures); | 133 | + futures.waitForFinished(); |
| 133 | 134 | ||
| 134 | merge(av, a); | 135 | merge(av, a); |
| 135 | merge(bv, b); | 136 | merge(bv, b); |
sdk/plugins/validate.cpp
| 1 | +#include <QFutureSynchronizer> | ||
| 1 | #include <QtConcurrentRun> | 2 | #include <QtConcurrentRun> |
| 2 | #include <openbr_plugin.h> | 3 | #include <openbr_plugin.h> |
| 3 | 4 | ||
| 4 | -#include "core/qtutils.h" | ||
| 5 | - | ||
| 6 | namespace br | 5 | namespace br |
| 7 | { | 6 | { |
| 8 | 7 | ||
| @@ -36,16 +35,16 @@ class CrossValidateTransform : public MetaTransform | @@ -36,16 +35,16 @@ class CrossValidateTransform : public MetaTransform | ||
| 36 | return; | 35 | return; |
| 37 | } | 36 | } |
| 38 | 37 | ||
| 39 | - QList< QFuture<void> > futures; | 38 | + QFutureSynchronizer<void> futures; |
| 40 | for (int i=0; i<numPartitions; i++) { | 39 | for (int i=0; i<numPartitions; i++) { |
| 41 | TemplateList partitionedData = data; | 40 | TemplateList partitionedData = data; |
| 42 | for (int j=partitionedData.size()-1; j>=0; j--) | 41 | for (int j=partitionedData.size()-1; j>=0; j--) |
| 43 | if (partitions[j] == i) | 42 | if (partitions[j] == i) |
| 44 | partitionedData.removeAt(j); | 43 | partitionedData.removeAt(j); |
| 45 | - if (Globals->parallelism) futures.append(QtConcurrent::run(transforms[i], &Transform::train, partitionedData)); | ||
| 46 | - else transforms[i]->train(partitionedData); | 44 | + if (Globals->parallelism) futures.addFuture(QtConcurrent::run(transforms[i], &Transform::train, partitionedData)); |
| 45 | + else transforms[i]->train(partitionedData); | ||
| 47 | } | 46 | } |
| 48 | - QtUtils::waitForFinished(futures); | 47 | + futures.waitForFinished(); |
| 49 | } | 48 | } |
| 50 | 49 | ||
| 51 | void project(const Template &src, Template &dst) const | 50 | void project(const Template &src, Template &dst) const |