absdevsreader.cpp
10.6 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*-----------------------------------------------------------*/
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
#include <cstring>
#include "cputopology.hpp"
/*-----------------------------------------------------------*/
int main(int argc, char** argv)
{
if (argc != 3)
{
std::cerr << "Usage: " << argv[0]
<< " <Abstract devices file> <verbosity (0|1)>"
<< std::endl;
exit(EXIT_FAILURE);
}
std::string abstractDevicesFile = argv[1];
bool verbosity = atoi(argv[2]);
std::ifstream absDevices(abstractDevicesFile.c_str());
if (!absDevices.is_open())
{
std::cerr << "Unable to open "
<< abstractDevicesFile
<< std::endl;
exit(EXIT_FAILURE);
}
unsigned int numLogicalCPUs, numPhysicalCPUs;
int rc = hcl::topology::getNumLogicalCpus(&numLogicalCPUs);
if (rc != 0)
{
std::cerr << "Error to get number of logical cores..."
<< std::endl;
exit(EXIT_FAILURE);
}
rc = hcl::topology::getNumPhysicalCpus(&numPhysicalCPUs);
if (rc != 0)
{
std::cerr << "Error to get number of physical cores..."
<< std::endl;
exit(EXIT_FAILURE);
}
std::cout << "Number of logical cores " << numLogicalCPUs
<< std::endl;
std::cout << "Number of physical cores " << numPhysicalCPUs
<< std::endl;
std::vector<unsigned int> nCoresList;
std::vector<std::string> coresList;
std::vector<std::string> abstractDevicesTable;
std::vector<std::string> powerPlatforms;
/*
* We always include the CPU
*/
powerPlatforms.push_back("CPUPCM");
unsigned int numCoresBound = 0;
std::string line;
nCoresList.push_back(numCoresBound);
while (std::getline(absDevices, line))
{
/*
* Ignore comment line...
*/
if (line.find('#') != std::string::npos)
{
continue;
}
std::stringstream ss(line);
std::string coreListing;
std::string gemmKernel;
unsigned int numMPIProcesses;
std::string numOMPThreads;
ss >> coreListing
>> gemmKernel
>> numMPIProcesses
>> numOMPThreads;
if (verbosity)
{
std::cout << coreListing << " "
<< gemmKernel << " "
<< numMPIProcesses << " "
<< numOMPThreads << std::endl;
}
if (coreListing.find('-') == std::string::npos)
{
/*
* Simplest case, just one core to bind.
*/
if (coreListing.find(',') == std::string::npos)
{
coresList.push_back(coreListing);
numCoresBound++;
nCoresList.push_back(numCoresBound);
}
else
{
/*
* There are comma separated list of cores...
*/
char* cstr = new char[coreListing.length() + 1];
strcpy(cstr, coreListing.c_str());
char* tok = strtok(cstr, ",");
while (tok != NULL)
{
coresList.push_back(tok);
numCoresBound++;
tok = strtok(NULL, ",");
}
delete []cstr;
nCoresList.push_back(numCoresBound);
}
}
else
{
/*
* Just one range token...
*/
if (coreListing.find(',') == std::string::npos)
{
std::vector<unsigned int> coreRange;
char* cstr = new char[coreListing.length() + 1];
strcpy(cstr, coreListing.c_str());
char* tok = strtok(cstr, "-");
while (tok != NULL)
{
coreRange.push_back(atoi(tok));
tok = strtok(NULL, ",");
}
delete []cstr;
/*
* We expect just two elements in core range...
*/
unsigned int start = coreRange[0];
unsigned int end = coreRange[1];
/*
* The MPI processes divide the cores equally amongst them...
*/
for (size_t e = 0; e < numMPIProcesses; e++)
{
numCoresBound += (end - start + 1) / numMPIProcesses;
nCoresList.push_back(numCoresBound);
}
for (size_t e = start; e <= end; e++)
{
coresList.push_back(std::to_string(e));
}
}
else
{
/*
* A mix of - and ,
*/
char* cstr1 = (char*)coreListing.c_str();
char* saveptr1, *saveptr2;
char* tok = strtok_r(cstr1, ",", &saveptr1);
while (tok != NULL)
{
std::cout << tok << std::endl;
char* tok2 = strtok_r(tok, "-", &saveptr2);
std::vector<unsigned int> coreRange;
while (tok2 != NULL)
{
std::cout << tok2 << std::endl;
coreRange.push_back(atoi(tok2));
tok2 = strtok_r(NULL, "-", &saveptr2);
}
/*
* We expect just two elements in core range...
*/
unsigned int start = coreRange[0];
unsigned int end = coreRange[1];
/*
* The MPI processes divide the cores equally amongst them...
*/
for (size_t e = 0; e < numMPIProcesses; e++)
{
numCoresBound += (end - start + 1) / numMPIProcesses;
nCoresList.push_back(numCoresBound);
}
for (size_t e = start; e <= end; e++)
{
coresList.push_back(std::to_string(e));
}
tok = strtok_r(NULL, ",", &saveptr1);
}
}
}
if (gemmKernel.find("CPU,GPU") != std::string::npos)
{
powerPlatforms.push_back("GPULITE");
}
if (gemmKernel.find("CPU,PHI") != std::string::npos)
{
powerPlatforms.push_back("PHILITE");
}
if (gemmKernel.find("CPU,FPGA") != std::string::npos)
{
powerPlatforms.push_back("FPGA");
}
for (size_t p = 0; p < numMPIProcesses; p++)
{
std::stringstream ssOut;
if (gemmKernel.find("CPU") != std::string::npos)
{
ssOut << "cpuinit, cpudgemm, cpudestroy";
}
if (gemmKernel.find("GPU") != std::string::npos)
{
ssOut << "gpuinit, gpudgemm, gpudestroy";
}
if (gemmKernel.find("PHI") != std::string::npos)
{
ssOut << "phiinit, phidgemm, phidestroy";
}
if (gemmKernel.find("FPGA") != std::string::npos)
{
ssOut << "fpgainit, fpgadgemm, fpgadestroy";
}
if (numOMPThreads.find('-') != std::string::npos)
{
ssOut << ", 0";
}
else
{
ssOut << ", " << numOMPThreads;
}
abstractDevicesTable.push_back(ssOut.str());
}
}
if (numCoresBound > numLogicalCPUs)
{
std::cerr << "Number of cores bound "
<< numCoresBound << " exceeded "
<< "the allowed number of logical cores."
<< std::endl;
exit(EXIT_FAILURE);
}
if (numCoresBound > numPhysicalCPUs)
{
std::cerr << "Warning: Number of cores bound exceeded "
<< "the allowed number of physical cores."
<< std::endl;
}
/*
* Spit out the abstract devices table...
*/
std::ofstream abstractDevicesOFile;
abstractDevicesOFile.open("absdevs.c");
abstractDevicesOFile << "\n/*----------------------------------"
<< "------------------------------------------*/\n"
<< std::endl;
abstractDevicesOFile << "#include \"absdevs.h\"" << std::endl;
abstractDevicesOFile << "\n/*----------------------------------"
<< "------------------------------------------*/\n"
<< std::endl;
/*
* Core bindings here...
*/
abstractDevicesOFile << "const unsigned int hcl_coreindex[] = {"
<< std::endl;
size_t n = nCoresList.size();
for (size_t e = 0; e < n; e++)
{
if (e == (n-1))
{
abstractDevicesOFile << nCoresList[e];
}
else
{
abstractDevicesOFile << nCoresList[e] << ",";
}
}
abstractDevicesOFile << "\n};" << std::endl;
abstractDevicesOFile << "const unsigned int hcl_corebindings[] = {"
<< std::endl;
n = coresList.size();
for (size_t e = 0; e < n; e++)
{
if (e == (n-1))
{
abstractDevicesOFile << coresList[e];
}
else
{
abstractDevicesOFile << coresList[e] << ",";
}
}
abstractDevicesOFile << "\n};" << std::endl;
abstractDevicesOFile << "\n/*----------------------------------"
<< "------------------------------------------*/\n"
<< std::endl;
abstractDevicesOFile.close();
std::ofstream abstractDevicesPowersOFile;
abstractDevicesPowersOFile.open("absdevpowers.c");
/*
* Compute Platforms here...
*/
abstractDevicesPowersOFile << "\n/*----------------------------------"
<< "------------------------------------------*/\n"
<< std::endl;
abstractDevicesPowersOFile << "const char* hcl_powerplatforms[] = {"
<< std::endl;
n = powerPlatforms.size();
for (size_t e = 0; e < n; e++)
{
if (e == (n-1))
{
abstractDevicesPowersOFile << "\"" << powerPlatforms[e] << "\"";
}
else
{
abstractDevicesPowersOFile << "\"" << powerPlatforms[e] << "\",";
}
}
abstractDevicesPowersOFile << "\n};" << std::endl;
abstractDevicesPowersOFile << "\n/*----------------------------------"
<< "------------------------------------------*/\n"
<< std::endl;
abstractDevicesPowersOFile.close();
std::cout << "Abstract devices file successfully parsed" << std::endl;
exit(EXIT_SUCCESS);
}
/*-----------------------------------------------------------*/