distributetemplate.cpp
4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright 2012 The MITRE Corporation *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <QtConcurrent>
#include <openbr/plugins/openbr_internal.h>
namespace br
{
static void _projectList(const Transform *transform, const TemplateList *src, TemplateList *dst)
{
transform->project(*src, *dst);
}
class DistributeTemplateTransform : public MetaTransform
{
Q_OBJECT
Q_PROPERTY(br::Transform* transform READ get_transform WRITE set_transform RESET reset_transform)
BR_PROPERTY(br::Transform*, transform, NULL)
public:
Transform *smartCopy(bool &newTransform)
{
if (!transform->timeVarying()) {
newTransform = false;
return this;
}
newTransform = true;
DistributeTemplateTransform *output = new DistributeTemplateTransform;
bool newChild = false;
output->transform = transform->smartCopy(newChild);
if (newChild)
output->transform->setParent(output);
return output;
}
void train(const QList<TemplateList> &data)
{
if (!transform->trainable) {
qWarning("Attempted to train untrainable transform, nothing will happen.");
return;
}
QList<TemplateList> separated;
foreach (const TemplateList &list, data) {
foreach (const Template &t, list) {
separated.append(TemplateList());
separated.last().append(t);
}
}
transform->train(separated);
}
void project(const Template &src, Template &dst) const
{
TemplateList input;
input.append(src);
TemplateList output;
project(input, output);
if (output.size() != 1) qFatal("output contains more than 1 template");
else dst = output[0];
}
// For each input template, form a single element TemplateList, push all those
// lists through transform, and form dst by concatenating the results.
// Process the single elemnt templates in parallel if parallelism is enabled.
void project(const TemplateList &src, TemplateList &dst) const
{
// Pre-allocate output for each template
QList<TemplateList> output_buffer;
output_buffer.reserve(src.size());
// Can't declare this local to the loop because it would go out of scope
QList<TemplateList> input_buffer;
input_buffer.reserve(src.size());
QFutureSynchronizer<void> futures;
for (int i =0; i < src.size();i++) {
input_buffer.append(TemplateList());
output_buffer.append(TemplateList());
}
QList<QFuture<void> > temp;
temp.reserve(src.size());
for (int i=0; i<src.size(); i++) {
input_buffer[i].append(src[i]);
if (Globals->parallelism > 1) temp.append(QtConcurrent::run(_projectList, transform, &input_buffer[i], &output_buffer[i]));
else _projectList(transform, &input_buffer[i], &output_buffer[i]);
}
// We add the futures in reverse order, since in Qt 5.1 at least the
// waiting thread will wait on them in the order added (which for uniform priority
// threads is the order of execution), and we want the waiting thread to go in the opposite order
// so that it can steal runnables and do something besides wait.
for (int i = temp.size() - 1; i >= 0; i--) {
futures.addFuture(temp[i]);
}
futures.waitForFinished();
for (int i=0; i<src.size(); i++) dst.append(output_buffer[i]);
}
void projectUpdate(const TemplateList &src, TemplateList &dst)
{
this->project(src, dst);
return;
}
void init()
{
if (!transform)
return;
trainable = transform->trainable;
}
};
BR_REGISTER(Transform, DistributeTemplateTransform)
} // namespace br
#include "core/distributetemplate.moc"