-
Notifications
You must be signed in to change notification settings - Fork 299
/
gpu_burn-drv.cpp
911 lines (789 loc) · 30.9 KB
/
gpu_burn-drv.cpp
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
/*
* Copyright (c) 2022, Ville Timonen
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
*this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
*documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
*AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
*IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
*FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
*DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
*SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
*OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
*OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
*those of the authors and should not be interpreted as representing official
*policies, either expressed or implied, of the FreeBSD Project.
*/
// Matrices are SIZE*SIZE.. POT should be efficiently implemented in CUBLAS
#define SIZE 8192ul
#define USEMEM 0.9 // Try to allocate 90% of memory
#define COMPARE_KERNEL "compare.ptx"
// Used to report op/s, measured through Visual Profiler, CUBLAS from CUDA 7.5
// (Seems that they indeed take the naive dim^3 approach)
//#define OPS_PER_MUL 17188257792ul // Measured for SIZE = 2048
#define OPS_PER_MUL 1100048498688ul // Extrapolated for SIZE = 8192
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <cstring>
#include <errno.h>
#include <exception>
#include <fstream>
#include <map>
#include <signal.h>
#include <stdexcept>
#include <string.h>
#include <string>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <thread>
#include <time.h>
#include <unistd.h>
#include <vector>
#include <regex>
#define SIGTERM_TIMEOUT_THRESHOLD_SECS 30 // number of seconds for sigterm to kill child processes before forcing a sigkill
#include "cublas_v2.h"
#define CUDA_ENABLE_DEPRECATED
#include <cuda.h>
void _checkError(int rCode, std::string file, int line, std::string desc = "") {
if (rCode != CUDA_SUCCESS) {
const char *err;
cuGetErrorString((CUresult)rCode, &err);
throw std::runtime_error(
(desc == "" ? std::string("Error (")
: (std::string("Error in ") + desc + " (")) +
file + ":" + std::to_string(line) + "): " + err);
// Yes, this *is* a memory leak, but this block is only executed on
// error, so it's not a big deal
}
}
void _checkError(cublasStatus_t rCode, std::string file, int line, std::string desc = "") {
if (rCode != CUBLAS_STATUS_SUCCESS) {
#if CUBLAS_VER_MAJOR >= 12
const char *err = cublasGetStatusString(rCode);
#else
const char *err = "";
#endif
throw std::runtime_error(
(desc == "" ? std::string("Error (")
: (std::string("Error in ") + desc + " (")) +
file + ":" + std::to_string(line) + "): " + err);
// Yes, this *is* a memory leak, but this block is only executed on
// error, so it's not a big deal
}
}
#define checkError(rCode, ...) \
_checkError(rCode, __FILE__, __LINE__, ##__VA_ARGS__)
double getTime() {
struct timeval t;
gettimeofday(&t, NULL);
return (double)t.tv_sec + (double)t.tv_usec / 1e6;
}
bool g_running = false;
template <class T> class GPU_Test {
public:
GPU_Test(int dev, bool doubles, bool tensors, const char *kernelFile)
: d_devNumber(dev), d_doubles(doubles), d_tensors(tensors), d_kernelFile(kernelFile){
checkError(cuDeviceGet(&d_dev, d_devNumber));
checkError(cuCtxCreate(&d_ctx, 0, d_dev));
bind();
// checkError(cublasInit());
checkError(cublasCreate(&d_cublas), "init");
if (d_tensors)
checkError(cublasSetMathMode(d_cublas, CUBLAS_TENSOR_OP_MATH));
checkError(cuMemAllocHost((void **)&d_faultyElemsHost, sizeof(int)));
d_error = 0;
g_running = true;
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = termHandler;
sigaction(SIGTERM, &action, NULL);
}
~GPU_Test() {
bind();
checkError(cuMemFree(d_Cdata), "Free A");
checkError(cuMemFree(d_Adata), "Free B");
checkError(cuMemFree(d_Bdata), "Free C");
cuMemFreeHost(d_faultyElemsHost);
printf("Freed memory for dev %d\n", d_devNumber);
cublasDestroy(d_cublas);
printf("Uninitted cublas\n");
}
static void termHandler(int signum) { g_running = false; }
unsigned long long int getErrors() {
if (*d_faultyElemsHost) {
d_error += (long long int)*d_faultyElemsHost;
}
unsigned long long int tempErrs = d_error;
d_error = 0;
return tempErrs;
}
size_t getIters() { return d_iters; }
void bind() { checkError(cuCtxSetCurrent(d_ctx), "Bind CTX"); }
size_t totalMemory() {
bind();
size_t freeMem, totalMem;
checkError(cuMemGetInfo(&freeMem, &totalMem));
return totalMem;
}
size_t availMemory() {
bind();
size_t freeMem, totalMem;
checkError(cuMemGetInfo(&freeMem, &totalMem));
return freeMem;
}
void initBuffers(T *A, T *B, ssize_t useBytes = 0) {
bind();
if (useBytes == 0)
useBytes = (ssize_t)((double)availMemory() * USEMEM);
if (useBytes < 0)
useBytes = (ssize_t)((double)availMemory() * (-useBytes / 100.0));
printf("Initialized device %d with %lu MB of memory (%lu MB available, "
"using %lu MB of it), %s%s\n",
d_devNumber, totalMemory() / 1024ul / 1024ul,
availMemory() / 1024ul / 1024ul, useBytes / 1024ul / 1024ul,
d_doubles ? "using DOUBLES" : "using FLOATS",
d_tensors ? ", using Tensor Cores" : "");
size_t d_resultSize = sizeof(T) * SIZE * SIZE;
d_iters = (useBytes - 2 * d_resultSize) /
d_resultSize; // We remove A and B sizes
printf("Results are %zu bytes each, thus performing %zu iterations\n",
d_resultSize, d_iters);
if ((size_t)useBytes < 3 * d_resultSize)
throw std::string("Low mem for result. aborting.\n");
checkError(cuMemAlloc(&d_Cdata, d_iters * d_resultSize), "C alloc");
checkError(cuMemAlloc(&d_Adata, d_resultSize), "A alloc");
checkError(cuMemAlloc(&d_Bdata, d_resultSize), "B alloc");
checkError(cuMemAlloc(&d_faultyElemData, sizeof(int)), "faulty data");
// Populating matrices A and B
checkError(cuMemcpyHtoD(d_Adata, A, d_resultSize), "A -> device");
checkError(cuMemcpyHtoD(d_Bdata, B, d_resultSize), "B -> device");
initCompareKernel();
}
void compute() {
bind();
static const float alpha = 1.0f;
static const float beta = 0.0f;
static const double alphaD = 1.0;
static const double betaD = 0.0;
for (size_t i = 0; i < d_iters; ++i) {
if (d_doubles)
checkError(
cublasDgemm(d_cublas, CUBLAS_OP_N, CUBLAS_OP_N, SIZE, SIZE,
SIZE, &alphaD, (const double *)d_Adata, SIZE,
(const double *)d_Bdata, SIZE, &betaD,
(double *)d_Cdata + i * SIZE * SIZE, SIZE),
"DGEMM");
else
checkError(
cublasSgemm(d_cublas, CUBLAS_OP_N, CUBLAS_OP_N, SIZE, SIZE,
SIZE, &alpha, (const float *)d_Adata, SIZE,
(const float *)d_Bdata, SIZE, &beta,
(float *)d_Cdata + i * SIZE * SIZE, SIZE),
"SGEMM");
}
}
void initCompareKernel() {
{
std::ifstream f(d_kernelFile);
checkError(f.good() ? CUDA_SUCCESS : CUDA_ERROR_NOT_FOUND,
std::string("couldn't find compare kernel: ") + d_kernelFile);
}
checkError(cuModuleLoad(&d_module, d_kernelFile), "load module");
checkError(cuModuleGetFunction(&d_function, d_module,
d_doubles ? "compareD" : "compare"),
"get func");
checkError(cuFuncSetCacheConfig(d_function, CU_FUNC_CACHE_PREFER_L1),
"L1 config");
checkError(cuParamSetSize(d_function, __alignof(T *) +
__alignof(int *) +
__alignof(size_t)),
"set param size");
checkError(cuParamSetv(d_function, 0, &d_Cdata, sizeof(T *)),
"set param");
checkError(cuParamSetv(d_function, __alignof(T *), &d_faultyElemData,
sizeof(T *)),
"set param");
checkError(cuParamSetv(d_function, __alignof(T *) + __alignof(int *),
&d_iters, sizeof(size_t)),
"set param");
checkError(cuFuncSetBlockShape(d_function, g_blockSize, g_blockSize, 1),
"set block size");
}
void compare() {
checkError(cuMemsetD32Async(d_faultyElemData, 0, 1, 0), "memset");
checkError(cuLaunchGridAsync(d_function, SIZE / g_blockSize,
SIZE / g_blockSize, 0),
"Launch grid");
checkError(cuMemcpyDtoHAsync(d_faultyElemsHost, d_faultyElemData,
sizeof(int), 0),
"Read faultyelemdata");
}
bool shouldRun() { return g_running; }
private:
bool d_doubles;
bool d_tensors;
int d_devNumber;
const char *d_kernelFile;
size_t d_iters;
size_t d_resultSize;
long long int d_error;
static const int g_blockSize = 16;
CUdevice d_dev;
CUcontext d_ctx;
CUmodule d_module;
CUfunction d_function;
CUdeviceptr d_Cdata;
CUdeviceptr d_Adata;
CUdeviceptr d_Bdata;
CUdeviceptr d_faultyElemData;
int *d_faultyElemsHost;
cublasHandle_t d_cublas;
};
// Returns the number of devices
int initCuda() {
try {
checkError(cuInit(0));
} catch (std::runtime_error e) {
fprintf(stderr, "Couldn't init CUDA: %s\n", e.what());
return 0;
}
int deviceCount = 0;
checkError(cuDeviceGetCount(&deviceCount));
if (!deviceCount)
throw std::string("No CUDA devices");
#ifdef USEDEV
if (USEDEV >= deviceCount)
throw std::string("Not enough devices for USEDEV");
#endif
return deviceCount;
}
template <class T>
void startBurn(int index, int writeFd, T *A, T *B, bool doubles, bool tensors,
ssize_t useBytes, const char *kernelFile) {
GPU_Test<T> *our;
try {
our = new GPU_Test<T>(index, doubles, tensors, kernelFile);
our->initBuffers(A, B, useBytes);
} catch (const std::exception &e) {
fprintf(stderr, "Couldn't init a GPU test: %s\n", e.what());
exit(EMEDIUMTYPE);
}
// The actual work
try {
int eventIndex = 0;
const int maxEvents = 2;
CUevent events[maxEvents];
for (int i = 0; i < maxEvents; ++i)
cuEventCreate(events + i, 0);
int nonWorkIters = maxEvents;
while (our->shouldRun()) {
our->compute();
our->compare();
checkError(cuEventRecord(events[eventIndex], 0), "Record event");
eventIndex = ++eventIndex % maxEvents;
while (cuEventQuery(events[eventIndex]) != CUDA_SUCCESS)
usleep(1000);
if (--nonWorkIters > 0)
continue;
int ops = our->getIters();
write(writeFd, &ops, sizeof(int));
ops = our->getErrors();
write(writeFd, &ops, sizeof(int));
}
for (int i = 0; i < maxEvents; ++i)
cuEventSynchronize(events[i]);
delete our;
} catch (const std::exception &e) {
fprintf(stderr, "Failure during compute: %s\n", e.what());
int ops = -1;
// Signalling that we failed
write(writeFd, &ops, sizeof(int));
write(writeFd, &ops, sizeof(int));
exit(ECONNREFUSED);
}
}
int pollTemp(pid_t *p) {
int tempPipe[2];
pipe(tempPipe);
pid_t myPid = fork();
if (!myPid) {
close(tempPipe[0]);
dup2(tempPipe[1], STDOUT_FILENO);
#if IS_JETSON
execlp("tegrastats", "tegrastats", "--interval", "5000", NULL);
fprintf(stderr, "Could not invoke tegrastats, no temps available\n");
#else
execlp("nvidia-smi", "nvidia-smi", "-l", "5", "-q", "-d", "TEMPERATURE",
NULL);
fprintf(stderr, "Could not invoke nvidia-smi, no temps available\n");
#endif
exit(ENODEV);
}
*p = myPid;
close(tempPipe[1]);
return tempPipe[0];
}
void updateTemps(int handle, std::vector<int> *temps) {
const int readSize = 10240;
static int gpuIter = 0;
char data[readSize + 1];
int curPos = 0;
do {
read(handle, data + curPos, sizeof(char));
} while (data[curPos++] != '\n');
data[curPos - 1] = 0;
#if IS_JETSON
std::string data_str(data);
std::regex pattern("GPU@([0-9]+)C");
std::smatch matches;
if (std::regex_search(data_str, matches, pattern)) {
if (matches.size() > 1) {
int tempValue = std::stoi(matches[1]);
temps->at(gpuIter) = tempValue;
gpuIter = (gpuIter + 1) % (temps->size());
}
}
#else
// FIXME: The syntax of this print might change in the future..
int tempValue;
if (sscanf(data,
" GPU Current Temp : %d C",
&tempValue) == 1) {
temps->at(gpuIter) = tempValue;
gpuIter = (gpuIter + 1) % (temps->size());
} else if (!strcmp(data, " Gpu "
" : N/A"))
gpuIter =
(gpuIter + 1) %
(temps->size()); // We rotate the iterator for N/A values as well
#endif
}
void listenClients(std::vector<int> clientFd, std::vector<pid_t> clientPid,
int runTime, std::chrono::seconds sigterm_timeout_threshold_secs) {
fd_set waitHandles;
pid_t tempPid;
int tempHandle = pollTemp(&tempPid);
int maxHandle = tempHandle;
FD_ZERO(&waitHandles);
FD_SET(tempHandle, &waitHandles);
for (size_t i = 0; i < clientFd.size(); ++i) {
if (clientFd.at(i) > maxHandle)
maxHandle = clientFd.at(i);
FD_SET(clientFd.at(i), &waitHandles);
}
std::vector<int> clientTemp;
std::vector<int> clientErrors;
std::vector<int> clientCalcs;
std::vector<struct timespec> clientUpdateTime;
std::vector<float> clientGflops;
std::vector<bool> clientFaulty;
time_t startTime = time(0);
for (size_t i = 0; i < clientFd.size(); ++i) {
clientTemp.push_back(0);
clientErrors.push_back(0);
clientCalcs.push_back(0);
struct timespec thisTime;
clock_gettime(CLOCK_REALTIME, &thisTime);
clientUpdateTime.push_back(thisTime);
clientGflops.push_back(0.0f);
clientFaulty.push_back(false);
}
int changeCount;
float nextReport = 10.0f;
bool childReport = false;
while (
(changeCount = select(maxHandle + 1, &waitHandles, NULL, NULL, NULL))) {
size_t thisTime = time(0);
struct timespec thisTimeSpec;
clock_gettime(CLOCK_REALTIME, &thisTimeSpec);
// Going through all descriptors
for (size_t i = 0; i < clientFd.size(); ++i)
if (FD_ISSET(clientFd.at(i), &waitHandles)) {
// First, reading processed
int processed, errors;
int res = read(clientFd.at(i), &processed, sizeof(int));
if (res < sizeof(int)) {
fprintf(stderr, "read[%zu] error %d", i, res);
processed = -1;
}
// Then errors
read(clientFd.at(i), &errors, sizeof(int));
clientErrors.at(i) += errors;
if (processed == -1)
clientCalcs.at(i) = -1;
else {
double flops = (double)processed * (double)OPS_PER_MUL;
struct timespec clientPrevTime = clientUpdateTime.at(i);
double clientTimeDelta =
(double)thisTimeSpec.tv_sec +
(double)thisTimeSpec.tv_nsec / 1000000000.0 -
((double)clientPrevTime.tv_sec +
(double)clientPrevTime.tv_nsec / 1000000000.0);
clientUpdateTime.at(i) = thisTimeSpec;
clientGflops.at(i) =
(double)((unsigned long long int)processed *
OPS_PER_MUL) /
clientTimeDelta / 1000.0 / 1000.0 / 1000.0;
clientCalcs.at(i) += processed;
}
childReport = true;
}
if (FD_ISSET(tempHandle, &waitHandles))
updateTemps(tempHandle, &clientTemp);
// Resetting the listeners
FD_ZERO(&waitHandles);
FD_SET(tempHandle, &waitHandles);
for (size_t i = 0; i < clientFd.size(); ++i)
FD_SET(clientFd.at(i), &waitHandles);
// Printing progress (if a child has initted already)
if (childReport) {
float elapsed =
fminf((float)(thisTime - startTime) / (float)runTime * 100.0f,
100.0f);
printf("\r%.1f%% ", elapsed);
printf("proc'd: ");
for (size_t i = 0; i < clientCalcs.size(); ++i) {
printf("%d (%.0f Gflop/s) ", clientCalcs.at(i),
clientGflops.at(i));
if (i != clientCalcs.size() - 1)
printf("- ");
}
printf(" errors: ");
for (size_t i = 0; i < clientErrors.size(); ++i) {
std::string note = "%d ";
if (clientCalcs.at(i) == -1)
note += " (DIED!)";
else if (clientErrors.at(i))
note += " (WARNING!)";
printf(note.c_str(), clientErrors.at(i));
if (i != clientCalcs.size() - 1)
printf("- ");
}
printf(" temps: ");
for (size_t i = 0; i < clientTemp.size(); ++i) {
printf(clientTemp.at(i) != 0 ? "%d C " : "-- ",
clientTemp.at(i));
if (i != clientCalcs.size() - 1)
printf("- ");
}
fflush(stdout);
for (size_t i = 0; i < clientErrors.size(); ++i)
if (clientErrors.at(i))
clientFaulty.at(i) = true;
if (nextReport < elapsed) {
nextReport = elapsed + 10.0f;
printf("\n\tSummary at: ");
fflush(stdout);
system("date"); // Printing a date
fflush(stdout);
printf("\n");
for (size_t i = 0; i < clientErrors.size(); ++i)
clientErrors.at(i) = 0;
}
}
// Checking whether all clients are dead
bool oneAlive = false;
for (size_t i = 0; i < clientCalcs.size(); ++i)
if (clientCalcs.at(i) != -1)
oneAlive = true;
if (!oneAlive) {
fprintf(stderr, "\n\nNo clients are alive! Aborting\n");
exit(ENOMEDIUM);
}
if (startTime + runTime < thisTime)
break;
}
printf("\nKilling processes with SIGTERM (soft kill)\n");
fflush(stdout);
for (size_t i = 0; i < clientPid.size(); ++i)
kill(clientPid.at(i), SIGTERM);
kill(tempPid, SIGTERM);
// processes should be terminated by SIGTERM within threshold time (so wait and then check pids)
std::this_thread::sleep_for(sigterm_timeout_threshold_secs);
// check each process and see if they are alive
std::vector<int> killed_processes; // track the number of killed processes
// loop through pids for each client / GPU
for (size_t i = 0; i < clientPid.size(); ++i) {
int status;
pid_t return_pid = waitpid(clientPid.at(i), &status, WNOHANG);
if (return_pid == clientPid.at(i)) {
/* child is finished. exit status in status */
killed_processes.push_back(return_pid);
}
}
// handle the tempPid
int status;
pid_t return_pid = waitpid(tempPid, &status, WNOHANG);
if (return_pid == tempPid) {
/* child is finished. exit status in status */
killed_processes.push_back(return_pid);
}
// number of killed process should be number GPUs + 1 (need to add tempPid process) to exit while loop early
if (killed_processes.size() != clientPid.size() + 1) {
printf("\nKilling processes with SIGKILL (force kill)\n");
for (size_t i = 0; i < clientPid.size(); ++i) {
// check if pid was already killed with SIGTERM before using SIGKILL
if (std::find(killed_processes.begin(), killed_processes.end(), clientPid.at(i)) == killed_processes.end())
kill(clientPid.at(i), SIGKILL);
}
// check if pid was already killed with SIGTERM before using SIGKILL
if (std::find(killed_processes.begin(), killed_processes.end(), tempPid) == killed_processes.end())
kill(tempPid, SIGKILL);
}
close(tempHandle);
while (wait(NULL) != -1)
;
printf("done\n");
printf("\nTested %d GPUs:\n", (int)clientPid.size());
for (size_t i = 0; i < clientPid.size(); ++i)
printf("\tGPU %d: %s\n", (int)i, clientFaulty.at(i) ? "FAULTY" : "OK");
}
template <class T>
void launch(int runLength, bool useDoubles, bool useTensorCores,
ssize_t useBytes, int device_id, const char * kernelFile,
std::chrono::seconds sigterm_timeout_threshold_secs) {
#if IS_JETSON
std::ifstream f_model("/proc/device-tree/model");
std::stringstream ss_model;
ss_model << f_model.rdbuf();
printf("%s\n", ss_model.str().c_str());
#else
system("nvidia-smi -L");
#endif
// Initting A and B with random data
T *A = (T *)malloc(sizeof(T) * SIZE * SIZE);
T *B = (T *)malloc(sizeof(T) * SIZE * SIZE);
srand(10);
for (size_t i = 0; i < SIZE * SIZE; ++i) {
A[i] = (T)((double)(rand() % 1000000) / 100000.0);
B[i] = (T)((double)(rand() % 1000000) / 100000.0);
}
// Forking a process.. This one checks the number of devices to use,
// returns the value, and continues to use the first one.
int mainPipe[2];
pipe(mainPipe);
int readMain = mainPipe[0];
std::vector<int> clientPipes;
std::vector<pid_t> clientPids;
clientPipes.push_back(readMain);
if (device_id > -1) {
pid_t myPid = fork();
if (!myPid) {
// Child
close(mainPipe[0]);
int writeFd = mainPipe[1];
initCuda();
int devCount = 1;
write(writeFd, &devCount, sizeof(int));
startBurn<T>(device_id, writeFd, A, B, useDoubles, useTensorCores,
useBytes, kernelFile);
close(writeFd);
return;
} else {
clientPids.push_back(myPid);
close(mainPipe[1]);
int devCount;
read(readMain, &devCount, sizeof(int));
listenClients(clientPipes, clientPids, runLength, sigterm_timeout_threshold_secs);
}
for (size_t i = 0; i < clientPipes.size(); ++i)
close(clientPipes.at(i));
} else {
pid_t myPid = fork();
if (!myPid) {
// Child
close(mainPipe[0]);
int writeFd = mainPipe[1];
int devCount = initCuda();
write(writeFd, &devCount, sizeof(int));
startBurn<T>(0, writeFd, A, B, useDoubles, useTensorCores,
useBytes, kernelFile);
close(writeFd);
return;
} else {
clientPids.push_back(myPid);
close(mainPipe[1]);
int devCount;
read(readMain, &devCount, sizeof(int));
if (!devCount) {
fprintf(stderr, "No CUDA devices\n");
exit(ENODEV);
} else {
for (int i = 1; i < devCount; ++i) {
int slavePipe[2];
pipe(slavePipe);
clientPipes.push_back(slavePipe[0]);
pid_t slavePid = fork();
if (!slavePid) {
// Child
close(slavePipe[0]);
initCuda();
startBurn<T>(i, slavePipe[1], A, B, useDoubles,
useTensorCores, useBytes, kernelFile);
close(slavePipe[1]);
return;
} else {
clientPids.push_back(slavePid);
close(slavePipe[1]);
}
}
listenClients(clientPipes, clientPids, runLength, sigterm_timeout_threshold_secs);
}
}
for (size_t i = 0; i < clientPipes.size(); ++i)
close(clientPipes.at(i));
}
free(A);
free(B);
}
void showHelp() {
printf("GPU Burn\n");
printf("Usage: gpu-burn [OPTIONS] [TIME]\n\n");
printf("-m X\tUse X MB of memory.\n");
printf("-m N%%\tUse N%% of the available GPU memory. Default is %d%%\n",
(int)(USEMEM * 100));
printf("-d\tUse doubles\n");
printf("-tc\tTry to use Tensor cores\n");
printf("-l\tLists all GPUs in the system\n");
printf("-i N\tExecute only on GPU N\n");
printf("-c FILE\tUse FILE as compare kernel. Default is %s\n",
COMPARE_KERNEL);
printf("-stts T\tSet timeout threshold to T seconds for using SIGTERM to abort child processes before using SIGKILL. Default is %d\n",
SIGTERM_TIMEOUT_THRESHOLD_SECS);
printf("-h\tShow this help message\n\n");
printf("Examples:\n");
printf(" gpu-burn -d 3600 # burns all GPUs with doubles for an hour\n");
printf(
" gpu-burn -m 50%% # burns using 50%% of the available GPU memory\n");
printf(" gpu-burn -l # list GPUs\n");
printf(" gpu-burn -i 2 # burns only GPU of index 2\n");
}
// NNN MB
// NN% <0
// 0 --- error
ssize_t decodeUSEMEM(const char *s) {
char *s2;
int64_t r = strtoll(s, &s2, 10);
if (s == s2)
return 0;
if (*s2 == '%')
return (s2[1] == 0) ? -r : 0;
return (*s2 == 0) ? r * 1024 * 1024 : 0;
}
int main(int argc, char **argv) {
int runLength = 10;
bool useDoubles = false;
bool useTensorCores = false;
int thisParam = 0;
ssize_t useBytes = 0; // 0 == use USEMEM% of free mem
int device_id = -1;
char *kernelFile = (char *)COMPARE_KERNEL;
std::chrono::seconds sigterm_timeout_threshold_secs = std::chrono::seconds(SIGTERM_TIMEOUT_THRESHOLD_SECS);
std::vector<std::string> args(argv, argv + argc);
for (size_t i = 1; i < args.size(); ++i) {
if (argc >= 2 && std::string(argv[i]).find("-h") != std::string::npos) {
showHelp();
return 0;
}
if (argc >= 2 && std::string(argv[i]).find("-l") != std::string::npos) {
int count = initCuda();
if (count == 0) {
throw std::runtime_error("No CUDA capable GPUs found.\n");
}
for (int i_dev = 0; i_dev < count; i_dev++) {
CUdevice device_l;
char device_name[255];
checkError(cuDeviceGet(&device_l, i_dev));
checkError(cuDeviceGetName(device_name, 255, device_l));
size_t device_mem_l;
checkError(cuDeviceTotalMem(&device_mem_l, device_l));
printf("ID %i: %s, %ldMB\n", i_dev, device_name,
device_mem_l / 1000 / 1000);
}
thisParam++;
return 0;
}
if (argc >= 2 && std::string(argv[i]).find("-d") != std::string::npos) {
useDoubles = true;
thisParam++;
}
if (argc >= 2 &&
std::string(argv[i]).find("-tc") != std::string::npos) {
useTensorCores = true;
thisParam++;
}
if (argc >= 2 && strncmp(argv[i], "-m", 2) == 0) {
thisParam++;
// -mNNN[%]
// -m NNN[%]
if (argv[i][2]) {
useBytes = decodeUSEMEM(argv[i] + 2);
} else if (i + 1 < args.size()) {
i++;
thisParam++;
useBytes = decodeUSEMEM(argv[i]);
} else {
fprintf(stderr, "Syntax error near -m\n");
exit(EINVAL);
}
if (useBytes == 0) {
fprintf(stderr, "Syntax error near -m\n");
exit(EINVAL);
}
}
if (argc >= 2 && strncmp(argv[i], "-i", 2) == 0) {
thisParam++;
if (argv[i][2]) {
device_id = strtol(argv[i] + 2, NULL, 0);
} else if (i + 1 < args.size()) {
i++;
thisParam++;
device_id = strtol(argv[i], NULL, 0);
} else {
fprintf(stderr, "Syntax error near -i\n");
exit(EINVAL);
}
}
if (argc >= 2 && strncmp(argv[i], "-c", 2) == 0) {
thisParam++;
if (argv[i + 1]) {
kernelFile = argv[i + 1];
thisParam++;
}
}
if (argc >= 2 && strncmp(argv[i], "-stts", 2) == 0) {
thisParam++;
if (argv[i + 1]) {
sigterm_timeout_threshold_secs = std::chrono::seconds(atoi(argv[i + 1]));
thisParam++;
}
}
}
if (argc - thisParam < 2)
printf("Run length not specified in the command line. ");
else
runLength = atoi(argv[1 + thisParam]);
printf("Using compare file: %s\n", kernelFile);
printf("Burning for %d seconds.\n", runLength);
if (useDoubles)
launch<double>(runLength, useDoubles, useTensorCores, useBytes,
device_id, kernelFile, sigterm_timeout_threshold_secs);
else
launch<float>(runLength, useDoubles, useTensorCores, useBytes,
device_id, kernelFile, sigterm_timeout_threshold_secs);
return 0;
}