This repository was archived by the owner on Mar 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 335
Expand file tree
/
Copy pathSubmanifoldConvolutionRules.h
More file actions
153 lines (135 loc) · 4.69 KB
/
SubmanifoldConvolutionRules.h
File metadata and controls
153 lines (135 loc) · 4.69 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
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2016-present, Facebook, Inc.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
#ifndef SUBMANIFOLDCONVOLUTIONRULES_H
#define SUBMANIFOLDCONVOLUTIONRULES_H
#include <algorithm>
// Full input region for an output point
template <Int dimension>
RectangularRegion<dimension>
InputRegionCalculator_Submanifold(const Point<dimension> &output, long *size) {
Point<dimension> lb, ub;
for (Int i = 0; i < dimension; i++) {
Int pad = size[i] / 2;
lb[i] = output[i] - pad;
ub[i] = output[i] + size[i] - 1 - pad;
}
return RectangularRegion<dimension>(lb, ub);
}
// Call for each convolutional / max-pooling layer, once for each batch item.
// rules is used to carry out the "lowering" whilst carrying out the convolution
template <Int dimension>
double SubmanifoldConvolution_SgToRules(SparseGrid<dimension> &grid,
RuleBook &rules, long *size) {
double countActiveInputs = 0;
for (auto const &outputIter : grid.mp) {
auto inRegion =
InputRegionCalculator_Submanifold<dimension>(outputIter.first, size);
Int rulesOffset = 0;
for (auto inputPoint : inRegion) {
auto inputIter = grid.mp.find(inputPoint);
if (inputIter != grid.mp.end()) {
rules[rulesOffset].push_back(inputIter->second + grid.ctr);
rules[rulesOffset].push_back(outputIter.second + grid.ctr);
countActiveInputs++;
}
rulesOffset++;
}
}
return countActiveInputs;
}
template <Int dimension>
double SubmanifoldConvolution_SgToRules_par(SparseGrid<dimension> &grid,
std::vector<RuleBook> &rulebooks,
long *size, const Int threadCount) {
double countActiveInputs = 0;
std::vector<std::thread> threads;
std::vector<int> activeInputs(threadCount, 0);
auto func = [&](const int order) {
auto outputIter = grid.mp.begin();
auto &rb = rulebooks[order];
int rem = grid.mp.size();
int aciveInputCount = 0;
if (rem > order) {
std::advance(outputIter, order);
rem -= order;
for (; outputIter != grid.mp.end();
std::advance(outputIter, std::min(threadCount, rem)),
rem -= threadCount) {
auto inRegion = InputRegionCalculator_Submanifold<dimension>(
outputIter->first, size);
Int rulesOffset = 0;
for (auto inputPoint : inRegion) {
auto inputIter = grid.mp.find(inputPoint);
if (inputIter != grid.mp.end()) {
aciveInputCount++;
rb[rulesOffset].push_back(inputIter->second + grid.ctr);
rb[rulesOffset].push_back(outputIter->second + grid.ctr);
}
rulesOffset++;
}
}
}
activeInputs[order] = aciveInputCount;
};
for (Int t = 0; t < threadCount; ++t) {
threads.push_back(std::thread(func, t));
}
for (Int t = 0; t < threadCount; ++t) {
threads[t].join();
countActiveInputs += activeInputs[t];
}
return countActiveInputs;
}
template <Int dimension>
Int SubmanifoldConvolution_SgsToRules(SparseGrids<dimension> &SGs,
RuleBook &rules, long *size) {
Int sd = volume<dimension>(size);
Int countActiveInputs = 0;
rules.clear();
rules.resize(sd);
for (Int i = 0; i < (Int)SGs.size(); i++)
countActiveInputs +=
SubmanifoldConvolution_SgToRules<dimension>(SGs[i], rules, size);
return countActiveInputs;
}
template <Int dimension>
Int SubmanifoldConvolution_SgsToRules_OMP(SparseGrids<dimension> &SGs,
RuleBook &rules, long *size) {
std::vector<std::vector<RuleBook>> rbs(SGs.size());
std::vector<double> countActiveInputs(SGs.size());
rules.clear();
Int sd = volume<dimension>(size);
rules.resize(sd);
const Int threadCount = 4;
for (Int i = 0; i < SGs.size(); ++i) {
std::vector<RuleBook> rulebooks;
for (Int t = 0; t < threadCount; ++t) {
rulebooks.push_back(RuleBook(sd));
}
rbs.push_back(rulebooks);
}
{
Int i;
#pragma omp parallel for private(i)
for (i = 0; i < (Int)SGs.size(); i++) {
countActiveInputs[i] = SubmanifoldConvolution_SgToRules_par<dimension>(
SGs[i], rbs[i], size, threadCount);
}
}
{
Int i;
#pragma omp parallel for private(i)
for (i = 0; i < sd; i++)
for (auto const &rb : rbs)
for (Int t = 0; t < threadCount; ++t)
rules[i].insert(rules[i].end(), rb[i][t].begin(), rb[i][t].end());
}
Int countActiveInputs_ = 0;
for (auto &i : countActiveInputs)
countActiveInputs_ += i;
return countActiveInputs_;
}
#endif /* SUBMANIFOLDCONVOLUTIONRULES_H */