driver-opencl.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975
  1. /*
  2. * Copyright 2011-2013 Con Kolivas
  3. * Copyright 2011-2014 Luke Dashjr
  4. * Copyright 2014 Nate Woolls
  5. * Copyright 2010 Jeff Garzik
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 3 of the License, or (at your option)
  10. * any later version. See COPYING for more details.
  11. */
  12. #include "config.h"
  13. #ifdef HAVE_CURSES
  14. // Must be before stdbool, since pdcurses typedefs bool :/
  15. #include <curses.h>
  16. #endif
  17. #ifndef WIN32
  18. #include <dlfcn.h>
  19. #else
  20. #include <windows.h>
  21. #endif
  22. #include <ctype.h>
  23. #include <math.h>
  24. #include <string.h>
  25. #include <stdbool.h>
  26. #include <stdint.h>
  27. #include <stdio.h>
  28. #include <sys/types.h>
  29. #ifndef WIN32
  30. #include <sys/resource.h>
  31. #endif
  32. #define OMIT_OPENCL_API
  33. #include "compat.h"
  34. #include "miner.h"
  35. #include "deviceapi.h"
  36. #include "driver-opencl.h"
  37. #include "findnonce.h"
  38. #include "ocl.h"
  39. #include "adl.h"
  40. #include "util.h"
  41. /* TODO: cleanup externals ********************/
  42. #ifdef HAVE_OPENCL
  43. /* Platform API */
  44. CL_API_ENTRY cl_int CL_API_CALL
  45. (*clGetPlatformIDs)(cl_uint /* num_entries */,
  46. cl_platform_id * /* platforms */,
  47. cl_uint * /* num_platforms */) CL_API_SUFFIX__VERSION_1_0;
  48. CL_API_ENTRY cl_int CL_API_CALL
  49. (*clGetPlatformInfo)(cl_platform_id /* platform */,
  50. cl_platform_info /* param_name */,
  51. size_t /* param_value_size */,
  52. void * /* param_value */,
  53. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  54. /* Device APIs */
  55. CL_API_ENTRY cl_int CL_API_CALL
  56. (*clGetDeviceIDs)(cl_platform_id /* platform */,
  57. cl_device_type /* device_type */,
  58. cl_uint /* num_entries */,
  59. cl_device_id * /* devices */,
  60. cl_uint * /* num_devices */) CL_API_SUFFIX__VERSION_1_0;
  61. CL_API_ENTRY cl_int CL_API_CALL
  62. (*clGetDeviceInfo)(cl_device_id /* device */,
  63. cl_device_info /* param_name */,
  64. size_t /* param_value_size */,
  65. void * /* param_value */,
  66. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  67. /* Context APIs */
  68. CL_API_ENTRY cl_context CL_API_CALL
  69. (*clCreateContextFromType)(const cl_context_properties * /* properties */,
  70. cl_device_type /* device_type */,
  71. void (CL_CALLBACK * /* pfn_notify*/ )(const char *, const void *, size_t, void *),
  72. void * /* user_data */,
  73. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  74. CL_API_ENTRY cl_int CL_API_CALL
  75. (*clReleaseContext)(cl_context /* context */) CL_API_SUFFIX__VERSION_1_0;
  76. /* Command Queue APIs */
  77. CL_API_ENTRY cl_command_queue CL_API_CALL
  78. (*clCreateCommandQueue)(cl_context /* context */,
  79. cl_device_id /* device */,
  80. cl_command_queue_properties /* properties */,
  81. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  82. CL_API_ENTRY cl_int CL_API_CALL
  83. (*clReleaseCommandQueue)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  84. /* Memory Object APIs */
  85. CL_API_ENTRY cl_mem CL_API_CALL
  86. (*clCreateBuffer)(cl_context /* context */,
  87. cl_mem_flags /* flags */,
  88. size_t /* size */,
  89. void * /* host_ptr */,
  90. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  91. /* Program Object APIs */
  92. CL_API_ENTRY cl_program CL_API_CALL
  93. (*clCreateProgramWithSource)(cl_context /* context */,
  94. cl_uint /* count */,
  95. const char ** /* strings */,
  96. const size_t * /* lengths */,
  97. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  98. CL_API_ENTRY cl_program CL_API_CALL
  99. (*clCreateProgramWithBinary)(cl_context /* context */,
  100. cl_uint /* num_devices */,
  101. const cl_device_id * /* device_list */,
  102. const size_t * /* lengths */,
  103. const unsigned char ** /* binaries */,
  104. cl_int * /* binary_status */,
  105. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  106. CL_API_ENTRY cl_int CL_API_CALL
  107. (*clReleaseProgram)(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0;
  108. CL_API_ENTRY cl_int CL_API_CALL
  109. (*clBuildProgram)(cl_program /* program */,
  110. cl_uint /* num_devices */,
  111. const cl_device_id * /* device_list */,
  112. const char * /* options */,
  113. void (CL_CALLBACK * /* pfn_notify */)(cl_program /* program */, void * /* user_data */),
  114. void * /* user_data */) CL_API_SUFFIX__VERSION_1_0;
  115. CL_API_ENTRY cl_int CL_API_CALL
  116. (*clGetProgramInfo)(cl_program /* program */,
  117. cl_program_info /* param_name */,
  118. size_t /* param_value_size */,
  119. void * /* param_value */,
  120. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  121. CL_API_ENTRY cl_int CL_API_CALL
  122. (*clGetProgramBuildInfo)(cl_program /* program */,
  123. cl_device_id /* device */,
  124. cl_program_build_info /* param_name */,
  125. size_t /* param_value_size */,
  126. void * /* param_value */,
  127. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  128. /* Kernel Object APIs */
  129. CL_API_ENTRY cl_kernel CL_API_CALL
  130. (*clCreateKernel)(cl_program /* program */,
  131. const char * /* kernel_name */,
  132. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  133. CL_API_ENTRY cl_int CL_API_CALL
  134. (*clReleaseKernel)(cl_kernel /* kernel */) CL_API_SUFFIX__VERSION_1_0;
  135. CL_API_ENTRY cl_int CL_API_CALL
  136. (*clSetKernelArg)(cl_kernel /* kernel */,
  137. cl_uint /* arg_index */,
  138. size_t /* arg_size */,
  139. const void * /* arg_value */) CL_API_SUFFIX__VERSION_1_0;
  140. /* Flush and Finish APIs */
  141. CL_API_ENTRY cl_int CL_API_CALL
  142. (*clFinish)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  143. /* Enqueued Commands APIs */
  144. CL_API_ENTRY cl_int CL_API_CALL
  145. (*clEnqueueReadBuffer)(cl_command_queue /* command_queue */,
  146. cl_mem /* buffer */,
  147. cl_bool /* blocking_read */,
  148. size_t /* offset */,
  149. size_t /* size */,
  150. void * /* ptr */,
  151. cl_uint /* num_events_in_wait_list */,
  152. const cl_event * /* event_wait_list */,
  153. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  154. CL_API_ENTRY cl_int CL_API_CALL
  155. (*clEnqueueWriteBuffer)(cl_command_queue /* command_queue */,
  156. cl_mem /* buffer */,
  157. cl_bool /* blocking_write */,
  158. size_t /* offset */,
  159. size_t /* size */,
  160. const void * /* ptr */,
  161. cl_uint /* num_events_in_wait_list */,
  162. const cl_event * /* event_wait_list */,
  163. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  164. CL_API_ENTRY cl_int CL_API_CALL
  165. (*clEnqueueNDRangeKernel)(cl_command_queue /* command_queue */,
  166. cl_kernel /* kernel */,
  167. cl_uint /* work_dim */,
  168. const size_t * /* global_work_offset */,
  169. const size_t * /* global_work_size */,
  170. const size_t * /* local_work_size */,
  171. cl_uint /* num_events_in_wait_list */,
  172. const cl_event * /* event_wait_list */,
  173. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  174. #ifdef WIN32
  175. #define dlsym (void*)GetProcAddress
  176. #define dlclose FreeLibrary
  177. #endif
  178. #define LOAD_OCL_SYM(sym) do { \
  179. if (!(sym = dlsym(cl, #sym))) { \
  180. applog(LOG_ERR, "Failed to load OpenCL symbol " #sym ", no GPUs usable"); \
  181. dlclose(cl); \
  182. return false; \
  183. } \
  184. } while(0)
  185. static bool
  186. load_opencl_symbols() {
  187. #if defined(__APPLE__)
  188. void *cl = dlopen("/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL", RTLD_LAZY);
  189. #elif !defined(WIN32)
  190. void *cl = dlopen("libOpenCL.so", RTLD_LAZY);
  191. #else
  192. HMODULE cl = LoadLibrary("OpenCL.dll");
  193. #endif
  194. if (!cl)
  195. {
  196. applog(LOG_ERR, "Failed to load OpenCL library, no GPUs usable");
  197. return false;
  198. }
  199. LOAD_OCL_SYM(clGetPlatformIDs);
  200. LOAD_OCL_SYM(clGetPlatformInfo);
  201. LOAD_OCL_SYM(clGetDeviceIDs);
  202. LOAD_OCL_SYM(clGetDeviceInfo);
  203. LOAD_OCL_SYM(clCreateContextFromType);
  204. LOAD_OCL_SYM(clReleaseContext);
  205. LOAD_OCL_SYM(clCreateCommandQueue);
  206. LOAD_OCL_SYM(clReleaseCommandQueue);
  207. LOAD_OCL_SYM(clCreateBuffer);
  208. LOAD_OCL_SYM(clCreateProgramWithSource);
  209. LOAD_OCL_SYM(clCreateProgramWithBinary);
  210. LOAD_OCL_SYM(clReleaseProgram);
  211. LOAD_OCL_SYM(clBuildProgram);
  212. LOAD_OCL_SYM(clGetProgramInfo);
  213. LOAD_OCL_SYM(clGetProgramBuildInfo);
  214. LOAD_OCL_SYM(clCreateKernel);
  215. LOAD_OCL_SYM(clReleaseKernel);
  216. LOAD_OCL_SYM(clSetKernelArg);
  217. LOAD_OCL_SYM(clFinish);
  218. LOAD_OCL_SYM(clEnqueueReadBuffer);
  219. LOAD_OCL_SYM(clEnqueueWriteBuffer);
  220. LOAD_OCL_SYM(clEnqueueNDRangeKernel);
  221. return true;
  222. }
  223. #endif
  224. struct opencl_kernel_interface {
  225. const char *kiname;
  226. queue_kernel_parameters_func_t queue_kernel_parameters_func;
  227. };
  228. #ifdef HAVE_CURSES
  229. extern WINDOW *mainwin, *statuswin, *logwin;
  230. extern void enable_curses(void);
  231. #endif
  232. extern int mining_threads;
  233. extern int opt_g_threads;
  234. extern bool ping;
  235. extern bool opt_loginput;
  236. extern char *opt_kernel_path;
  237. extern int gpur_thr_id;
  238. extern bool opt_noadl;
  239. extern bool have_opencl;
  240. static _clState *clStates[MAX_GPUDEVICES];
  241. static struct opencl_kernel_interface kernel_interfaces[];
  242. extern void *miner_thread(void *userdata);
  243. extern int dev_from_id(int thr_id);
  244. extern void decay_time(double *f, double fadd);
  245. /**********************************************/
  246. #ifdef HAVE_ADL
  247. extern float gpu_temp(int gpu);
  248. extern int gpu_fanspeed(int gpu);
  249. extern int gpu_fanpercent(int gpu);
  250. #endif
  251. void opencl_early_init()
  252. {
  253. static struct opencl_device_data dataarray[MAX_GPUDEVICES];
  254. for (int i = 0; i < MAX_GPUDEVICES; ++i)
  255. {
  256. struct opencl_device_data * const data = &dataarray[i];
  257. *data = (struct opencl_device_data){
  258. .dynamic = true,
  259. .intensity = intensity_not_set,
  260. #ifdef USE_SCRYPT
  261. .lookup_gap = 2,
  262. #endif
  263. };
  264. gpus[i] = (struct cgpu_info){
  265. .device_data = data,
  266. };
  267. }
  268. }
  269. static
  270. const char *_set_list(char * const arg, const char * const emsg, bool (*set_func)(struct cgpu_info *, const char *))
  271. {
  272. int i, device = 0;
  273. char *nextptr, buf[0x10];
  274. nextptr = strtok(arg, ",");
  275. if (nextptr == NULL)
  276. return emsg;
  277. if (!set_func(&gpus[device++], nextptr))
  278. return emsg;
  279. snprintf(buf, sizeof(buf), "%s", nextptr);
  280. while ((nextptr = strtok(NULL, ",")) != NULL)
  281. if (!set_func(&gpus[device++], nextptr))
  282. return emsg;
  283. if (device == 1) {
  284. for (i = device; i < MAX_GPUDEVICES; i++)
  285. set_func(&gpus[i], buf);
  286. }
  287. return NULL;
  288. }
  289. #define _SET_INTERFACE(PNAME) \
  290. static \
  291. 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) \
  292. { \
  293. if (!_set_ ## PNAME (proc, newvalue)) \
  294. return "Invalid value for " #PNAME; \
  295. return NULL; \
  296. } \
  297. // END OF _SET_INTERFACE
  298. #define _SET_INT_LIST2(PNAME, VCHECK, FIELD) \
  299. static \
  300. bool _set_ ## PNAME (struct cgpu_info * const cgpu, const char * const _val) \
  301. { \
  302. const int v = atoi(_val); \
  303. if (!(VCHECK)) \
  304. return false; \
  305. FIELD = v; \
  306. return true; \
  307. } \
  308. _SET_INTERFACE(PNAME) \
  309. const char *set_ ## PNAME(char *arg) \
  310. { \
  311. return _set_list(arg, "Invalid value passed to " #PNAME, _set_ ## PNAME); \
  312. } \
  313. // END OF _SET_INT_LIST
  314. #define _SET_INT_LIST(PNAME, VCHECK, FIELD) \
  315. _SET_INT_LIST2(PNAME, VCHECK, ((struct opencl_device_data *)cgpu->device_data)->FIELD)
  316. #ifdef HAVE_OPENCL
  317. _SET_INT_LIST(vector , (v == 1 || v == 2 || v == 4), vwidth )
  318. _SET_INT_LIST(worksize, (v >= 1 && v <= 9999) , work_size)
  319. #ifdef USE_SCRYPT
  320. _SET_INT_LIST(shaders , true, shaders)
  321. _SET_INT_LIST(lookup_gap , true, lookup_gap)
  322. _SET_INT_LIST(thread_concurrency, true, thread_concurrency)
  323. #endif
  324. enum cl_kernels select_kernel(const char * const arg)
  325. {
  326. for (unsigned i = 1; i < (unsigned)OPENCL_KERNEL_INTERFACE_COUNT; ++i)
  327. if (!strcasecmp(arg, kernel_interfaces[i].kiname))
  328. return i;
  329. return KL_NONE;
  330. }
  331. const char *opencl_get_kernel_interface_name(const enum cl_kernels kern)
  332. {
  333. struct opencl_kernel_interface *ki = &kernel_interfaces[kern];
  334. return ki->kiname;
  335. }
  336. static
  337. bool _set_kernel(struct cgpu_info * const cgpu, const char *_val)
  338. {
  339. struct opencl_device_data * const data = cgpu->device_data;
  340. size_t knamelen = strlen(_val);
  341. char filename[knamelen + 3 + 1];
  342. sprintf(filename, "%s.cl", _val);
  343. int dummy_srclen;
  344. enum cl_kernels interface;
  345. char *src = opencl_kernel_source(filename, &dummy_srclen, &interface);
  346. if (!src)
  347. return false;
  348. free(src);
  349. char **kfp = &data->kernel_file_sha256d;
  350. #ifdef USE_SCRYPT
  351. if (interface == KL_SCRYPT)
  352. kfp = &data->kernel_file_scrypt;
  353. #endif
  354. free(*kfp);
  355. *kfp = strdup(_val);
  356. return true;
  357. }
  358. _SET_INTERFACE(kernel)
  359. const char *set_kernel(char *arg)
  360. {
  361. return _set_list(arg, "Invalid value passed to set_kernel", _set_kernel);
  362. }
  363. #endif
  364. static
  365. 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)
  366. {
  367. struct opencl_device_data * const data = proc->device_data;
  368. char *end;
  369. bool nv;
  370. nv = bfg_strtobool(newvalue, &end, 0);
  371. if (newvalue[0] && !end[0])
  372. data->opt_opencl_binaries = nv ? OBU_LOADSAVE : OBU_NONE;
  373. else
  374. if (!(strcasecmp(newvalue, "load") && strcasecmp(newvalue, "read")))
  375. data->opt_opencl_binaries = OBU_LOAD;
  376. else
  377. if (!(strcasecmp(newvalue, "save") && strcasecmp(newvalue, "write")))
  378. data->opt_opencl_binaries = OBU_SAVE;
  379. else
  380. if (!(strcasecmp(newvalue, "both")))
  381. data->opt_opencl_binaries = OBU_LOADSAVE;
  382. else
  383. if (!(strcasecmp(newvalue, "default")))
  384. data->opt_opencl_binaries = OBU_DEFAULT;
  385. else
  386. return "Invalid value passed to opencl binary";
  387. return NULL;
  388. }
  389. #ifdef HAVE_ADL
  390. /* This function allows us to map an adl device to an opencl device for when
  391. * simple enumeration has failed to match them. */
  392. static
  393. 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)
  394. {
  395. struct opencl_device_data * const data = proc->device_data;
  396. data->virtual_adl = atoi(newvalue);
  397. data->mapped = true;
  398. return NULL;
  399. }
  400. char *set_gpu_map(char *arg)
  401. {
  402. struct opencl_device_data *data;
  403. int val1 = 0, val2 = 0;
  404. char *nextptr;
  405. nextptr = strtok(arg, ",");
  406. if (nextptr == NULL)
  407. return "Invalid parameters for set gpu map";
  408. if (sscanf(arg, "%d:%d", &val1, &val2) != 2)
  409. return "Invalid description for map pair";
  410. if (val1 < 0 || val1 > MAX_GPUDEVICES || val2 < 0 || val2 > MAX_GPUDEVICES)
  411. return "Invalid value passed to set_gpu_map";
  412. data = gpus[val1].device_data;
  413. data->virtual_adl = val2;
  414. data->mapped = true;
  415. while ((nextptr = strtok(NULL, ",")) != NULL) {
  416. if (sscanf(nextptr, "%d:%d", &val1, &val2) != 2)
  417. return "Invalid description for map pair";
  418. if (val1 < 0 || val1 > MAX_GPUDEVICES || val2 < 0 || val2 > MAX_GPUDEVICES)
  419. return "Invalid value passed to set_gpu_map";
  420. data = gpus[val1].device_data;
  421. data->virtual_adl = val2;
  422. data->mapped = true;
  423. }
  424. return NULL;
  425. }
  426. static
  427. bool _set_gpu_engine(struct cgpu_info * const cgpu, const char * const _val)
  428. {
  429. int val1, val2;
  430. get_intrange(_val, &val1, &val2);
  431. if (val1 < 0 || val1 > 9999 || val2 < 0 || val2 > 9999 || val2 < val1)
  432. return false;
  433. struct opencl_device_data * const data = cgpu->device_data;
  434. struct gpu_adl * const ga = &data->adl;
  435. data->min_engine = val1;
  436. data->gpu_engine = val2;
  437. ga->autoengine = (val1 != val2);
  438. return true;
  439. }
  440. _SET_INTERFACE(gpu_engine)
  441. const char *set_gpu_engine(char *arg)
  442. {
  443. return _set_list(arg, "Invalid value passed to set_gpu_engine", _set_gpu_engine);
  444. }
  445. static
  446. 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)
  447. {
  448. struct opencl_device_data * const data = proc->device_data;
  449. struct gpu_adl * const ga = &data->adl;
  450. int val1, val2;
  451. get_intrange(newvalue, &val1, &val2);
  452. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  453. return "Invalid value for clock";
  454. if (val1 == val2)
  455. {
  456. if (set_engineclock(proc->device_id, val1))
  457. return "Failed to set gpu_engine";
  458. ga->autoengine = false;
  459. }
  460. else
  461. {
  462. // Ensure current clock is within range
  463. if (ga->lastengine < val1)
  464. {
  465. if (set_engineclock(proc->device_id, val1))
  466. return "Failed to set gpu_engine";
  467. }
  468. else
  469. if (ga->lastengine > val2)
  470. if (set_engineclock(proc->device_id, val2))
  471. return "Failed to set gpu_engine";
  472. data->min_engine = val1;
  473. data->gpu_engine = val2;
  474. ga->autoengine = true;
  475. }
  476. return NULL;
  477. }
  478. static
  479. bool _set_gpu_fan(struct cgpu_info * const cgpu, const char * const _val)
  480. {
  481. int val1, val2;
  482. get_intrange(_val, &val1, &val2);
  483. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  484. return false;
  485. struct opencl_device_data * const data = cgpu->device_data;
  486. struct gpu_adl * const ga = &data->adl;
  487. data->min_fan = val1;
  488. data->gpu_fan = val2;
  489. ga->autofan = (val1 != val2);
  490. return true;
  491. }
  492. _SET_INTERFACE(gpu_fan)
  493. const char *set_gpu_fan(char *arg)
  494. {
  495. return _set_list(arg, "Invalid value passed to set_gpu_fan", _set_gpu_fan);
  496. }
  497. static
  498. 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)
  499. {
  500. struct opencl_device_data * const data = proc->device_data;
  501. struct gpu_adl * const ga = &data->adl;
  502. int val1, val2;
  503. get_intrange(newvalue, &val1, &val2);
  504. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  505. return "Invalid value for fan";
  506. if (val1 == val2)
  507. {
  508. if (set_fanspeed(proc->device_id, val1))
  509. return "Failed to set gpu_fan";
  510. ga->autofan = false;
  511. }
  512. else
  513. {
  514. // Ensure current fan is within range
  515. if (ga->targetfan < val1)
  516. {
  517. if (set_fanspeed(proc->device_id, val1))
  518. return "Failed to set gpu_fan";
  519. }
  520. else
  521. if (ga->targetfan > val2)
  522. if (set_fanspeed(proc->device_id, val2))
  523. return "Failed to set gpu_fan";
  524. data->min_fan = val1;
  525. data->gpu_fan = val2;
  526. ga->autofan = true;
  527. }
  528. return NULL;
  529. }
  530. _SET_INT_LIST(gpu_memclock , (v >= 1 && v < 9999), gpu_memclock )
  531. static
  532. 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)
  533. {
  534. if (set_memoryclock(proc->device_id, atoi(newvalue)))
  535. return "Failed to set gpu_memclock";
  536. return NULL;
  537. }
  538. _SET_INT_LIST(gpu_memdiff , (v >= -9999 && v <= 9999), gpu_memdiff )
  539. static
  540. 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)
  541. {
  542. struct opencl_device_data * const data = proc->device_data;
  543. if (!_set_gpu_memdiff(proc, newvalue))
  544. return "Invalid value for gpu_memdiff";
  545. set_engineclock(proc->device_id, data->gpu_engine);
  546. return NULL;
  547. }
  548. _SET_INT_LIST(gpu_powertune, (v >= -99 && v <= 99), gpu_powertune)
  549. _SET_INT_LIST(gpu_vddc , (v >= 0 && v < 9999), gpu_vddc )
  550. static
  551. 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)
  552. {
  553. if (set_vddc(proc->device_id, atof(newvalue)))
  554. return "Failed to set gpu_vddc";
  555. return NULL;
  556. }
  557. _SET_INT_LIST(temp_overheat, (v >= 0 && v < 200), adl.overtemp )
  558. #endif
  559. #ifdef HAVE_OPENCL
  560. double oclthreads_to_xintensity(const unsigned long oclthreads, const cl_uint max_compute_units)
  561. {
  562. return (double)oclthreads / (double)max_compute_units / 64.;
  563. }
  564. unsigned long xintensity_to_oclthreads(const double xintensity, const cl_uint max_compute_units)
  565. {
  566. return xintensity * max_compute_units * 0x40;
  567. }
  568. static int min_intensity, max_intensity;
  569. // NOTE: This can't be attribute-constructor because then it would race with the mining_algorithms list being populated
  570. static
  571. void opencl_calc_intensity_range()
  572. {
  573. RUNONCE();
  574. min_intensity = INT_MAX;
  575. max_intensity = INT_MIN;
  576. struct mining_algorithm *malgo;
  577. LL_FOREACH(mining_algorithms, malgo)
  578. {
  579. const int malgo_min_intensity = malgo->opencl_oclthreads_to_intensity(malgo->opencl_min_oclthreads);
  580. const int malgo_max_intensity = malgo->opencl_oclthreads_to_intensity(malgo->opencl_max_oclthreads);
  581. if (malgo_min_intensity < min_intensity)
  582. min_intensity = malgo_min_intensity;
  583. if (malgo_max_intensity > max_intensity)
  584. max_intensity = malgo_max_intensity;
  585. }
  586. }
  587. bool opencl_set_intensity_from_str(struct cgpu_info * const cgpu, const char *_val)
  588. {
  589. struct opencl_device_data * const data = cgpu->device_data;
  590. unsigned long oclthreads = 0;
  591. float intensity = intensity_not_set;
  592. bool dynamic = false;
  593. if (!strncasecmp(_val, "d", 1))
  594. {
  595. dynamic = true;
  596. ++_val;
  597. }
  598. if (!strncasecmp(_val, "x", 1))
  599. {
  600. const double v = atof(&_val[1]);
  601. if (v < 1 || v > 9999)
  602. return false;
  603. // thr etc won't be initialised here, so avoid dereferencing it
  604. if (data->oclthreads)
  605. {
  606. struct thr_info * const thr = cgpu->thr[0];
  607. const int thr_id = thr->id;
  608. _clState * const clState = clStates[thr_id];
  609. oclthreads = xintensity_to_oclthreads(v, clState->max_compute_units);
  610. }
  611. }
  612. else
  613. if (isdigit(_val[0]))
  614. {
  615. const double v = atof(_val);
  616. opencl_calc_intensity_range();
  617. if (v < min_intensity || v > max_intensity)
  618. return false;
  619. oclthreads = 1;
  620. intensity = v;
  621. }
  622. // Make actual assignments after we know the values are valid
  623. data->dynamic = dynamic;
  624. if (data->oclthreads)
  625. {
  626. data->oclthreads = oclthreads;
  627. data->intensity = intensity;
  628. pause_dynamic_threads(cgpu->device_id);
  629. }
  630. else
  631. {
  632. if (unlikely(data->_init_intensity))
  633. free(data->_init_intensity);
  634. data->_init_intensity = strdup(_val);
  635. }
  636. return true;
  637. }
  638. #define _set_intensity opencl_set_intensity_from_str
  639. _SET_INTERFACE(intensity)
  640. const char *set_intensity(char *arg)
  641. {
  642. return _set_list(arg, "Invalid value passed to intensity", _set_intensity);
  643. }
  644. _SET_INT_LIST2(gpu_threads, (v >= 1 && v <= 10), cgpu->threads)
  645. #endif
  646. void write_config_opencl(FILE * const fcfg)
  647. {
  648. #ifdef HAVE_ADL
  649. if (opt_reorder)
  650. fprintf(fcfg, ",\n\"gpu-reorder\" : true");
  651. #endif
  652. }
  653. #ifdef HAVE_OPENCL
  654. BFG_REGISTER_DRIVER(opencl_api)
  655. static const struct bfg_set_device_definition opencl_set_device_funcs_probe[];
  656. static const struct bfg_set_device_definition opencl_set_device_funcs[];
  657. char *print_ndevs_and_exit(int *ndevs)
  658. {
  659. opt_log_output = true;
  660. opencl_api.drv_detect();
  661. clear_adl(*ndevs);
  662. applog(LOG_INFO, "%i GPU devices max detected", *ndevs);
  663. exit(*ndevs);
  664. }
  665. #endif
  666. struct cgpu_info gpus[MAX_GPUDEVICES]; /* Maximum number apparently possible */
  667. struct cgpu_info *cpus;
  668. #ifdef HAVE_OPENCL
  669. /* In dynamic mode, only the first thread of each device will be in use.
  670. * This potentially could start a thread that was stopped with the start-stop
  671. * options if one were to disable dynamic from the menu on a paused GPU */
  672. void pause_dynamic_threads(int gpu)
  673. {
  674. struct cgpu_info *cgpu = &gpus[gpu];
  675. struct opencl_device_data * const data = cgpu->device_data;
  676. int i;
  677. for (i = 1; i < cgpu->threads; i++) {
  678. struct thr_info *thr;
  679. thr = cgpu->thr[i];
  680. if (!thr->pause && data->dynamic) {
  681. applog(LOG_WARNING, "Disabling extra threads due to dynamic mode.");
  682. }
  683. thr->pause = data->dynamic;
  684. if (!data->dynamic && cgpu->deven != DEV_DISABLED)
  685. mt_enable(thr);
  686. }
  687. }
  688. struct device_drv opencl_api;
  689. #endif /* HAVE_OPENCL */
  690. float opencl_proc_get_intensity(struct cgpu_info * const proc, const char ** const iunit)
  691. {
  692. struct opencl_device_data * const data = proc->device_data;
  693. struct thr_info *thr = proc->thr[0];
  694. const int thr_id = thr->id;
  695. _clState * const clState = clStates[thr_id];
  696. float intensity = data->intensity;
  697. if (intensity == intensity_not_set)
  698. {
  699. intensity = oclthreads_to_xintensity(data->oclthreads, clState->max_compute_units);
  700. *iunit = data->dynamic ? "dx" : "x";
  701. }
  702. else
  703. *iunit = data->dynamic ? "d" : "";
  704. return intensity;
  705. }
  706. #if defined(HAVE_OPENCL) && defined(HAVE_CURSES)
  707. static
  708. void opencl_wlogprint_status(struct cgpu_info *cgpu)
  709. {
  710. struct opencl_device_data * const data = cgpu->device_data;
  711. struct thr_info *thr = cgpu->thr[0];
  712. int i;
  713. char checkin[40];
  714. double displayed_rolling;
  715. bool mhash_base = !(cgpu->rolling < 1);
  716. char logline[255];
  717. strcpy(logline, ""); // In case it has no data
  718. {
  719. const char *iunit;
  720. float intensity = opencl_proc_get_intensity(cgpu, &iunit);
  721. tailsprintf(logline, sizeof(logline), "I:%s%g ",
  722. iunit,
  723. intensity);
  724. }
  725. #ifdef HAVE_ADL
  726. if (data->has_adl) {
  727. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  728. float temp = 0, vddc = 0;
  729. if (gpu_stats(cgpu->device_id, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune)) {
  730. if (fanspeed != -1 || fanpercent != -1) {
  731. tailsprintf(logline, sizeof(logline), "F:");
  732. if (fanspeed > 9999)
  733. fanspeed = 9999;
  734. if (fanpercent != -1)
  735. {
  736. tailsprintf(logline, sizeof(logline), "%d%%", fanpercent);
  737. if (fanspeed != -1)
  738. tailsprintf(logline, sizeof(logline), "(%dRPM)", fanspeed);
  739. }
  740. else
  741. tailsprintf(logline, sizeof(logline), "%dRPM", fanspeed);
  742. tailsprintf(logline, sizeof(logline), " ");
  743. }
  744. if (engineclock != -1)
  745. tailsprintf(logline, sizeof(logline), "E:%dMHz ", engineclock);
  746. if (memclock != -1)
  747. tailsprintf(logline, sizeof(logline), "M:%dMHz ", memclock);
  748. if (vddc != -1)
  749. tailsprintf(logline, sizeof(logline), "V:%.3fV ", vddc);
  750. if (activity != -1)
  751. tailsprintf(logline, sizeof(logline), "A:%d%% ", activity);
  752. if (powertune != -1)
  753. tailsprintf(logline, sizeof(logline), "P:%d%%", powertune);
  754. }
  755. }
  756. #endif
  757. wlogprint("%s\n", logline);
  758. wlogprint("Last initialised: %s\n", cgpu->init);
  759. for (i = 0; i < mining_threads; i++) {
  760. thr = get_thread(i);
  761. if (thr->cgpu != cgpu)
  762. continue;
  763. get_datestamp(checkin, sizeof(checkin), time(NULL) - timer_elapsed(&thr->last, NULL));
  764. displayed_rolling = thr->rolling;
  765. if (!mhash_base)
  766. displayed_rolling *= 1000;
  767. snprintf(logline, sizeof(logline), "Thread %d: %.1f %sh/s %s ", i, displayed_rolling, mhash_base ? "M" : "K" , cgpu->deven != DEV_DISABLED ? "Enabled" : "Disabled");
  768. switch (cgpu->status) {
  769. default:
  770. case LIFE_WELL:
  771. tailsprintf(logline, sizeof(logline), "ALIVE");
  772. break;
  773. case LIFE_SICK:
  774. tailsprintf(logline, sizeof(logline), "SICK reported in %s", checkin);
  775. break;
  776. case LIFE_DEAD:
  777. tailsprintf(logline, sizeof(logline), "DEAD reported in %s", checkin);
  778. break;
  779. case LIFE_INIT:
  780. case LIFE_NOSTART:
  781. tailsprintf(logline, sizeof(logline), "Never started");
  782. break;
  783. }
  784. if (thr->pause)
  785. tailsprintf(logline, sizeof(logline), " paused");
  786. wlogprint("%s\n", logline);
  787. }
  788. }
  789. static
  790. void opencl_tui_wlogprint_choices(struct cgpu_info *cgpu)
  791. {
  792. wlogprint("[I]ntensity [R]estart GPU ");
  793. #ifdef HAVE_ADL
  794. struct opencl_device_data * const data = cgpu->device_data;
  795. if (data->has_adl)
  796. wlogprint("[C]hange settings ");
  797. #endif
  798. }
  799. static
  800. const char *opencl_tui_handle_choice(struct cgpu_info *cgpu, int input)
  801. {
  802. struct opencl_device_data * const data = cgpu->device_data;
  803. switch (input)
  804. {
  805. case 'i': case 'I':
  806. {
  807. char promptbuf[0x40];
  808. char *intvar;
  809. opencl_calc_intensity_range();
  810. snprintf(promptbuf, sizeof(promptbuf), "Set GPU scan intensity (d or %d -> %d)", min_intensity, max_intensity);
  811. intvar = curses_input(promptbuf);
  812. if (!intvar)
  813. return "Invalid intensity\n";
  814. if (!strncasecmp(intvar, "d", 1)) {
  815. data->dynamic = true;
  816. pause_dynamic_threads(cgpu->device_id);
  817. free(intvar);
  818. return "Dynamic mode enabled\n";
  819. }
  820. if (!_set_intensity(cgpu, intvar))
  821. return "Invalid intensity (out of range)\n";
  822. free(intvar);
  823. return "Intensity changed\n";
  824. }
  825. case 'r': case 'R':
  826. reinit_device(cgpu);
  827. return "Attempting to restart\n";
  828. case 'c': case 'C':
  829. {
  830. char logline[256];
  831. clear_logwin();
  832. get_statline3(logline, sizeof(logline), cgpu, true, true);
  833. wattron(logwin, A_BOLD);
  834. wlogprint("%s", logline);
  835. wattroff(logwin, A_BOLD);
  836. wlogprint("\n");
  837. change_gpusettings(cgpu->device_id);
  838. return ""; // Force refresh
  839. }
  840. }
  841. return NULL;
  842. }
  843. #endif
  844. #ifdef HAVE_OPENCL
  845. #define CL_SET_BLKARG(blkvar) status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->blkvar)
  846. #define CL_SET_ARG(var) status |= clSetKernelArg(*kernel, num++, sizeof(var), (void *)&var)
  847. #define CL_SET_VARG(args, var) status |= clSetKernelArg(*kernel, num++, args * sizeof(uint), (void *)var)
  848. static
  849. void *_opencl_work_data_dup(struct work * const work)
  850. {
  851. struct opencl_work_data *p = malloc(sizeof(*p));
  852. memcpy(p, work->device_data, sizeof(*p));
  853. #ifdef USE_SCRYPT
  854. p->work = work;
  855. #endif
  856. return p;
  857. }
  858. static
  859. void _opencl_work_data_free(struct work * const work)
  860. {
  861. free(work->device_data);
  862. }
  863. static
  864. struct opencl_work_data *_opencl_work_data(struct work * const work)
  865. {
  866. if (!work->device_data)
  867. {
  868. work->device_data = calloc(1, sizeof(struct opencl_work_data));
  869. work->device_data_dup_func = _opencl_work_data_dup;
  870. work->device_data_free_func = _opencl_work_data_free;
  871. }
  872. return work->device_data;
  873. }
  874. static
  875. cl_int queue_poclbm_kernel(const struct opencl_kernel_info * const kinfo, _clState * const clState, struct work * const work, const cl_uint threads)
  876. {
  877. struct opencl_work_data * const blk = _opencl_work_data(work);
  878. const cl_kernel * const kernel = &kinfo->kernel;
  879. unsigned int num = 0;
  880. cl_int status = 0;
  881. CL_SET_BLKARG(ctx_a);
  882. CL_SET_BLKARG(ctx_b);
  883. CL_SET_BLKARG(ctx_c);
  884. CL_SET_BLKARG(ctx_d);
  885. CL_SET_BLKARG(ctx_e);
  886. CL_SET_BLKARG(ctx_f);
  887. CL_SET_BLKARG(ctx_g);
  888. CL_SET_BLKARG(ctx_h);
  889. CL_SET_BLKARG(cty_b);
  890. CL_SET_BLKARG(cty_c);
  891. CL_SET_BLKARG(cty_f);
  892. CL_SET_BLKARG(cty_g);
  893. CL_SET_BLKARG(cty_h);
  894. if (!kinfo->goffset)
  895. {
  896. cl_uint vwidth = clState->vwidth;
  897. uint *nonces = alloca(sizeof(uint) * vwidth);
  898. unsigned int i;
  899. for (i = 0; i < vwidth; i++)
  900. nonces[i] = work->blk.nonce + (i * threads);
  901. CL_SET_VARG(vwidth, nonces);
  902. }
  903. CL_SET_BLKARG(fW0);
  904. CL_SET_BLKARG(fW1);
  905. CL_SET_BLKARG(fW2);
  906. CL_SET_BLKARG(fW3);
  907. CL_SET_BLKARG(fW15);
  908. CL_SET_BLKARG(fW01r);
  909. CL_SET_BLKARG(D1A);
  910. CL_SET_BLKARG(C1addK5);
  911. CL_SET_BLKARG(B1addK6);
  912. CL_SET_BLKARG(W16addK16);
  913. CL_SET_BLKARG(W17addK17);
  914. CL_SET_BLKARG(PreVal4addT1);
  915. CL_SET_BLKARG(PreVal0);
  916. CL_SET_ARG(clState->outputBuffer);
  917. return status;
  918. }
  919. static
  920. cl_int queue_phatk_kernel(const struct opencl_kernel_info * const kinfo, _clState * const clState, struct work * const work, __maybe_unused const cl_uint threads)
  921. {
  922. struct opencl_work_data * const blk = _opencl_work_data(work);
  923. const cl_kernel * const kernel = &kinfo->kernel;
  924. cl_uint vwidth = clState->vwidth;
  925. unsigned int i, num = 0;
  926. cl_int status = 0;
  927. uint *nonces;
  928. CL_SET_BLKARG(ctx_a);
  929. CL_SET_BLKARG(ctx_b);
  930. CL_SET_BLKARG(ctx_c);
  931. CL_SET_BLKARG(ctx_d);
  932. CL_SET_BLKARG(ctx_e);
  933. CL_SET_BLKARG(ctx_f);
  934. CL_SET_BLKARG(ctx_g);
  935. CL_SET_BLKARG(ctx_h);
  936. CL_SET_BLKARG(cty_b);
  937. CL_SET_BLKARG(cty_c);
  938. CL_SET_BLKARG(cty_d);
  939. CL_SET_BLKARG(cty_f);
  940. CL_SET_BLKARG(cty_g);
  941. CL_SET_BLKARG(cty_h);
  942. nonces = alloca(sizeof(uint) * vwidth);
  943. for (i = 0; i < vwidth; i++)
  944. nonces[i] = work->blk.nonce + i;
  945. CL_SET_VARG(vwidth, nonces);
  946. CL_SET_BLKARG(W16);
  947. CL_SET_BLKARG(W17);
  948. CL_SET_BLKARG(PreVal4_2);
  949. CL_SET_BLKARG(PreVal0);
  950. CL_SET_BLKARG(PreW18);
  951. CL_SET_BLKARG(PreW19);
  952. CL_SET_BLKARG(PreW31);
  953. CL_SET_BLKARG(PreW32);
  954. CL_SET_ARG(clState->outputBuffer);
  955. return status;
  956. }
  957. static
  958. cl_int queue_diakgcn_kernel(const struct opencl_kernel_info * const kinfo, _clState * const clState, struct work * const work, __maybe_unused const cl_uint threads)
  959. {
  960. struct opencl_work_data * const blk = _opencl_work_data(work);
  961. const cl_kernel * const kernel = &kinfo->kernel;
  962. unsigned int num = 0;
  963. cl_int status = 0;
  964. if (!kinfo->goffset) {
  965. cl_uint vwidth = clState->vwidth;
  966. uint *nonces = alloca(sizeof(uint) * vwidth);
  967. unsigned int i;
  968. for (i = 0; i < vwidth; i++)
  969. nonces[i] = work->blk.nonce + i;
  970. CL_SET_VARG(vwidth, nonces);
  971. }
  972. CL_SET_BLKARG(PreVal0);
  973. CL_SET_BLKARG(PreVal4_2);
  974. CL_SET_BLKARG(cty_h);
  975. CL_SET_BLKARG(D1A);
  976. CL_SET_BLKARG(cty_b);
  977. CL_SET_BLKARG(cty_c);
  978. CL_SET_BLKARG(cty_f);
  979. CL_SET_BLKARG(cty_g);
  980. CL_SET_BLKARG(C1addK5);
  981. CL_SET_BLKARG(B1addK6);
  982. CL_SET_BLKARG(PreVal0addK7);
  983. CL_SET_BLKARG(W16addK16);
  984. CL_SET_BLKARG(W17addK17);
  985. CL_SET_BLKARG(PreW18);
  986. CL_SET_BLKARG(PreW19);
  987. CL_SET_BLKARG(W16);
  988. CL_SET_BLKARG(W17);
  989. CL_SET_BLKARG(PreW31);
  990. CL_SET_BLKARG(PreW32);
  991. CL_SET_BLKARG(ctx_a);
  992. CL_SET_BLKARG(ctx_b);
  993. CL_SET_BLKARG(ctx_c);
  994. CL_SET_BLKARG(ctx_d);
  995. CL_SET_BLKARG(ctx_e);
  996. CL_SET_BLKARG(ctx_f);
  997. CL_SET_BLKARG(ctx_g);
  998. CL_SET_BLKARG(ctx_h);
  999. CL_SET_BLKARG(zeroA);
  1000. CL_SET_BLKARG(zeroB);
  1001. CL_SET_BLKARG(oneA);
  1002. CL_SET_BLKARG(twoA);
  1003. CL_SET_BLKARG(threeA);
  1004. CL_SET_BLKARG(fourA);
  1005. CL_SET_BLKARG(fiveA);
  1006. CL_SET_BLKARG(sixA);
  1007. CL_SET_BLKARG(sevenA);
  1008. CL_SET_ARG(clState->outputBuffer);
  1009. return status;
  1010. }
  1011. static
  1012. cl_int queue_diablo_kernel(const struct opencl_kernel_info * const kinfo, _clState * const clState, struct work * const work, const cl_uint threads)
  1013. {
  1014. struct opencl_work_data * const blk = _opencl_work_data(work);
  1015. const cl_kernel * const kernel = &kinfo->kernel;
  1016. unsigned int num = 0;
  1017. cl_int status = 0;
  1018. if (!kinfo->goffset) {
  1019. cl_uint vwidth = clState->vwidth;
  1020. uint *nonces = alloca(sizeof(uint) * vwidth);
  1021. unsigned int i;
  1022. for (i = 0; i < vwidth; i++)
  1023. nonces[i] = work->blk.nonce + (i * threads);
  1024. CL_SET_VARG(vwidth, nonces);
  1025. }
  1026. CL_SET_BLKARG(PreVal0);
  1027. CL_SET_BLKARG(PreVal0addK7);
  1028. CL_SET_BLKARG(PreVal4addT1);
  1029. CL_SET_BLKARG(PreW18);
  1030. CL_SET_BLKARG(PreW19);
  1031. CL_SET_BLKARG(W16);
  1032. CL_SET_BLKARG(W17);
  1033. CL_SET_BLKARG(W16addK16);
  1034. CL_SET_BLKARG(W17addK17);
  1035. CL_SET_BLKARG(PreW31);
  1036. CL_SET_BLKARG(PreW32);
  1037. CL_SET_BLKARG(D1A);
  1038. CL_SET_BLKARG(cty_b);
  1039. CL_SET_BLKARG(cty_c);
  1040. CL_SET_BLKARG(cty_h);
  1041. CL_SET_BLKARG(cty_f);
  1042. CL_SET_BLKARG(cty_g);
  1043. CL_SET_BLKARG(C1addK5);
  1044. CL_SET_BLKARG(B1addK6);
  1045. CL_SET_BLKARG(ctx_a);
  1046. CL_SET_BLKARG(ctx_b);
  1047. CL_SET_BLKARG(ctx_c);
  1048. CL_SET_BLKARG(ctx_d);
  1049. CL_SET_BLKARG(ctx_e);
  1050. CL_SET_BLKARG(ctx_f);
  1051. CL_SET_BLKARG(ctx_g);
  1052. CL_SET_BLKARG(ctx_h);
  1053. CL_SET_ARG(clState->outputBuffer);
  1054. return status;
  1055. }
  1056. #ifdef USE_SCRYPT
  1057. static
  1058. cl_int queue_scrypt_kernel(const struct opencl_kernel_info * const kinfo, _clState * const clState, struct work * const work, __maybe_unused const cl_uint threads)
  1059. {
  1060. unsigned char *midstate = work->midstate;
  1061. const cl_kernel * const kernel = &kinfo->kernel;
  1062. unsigned int num = 0;
  1063. cl_uint le_target;
  1064. cl_int status = 0;
  1065. le_target = *(cl_uint *)(work->target + 28);
  1066. clState->cldata = work->data;
  1067. status = clEnqueueWriteBuffer(clState->commandQueue, clState->CLbuffer0, true, 0, 80, clState->cldata, 0, NULL,NULL);
  1068. CL_SET_ARG(clState->CLbuffer0);
  1069. CL_SET_ARG(clState->outputBuffer);
  1070. CL_SET_ARG(clState->padbuffer8);
  1071. CL_SET_VARG(4, &midstate[0]);
  1072. CL_SET_VARG(4, &midstate[16]);
  1073. CL_SET_ARG(le_target);
  1074. return status;
  1075. }
  1076. #endif
  1077. #endif /* HAVE_OPENCL */
  1078. static
  1079. struct opencl_kernel_interface kernel_interfaces[] = {
  1080. {NULL},
  1081. {"poclbm", queue_poclbm_kernel },
  1082. {"phatk", queue_phatk_kernel },
  1083. {"diakgcn", queue_diakgcn_kernel},
  1084. {"diablo", queue_diablo_kernel },
  1085. #ifdef USE_SCRYPT
  1086. {"scrypt", queue_scrypt_kernel },
  1087. #endif
  1088. };
  1089. #ifdef HAVE_OPENCL
  1090. /* We have only one thread that ever re-initialises GPUs, thus if any GPU
  1091. * init command fails due to a completely wedged GPU, the thread will never
  1092. * return, unable to harm other GPUs. If it does return, it means we only had
  1093. * a soft failure and then the reinit_gpu thread is ready to tackle another
  1094. * GPU */
  1095. void *reinit_gpu(void *userdata)
  1096. {
  1097. struct thr_info *mythr = userdata;
  1098. struct cgpu_info *cgpu, *sel_cgpu;
  1099. struct thr_info *thr;
  1100. char name[256];
  1101. int thr_id;
  1102. int i;
  1103. pthread_detach(pthread_self());
  1104. RenameThread("reinit_gpu");
  1105. select_cgpu:
  1106. sel_cgpu =
  1107. cgpu = tq_pop(mythr->q);
  1108. if (!cgpu)
  1109. goto out;
  1110. struct opencl_device_data * const data = cgpu->device_data;
  1111. if (clDevicesNum() != nDevs) {
  1112. applog(LOG_WARNING, "Hardware not reporting same number of active devices, will not attempt to restart GPU");
  1113. goto out;
  1114. }
  1115. for (i = 0; i < cgpu->threads; ++i)
  1116. {
  1117. thr = cgpu->thr[i];
  1118. thr_id = thr->id;
  1119. thr->rolling = thr->cgpu->rolling = 0;
  1120. /* Reports the last time we tried to revive a sick GPU */
  1121. cgtime(&thr->sick);
  1122. if (!pthread_cancel(thr->pth)) {
  1123. applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
  1124. } else
  1125. applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
  1126. }
  1127. for (i = 0; i < cgpu->threads; ++i)
  1128. {
  1129. int virtual_gpu;
  1130. thr = cgpu->thr[i];
  1131. thr_id = thr->id;
  1132. virtual_gpu = data->virtual_gpu;
  1133. /* Lose this ram cause we may get stuck here! */
  1134. //tq_freeze(thr->q);
  1135. thr->q = tq_new();
  1136. if (!thr->q)
  1137. quithere(1, "Failed to tq_new");
  1138. /* Lose this ram cause we may dereference in the dying thread! */
  1139. //free(clState);
  1140. applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
  1141. clStates[thr_id] = opencl_create_clState(virtual_gpu, name, sizeof(name));
  1142. if (!clStates[thr_id]) {
  1143. applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
  1144. goto select_cgpu;
  1145. }
  1146. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1147. if (unlikely(thr_info_create(thr, NULL, miner_thread, thr))) {
  1148. applog(LOG_ERR, "thread %d create failed", thr_id);
  1149. return NULL;
  1150. }
  1151. applog(LOG_WARNING, "Thread %d restarted", thr_id);
  1152. }
  1153. get_now_datestamp(sel_cgpu->init, sizeof(sel_cgpu->init));
  1154. proc_enable(cgpu);
  1155. goto select_cgpu;
  1156. out:
  1157. return NULL;
  1158. }
  1159. #else
  1160. void *reinit_gpu(__maybe_unused void *userdata)
  1161. {
  1162. return NULL;
  1163. }
  1164. #endif
  1165. #ifdef HAVE_OPENCL
  1166. struct device_drv opencl_api;
  1167. static int opencl_autodetect()
  1168. {
  1169. RUNONCE(0);
  1170. #ifndef WIN32
  1171. if (!getenv("DISPLAY")) {
  1172. applog(LOG_DEBUG, "DISPLAY not set, setting :0 just in case");
  1173. setenv("DISPLAY", ":0", 1);
  1174. }
  1175. #endif
  1176. if (!load_opencl_symbols()) {
  1177. nDevs = 0;
  1178. return 0;
  1179. }
  1180. int i;
  1181. nDevs = clDevicesNum();
  1182. if (nDevs < 0) {
  1183. applog(LOG_ERR, "clDevicesNum returned error, no GPUs usable");
  1184. nDevs = 0;
  1185. }
  1186. if (!nDevs)
  1187. return 0;
  1188. if (opt_g_threads == -1) {
  1189. // NOTE: This should ideally default to 2 for non-scrypt
  1190. opt_g_threads = 1;
  1191. }
  1192. #ifdef HAVE_SENSORS
  1193. const sensors_chip_name *cn;
  1194. int c = 0;
  1195. sensors_init(NULL);
  1196. sensors_chip_name cnm;
  1197. if (sensors_parse_chip_name("radeon-*", &cnm))
  1198. c = -1;
  1199. #endif
  1200. for (i = 0; i < nDevs; ++i) {
  1201. struct cgpu_info *cgpu;
  1202. cgpu = &gpus[i];
  1203. struct opencl_device_data * const data = cgpu->device_data;
  1204. cgpu->deven = DEV_ENABLED;
  1205. cgpu->drv = &opencl_api;
  1206. cgpu->device_id = i;
  1207. if (cgpu->threads == 0)
  1208. cgpu->threads = opt_g_threads;
  1209. data->virtual_gpu = i;
  1210. #ifdef HAVE_SENSORS
  1211. cn = (c == -1) ? NULL : sensors_get_detected_chips(&cnm, &c);
  1212. data->sensor = cn;
  1213. #endif
  1214. cgpu->set_device_funcs = opencl_set_device_funcs_probe;
  1215. cgpu_set_defaults(cgpu);
  1216. cgpu->set_device_funcs = opencl_set_device_funcs;
  1217. add_cgpu(cgpu);
  1218. }
  1219. if (!opt_noadl)
  1220. init_adl(nDevs);
  1221. return nDevs;
  1222. }
  1223. static void opencl_detect()
  1224. {
  1225. int flags = GDF_DEFAULT_NOAUTO;
  1226. struct mining_goal_info *goal, *tmpgoal;
  1227. HASH_ITER(hh, mining_goals, goal, tmpgoal)
  1228. {
  1229. if (!goal->malgo->opencl_nodefault)
  1230. flags &= ~GDF_DEFAULT_NOAUTO;
  1231. }
  1232. generic_detect(&opencl_api, NULL, opencl_autodetect, flags);
  1233. }
  1234. static void reinit_opencl_device(struct cgpu_info *gpu)
  1235. {
  1236. #ifdef HAVE_ADL
  1237. struct opencl_device_data * const data = gpu->device_data;
  1238. if (adl_active && data->has_adl && gpu_activity(gpu->device_id) > 50)
  1239. {
  1240. applogr(, LOG_ERR, "%s: Still showing activity (suggests a hard hang); cancelling reinitialise.",
  1241. gpu->dev_repr);
  1242. }
  1243. #endif
  1244. tq_push(control_thr[gpur_thr_id].q, gpu);
  1245. }
  1246. static
  1247. bool opencl_get_stats(struct cgpu_info * const gpu)
  1248. {
  1249. __maybe_unused struct opencl_device_data * const data = gpu->device_data;
  1250. #ifdef HAVE_SENSORS
  1251. if (data->sensor)
  1252. {
  1253. const sensors_chip_name *cn = data->sensor;
  1254. const sensors_feature *feat;
  1255. for (int f = 0; (feat = sensors_get_features(cn, &f)); )
  1256. {
  1257. const sensors_subfeature *subf;
  1258. subf = sensors_get_subfeature(cn, feat, SENSORS_SUBFEATURE_TEMP_INPUT);
  1259. if (!(subf && subf->flags & SENSORS_MODE_R))
  1260. continue;
  1261. double val;
  1262. int rc = sensors_get_value(cn, subf->number, &val);
  1263. if (rc)
  1264. continue;
  1265. gpu->temp = val;
  1266. return true;
  1267. }
  1268. }
  1269. #endif
  1270. #ifdef HAVE_ADL
  1271. if (data->has_adl) {
  1272. int gpuid = gpu->device_id;
  1273. gpu_temp(gpuid);
  1274. gpu_fanspeed(gpuid);
  1275. }
  1276. #endif
  1277. return true;
  1278. }
  1279. static
  1280. void opencl_watchdog(struct cgpu_info * const cgpu, __maybe_unused const struct timeval * const tv_now)
  1281. {
  1282. #ifdef HAVE_ADL
  1283. struct opencl_device_data * const data = cgpu->device_data;
  1284. const int gpu = cgpu->device_id;
  1285. enum dev_enable *denable = &cgpu->deven;
  1286. if (adl_active && data->has_adl)
  1287. gpu_autotune(gpu, denable);
  1288. if (opt_debug && data->has_adl) {
  1289. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  1290. float temp = 0, vddc = 0;
  1291. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune))
  1292. applog(LOG_DEBUG, "%.1f C F: %d%%(%dRPM) E: %dMHz M: %dMHz V: %.3fV A: %d%% P: %d%%",
  1293. temp, fanpercent, fanspeed, engineclock, memclock, vddc, activity, powertune);
  1294. }
  1295. #endif
  1296. }
  1297. static struct api_data*
  1298. get_opencl_api_extra_device_status(struct cgpu_info *gpu)
  1299. {
  1300. struct opencl_device_data * const data = gpu->device_data;
  1301. struct thr_info * const thr = gpu->thr[0];
  1302. const int thr_id = thr->id;
  1303. _clState * const clState = clStates[thr_id];
  1304. struct api_data*root = NULL;
  1305. float gt, gv;
  1306. int ga, gf, gp, gc, gm, pt;
  1307. #ifdef HAVE_ADL
  1308. if (!gpu_stats(gpu->device_id, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  1309. #endif
  1310. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  1311. root = api_add_int(root, "Fan Speed", &gf, true);
  1312. root = api_add_int(root, "Fan Percent", &gp, true);
  1313. root = api_add_int(root, "GPU Clock", &gc, true);
  1314. root = api_add_int(root, "Memory Clock", &gm, true);
  1315. root = api_add_volts(root, "GPU Voltage", &gv, true);
  1316. root = api_add_int(root, "GPU Activity", &ga, true);
  1317. root = api_add_int(root, "Powertune", &pt, true);
  1318. char intensity[20];
  1319. uint32_t oclthreads;
  1320. double intensityf = data->intensity;
  1321. // FIXME: Some way to express intensities malgo-neutral?
  1322. struct mining_goal_info * const goal = get_mining_goal("default");
  1323. struct mining_algorithm * const malgo = goal->malgo;
  1324. if (data->intensity == intensity_not_set)
  1325. {
  1326. oclthreads = data->oclthreads;
  1327. intensityf = malgo->opencl_oclthreads_to_intensity(oclthreads);
  1328. }
  1329. else
  1330. oclthreads = malgo->opencl_intensity_to_oclthreads(intensityf);
  1331. double xintensity = oclthreads_to_xintensity(oclthreads, clState->max_compute_units);
  1332. if (data->dynamic)
  1333. strcpy(intensity, "D");
  1334. else
  1335. sprintf(intensity, "%g", intensityf);
  1336. root = api_add_string(root, "Intensity", intensity, true);
  1337. root = api_add_uint32(root, "OCLThreads", &oclthreads, true);
  1338. root = api_add_double(root, "CIntensity", &intensityf, true);
  1339. root = api_add_double(root, "XIntensity", &xintensity, true);
  1340. return root;
  1341. }
  1342. struct opencl_thread_data {
  1343. uint32_t *res;
  1344. };
  1345. static uint32_t *blank_res;
  1346. static bool opencl_thread_prepare(struct thr_info *thr)
  1347. {
  1348. char name[256];
  1349. struct cgpu_info *cgpu = thr->cgpu;
  1350. struct opencl_device_data * const data = cgpu->device_data;
  1351. int gpu = cgpu->device_id;
  1352. int virtual_gpu = data->virtual_gpu;
  1353. int i = thr->id;
  1354. static bool failmessage = false;
  1355. int buffersize = OPENCL_MAX_BUFFERSIZE;
  1356. if (!blank_res)
  1357. blank_res = calloc(buffersize, 1);
  1358. if (!blank_res) {
  1359. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1360. return false;
  1361. }
  1362. strcpy(name, "");
  1363. applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
  1364. clStates[i] = opencl_create_clState(virtual_gpu, name, sizeof(name));
  1365. if (!clStates[i]) {
  1366. #ifdef HAVE_CURSES
  1367. if (use_curses)
  1368. enable_curses();
  1369. #endif
  1370. applog(LOG_ERR, "Failed to init GPU thread %d, disabling device %d", i, gpu);
  1371. if (!failmessage) {
  1372. applog(LOG_ERR, "Restarting the GPU from the menu will not fix this.");
  1373. applog(LOG_ERR, "Try restarting BFGMiner.");
  1374. failmessage = true;
  1375. #ifdef HAVE_CURSES
  1376. char *buf;
  1377. if (use_curses) {
  1378. buf = curses_input("Press enter to continue");
  1379. if (buf)
  1380. free(buf);
  1381. }
  1382. #endif
  1383. }
  1384. cgpu->deven = DEV_DISABLED;
  1385. cgpu->status = LIFE_NOSTART;
  1386. dev_error(cgpu, REASON_DEV_NOSTART);
  1387. return false;
  1388. }
  1389. if (!cgpu->name)
  1390. cgpu->name = trimmed_strdup(name);
  1391. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1392. get_now_datestamp(cgpu->init, sizeof(cgpu->init));
  1393. have_opencl = true;
  1394. return true;
  1395. }
  1396. static bool opencl_thread_init(struct thr_info *thr)
  1397. {
  1398. const int thr_id = thr->id;
  1399. struct cgpu_info *gpu = thr->cgpu;
  1400. struct opencl_thread_data *thrdata;
  1401. _clState *clState = clStates[thr_id];
  1402. cl_int status = 0;
  1403. thrdata = calloc(1, sizeof(*thrdata));
  1404. thr->cgpu_data = thrdata;
  1405. int buffersize = OPENCL_MAX_BUFFERSIZE;
  1406. if (!thrdata) {
  1407. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1408. return false;
  1409. }
  1410. thrdata->res = calloc(buffersize, 1);
  1411. if (!thrdata->res) {
  1412. free(thrdata);
  1413. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1414. return false;
  1415. }
  1416. status |= clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  1417. buffersize, blank_res, 0, NULL, NULL);
  1418. if (unlikely(status != CL_SUCCESS)) {
  1419. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1420. return false;
  1421. }
  1422. gpu->status = LIFE_WELL;
  1423. gpu->device_last_well = time(NULL);
  1424. return true;
  1425. }
  1426. static bool opencl_prepare_work(struct thr_info __maybe_unused *thr, struct work *work)
  1427. {
  1428. const struct mining_algorithm * const malgo = work_mining_algorithm(work);
  1429. if (malgo->algo == POW_SHA256D)
  1430. {
  1431. struct opencl_work_data * const blk = _opencl_work_data(work);
  1432. precalc_hash(blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1433. }
  1434. return true;
  1435. }
  1436. extern int opt_dynamic_interval;
  1437. const struct opencl_kernel_info *opencl_scanhash_get_kernel(struct cgpu_info * const cgpu, _clState * const clState, const struct mining_algorithm * const malgo)
  1438. {
  1439. struct opencl_device_data * const data = cgpu->device_data;
  1440. struct opencl_kernel_info *kernelinfo = NULL;
  1441. char *kernel_file;
  1442. switch (malgo->algo)
  1443. {
  1444. case POW_SHA256D:
  1445. kernelinfo = &clState->kernel_sha256d;
  1446. if (!data->kernel_file_sha256d)
  1447. {
  1448. const char * const vbuff = clState->platform_ver_str;
  1449. if (clState->is_mesa)
  1450. {
  1451. applog(LOG_INFO, "Selecting phatk kernel for Mesa");
  1452. data->kernel_file_sha256d = strdup("phatk");
  1453. }
  1454. else /* Detect all 2.6 SDKs not with Tahiti and use diablo kernel */
  1455. if (!strstr(cgpu->name, "Tahiti") &&
  1456. (strstr(vbuff, "844.4") || // Linux 64 bit ATI 2.6 SDK
  1457. strstr(vbuff, "851.4") || // Windows 64 bit ""
  1458. strstr(vbuff, "831.4") ||
  1459. strstr(vbuff, "898.1") || // 12.2 driver SDK
  1460. strstr(vbuff, "923.1") || // 12.4
  1461. strstr(vbuff, "938.2") || // SDK 2.7
  1462. strstr(vbuff, "1113.2"))) // SDK 2.8
  1463. {
  1464. applog(LOG_INFO, "Selecting diablo kernel");
  1465. data->kernel_file_sha256d = strdup("diablo");
  1466. }
  1467. else /* Detect all 7970s, older ATI and NVIDIA and use poclbm */
  1468. if (strstr(cgpu->name, "Tahiti") || !clState->hasBitAlign)
  1469. {
  1470. applog(LOG_INFO, "Selecting poclbm kernel");
  1471. data->kernel_file_sha256d = strdup("poclbm");
  1472. }
  1473. else /* Use phatk for the rest R5xxx R6xxx */
  1474. {
  1475. applog(LOG_INFO, "Selecting phatk kernel");
  1476. data->kernel_file_sha256d = strdup("phatk");
  1477. }
  1478. }
  1479. kernel_file = data->kernel_file_sha256d;
  1480. break;
  1481. #ifdef USE_SCRYPT
  1482. case POW_SCRYPT:
  1483. kernelinfo = &clState->kernel_scrypt;
  1484. BFGINIT(data->kernel_file_scrypt, strdup("scrypt"));
  1485. kernel_file = data->kernel_file_scrypt;
  1486. break;
  1487. #endif
  1488. }
  1489. if (!kernelinfo)
  1490. applogr(NULL, LOG_ERR, "%s: Unsupported mining algorithm", cgpu->dev_repr);
  1491. if (!kernelinfo->loaded)
  1492. {
  1493. if (!opencl_load_kernel(cgpu, clState, cgpu->name, kernelinfo, kernel_file, malgo))
  1494. applogr(NULL, LOG_ERR, "%s: Failed to load kernel", cgpu->dev_repr);
  1495. kernelinfo->queue_kernel_parameters = kernel_interfaces[kernelinfo->interface].queue_kernel_parameters_func;
  1496. }
  1497. return kernelinfo;
  1498. }
  1499. static int64_t opencl_scanhash(struct thr_info *thr, struct work *work,
  1500. int64_t __maybe_unused max_nonce)
  1501. {
  1502. const int thr_id = thr->id;
  1503. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1504. struct cgpu_info *gpu = thr->cgpu;
  1505. struct opencl_device_data * const data = gpu->device_data;
  1506. _clState *clState = clStates[thr_id];
  1507. const struct mining_algorithm * const malgo = work_mining_algorithm(work);
  1508. const struct opencl_kernel_info *kinfo = opencl_scanhash_get_kernel(gpu, clState, malgo);
  1509. if (!kinfo)
  1510. return -1;
  1511. const cl_kernel * const kernel = &kinfo->kernel;
  1512. const int dynamic_us = opt_dynamic_interval * 1000;
  1513. cl_int status;
  1514. size_t globalThreads[1];
  1515. size_t localThreads[1] = { kinfo->wsize };
  1516. int64_t hashes;
  1517. int found = FOUND;
  1518. int buffersize = BUFFERSIZE;
  1519. #ifdef USE_SCRYPT
  1520. if (malgo->algo == POW_SCRYPT)
  1521. {
  1522. found = SCRYPT_FOUND;
  1523. buffersize = SCRYPT_BUFFERSIZE;
  1524. }
  1525. #endif
  1526. if (data->intensity != intensity_not_set)
  1527. data->oclthreads = malgo->opencl_intensity_to_oclthreads(data->intensity);
  1528. /* Windows' timer resolution is only 15ms so oversample 5x */
  1529. if (data->dynamic && (++data->intervals * dynamic_us) > 70000) {
  1530. struct timeval tv_gpuend;
  1531. double gpu_us;
  1532. cgtime(&tv_gpuend);
  1533. gpu_us = us_tdiff(&tv_gpuend, &data->tv_gpustart) / data->intervals;
  1534. if (gpu_us > dynamic_us) {
  1535. const unsigned long min_oclthreads = malgo->opencl_min_oclthreads;
  1536. data->oclthreads /= 2;
  1537. if (data->oclthreads < min_oclthreads)
  1538. data->oclthreads = min_oclthreads;
  1539. } else if (gpu_us < dynamic_us / 2) {
  1540. const unsigned long max_oclthreads = malgo->opencl_max_oclthreads;
  1541. data->oclthreads *= 2;
  1542. if (data->oclthreads > max_oclthreads)
  1543. data->oclthreads = max_oclthreads;
  1544. }
  1545. if (data->intensity != intensity_not_set)
  1546. data->intensity = malgo->opencl_oclthreads_to_intensity(data->oclthreads);
  1547. memcpy(&(data->tv_gpustart), &tv_gpuend, sizeof(struct timeval));
  1548. data->intervals = 0;
  1549. }
  1550. if (data->oclthreads < localThreads[0])
  1551. data->oclthreads = localThreads[0];
  1552. globalThreads[0] = data->oclthreads;
  1553. hashes = globalThreads[0];
  1554. hashes *= clState->vwidth;
  1555. if (hashes > gpu->max_hashes)
  1556. gpu->max_hashes = hashes;
  1557. status = kinfo->queue_kernel_parameters(kinfo, clState, work, globalThreads[0]);
  1558. if (unlikely(status != CL_SUCCESS)) {
  1559. applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
  1560. return -1;
  1561. }
  1562. if (kinfo->goffset)
  1563. {
  1564. size_t global_work_offset[1];
  1565. global_work_offset[0] = work->blk.nonce;
  1566. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, global_work_offset,
  1567. globalThreads, localThreads, 0, NULL, NULL);
  1568. } else
  1569. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1570. globalThreads, localThreads, 0, NULL, NULL);
  1571. if (unlikely(status != CL_SUCCESS)) {
  1572. applog(LOG_ERR, "Error %d: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)", status);
  1573. return -1;
  1574. }
  1575. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1576. buffersize, thrdata->res, 0, NULL, NULL);
  1577. if (unlikely(status != CL_SUCCESS)) {
  1578. applog(LOG_ERR, "Error: clEnqueueReadBuffer failed error %d. (clEnqueueReadBuffer)", status);
  1579. return -1;
  1580. }
  1581. /* The amount of work scanned can fluctuate when intensity changes
  1582. * and since we do this one cycle behind, we increment the work more
  1583. * than enough to prevent repeating work */
  1584. work->blk.nonce += gpu->max_hashes;
  1585. /* This finish flushes the readbuffer set with CL_FALSE in clEnqueueReadBuffer */
  1586. clFinish(clState->commandQueue);
  1587. /* FOUND entry is used as a counter to say how many nonces exist */
  1588. if (thrdata->res[found]) {
  1589. /* Clear the buffer again */
  1590. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1591. buffersize, blank_res, 0, NULL, NULL);
  1592. if (unlikely(status != CL_SUCCESS)) {
  1593. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1594. return -1;
  1595. }
  1596. applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
  1597. postcalc_hash_async(thr, work, thrdata->res);
  1598. memset(thrdata->res, 0, buffersize);
  1599. /* This finish flushes the writebuffer set with CL_FALSE in clEnqueueWriteBuffer */
  1600. clFinish(clState->commandQueue);
  1601. }
  1602. return hashes;
  1603. }
  1604. static
  1605. void opencl_clean_kernel_info(struct opencl_kernel_info * const kinfo)
  1606. {
  1607. clReleaseKernel(kinfo->kernel);
  1608. clReleaseProgram(kinfo->program);
  1609. }
  1610. static void opencl_thread_shutdown(struct thr_info *thr)
  1611. {
  1612. const int thr_id = thr->id;
  1613. _clState *clState = clStates[thr_id];
  1614. opencl_clean_kernel_info(&clState->kernel_sha256d);
  1615. opencl_clean_kernel_info(&clState->kernel_scrypt);
  1616. clReleaseCommandQueue(clState->commandQueue);
  1617. clReleaseContext(clState->context);
  1618. }
  1619. static
  1620. const char *opencl_cannot_set(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  1621. {
  1622. return "Cannot set at runtime (use --set-device on commandline)";
  1623. }
  1624. static const struct bfg_set_device_definition opencl_set_device_funcs_probe[] = {
  1625. {"intensity", opencl_init_intensity},
  1626. {"kernel", opencl_init_kernel},
  1627. {"threads", opencl_init_gpu_threads},
  1628. {"vector", opencl_init_vector},
  1629. {"work_size", opencl_init_worksize},
  1630. {"binary", opencl_init_binary},
  1631. #ifdef HAVE_ADL
  1632. {"adl_mapping", opencl_init_gpu_map},
  1633. {"clock", opencl_init_gpu_engine},
  1634. {"fan", opencl_init_gpu_fan},
  1635. {"memclock", opencl_init_gpu_memclock},
  1636. {"memdiff", opencl_init_gpu_memdiff},
  1637. {"powertune", opencl_init_gpu_powertune},
  1638. {"temp_overheat", opencl_init_temp_overheat},
  1639. {"voltage", opencl_init_gpu_vddc},
  1640. #endif
  1641. #ifdef USE_SCRYPT
  1642. {"shaders", opencl_init_shaders},
  1643. {"lookup_gap", opencl_init_lookup_gap},
  1644. {"thread_concurrency", opencl_init_thread_concurrency},
  1645. #endif
  1646. {NULL}
  1647. };
  1648. static const struct bfg_set_device_definition opencl_set_device_funcs[] = {
  1649. {"intensity", opencl_init_intensity, "Intensity of GPU scanning (d, -10 -> 31, or x1 to x9999)"},
  1650. {"kernel", opencl_cannot_set, "Mining kernel code to use"},
  1651. {"threads", opencl_cannot_set, "Number of threads"},
  1652. {"vector", opencl_cannot_set, ""},
  1653. {"work_size", opencl_cannot_set, ""},
  1654. {"binary", opencl_cannot_set, ""},
  1655. #ifdef HAVE_ADL
  1656. {"adl_mapping", opencl_cannot_set, "Map to ADL device"},
  1657. {"clock", opencl_set_gpu_engine, "GPU engine clock"},
  1658. {"fan", opencl_set_gpu_fan, "GPU fan percentage range"},
  1659. {"memclock", opencl_set_gpu_memclock, "GPU memory clock"},
  1660. {"memdiff", opencl_set_gpu_memdiff, "Clock speed difference between GPU and memory (auto-gpu mode only)"},
  1661. {"powertune", opencl_cannot_set, "GPU powertune percentage"},
  1662. {"temp_overheat", opencl_init_temp_overheat, "Overheat temperature when automatically managing fan and GPU speeds"},
  1663. {"voltage", opencl_set_gpu_vddc, "GPU voltage"},
  1664. #endif
  1665. #ifdef USE_SCRYPT
  1666. {"shaders", opencl_cannot_set, "GPU shaders per card (scrypt only)"},
  1667. {"lookup_gap", opencl_cannot_set, "GPU lookup gap (scrypt only)"},
  1668. {"thread_concurrency", opencl_cannot_set, "GPU thread concurrency (scrypt only)"},
  1669. #endif
  1670. {NULL}
  1671. };
  1672. struct device_drv opencl_api = {
  1673. .dname = "opencl",
  1674. .name = "OCL",
  1675. .probe_priority = 110,
  1676. .drv_min_nonce_diff = common_sha256d_and_scrypt_min_nonce_diff,
  1677. .drv_detect = opencl_detect,
  1678. .reinit_device = reinit_opencl_device,
  1679. .watchdog = opencl_watchdog,
  1680. .get_stats = opencl_get_stats,
  1681. #ifdef HAVE_CURSES
  1682. .proc_wlogprint_status = opencl_wlogprint_status,
  1683. .proc_tui_wlogprint_choices = opencl_tui_wlogprint_choices,
  1684. .proc_tui_handle_choice = opencl_tui_handle_choice,
  1685. #endif
  1686. .get_api_extra_device_status = get_opencl_api_extra_device_status,
  1687. .thread_prepare = opencl_thread_prepare,
  1688. .thread_init = opencl_thread_init,
  1689. .prepare_work = opencl_prepare_work,
  1690. .scanhash = opencl_scanhash,
  1691. .thread_shutdown = opencl_thread_shutdown,
  1692. };
  1693. #endif