forked from BOINC/boinc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu_detect.cpp
686 lines (613 loc) · 20 KB
/
gpu_detect.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
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2009 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
// client-specific GPU code. Mostly GPU detection
#ifndef _DEBUG
#define USE_CHILD_PROCESS_TO_DETECT_GPUS 1
#endif
#include "cpp.h"
#ifdef _WIN32
#include "boinc_win.h"
#ifdef _MSC_VER
#define snprintf _snprintf
#define chdir _chdir
#endif
#else
#include "config.h"
#include <setjmp.h>
#include <signal.h>
#endif
#include "coproc.h"
#include "gpu_detect.h"
#include "file_names.h"
#include "util.h"
#include "str_replace.h"
#include "client_msgs.h"
#include "client_state.h"
using std::string;
using std::vector;
#ifndef _WIN32
jmp_buf resume;
void segv_handler(int) {
longjmp(resume, 1);
}
#endif
vector<COPROC_ATI> ati_gpus;
vector<COPROC_NVIDIA> nvidia_gpus;
vector<COPROC_INTEL> intel_gpus;
vector<OPENCL_DEVICE_PROP> ati_opencls;
vector<OPENCL_DEVICE_PROP> nvidia_opencls;
vector<OPENCL_DEVICE_PROP> intel_gpu_opencls;
vector<OPENCL_DEVICE_PROP> other_opencls;
vector<OPENCL_CPU_PROP> cpu_opencls;
static char* client_path;
// argv[0] from the command used to run client.
// May be absolute or relative.
static char client_dir[MAXPATHLEN];
// current directory at start of client
void COPROCS::get(
bool use_all, vector<string>&descs, vector<string>&warnings,
IGNORE_GPU_INSTANCE& ignore_gpu_instance
) {
#if USE_CHILD_PROCESS_TO_DETECT_GPUS
int retval = 0;
char buf[256];
retval = launch_child_process_to_detect_gpus();
if (retval) {
snprintf(buf, sizeof(buf),
"launch_child_process_to_detect_gpus() returned error %d",
retval
);
warnings.push_back(buf);
}
retval = read_coproc_info_file(warnings);
if (retval) {
snprintf(buf, sizeof(buf),
"read_coproc_info_file() returned error %d",
retval
);
warnings.push_back(buf);
}
#else
detect_gpus(warnings);
#endif
correlate_gpus(use_all, descs, ignore_gpu_instance);
}
void COPROCS::detect_gpus(std::vector<std::string> &warnings) {
#ifdef _WIN32
try {
nvidia.get(warnings);
}
catch (...) {
warnings.push_back("Caught SIGSEGV in NVIDIA GPU detection");
}
try {
ati.get(warnings);
}
catch (...) {
warnings.push_back("Caught SIGSEGV in ATI GPU detection");
}
try {
intel_gpu.get(warnings);
}
catch (...) {
warnings.push_back("Caught SIGSEGV in INTEL GPU detection");
}
try {
// OpenCL detection must come last
get_opencl(warnings);
}
catch (...) {
warnings.push_back("Caught SIGSEGV in OpenCL detection");
}
#else
void (*old_sig)(int) = signal(SIGSEGV, segv_handler);
if (setjmp(resume)) {
warnings.push_back("Caught SIGSEGV in NVIDIA GPU detection");
} else {
nvidia.get(warnings);
}
#ifndef __APPLE__ // ATI does not yet support CAL on Macs
if (setjmp(resume)) {
warnings.push_back("Caught SIGSEGV in ATI GPU detection");
} else {
ati.get(warnings);
}
#endif
if (setjmp(resume)) {
warnings.push_back("Caught SIGSEGV in INTEL GPU detection");
} else {
intel_gpu.get(warnings);
}
if (setjmp(resume)) {
warnings.push_back("Caught SIGSEGV in OpenCL detection");
} else {
// OpenCL detection must come last
get_opencl(warnings);
}
signal(SIGSEGV, old_sig);
#endif
}
void COPROCS::correlate_gpus(
bool use_all,
std::vector<std::string> &descs,
IGNORE_GPU_INSTANCE &ignore_gpu_instance
) {
unsigned int i;
char buf[256], buf2[256];
nvidia.correlate(use_all, ignore_gpu_instance[PROC_TYPE_NVIDIA_GPU]);
ati.correlate(use_all, ignore_gpu_instance[PROC_TYPE_AMD_GPU]);
intel_gpu.correlate(use_all, ignore_gpu_instance[PROC_TYPE_INTEL_GPU]);
correlate_opencl(use_all, ignore_gpu_instance);
// NOTE: OpenCL can report a max of only 4GB.
for (i=0; i<cpu_opencls.size(); i++) {
gstate.host_info.opencl_cpu_prop[gstate.host_info.num_opencl_cpu_platforms++] = cpu_opencls[i];
}
for (i=0; i<nvidia_gpus.size(); i++) {
// This is really CUDA description
nvidia_gpus[i].description(buf, sizeof(buf));
switch(nvidia_gpus[i].is_used) {
case COPROC_IGNORED:
snprintf(buf2, sizeof(buf2),
"CUDA: NVIDIA GPU %d (ignored by config): %s",
nvidia_gpus[i].device_num, buf
);
break;
case COPROC_USED:
snprintf(buf2, sizeof(buf2),
"CUDA: NVIDIA GPU %d: %s",
nvidia_gpus[i].device_num, buf
);
break;
case COPROC_UNUSED:
default:
snprintf(buf2, sizeof(buf2),
"CUDA: NVIDIA GPU %d (not used): %s",
nvidia_gpus[i].device_num, buf
);
#ifdef __APPLE__
if ((nvidia_gpus[i].cuda_version >= 6050) &&
nvidia_gpus[i].prop.major < 2) {
// This will be called only if CUDA recognized and reported the GPU
msg_printf(NULL, MSG_USER_ALERT, "NVIDIA GPU %d: %s %s",
nvidia_gpus[i].device_num, nvidia_gpus[i].prop.name,
_("cannot be used for CUDA or OpenCL computation with CUDA driver 6.5 or later")
);
}
#endif
break;
}
descs.push_back(string(buf2));
}
for (i=0; i<ati_gpus.size(); i++) {
// This is really CAL description
ati_gpus[i].description(buf, sizeof(buf));
switch(ati_gpus[i].is_used) {
case COPROC_IGNORED:
snprintf(buf2, sizeof(buf2),
"CAL: ATI GPU %d (ignored by config): %s",
ati_gpus[i].device_num, buf
);
break;
case COPROC_USED:
snprintf(buf2, sizeof(buf2),
"CAL: ATI GPU %d: %s",
ati_gpus[i].device_num, buf
);
break;
case COPROC_UNUSED:
default:
snprintf(buf2, sizeof(buf2),
"CAL: ATI GPU %d: (not used) %s",
ati_gpus[i].device_num, buf
);
break;
}
descs.push_back(string(buf2));
}
// Create descriptions for OpenCL NVIDIA GPUs
//
for (i=0; i<nvidia_opencls.size(); i++) {
if (nvidia_opencls[i].warn_bad_cuda) {
// This will be called only if CUDA did _not_ recognize and report the GPU
msg_printf(NULL, MSG_USER_ALERT, "NVIDIA GPU %d: %s %s",
nvidia_opencls[i].device_num, nvidia_opencls[i].name,
_("cannot be used for CUDA or OpenCL computation with CUDA driver 6.5 or later")
);
}
nvidia_opencls[i].description(buf, sizeof(buf), proc_type_name(PROC_TYPE_NVIDIA_GPU));
descs.push_back(string(buf));
}
// Create descriptions for OpenCL ATI GPUs
//
for (i=0; i<ati_opencls.size(); i++) {
ati_opencls[i].description(buf, sizeof(buf), proc_type_name(PROC_TYPE_AMD_GPU));
descs.push_back(string(buf));
}
// Create descriptions for OpenCL Intel GPUs
//
for (i=0; i<intel_gpu_opencls.size(); i++) {
intel_gpu_opencls[i].description(buf, sizeof(buf), proc_type_name(PROC_TYPE_INTEL_GPU));
descs.push_back(string(buf));
}
// Create descriptions for other OpenCL GPUs
//
int max_other_coprocs = MAX_RSC-1; // coprocs[0] is reserved for CPU
if (have_nvidia()) max_other_coprocs--;
if (have_ati()) max_other_coprocs--;
if (have_intel_gpu()) max_other_coprocs--;
// TODO: Should we implement cc_config ignore vectors for other (future) OpenCL coprocessors?
for (i=0; i<other_opencls.size(); i++) {
if ((int)i > max_other_coprocs) {
other_opencls[i].is_used = COPROC_UNUSED;
}
other_opencls[i].description(buf, sizeof(buf), other_opencls[i].name);
descs.push_back(string(buf));
}
// Create descriptions for OpenCL CPUs
//
for (i=0; i<cpu_opencls.size(); i++) {
cpu_opencls[i].description(buf, sizeof(buf));
descs.push_back(string(buf));
}
ati_gpus.clear();
nvidia_gpus.clear();
intel_gpus.clear();
ati_opencls.clear();
nvidia_opencls.clear();
intel_gpu_opencls.clear();
cpu_opencls.clear();
}
// This is called from CLIENT_STATE::init()
// after adding NVIDIA, ATI and Intel GPUs
// If we don't care about the order of GPUs in COPROCS::coprocs[],
// this code could be included at the end of COPROCS::correlate_gpus().
//
int COPROCS::add_other_coproc_types() {
int retval = 0;
for (unsigned int i=0; i<other_opencls.size(); i++) {
if (other_opencls[i].is_used != COPROC_USED) continue;
if (n_rsc >= MAX_RSC) {
retval = ERR_BUFFER_OVERFLOW;
break;
}
COPROC c;
// For device types other than NVIDIA, ATI or Intel GPU.
// we put each instance into a separate other_opencls element,
// so count=1.
//
c.count = 1;
c.opencl_device_count = 1;
c.opencl_prop = other_opencls[i];
c.available_ram = c.opencl_prop.global_mem_size;
c.device_num = c.opencl_prop.device_num;
c.peak_flops = c.opencl_prop.peak_flops;
c.have_opencl = true;
c.opencl_device_indexes[0] = c.opencl_prop.opencl_device_index;
c.opencl_device_ids[0] = c.opencl_prop.device_id;
c.instance_has_opencl[0] = true;
safe_strcpy(c.type, other_opencls[i].name);
// Don't call COPROCS::add() because duplicate type is legal here
coprocs[n_rsc++] = c;
}
other_opencls.clear();
return retval;
}
// Some dual-GPU laptops (e.g., Macbook Pro) don't
// power down the more powerful GPU until all
// applications which used them exit. To save
// battery life, the client launches a second
// instance of the client as a child process to
// detect and get information about the GPUs.
// The child process writes the info to a temp
// file which our main client then reads.
//
void COPROCS::set_path_to_client(char *path) {
client_path = path;
// The path may be relative to the current directory
boinc_getcwd(client_dir);
}
int COPROCS::write_coproc_info_file(vector<string> &warnings) {
MIOFILE mf;
unsigned int i, temp;
FILE* f;
f = boinc_fopen(COPROC_INFO_FILENAME, "wb");
if (!f) return ERR_FOPEN;
mf.init_file(f);
mf.printf(" <coprocs>\n");
if (nvidia.have_cuda) {
mf.printf(" <have_cuda>1</have_cuda>\n");
mf.printf(" <cuda_version>%d</cuda_version>\n", nvidia.cuda_version);
}
for (i=0; i<ati_gpus.size(); ++i) {
ati_gpus[i].write_xml(mf, false);
}
for (i=0; i<nvidia_gpus.size(); ++i) {
temp = nvidia_gpus[i].count;
nvidia_gpus[i].count = 1;
nvidia_gpus[i].pci_infos[0] = nvidia_gpus[i].pci_info;
nvidia_gpus[i].write_xml(mf, false);
nvidia_gpus[i].count = temp;
}
for (i=0; i<intel_gpus.size(); ++i) {
intel_gpus[i].write_xml(mf, false);
}
for (i=0; i<ati_opencls.size(); ++i) {
ati_opencls[i].write_xml(mf, "ati_opencl", true);
}
for (i=0; i<nvidia_opencls.size(); ++i) {
nvidia_opencls[i].write_xml(mf, "nvidia_opencl", true);
}
for (i=0; i<intel_gpu_opencls.size(); ++i) {
intel_gpu_opencls[i].write_xml(mf, "intel_gpu_opencl", true);
}
for (i=0; i<other_opencls.size(); i++) {
other_opencls[i].write_xml(mf, "other_opencl", true);
}
for (i=0; i<cpu_opencls.size(); i++) {
cpu_opencls[i].write_xml(mf);
}
for (i=0; i<warnings.size(); ++i) {
mf.printf("<warning>%s</warning>\n", warnings[i].c_str());
}
mf.printf(" </coprocs>\n");
fclose(f);
return 0;
}
int COPROCS::read_coproc_info_file(vector<string> &warnings) {
MIOFILE mf;
int retval;
FILE* f;
string s;
COPROC_ATI my_ati_gpu;
COPROC_NVIDIA my_nvidia_gpu;
COPROC_INTEL my_intel_gpu;
OPENCL_DEVICE_PROP ati_opencl;
OPENCL_DEVICE_PROP nvidia_opencl;
OPENCL_DEVICE_PROP intel_gpu_opencl;
OPENCL_DEVICE_PROP other_opencl;
OPENCL_CPU_PROP cpu_opencl;
ati_gpus.clear();
nvidia_gpus.clear();
intel_gpus.clear();
ati_opencls.clear();
nvidia_opencls.clear();
intel_gpu_opencls.clear();
other_opencls.clear();
cpu_opencls.clear();
f = boinc_fopen(COPROC_INFO_FILENAME, "r");
if (!f) return ERR_FOPEN;
XML_PARSER xp(&mf);
mf.init_file(f);
if (!xp.parse_start("coprocs")) {
fclose(f);
return ERR_XML_PARSE;
}
while (!xp.get_tag()) {
if (xp.match_tag("/coprocs")) {
fclose(f);
return 0;
}
if (xp.parse_bool("have_cuda", nvidia.have_cuda)) continue;
if (xp.parse_int("cuda_version", nvidia.cuda_version)) {
continue;
}
if (xp.match_tag("coproc_ati")) {
retval = my_ati_gpu.parse(xp);
if (retval) {
my_ati_gpu.clear();
} else {
my_ati_gpu.device_num = (int)ati_gpus.size();
ati_gpus.push_back(my_ati_gpu);
}
continue;
}
if (xp.match_tag("coproc_cuda")) {
retval = my_nvidia_gpu.parse(xp);
if (retval) {
my_nvidia_gpu.clear();
} else {
my_nvidia_gpu.device_num = (int)nvidia_gpus.size();
my_nvidia_gpu.pci_info = my_nvidia_gpu.pci_infos[0];
memset(&my_nvidia_gpu.pci_infos[0], 0, sizeof(struct PCI_INFO));
nvidia_gpus.push_back(my_nvidia_gpu);
}
continue;
}
if (xp.match_tag("coproc_intel_gpu")) {
retval = my_intel_gpu.parse(xp);
if (retval) {
my_intel_gpu.clear();
} else {
my_intel_gpu.device_num = (int)intel_gpus.size();
intel_gpus.push_back(my_intel_gpu);
}
continue;
}
if (xp.match_tag("ati_opencl")) {
memset(&ati_opencl, 0, sizeof(ati_opencl));
retval = ati_opencl.parse(xp, "/ati_opencl");
if (retval) {
memset(&ati_opencl, 0, sizeof(ati_opencl));
} else {
ati_opencl.is_used = COPROC_IGNORED;
ati_opencls.push_back(ati_opencl);
}
continue;
}
if (xp.match_tag("nvidia_opencl")) {
memset(&nvidia_opencl, 0, sizeof(nvidia_opencl));
retval = nvidia_opencl.parse(xp, "/nvidia_opencl");
if (retval) {
memset(&nvidia_opencl, 0, sizeof(nvidia_opencl));
} else {
nvidia_opencl.is_used = COPROC_IGNORED;
nvidia_opencls.push_back(nvidia_opencl);
}
continue;
}
if (xp.match_tag("intel_gpu_opencl")) {
memset(&intel_gpu_opencl, 0, sizeof(intel_gpu_opencl));
retval = intel_gpu_opencl.parse(xp, "/intel_gpu_opencl");
if (retval) {
memset(&intel_gpu_opencl, 0, sizeof(intel_gpu_opencl));
} else {
intel_gpu_opencl.is_used = COPROC_IGNORED;
intel_gpu_opencls.push_back(intel_gpu_opencl);
}
continue;
}
if (xp.match_tag("other_opencl")) {
memset(&other_opencl, 0, sizeof(other_opencl));
retval = other_opencl.parse(xp, "/other_opencl");
if (retval) {
memset(&other_opencl, 0, sizeof(other_opencl));
} else {
other_opencl.is_used = COPROC_USED;
other_opencls.push_back(other_opencl);
}
continue;
}
if (xp.match_tag("opencl_cpu_prop")) {
memset(&cpu_opencl, 0, sizeof(cpu_opencl));
retval = cpu_opencl.parse(xp);
if (retval) {
memset(&cpu_opencl, 0, sizeof(cpu_opencl));
} else {
cpu_opencl.opencl_prop.is_used = COPROC_IGNORED;
cpu_opencls.push_back(cpu_opencl);
}
continue;
}
if (xp.parse_string("warning", s)) {
warnings.push_back(s);
continue;
}
// TODO: parse OpenCL info for CPU when implemented:
// gstate.host_info.have_cpu_opencl
// gstate.host_info.cpu_opencl_prop
}
fclose(f);
return ERR_XML_PARSE;
}
int COPROCS::launch_child_process_to_detect_gpus() {
#ifdef _WIN32
HANDLE prog;
#else
int prog;
#endif
char quoted_data_dir[MAXPATHLEN+2];
char data_dir[MAXPATHLEN];
int retval = 0;
retval = boinc_delete_file(COPROC_INFO_FILENAME);
if (retval) {
msg_printf(0, MSG_INFO,
"Failed to delete old %s. error code %d",
COPROC_INFO_FILENAME, retval
);
} else {
for (;;) {
if (!boinc_file_exists(COPROC_INFO_FILENAME)) break;
boinc_sleep(0.01);
}
}
boinc_getcwd(data_dir);
#ifdef _WIN32
strlcpy(quoted_data_dir, "\"", sizeof(quoted_data_dir));
strlcat(quoted_data_dir, data_dir, sizeof(quoted_data_dir));
strlcat(quoted_data_dir, "\"", sizeof(quoted_data_dir));
#else
strlcpy(quoted_data_dir, data_dir, sizeof(quoted_data_dir));
#endif
if (log_flags.coproc_debug) {
msg_printf(0, MSG_INFO,
"[coproc] launching child process at %s",
client_path
);
msg_printf(0, MSG_INFO,
"[coproc] relative to directory %s",
client_dir
);
msg_printf(0, MSG_INFO,
"[coproc] with data directory %s",
quoted_data_dir
);
}
int argc = 4;
char* const argv[5] = {
#ifdef _WIN32
const_cast<char *>("boinc.exe"),
#else
const_cast<char *>("boinc"),
#endif
const_cast<char *>("--detect_gpus"),
const_cast<char *>("--dir"),
const_cast<char *>(quoted_data_dir),
NULL
};
chdir(client_dir);
retval = run_program(
client_dir,
client_path,
argc,
argv,
#ifdef _DEBUG
1,
#else
0,
#endif
prog
);
chdir(data_dir);
if (retval) {
if (log_flags.coproc_debug) {
msg_printf(0, MSG_INFO,
"[coproc] run_program of child process returned error %d",
retval
);
}
return retval;
}
retval = get_exit_status(prog);
if (retval) {
msg_printf(0, MSG_INFO,
"GPU detection failed. error code %d",
retval
);
}
return 0;
}
// print descriptions of coprocs specified in cc_config.xml,
// and make sure counts are <= 64
//
void COPROCS::bound_counts() {
for (int j=1; j<n_rsc; j++) {
msg_printf(NULL, MSG_INFO, "Coprocessor specified in cc_config.xml. Type %s (%s); count %d",
coprocs[j].type,
coprocs[j].non_gpu?"non-GPU":"GPU",
coprocs[j].count
);
if (coprocs[j].count > MAX_COPROC_INSTANCES) {
msg_printf(NULL, MSG_USER_ALERT,
"%d instances of %s specified in cc_config.xml; max is %d",
coprocs[j].count,
coprocs[j].type,
MAX_COPROC_INSTANCES
);
coprocs[j].count = MAX_COPROC_INSTANCES;
}
}
}