driver-opencl.c 56 KB

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