forked from luke-jr/bfgminer
-
Notifications
You must be signed in to change notification settings - Fork 10
/
driver-opencl.c
1888 lines (1609 loc) · 52.7 KB
/
driver-opencl.c
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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2011-2012 Con Kolivas
* Copyright 2011-2013 Luke Dashjr
* Copyright 2010 Jeff Garzik
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version. See COPYING for more details.
*/
#include "config.h"
#ifdef HAVE_CURSES
// Must be before stdbool, since pdcurses typedefs bool :/
#include <curses.h>
#endif
#ifndef WIN32
#include <dlfcn.h>
#else
#include <windows.h>
#endif
#include <ctype.h>
#include <math.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/types.h>
#ifndef WIN32
#include <sys/resource.h>
#endif
#define OMIT_OPENCL_API
#include "compat.h"
#include "miner.h"
#include "deviceapi.h"
#include "driver-opencl.h"
#include "findnonce.h"
#include "ocl.h"
#include "adl.h"
#include "util.h"
/* TODO: cleanup externals ********************/
#ifdef HAVE_OPENCL
/* Platform API */
CL_API_ENTRY cl_int CL_API_CALL
(*clGetPlatformIDs)(cl_uint /* num_entries */,
cl_platform_id * /* platforms */,
cl_uint * /* num_platforms */) CL_API_SUFFIX__VERSION_1_0;
CL_API_ENTRY cl_int CL_API_CALL
(*clGetPlatformInfo)(cl_platform_id /* platform */,
cl_platform_info /* param_name */,
size_t /* param_value_size */,
void * /* param_value */,
size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
/* Device APIs */
CL_API_ENTRY cl_int CL_API_CALL
(*clGetDeviceIDs)(cl_platform_id /* platform */,
cl_device_type /* device_type */,
cl_uint /* num_entries */,
cl_device_id * /* devices */,
cl_uint * /* num_devices */) CL_API_SUFFIX__VERSION_1_0;
CL_API_ENTRY cl_int CL_API_CALL
(*clGetDeviceInfo)(cl_device_id /* device */,
cl_device_info /* param_name */,
size_t /* param_value_size */,
void * /* param_value */,
size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
/* Context APIs */
CL_API_ENTRY cl_context CL_API_CALL
(*clCreateContextFromType)(const cl_context_properties * /* properties */,
cl_device_type /* device_type */,
void (CL_CALLBACK * /* pfn_notify*/ )(const char *, const void *, size_t, void *),
void * /* user_data */,
cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
CL_API_ENTRY cl_int CL_API_CALL
(*clReleaseContext)(cl_context /* context */) CL_API_SUFFIX__VERSION_1_0;
/* Command Queue APIs */
CL_API_ENTRY cl_command_queue CL_API_CALL
(*clCreateCommandQueue)(cl_context /* context */,
cl_device_id /* device */,
cl_command_queue_properties /* properties */,
cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
CL_API_ENTRY cl_int CL_API_CALL
(*clReleaseCommandQueue)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
/* Memory Object APIs */
CL_API_ENTRY cl_mem CL_API_CALL
(*clCreateBuffer)(cl_context /* context */,
cl_mem_flags /* flags */,
size_t /* size */,
void * /* host_ptr */,
cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
/* Program Object APIs */
CL_API_ENTRY cl_program CL_API_CALL
(*clCreateProgramWithSource)(cl_context /* context */,
cl_uint /* count */,
const char ** /* strings */,
const size_t * /* lengths */,
cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
CL_API_ENTRY cl_program CL_API_CALL
(*clCreateProgramWithBinary)(cl_context /* context */,
cl_uint /* num_devices */,
const cl_device_id * /* device_list */,
const size_t * /* lengths */,
const unsigned char ** /* binaries */,
cl_int * /* binary_status */,
cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
CL_API_ENTRY cl_int CL_API_CALL
(*clReleaseProgram)(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0;
CL_API_ENTRY cl_int CL_API_CALL
(*clBuildProgram)(cl_program /* program */,
cl_uint /* num_devices */,
const cl_device_id * /* device_list */,
const char * /* options */,
void (CL_CALLBACK * /* pfn_notify */)(cl_program /* program */, void * /* user_data */),
void * /* user_data */) CL_API_SUFFIX__VERSION_1_0;
CL_API_ENTRY cl_int CL_API_CALL
(*clGetProgramInfo)(cl_program /* program */,
cl_program_info /* param_name */,
size_t /* param_value_size */,
void * /* param_value */,
size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
CL_API_ENTRY cl_int CL_API_CALL
(*clGetProgramBuildInfo)(cl_program /* program */,
cl_device_id /* device */,
cl_program_build_info /* param_name */,
size_t /* param_value_size */,
void * /* param_value */,
size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
/* Kernel Object APIs */
CL_API_ENTRY cl_kernel CL_API_CALL
(*clCreateKernel)(cl_program /* program */,
const char * /* kernel_name */,
cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
CL_API_ENTRY cl_int CL_API_CALL
(*clReleaseKernel)(cl_kernel /* kernel */) CL_API_SUFFIX__VERSION_1_0;
CL_API_ENTRY cl_int CL_API_CALL
(*clSetKernelArg)(cl_kernel /* kernel */,
cl_uint /* arg_index */,
size_t /* arg_size */,
const void * /* arg_value */) CL_API_SUFFIX__VERSION_1_0;
/* Flush and Finish APIs */
CL_API_ENTRY cl_int CL_API_CALL
(*clFinish)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
/* Enqueued Commands APIs */
CL_API_ENTRY cl_int CL_API_CALL
(*clEnqueueReadBuffer)(cl_command_queue /* command_queue */,
cl_mem /* buffer */,
cl_bool /* blocking_read */,
size_t /* offset */,
size_t /* size */,
void * /* ptr */,
cl_uint /* num_events_in_wait_list */,
const cl_event * /* event_wait_list */,
cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
CL_API_ENTRY cl_int CL_API_CALL
(*clEnqueueWriteBuffer)(cl_command_queue /* command_queue */,
cl_mem /* buffer */,
cl_bool /* blocking_write */,
size_t /* offset */,
size_t /* size */,
const void * /* ptr */,
cl_uint /* num_events_in_wait_list */,
const cl_event * /* event_wait_list */,
cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
CL_API_ENTRY cl_int CL_API_CALL
(*clEnqueueNDRangeKernel)(cl_command_queue /* command_queue */,
cl_kernel /* kernel */,
cl_uint /* work_dim */,
const size_t * /* global_work_offset */,
const size_t * /* global_work_size */,
const size_t * /* local_work_size */,
cl_uint /* num_events_in_wait_list */,
const cl_event * /* event_wait_list */,
cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
#ifdef WIN32
#define dlsym (void*)GetProcAddress
#define dlclose FreeLibrary
#endif
#define LOAD_OCL_SYM(sym) do { \
if (!(sym = dlsym(cl, #sym))) { \
applog(LOG_ERR, "Failed to load OpenCL symbol " #sym ", no GPUs usable"); \
dlclose(cl); \
return false; \
} \
} while(0)
static bool
load_opencl_symbols() {
#if defined(__APPLE__)
void *cl = dlopen("/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL", RTLD_LAZY);
#elif !defined(WIN32)
void *cl = dlopen("libOpenCL.so", RTLD_LAZY);
#else
HMODULE cl = LoadLibrary("OpenCL.dll");
#endif
if (!cl)
{
applog(LOG_ERR, "Failed to load OpenCL library, no GPUs usable");
return false;
}
LOAD_OCL_SYM(clGetPlatformIDs);
LOAD_OCL_SYM(clGetPlatformInfo);
LOAD_OCL_SYM(clGetDeviceIDs);
LOAD_OCL_SYM(clGetDeviceInfo);
LOAD_OCL_SYM(clCreateContextFromType);
LOAD_OCL_SYM(clReleaseContext);
LOAD_OCL_SYM(clCreateCommandQueue);
LOAD_OCL_SYM(clReleaseCommandQueue);
LOAD_OCL_SYM(clCreateBuffer);
LOAD_OCL_SYM(clCreateProgramWithSource);
LOAD_OCL_SYM(clCreateProgramWithBinary);
LOAD_OCL_SYM(clReleaseProgram);
LOAD_OCL_SYM(clBuildProgram);
LOAD_OCL_SYM(clGetProgramInfo);
LOAD_OCL_SYM(clGetProgramBuildInfo);
LOAD_OCL_SYM(clCreateKernel);
LOAD_OCL_SYM(clReleaseKernel);
LOAD_OCL_SYM(clSetKernelArg);
LOAD_OCL_SYM(clFinish);
LOAD_OCL_SYM(clEnqueueReadBuffer);
LOAD_OCL_SYM(clEnqueueWriteBuffer);
LOAD_OCL_SYM(clEnqueueNDRangeKernel);
return true;
}
#endif
typedef cl_int (*queue_kernel_parameters_func_t)(_clState *, struct work *, cl_uint);
struct opencl_kernel_interface {
const char *kiname;
queue_kernel_parameters_func_t queue_kernel_parameters_func;
};
#ifdef HAVE_CURSES
extern WINDOW *mainwin, *statuswin, *logwin;
extern void enable_curses(void);
#endif
extern int mining_threads;
extern int opt_g_threads;
extern bool ping;
extern bool opt_loginput;
extern char *opt_kernel_path;
extern int gpur_thr_id;
extern bool opt_noadl;
extern bool have_opencl;
static _clState *clStates[MAX_GPUDEVICES];
static struct opencl_kernel_interface kernel_interfaces[];
extern void *miner_thread(void *userdata);
extern int dev_from_id(int thr_id);
extern void decay_time(double *f, double fadd);
/**********************************************/
#ifdef HAVE_ADL
extern float gpu_temp(int gpu);
extern int gpu_fanspeed(int gpu);
extern int gpu_fanpercent(int gpu);
#endif
void opencl_early_init()
{
static struct opencl_device_data dataarray[MAX_GPUDEVICES];
for (int i = 0; i < MAX_GPUDEVICES; ++i)
{
struct opencl_device_data * const data = &dataarray[i];
*data = (struct opencl_device_data){
.dynamic = true,
};
gpus[i] = (struct cgpu_info){
.device_data = data,
};
}
}
static
const char *_set_list(char * const arg, const char * const emsg, bool (*set_func)(struct cgpu_info *, const char *))
{
int i, device = 0;
char *nextptr, buf[0x10];
nextptr = strtok(arg, ",");
if (nextptr == NULL)
return emsg;
if (!set_func(&gpus[device++], nextptr))
return emsg;
snprintf(buf, sizeof(buf), "%s", nextptr);
while ((nextptr = strtok(NULL, ",")) != NULL)
if (!set_func(&gpus[device++], nextptr))
return emsg;
if (device == 1) {
for (i = device; i < MAX_GPUDEVICES; i++)
set_func(&gpus[i], buf);
}
return NULL;
}
#define _SET_INTERFACE(PNAME) \
static \
const char *opencl_init_ ## PNAME (struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success) \
{ \
if (!_set_ ## PNAME (proc, newvalue)) \
return "Invalid value for " #PNAME; \
return NULL; \
} \
// END OF _SET_INTERFACE
#define _SET_INT_LIST2(PNAME, VCHECK, FIELD) \
static \
bool _set_ ## PNAME (struct cgpu_info * const cgpu, const char * const _val) \
{ \
const int v = atoi(_val); \
if (!(VCHECK)) \
return false; \
FIELD = v; \
return true; \
} \
_SET_INTERFACE(PNAME) \
const char *set_ ## PNAME(char *arg) \
{ \
return _set_list(arg, "Invalid value passed to " #PNAME, _set_ ## PNAME); \
} \
// END OF _SET_INT_LIST
#define _SET_INT_LIST(PNAME, VCHECK, FIELD) \
_SET_INT_LIST2(PNAME, VCHECK, ((struct opencl_device_data *)cgpu->device_data)->FIELD)
#ifdef HAVE_OPENCL
_SET_INT_LIST(vector , (v == 1 || v == 2 || v == 4), vwidth )
_SET_INT_LIST(worksize, (v >= 1 && v <= 9999) , work_size)
#ifdef USE_SCRYPT
_SET_INT_LIST(shaders , true, shaders)
_SET_INT_LIST(lookup_gap , true, opt_lg )
_SET_INT_LIST(thread_concurrency, true, opt_tc )
#endif
enum cl_kernels select_kernel(const char * const arg)
{
if (!strcmp(arg, "diablo"))
return KL_DIABLO;
if (!strcmp(arg, "diakgcn"))
return KL_DIAKGCN;
if (!strcmp(arg, "poclbm"))
return KL_POCLBM;
if (!strcmp(arg, "phatk"))
return KL_PHATK;
#ifdef USE_SCRYPT
if (!strcmp(arg, "scrypt"))
return KL_SCRYPT;
#endif
return KL_NONE;
}
const char *opencl_get_kernel_interface_name(const enum cl_kernels kern)
{
struct opencl_kernel_interface *ki = &kernel_interfaces[kern];
return ki->kiname;
}
static
bool _set_kernel(struct cgpu_info * const cgpu, const char *_val)
{
FILE *F;
struct opencl_device_data * const data = cgpu->device_data;
size_t knamelen = strlen(_val);
char filename[knamelen + 3 + 1];
sprintf(filename, "%s.cl", _val);
F = opencl_open_kernel(filename);
if (!F)
return false;
fclose(F);
free(data->kernel_file);
data->kernel_file = strdup(_val);
return true;
}
_SET_INTERFACE(kernel)
const char *set_kernel(char *arg)
{
return _set_list(arg, "Invalid value passed to set_kernel", _set_kernel);
}
#endif
static
const char *opencl_init_binary(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
{
struct opencl_device_data * const data = proc->device_data;
char *end;
bool nv;
nv = bfg_strtobool(newvalue, &end, 0);
if (newvalue[0] && !end[0])
data->opt_opencl_binaries = nv ? OBU_LOADSAVE : OBU_NONE;
else
if (!(strcasecmp(newvalue, "load") && strcasecmp(newvalue, "read")))
data->opt_opencl_binaries = OBU_LOAD;
else
if (!(strcasecmp(newvalue, "save") && strcasecmp(newvalue, "write")))
data->opt_opencl_binaries = OBU_SAVE;
else
if (!(strcasecmp(newvalue, "both")))
data->opt_opencl_binaries = OBU_LOADSAVE;
else
if (!(strcasecmp(newvalue, "default")))
data->opt_opencl_binaries = OBU_DEFAULT;
else
return "Invalid value passed to opencl binary";
return NULL;
}
#ifdef HAVE_ADL
/* This function allows us to map an adl device to an opencl device for when
* simple enumeration has failed to match them. */
static
const char *opencl_init_gpu_map(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
{
struct opencl_device_data * const data = proc->device_data;
data->virtual_adl = atoi(newvalue);
data->mapped = true;
return NULL;
}
char *set_gpu_map(char *arg)
{
struct opencl_device_data *data;
int val1 = 0, val2 = 0;
char *nextptr;
nextptr = strtok(arg, ",");
if (nextptr == NULL)
return "Invalid parameters for set gpu map";
if (sscanf(arg, "%d:%d", &val1, &val2) != 2)
return "Invalid description for map pair";
if (val1 < 0 || val1 > MAX_GPUDEVICES || val2 < 0 || val2 > MAX_GPUDEVICES)
return "Invalid value passed to set_gpu_map";
data = gpus[val1].device_data;
data->virtual_adl = val2;
data->mapped = true;
while ((nextptr = strtok(NULL, ",")) != NULL) {
if (sscanf(nextptr, "%d:%d", &val1, &val2) != 2)
return "Invalid description for map pair";
if (val1 < 0 || val1 > MAX_GPUDEVICES || val2 < 0 || val2 > MAX_GPUDEVICES)
return "Invalid value passed to set_gpu_map";
data = gpus[val1].device_data;
data->virtual_adl = val2;
data->mapped = true;
}
return NULL;
}
static
bool _set_gpu_engine(struct cgpu_info * const cgpu, const char * const _val)
{
int val1, val2;
get_intrange(_val, &val1, &val2);
if (val1 < 0 || val1 > 9999 || val2 < 0 || val2 > 9999 || val2 < val1)
return false;
struct opencl_device_data * const data = cgpu->device_data;
struct gpu_adl * const ga = &data->adl;
data->min_engine = val1;
data->gpu_engine = val2;
ga->autoengine = (val1 != val2);
return true;
}
_SET_INTERFACE(gpu_engine)
const char *set_gpu_engine(char *arg)
{
return _set_list(arg, "Invalid value passed to set_gpu_engine", _set_gpu_engine);
}
static
const char *opencl_set_gpu_engine(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
{
struct opencl_device_data * const data = proc->device_data;
struct gpu_adl * const ga = &data->adl;
int val1, val2;
get_intrange(newvalue, &val1, &val2);
if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
return "Invalid value for clock";
if (val1 == val2)
{
if (set_engineclock(proc->device_id, val1))
return "Failed to set gpu_engine";
ga->autoengine = false;
}
else
{
// Ensure current clock is within range
if (ga->lastengine < val1)
{
if (set_engineclock(proc->device_id, val1))
return "Failed to set gpu_engine";
}
else
if (ga->lastengine > val2)
if (set_engineclock(proc->device_id, val2))
return "Failed to set gpu_engine";
data->min_engine = val1;
data->gpu_engine = val2;
ga->autoengine = true;
}
return NULL;
}
static
bool _set_gpu_fan(struct cgpu_info * const cgpu, const char * const _val)
{
int val1, val2;
get_intrange(_val, &val1, &val2);
if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
return false;
struct opencl_device_data * const data = cgpu->device_data;
struct gpu_adl * const ga = &data->adl;
data->min_fan = val1;
data->gpu_fan = val2;
ga->autofan = (val1 != val2);
return true;
}
_SET_INTERFACE(gpu_fan)
const char *set_gpu_fan(char *arg)
{
return _set_list(arg, "Invalid value passed to set_gpu_fan", _set_gpu_fan);
}
static
const char *opencl_set_gpu_fan(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
{
struct opencl_device_data * const data = proc->device_data;
struct gpu_adl * const ga = &data->adl;
int val1, val2;
get_intrange(newvalue, &val1, &val2);
if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
return "Invalid value for fan";
if (val1 == val2)
{
if (set_fanspeed(proc->device_id, val1))
return "Failed to set gpu_fan";
ga->autofan = false;
}
else
{
// Ensure current fan is within range
if (ga->targetfan < val1)
{
if (set_fanspeed(proc->device_id, val1))
return "Failed to set gpu_fan";
}
else
if (ga->targetfan > val2)
if (set_fanspeed(proc->device_id, val2))
return "Failed to set gpu_fan";
data->min_fan = val1;
data->gpu_fan = val2;
ga->autofan = true;
}
return NULL;
}
_SET_INT_LIST(gpu_memclock , (v >= 1 && v < 9999), gpu_memclock )
static
const char *opencl_set_gpu_memclock(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
{
if (set_memoryclock(proc->device_id, atoi(newvalue)))
return "Failed to set gpu_memclock";
return NULL;
}
_SET_INT_LIST(gpu_memdiff , (v >= -9999 && v <= 9999), gpu_memdiff )
static
const char *opencl_set_gpu_memdiff(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
{
struct opencl_device_data * const data = proc->device_data;
if (!_set_gpu_memdiff(proc, newvalue))
return "Invalid value for gpu_memdiff";
set_engineclock(proc->device_id, data->gpu_engine);
return NULL;
}
_SET_INT_LIST(gpu_powertune, (v >= -99 && v <= 99), gpu_powertune)
_SET_INT_LIST(gpu_vddc , (v >= 0 && v < 9999), gpu_vddc )
static
const char *opencl_set_gpu_vddc(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
{
if (set_vddc(proc->device_id, atof(newvalue)))
return "Failed to set gpu_vddc";
return NULL;
}
_SET_INT_LIST(temp_overheat, (v >= 0 && v < 200), adl.overtemp )
#endif
#ifdef HAVE_OPENCL
// SHA256d "intensity" has an artificial offset of -15
double oclthreads_to_intensity(const unsigned long oclthreads, const bool is_sha256d)
{
double intensity = log2(oclthreads);
if (is_sha256d)
intensity -= 15.;
return intensity;
}
unsigned long intensity_to_oclthreads(double intensity, const bool is_sha256d)
{
if (is_sha256d)
intensity += 15;
return pow(2, intensity);
}
double oclthreads_to_xintensity(const unsigned long oclthreads, const cl_uint max_compute_units)
{
return (double)oclthreads / (double)max_compute_units / 64.;
}
unsigned long xintensity_to_oclthreads(const double xintensity, const cl_uint max_compute_units)
{
return xintensity * max_compute_units * 0x40;
}
bool opencl_set_intensity_from_str(struct cgpu_info * const cgpu, const char *_val)
{
struct opencl_device_data * const data = cgpu->device_data;
unsigned long oclthreads = 0;
bool dynamic = false;
if (!strncasecmp(_val, "d", 1))
{
dynamic = true;
++_val;
}
if (!strncasecmp(_val, "x", 1))
{
const double v = atof(&_val[1]);
if (v < 1 || v > 9999)
return false;
// thr etc won't be initialised here, so avoid dereferencing it
if (data->oclthreads)
{
struct thr_info * const thr = cgpu->thr[0];
const int thr_id = thr->id;
_clState * const clState = clStates[thr_id];
oclthreads = xintensity_to_oclthreads(v, clState->max_compute_units);
}
}
else
if (isdigit(_val[0]))
{
const double v = atof(_val);
if (v < MIN_INTENSITY || v > MAX_GPU_INTENSITY)
return false;
oclthreads = intensity_to_oclthreads(v, !opt_scrypt);
}
// Make actual assignments after we know the values are valid
data->dynamic = dynamic;
if (data->oclthreads)
{
data->oclthreads = oclthreads;
pause_dynamic_threads(cgpu->device_id);
}
else
{
if (unlikely(data->_init_intensity))
free(data->_init_intensity);
data->_init_intensity = strdup(_val);
}
return true;
}
#define _set_intensity opencl_set_intensity_from_str
_SET_INTERFACE(intensity)
const char *set_intensity(char *arg)
{
return _set_list(arg, "Invalid value passed to intensity", _set_intensity);
}
_SET_INT_LIST2(gpu_threads, (v >= 1 && v <= 10), cgpu->threads)
#endif
void write_config_opencl(FILE * const fcfg)
{
#ifdef HAVE_ADL
if (opt_reorder)
fprintf(fcfg, ",\n\"gpu-reorder\" : true");
#endif
}
#ifdef HAVE_OPENCL
BFG_REGISTER_DRIVER(opencl_api)
static const struct bfg_set_device_definition opencl_set_device_funcs_probe[];
static const struct bfg_set_device_definition opencl_set_device_funcs[];
char *print_ndevs_and_exit(int *ndevs)
{
opt_log_output = true;
opencl_api.drv_detect();
clear_adl(*ndevs);
applog(LOG_INFO, "%i GPU devices max detected", *ndevs);
exit(*ndevs);
}
#endif
struct cgpu_info gpus[MAX_GPUDEVICES]; /* Maximum number apparently possible */
struct cgpu_info *cpus;
#ifdef HAVE_OPENCL
/* In dynamic mode, only the first thread of each device will be in use.
* This potentially could start a thread that was stopped with the start-stop
* options if one were to disable dynamic from the menu on a paused GPU */
void pause_dynamic_threads(int gpu)
{
struct cgpu_info *cgpu = &gpus[gpu];
struct opencl_device_data * const data = cgpu->device_data;
int i;
for (i = 1; i < cgpu->threads; i++) {
struct thr_info *thr;
thr = cgpu->thr[i];
if (!thr->pause && data->dynamic) {
applog(LOG_WARNING, "Disabling extra threads due to dynamic mode.");
}
thr->pause = data->dynamic;
if (!data->dynamic && cgpu->deven != DEV_DISABLED)
mt_enable(thr);
}
}
struct device_drv opencl_api;
#endif /* HAVE_OPENCL */
#if defined(HAVE_OPENCL) && defined(HAVE_CURSES)
static
void opencl_wlogprint_status(struct cgpu_info *cgpu)
{
struct opencl_device_data * const data = cgpu->device_data;
struct thr_info *thr = cgpu->thr[0];
const int thr_id = thr->id;
_clState * const clState = clStates[thr_id];
int i;
char checkin[40];
double displayed_rolling;
bool mhash_base = !(cgpu->rolling < 1);
char logline[255];
strcpy(logline, ""); // In case it has no data
{
double intensity = oclthreads_to_intensity(data->oclthreads, !opt_scrypt);
double xintensity = oclthreads_to_xintensity(data->oclthreads, clState->max_compute_units);
const char *iunit = "";
if (xintensity - (int)xintensity < intensity - (int)intensity)
{
intensity = xintensity;
iunit = "x";
}
tailsprintf(logline, sizeof(logline), "I:%s%s%g ",
(data->dynamic ? "d" : ""),
iunit,
intensity);
}
#ifdef HAVE_ADL
if (data->has_adl) {
int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
float temp = 0, vddc = 0;
if (gpu_stats(cgpu->device_id, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune)) {
if (fanspeed != -1 || fanpercent != -1) {
tailsprintf(logline, sizeof(logline), "F:");
if (fanspeed > 9999)
fanspeed = 9999;
if (fanpercent != -1)
{
tailsprintf(logline, sizeof(logline), "%d%%", fanpercent);
if (fanspeed != -1)
tailsprintf(logline, sizeof(logline), "(%dRPM)", fanspeed);
}
else
tailsprintf(logline, sizeof(logline), "%dRPM", fanspeed);
tailsprintf(logline, sizeof(logline), " ");
}
if (engineclock != -1)
tailsprintf(logline, sizeof(logline), "E:%dMHz ", engineclock);
if (memclock != -1)
tailsprintf(logline, sizeof(logline), "M:%dMHz ", memclock);
if (vddc != -1)
tailsprintf(logline, sizeof(logline), "V:%.3fV ", vddc);
if (activity != -1)
tailsprintf(logline, sizeof(logline), "A:%d%% ", activity);
if (powertune != -1)
tailsprintf(logline, sizeof(logline), "P:%d%%", powertune);
}
}
#endif
wlogprint("%s\n", logline);
wlogprint("Last initialised: %s\n", cgpu->init);
for (i = 0; i < mining_threads; i++) {
thr = get_thread(i);
if (thr->cgpu != cgpu)
continue;
get_datestamp(checkin, sizeof(checkin), time(NULL) - timer_elapsed(&thr->last, NULL));
displayed_rolling = thr->rolling;
if (!mhash_base)
displayed_rolling *= 1000;
snprintf(logline, sizeof(logline), "Thread %d: %.1f %sh/s %s ", i, displayed_rolling, mhash_base ? "M" : "K" , cgpu->deven != DEV_DISABLED ? "Enabled" : "Disabled");
switch (cgpu->status) {
default:
case LIFE_WELL:
tailsprintf(logline, sizeof(logline), "ALIVE");
break;
case LIFE_SICK:
tailsprintf(logline, sizeof(logline), "SICK reported in %s", checkin);
break;
case LIFE_DEAD:
tailsprintf(logline, sizeof(logline), "DEAD reported in %s", checkin);
break;
case LIFE_INIT:
case LIFE_NOSTART:
tailsprintf(logline, sizeof(logline), "Never started");
break;
}
if (thr->pause)
tailsprintf(logline, sizeof(logline), " paused");
wlogprint("%s\n", logline);
}
}
static
void opencl_tui_wlogprint_choices(struct cgpu_info *cgpu)
{
wlogprint("[I]ntensity [R]estart GPU ");
#ifdef HAVE_ADL
struct opencl_device_data * const data = cgpu->device_data;
if (data->has_adl)
wlogprint("[C]hange settings ");
#endif
}
static
const char *opencl_tui_handle_choice(struct cgpu_info *cgpu, int input)
{
struct opencl_device_data * const data = cgpu->device_data;
switch (input)
{
case 'i': case 'I':
{
char *intvar;
if (opt_scrypt) {
intvar = curses_input("Set GPU scan intensity (d or "
MIN_SCRYPT_INTENSITY_STR " -> "
MAX_SCRYPT_INTENSITY_STR ")");
} else {
intvar = curses_input("Set GPU scan intensity (d or "
MIN_SHA_INTENSITY_STR " -> "
MAX_SHA_INTENSITY_STR ")");
}
if (!intvar)
return "Invalid intensity\n";
if (!strncasecmp(intvar, "d", 1)) {
data->dynamic = true;
pause_dynamic_threads(cgpu->device_id);
free(intvar);
return "Dynamic mode enabled\n";
}
if (!_set_intensity(cgpu, intvar))
return "Invalid intensity (out of range)\n";
free(intvar);
return "Intensity changed\n";
}
case 'r': case 'R':
reinit_device(cgpu);
return "Attempting to restart\n";
case 'c': case 'C':
{
char logline[256];
clear_logwin();
get_statline3(logline, sizeof(logline), cgpu, true, true);
wattron(logwin, A_BOLD);
wlogprint("%s", logline);
wattroff(logwin, A_BOLD);
wlogprint("\n");
change_gpusettings(cgpu->device_id);
return ""; // Force refresh
}
}
return NULL;
}
#endif
#ifdef HAVE_OPENCL
#define CL_SET_BLKARG(blkvar) status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->blkvar)
#define CL_SET_ARG(var) status |= clSetKernelArg(*kernel, num++, sizeof(var), (void *)&var)
#define CL_SET_VARG(args, var) status |= clSetKernelArg(*kernel, num++, args * sizeof(uint), (void *)var)
static
void *_opencl_work_data_dup(struct work * const work)
{
struct opencl_work_data *p = malloc(sizeof(*p));
memcpy(p, work->device_data, sizeof(*p));
#ifdef USE_SCRYPT
p->work = work;
#endif
return p;
}
static
void _opencl_work_data_free(struct work * const work)
{
free(work->device_data);
}
static
struct opencl_work_data *_opencl_work_data(struct work * const work)
{
if (!work->device_data)
{