driver-opencl.c 53 KB

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