driver-opencl.c 53 KB

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