-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathlaunchedDevice.cpp
More file actions
283 lines (246 loc) · 9.37 KB
/
launchedDevice.cpp
File metadata and controls
283 lines (246 loc) · 9.37 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <map>
#include <occa/core/base.hpp>
#include <occa/types/primitive.hpp>
#include <occa/internal/core/launchedDevice.hpp>
#include <occa/internal/core/launchedKernel.hpp>
#include <occa/internal/modes/serial/device.hpp>
#include <occa/internal/modes/serial/kernel.hpp>
#include <occa/internal/utils/string.hpp>
namespace occa {
launchedModeDevice_t::launchedModeDevice_t(const occa::json &properties_) :
modeDevice_t(properties_) {
needsLauncherKernel = true;
}
bool launchedModeDevice_t::parseFile(const std::string &filename,
const std::string &outputFile,
const std::string &launcherOutputFile,
const occa::json &kernelProps,
lang::sourceMetadata_t &launcherMetadata,
lang::sourceMetadata_t &deviceMetadata) {
lang::okl::withLauncher &parser = *(createParser(kernelProps));
parser.parseFile(filename);
// Verify if parsing succeeded
if (!parser.succeeded()) {
if (!kernelProps.get("silent", false)) {
OCCA_FORCE_ERROR("Unable to transform OKL kernel [" << filename << "]");
}
delete &parser;
return false;
}
io::stageFiles(
{ outputFile, launcherOutputFile },
true,
[&](const strVector &tempFilenames) -> bool {
const std::string &tempOutputFilename = tempFilenames[0];
const std::string &tempLauncherOutputFilename = tempFilenames[1];
parser.writeToFile(tempOutputFilename);
parser.launcherParser.writeToFile(tempLauncherOutputFilename);
return true;
}
);
parser.launcherParser.setSourceMetadata(launcherMetadata);
parser.setSourceMetadata(deviceMetadata);
delete &parser;
return true;
}
modeKernel_t* launchedModeDevice_t::buildKernel(const std::string &filename,
const std::string &kernelName,
const hash_t kernelHash,
const occa::json &kernelProps) {
bool usingOkl = kernelProps.get("okl/enabled", true);
launchedModeKernel_t *kernel = (launchedModeKernel_t*) (
buildKernel(filename,
kernelName,
kernelHash,
usingOkl,
kernelProps)
);
if (usingOkl) {
std::vector<modeKernel_t*> &deviceKernels = kernel->deviceKernels;
const int kernelCount = (int) deviceKernels.size();
for (int i = 0; i < kernelCount; ++i) {
modeKernel_t *deviceKernel = deviceKernels[i];
// The launchedKernel handles deleting the launcher + device kernels
removeKernelRef(deviceKernel);
deviceKernel->dontUseRefs();
// Some backends inject additional arguments
deviceKernel->properties["type_validation"] = false;
}
}
return kernel;
}
modeKernel_t* launchedModeDevice_t::buildKernel(const std::string &filename,
const std::string &kernelName,
const hash_t kernelHash,
const bool usingOkl,
const occa::json &kernelProps) {
const std::string hashDir = io::hashDir(filename, kernelHash);
const std::string binaryHashDir = io::hashDir(filename, kernelHash, true);
std::string sourceFilename = hashDir + kc::cachedSourceFilename(filename);
const std::string binaryFilename = binaryHashDir + kc::binaryFile;
// Check if binary exists and is finished
const bool foundBinary = io::isFile(binaryFilename);
const bool verbose = kernelProps.get("verbose", false);
if (foundBinary) {
if (verbose) {
io::stdout << "Loading cached ["
<< kernelName
<< "] from ["
<< filename
<< "] in [" << binaryFilename << "]\n";
}
modeKernel_t* k;
if (usingOkl) {
lang::sourceMetadata_t launcherMetadata = (
lang::sourceMetadata_t::fromBuildFile(hashDir + kc::launcherBuildFile)
);
lang::sourceMetadata_t deviceMetadata = (
lang::sourceMetadata_t::fromBuildFile(hashDir + kc::buildFile)
);
k = buildOKLKernelFromBinary(kernelHash,
hashDir,
kernelName,
sourceFilename,
binaryFilename,
launcherMetadata,
deviceMetadata,
kernelProps);
} else {
k = buildKernelFromBinary(binaryFilename,
kernelName,
kernelProps);
}
if (k) {
k->sourceFilename = filename;
k->binaryFilename = binaryFilename;
}
return k;
}
lang::sourceMetadata_t launcherMetadata, deviceMetadata;
if (usingOkl) {
// Cache raw origin
sourceFilename = (
io::cacheFile(filename,
kc::cachedRawSourceFilename(filename),
kernelHash,
assembleKernelHeader(kernelProps))
);
const std::string outputFile = hashDir + kc::cachedSourceFilename(filename);
const std::string launcherOutputFile = hashDir + kc::launcherSourceFile;
bool valid = parseFile(sourceFilename,
outputFile,
launcherOutputFile,
kernelProps,
launcherMetadata,
deviceMetadata);
if (!valid) {
return NULL;
}
sourceFilename = outputFile;
buildLauncherKernel(kernelHash,
hashDir,
kernelName,
launcherMetadata);
// No OKL means no build file is generated,
// so we need to build it
host()
.getModeDevice()
->writeKernelBuildFile(hashDir + kc::launcherBuildFile,
kernelHash,
occa::json(),
launcherMetadata);
writeKernelBuildFile(hashDir + kc::buildFile,
kernelHash,
kernelProps,
deviceMetadata);
} else {
// Cache in sourceFile to directly compile file
sourceFilename = (
io::cacheFile(filename,
kc::cachedSourceFilename(filename),
kernelHash,
assembleKernelHeader(kernelProps))
);
}
modeKernel_t *k;
io::stageFile(
binaryFilename,
false,
[&](const std::string &tempFilename) -> bool {
k = buildKernelFromProcessedSource(
kernelHash,
hashDir,
kernelName,
sourceFilename,
tempFilename,
usingOkl,
launcherMetadata,
deviceMetadata,
kernelProps
);
return true;
}
);
if (k) {
k->sourceFilename = filename;
k->binaryFilename = binaryFilename;
}
return k;
}
modeKernel_t* launchedModeDevice_t::buildLauncherKernel(
const hash_t kernelHash,
const std::string &hashDir,
const std::string &kernelName,
lang::sourceMetadata_t sourceMetadata
) {
const std::string launcherOutputFile = hashDir + kc::launcherSourceFile;
serial::device *hostDevice = (serial::device*) host().getModeDevice();
modeKernel_t *launcherKernel = hostDevice->buildLauncherKernel(
launcherOutputFile,
kernelName,
kernelHash
);
if (!launcherKernel) {
return NULL;
}
// Launcher and device kernels use the same refs as the wrapper kernel
launcherKernel->dontUseRefs();
launcherKernel->metadata = sourceMetadata.kernelsMetadata[kernelName];
return launcherKernel;
}
orderedKernelMetadata launchedModeDevice_t::getLaunchedKernelsMetadata(
const std::string &kernelName,
lang::sourceMetadata_t &deviceMetadata
) {
// Find device kernels
typedef std::map<int, lang::kernelMetadata_t> kernelOrderMap;
kernelOrderMap kernelMetadataMap;
const std::string prefix = "_occa_" + kernelName + "_";
lang::kernelMetadataMap &kernelsMetadata = deviceMetadata.kernelsMetadata;
lang::kernelMetadataMap::iterator it = kernelsMetadata.begin();
while (it != kernelsMetadata.end()) {
const std::string &name = it->first;
lang::kernelMetadata_t &metadata = it->second;
++it;
if (!startsWith(name, prefix)) {
continue;
}
std::string suffix = name.substr(prefix.size());
const char *c = suffix.c_str();
primitive number = primitive::load(c, false);
// Make sure we reached the end ['\0']
// and have a number
if (*c || number.isNaN()) {
continue;
}
kernelMetadataMap[number] = metadata;
}
// Setup vector from ordered metadata
orderedKernelMetadata kernelMetadata;
kernelOrderMap::iterator kIt;
for (kIt = kernelMetadataMap.begin(); kIt != kernelMetadataMap.end(); ++kIt) {
kernelMetadata.push_back(kIt->second);
}
return kernelMetadata;
}
}