-
Notifications
You must be signed in to change notification settings - Fork 2
/
BatchProducer.cu
187 lines (175 loc) · 7.11 KB
/
BatchProducer.cu
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// don't delete the returned threads when done with them; ownership is with
// cnn.batchPool
#define MULTITHREAD_BATCH_PRODUCTION
#include "BatchProducer.h"
#include "SpatiallySparseBatch.h"
#include "SpatiallySparseBatchInterface.h"
#include "utilities.h"
#include <algorithm>
#include <functional>
#include <mutex>
#include <chrono>
#include <cassert>
BatchProducer::BatchProducer(SparseConvNetCUDA &cnn,
SpatiallySparseDataset &dataset, int spatialSize,
int batchSize)
: cnn(cnn), batchCounter(-1), dataset(dataset), spatialSize(spatialSize),
batchSize(batchSize) {
assert(batchSize > 0);
nBatches = (dataset.pictures.size() + batchSize - 1) / batchSize;
permutation = range(dataset.pictures.size());
if (dataset.type == TRAINBATCH) {
RNG rng;
rng.vectorShuffle(permutation);
}
while (cnn.batchPool[0].interfaces.size() <= cnn.layers.size()) {
cnn.sharedSubInterfaces.push_back(new SpatiallySparseBatchSubInterface());
for (int c = 0; c < cnn.nBatchProducerThreads; c++) {
cnn.batchPool[c].interfaces.emplace_back(cnn.sharedSubInterfaces.back());
}
}
#ifdef MULTITHREAD_BATCH_PRODUCTION
for (int nThread = 0; nThread < cnn.nBatchProducerThreads; ++nThread)
workers.emplace_back(&BatchProducer::batchProducerThread, this, nThread);
#endif
}
void BatchProducer::preprocessBatch(int c, int cc, RNG &rng) {
// printf("BatchProducer::preprocessBatch c=%d cc=%d f=%d ss=%d bb=%d\n", c, cc, dataset.nFeatures, spatialSize, batchSize);
cnn.batchPool[cc].reset();
cnn.batchPool[cc].type = dataset.type;
cnn.batchPool[cc].interfaces[0].nFeatures = dataset.nFeatures;
cnn.batchPool[cc].interfaces[0].spatialSize = spatialSize;
cnn.batchPool[cc].interfaces[0].featuresPresent.hVector() =
range(dataset.nFeatures);
for (int i = c * batchSize;
i < min((c + 1) * batchSize, (int)(dataset.pictures.size())); i++) {
Picture *pic = dataset.pictures[permutation[i]]->distort(rng, dataset.type);
cnn.batchPool[cc].sampleNumbers.push_back(permutation[i]);
cnn.batchPool[cc].batchSize++;
cnn.batchPool[cc].interfaces[0].grids.push_back(SparseGrid());
cnn.batchPool[cc].labels.hVector().push_back(pic->label);
pic->codifyInputData(
cnn.batchPool[cc].interfaces[0].grids.back(),
cnn.batchPool[cc].interfaces[0].sub->features.hVector(),
cnn.batchPool[cc].interfaces[0].nSpatialSites,
cnn.batchPool[cc].interfaces[0].spatialSize);
if (pic != dataset.pictures[permutation[i]])
delete pic;
// printf("BatchProducer::preprocessBatch codifyInputData site=%d size=%d \n", cnn.batchPool[cc].interfaces[0].nSpatialSites, cnn.batchPool[cc].interfaces[0].spatialSize);
}
assert(cnn.batchPool[cc].interfaces[0].sub->features.size() ==
cnn.batchPool[cc].interfaces[0].nFeatures *
cnn.batchPool[cc].interfaces[0].nSpatialSites);
if (cnn.inputNormalizingConstants.size() > 0) {
std::vector<float> &features =
cnn.batchPool[cc].interfaces[0].sub->features.hVector();
for (int i = 0; i < features.size(); ++i)
features[i] *= cnn.inputNormalizingConstants
[i % (cnn.batchPool[cc].interfaces[0].nFeatures)];
}
for (int i = 0; i < cnn.layers.size(); i++)
cnn.layers[i]->preprocess(cnn.batchPool[cc],
cnn.batchPool[cc].interfaces[i],
cnn.batchPool[cc].interfaces[i + 1]);
// Shifted to line 126 !!!!!!
// cnn.batchPool[cc].interfaces[0].sub->features.copyToGPUAsync(
// cnn.batchMemStreams[cc]);
// cnn.batchPool[cc].labels.copyToGPUAsync(cnn.batchMemStreams[cc]);
// for (int i = 0; i <= cnn.layers.size(); ++i) {
// cnn.batchPool[cc].interfaces[i].featuresPresent.copyToGPUAsync(
// cnn.batchMemStreams[cc]);
// cnn.batchPool[cc].interfaces[i].rules.copyToGPUAsync(
// cnn.batchMemStreams[cc]);
// }
// cudaStreamSynchronize(cnn.batchMemStreams[cc].stream);
}
#ifdef MULTITHREAD_BATCH_PRODUCTION
void BatchProducer::batchProducerThread(int nThread) {
//printf("batchProducerThread t=%d, b=%d bb=%d\n", nThread, nBatches, cnn.nBatchProducerThreads);
cudaSetDevice(cnn.deviceID);
// cudaSafeCall(cudaStreamDestroy(cnn.batchMemStreams[nThread].stream));
// cudaSafeCall(cudaStreamCreate(&cnn.batchMemStreams[nThread].stream));
RNG rng;
for (int c = nThread; c < nBatches; c += cnn.nBatchProducerThreads) {
cnn.batchLock[nThread].lock();
while (cnn.batchPool[nThread].batchSize >
0) { // Don't overwrite unused batches
cnn.batchLock[nThread].unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
cnn.batchLock[nThread].lock();
}
preprocessBatch(c, nThread, rng);
cnn.batchLock[nThread].unlock();
}
}
SpatiallySparseBatch *BatchProducer::nextBatch() {
if (batchCounter >= 0) {
int cc = batchCounter % cnn.nBatchProducerThreads;
cnn.batchLock[cc].unlock();
cnn.batchPool[cc].batchSize = 0;
}
batchCounter++;
if (batchCounter < nBatches) {
int cc = batchCounter % cnn.nBatchProducerThreads;
for (bool ready = false; not ready;) {
bool accessible = cnn.batchLock[cc].try_lock();
if (accessible)
if (cnn.batchPool[cc].batchSize == 0)
cnn.batchLock[cc].unlock();
else
ready = true;
}
/////////////////////////////////////////////////////////
cnn.batchPool[cc].interfaces[0].sub->features.copyToGPUAsync(cnn.memStream);
cnn.batchPool[cc].labels.copyToGPUAsync(cnn.memStream);
for (int i = 0; i <= cnn.layers.size(); ++i) {
cnn.batchPool[cc].interfaces[i].featuresPresent.copyToGPUAsync(
cnn.memStream);
cnn.batchPool[cc].interfaces[i].rules.copyToGPUAsync(cnn.memStream);
}
cudaStreamSynchronize(cnn.memStream.stream);
/////////////////////////////////////////////////////////
return &cnn.batchPool[cc];
} else {
for (int i = 0; i < cnn.nBatchProducerThreads; i++)
workers[i].join();
return NULL;
}
}
#else
void BatchProducer::batchProducerThread(int nThread) {}
SpatiallySparseBatch *BatchProducer::nextBatch() {
if (batchCounter >= 0) {
int cc = batchCounter % cnn.nBatchProducerThreads;
cnn.batchPool[cc].batchSize = 0;
}
batchCounter++;
if (batchCounter == nBatches) {
return NULL;
} else {
RNG rng;
int cc = batchCounter % cnn.nBatchProducerThreads;
;
preprocessBatch(batchCounter, cc, rng);
/////////////////////////////////////////////////////////
cnn.batchPool[cc].interfaces[0].sub->features.copyToGPUAsync(cnn.memStream);
cnn.batchPool[cc].labels.copyToGPUAsync(cnn.memStream);
for (int i = 0; i <= cnn.layers.size(); ++i) {
cnn.batchPool[cc].interfaces[i].featuresPresent.copyToGPUAsync(
cnn.memStream);
cnn.batchPool[cc].interfaces[i].rules.copyToGPUAsync(cnn.memStream);
}
cudaStreamSynchronize(cnn.memStream.stream);
/////////////////////////////////////////////////////////
return &cnn.batchPool[cc];
}
}
#endif
BatchProducer::~BatchProducer() {
if (batchCounter < nBatches) {
SpatiallySparseBatch *batch = nextBatch();
while (batch) {
batch = nextBatch();
}
}
}