driver-opencl.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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 <string.h>
  22. #include <stdbool.h>
  23. #include <stdint.h>
  24. #include <sys/types.h>
  25. #ifndef WIN32
  26. #include <sys/resource.h>
  27. #endif
  28. #define OMIT_OPENCL_API
  29. #include "compat.h"
  30. #include "miner.h"
  31. #include "deviceapi.h"
  32. #include "driver-opencl.h"
  33. #include "findnonce.h"
  34. #include "ocl.h"
  35. #include "adl.h"
  36. #include "util.h"
  37. /* TODO: cleanup externals ********************/
  38. #ifdef HAVE_OPENCL
  39. /* Platform API */
  40. CL_API_ENTRY cl_int CL_API_CALL
  41. (*clGetPlatformIDs)(cl_uint /* num_entries */,
  42. cl_platform_id * /* platforms */,
  43. cl_uint * /* num_platforms */) CL_API_SUFFIX__VERSION_1_0;
  44. CL_API_ENTRY cl_int CL_API_CALL
  45. (*clGetPlatformInfo)(cl_platform_id /* platform */,
  46. cl_platform_info /* param_name */,
  47. size_t /* param_value_size */,
  48. void * /* param_value */,
  49. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  50. /* Device APIs */
  51. CL_API_ENTRY cl_int CL_API_CALL
  52. (*clGetDeviceIDs)(cl_platform_id /* platform */,
  53. cl_device_type /* device_type */,
  54. cl_uint /* num_entries */,
  55. cl_device_id * /* devices */,
  56. cl_uint * /* num_devices */) CL_API_SUFFIX__VERSION_1_0;
  57. CL_API_ENTRY cl_int CL_API_CALL
  58. (*clGetDeviceInfo)(cl_device_id /* device */,
  59. cl_device_info /* param_name */,
  60. size_t /* param_value_size */,
  61. void * /* param_value */,
  62. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  63. /* Context APIs */
  64. CL_API_ENTRY cl_context CL_API_CALL
  65. (*clCreateContextFromType)(const cl_context_properties * /* properties */,
  66. cl_device_type /* device_type */,
  67. void (CL_CALLBACK * /* pfn_notify*/ )(const char *, const void *, size_t, void *),
  68. void * /* user_data */,
  69. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  70. CL_API_ENTRY cl_int CL_API_CALL
  71. (*clReleaseContext)(cl_context /* context */) CL_API_SUFFIX__VERSION_1_0;
  72. /* Command Queue APIs */
  73. CL_API_ENTRY cl_command_queue CL_API_CALL
  74. (*clCreateCommandQueue)(cl_context /* context */,
  75. cl_device_id /* device */,
  76. cl_command_queue_properties /* properties */,
  77. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  78. CL_API_ENTRY cl_int CL_API_CALL
  79. (*clReleaseCommandQueue)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  80. /* Memory Object APIs */
  81. CL_API_ENTRY cl_mem CL_API_CALL
  82. (*clCreateBuffer)(cl_context /* context */,
  83. cl_mem_flags /* flags */,
  84. size_t /* size */,
  85. void * /* host_ptr */,
  86. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  87. /* Program Object APIs */
  88. CL_API_ENTRY cl_program CL_API_CALL
  89. (*clCreateProgramWithSource)(cl_context /* context */,
  90. cl_uint /* count */,
  91. const char ** /* strings */,
  92. const size_t * /* lengths */,
  93. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  94. CL_API_ENTRY cl_program CL_API_CALL
  95. (*clCreateProgramWithBinary)(cl_context /* context */,
  96. cl_uint /* num_devices */,
  97. const cl_device_id * /* device_list */,
  98. const size_t * /* lengths */,
  99. const unsigned char ** /* binaries */,
  100. cl_int * /* binary_status */,
  101. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  102. CL_API_ENTRY cl_int CL_API_CALL
  103. (*clReleaseProgram)(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0;
  104. CL_API_ENTRY cl_int CL_API_CALL
  105. (*clBuildProgram)(cl_program /* program */,
  106. cl_uint /* num_devices */,
  107. const cl_device_id * /* device_list */,
  108. const char * /* options */,
  109. void (CL_CALLBACK * /* pfn_notify */)(cl_program /* program */, void * /* user_data */),
  110. void * /* user_data */) CL_API_SUFFIX__VERSION_1_0;
  111. CL_API_ENTRY cl_int CL_API_CALL
  112. (*clGetProgramInfo)(cl_program /* program */,
  113. cl_program_info /* param_name */,
  114. size_t /* param_value_size */,
  115. void * /* param_value */,
  116. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  117. CL_API_ENTRY cl_int CL_API_CALL
  118. (*clGetProgramBuildInfo)(cl_program /* program */,
  119. cl_device_id /* device */,
  120. cl_program_build_info /* param_name */,
  121. size_t /* param_value_size */,
  122. void * /* param_value */,
  123. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  124. /* Kernel Object APIs */
  125. CL_API_ENTRY cl_kernel CL_API_CALL
  126. (*clCreateKernel)(cl_program /* program */,
  127. const char * /* kernel_name */,
  128. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  129. CL_API_ENTRY cl_int CL_API_CALL
  130. (*clReleaseKernel)(cl_kernel /* kernel */) CL_API_SUFFIX__VERSION_1_0;
  131. CL_API_ENTRY cl_int CL_API_CALL
  132. (*clSetKernelArg)(cl_kernel /* kernel */,
  133. cl_uint /* arg_index */,
  134. size_t /* arg_size */,
  135. const void * /* arg_value */) CL_API_SUFFIX__VERSION_1_0;
  136. /* Flush and Finish APIs */
  137. CL_API_ENTRY cl_int CL_API_CALL
  138. (*clFinish)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  139. /* Enqueued Commands APIs */
  140. CL_API_ENTRY cl_int CL_API_CALL
  141. (*clEnqueueReadBuffer)(cl_command_queue /* command_queue */,
  142. cl_mem /* buffer */,
  143. cl_bool /* blocking_read */,
  144. size_t /* offset */,
  145. size_t /* size */,
  146. void * /* ptr */,
  147. cl_uint /* num_events_in_wait_list */,
  148. const cl_event * /* event_wait_list */,
  149. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  150. CL_API_ENTRY cl_int CL_API_CALL
  151. (*clEnqueueWriteBuffer)(cl_command_queue /* command_queue */,
  152. cl_mem /* buffer */,
  153. cl_bool /* blocking_write */,
  154. size_t /* offset */,
  155. size_t /* size */,
  156. const void * /* ptr */,
  157. cl_uint /* num_events_in_wait_list */,
  158. const cl_event * /* event_wait_list */,
  159. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  160. CL_API_ENTRY cl_int CL_API_CALL
  161. (*clEnqueueNDRangeKernel)(cl_command_queue /* command_queue */,
  162. cl_kernel /* kernel */,
  163. cl_uint /* work_dim */,
  164. const size_t * /* global_work_offset */,
  165. const size_t * /* global_work_size */,
  166. const size_t * /* local_work_size */,
  167. cl_uint /* num_events_in_wait_list */,
  168. const cl_event * /* event_wait_list */,
  169. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  170. #ifdef WIN32
  171. #define dlsym (void*)GetProcAddress
  172. #define dlclose FreeLibrary
  173. #endif
  174. #define LOAD_OCL_SYM(sym) do { \
  175. if (!(sym = dlsym(cl, #sym))) { \
  176. applog(LOG_ERR, "Failed to load OpenCL symbol " #sym ", no GPUs usable"); \
  177. dlclose(cl); \
  178. return false; \
  179. } \
  180. } while(0)
  181. static bool
  182. load_opencl_symbols() {
  183. #if defined(__APPLE__)
  184. void *cl = dlopen("/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL", RTLD_LAZY);
  185. #elif !defined(WIN32)
  186. void *cl = dlopen("libOpenCL.so", RTLD_LAZY);
  187. #else
  188. HMODULE cl = LoadLibrary("OpenCL.dll");
  189. #endif
  190. if (!cl)
  191. {
  192. applog(LOG_ERR, "Failed to load OpenCL library, no GPUs usable");
  193. return false;
  194. }
  195. LOAD_OCL_SYM(clGetPlatformIDs);
  196. LOAD_OCL_SYM(clGetPlatformInfo);
  197. LOAD_OCL_SYM(clGetDeviceIDs);
  198. LOAD_OCL_SYM(clGetDeviceInfo);
  199. LOAD_OCL_SYM(clCreateContextFromType);
  200. LOAD_OCL_SYM(clReleaseContext);
  201. LOAD_OCL_SYM(clCreateCommandQueue);
  202. LOAD_OCL_SYM(clReleaseCommandQueue);
  203. LOAD_OCL_SYM(clCreateBuffer);
  204. LOAD_OCL_SYM(clCreateProgramWithSource);
  205. LOAD_OCL_SYM(clCreateProgramWithBinary);
  206. LOAD_OCL_SYM(clReleaseProgram);
  207. LOAD_OCL_SYM(clBuildProgram);
  208. LOAD_OCL_SYM(clGetProgramInfo);
  209. LOAD_OCL_SYM(clGetProgramBuildInfo);
  210. LOAD_OCL_SYM(clCreateKernel);
  211. LOAD_OCL_SYM(clReleaseKernel);
  212. LOAD_OCL_SYM(clSetKernelArg);
  213. LOAD_OCL_SYM(clFinish);
  214. LOAD_OCL_SYM(clEnqueueReadBuffer);
  215. LOAD_OCL_SYM(clEnqueueWriteBuffer);
  216. LOAD_OCL_SYM(clEnqueueNDRangeKernel);
  217. return true;
  218. }
  219. #endif
  220. #ifdef HAVE_CURSES
  221. extern WINDOW *mainwin, *statuswin, *logwin;
  222. extern void enable_curses(void);
  223. #endif
  224. extern int mining_threads;
  225. extern int opt_g_threads;
  226. extern bool ping;
  227. extern bool opt_loginput;
  228. extern char *opt_kernel_path;
  229. extern int gpur_thr_id;
  230. extern bool opt_noadl;
  231. extern bool have_opencl;
  232. extern void *miner_thread(void *userdata);
  233. extern int dev_from_id(int thr_id);
  234. extern void decay_time(double *f, double fadd);
  235. /**********************************************/
  236. #ifdef HAVE_ADL
  237. extern float gpu_temp(int gpu);
  238. extern int gpu_fanspeed(int gpu);
  239. extern int gpu_fanpercent(int gpu);
  240. #endif
  241. #ifdef HAVE_SENSORS
  242. #include <sensors/sensors.h>
  243. struct opencl_device_data {
  244. const sensors_chip_name *sensor;
  245. };
  246. #endif
  247. static
  248. const char *_set_list(char * const arg, const char * const emsg, bool (*set_func)(struct cgpu_info *, const char *))
  249. {
  250. int i, device = 0;
  251. char *nextptr, buf[0x10];
  252. nextptr = strtok(arg, ",");
  253. if (nextptr == NULL)
  254. return emsg;
  255. if (!set_func(&gpus[device++], nextptr))
  256. return emsg;
  257. snprintf(buf, sizeof(buf), "%s", nextptr);
  258. while ((nextptr = strtok(NULL, ",")) != NULL)
  259. if (!set_func(&gpus[device++], nextptr))
  260. return emsg;
  261. if (device == 1) {
  262. for (i = device; i < MAX_GPUDEVICES; i++)
  263. set_func(&gpus[i], buf);
  264. }
  265. return NULL;
  266. }
  267. #define _SET_INT_LIST(PNAME, VCHECK, FIELD) \
  268. static \
  269. bool _set_ ## PNAME (struct cgpu_info * const cgpu, const char * const _val) \
  270. { \
  271. const int v = atoi(_val); \
  272. if (!(VCHECK)) \
  273. return false; \
  274. cgpu->FIELD = v; \
  275. return true; \
  276. } \
  277. const char *set_ ## PNAME(char *arg) \
  278. { \
  279. return _set_list(arg, "Invalid value passed to " #PNAME, _set_ ## PNAME); \
  280. } \
  281. // END OF _SET_INT_LIST
  282. #ifdef HAVE_OPENCL
  283. _SET_INT_LIST(vector , (v == 1 || v == 2 || v == 4), vwidth )
  284. _SET_INT_LIST(worksize, (v >= 1 && v <= 9999) , work_size)
  285. #ifdef USE_SCRYPT
  286. _SET_INT_LIST(shaders , true, shaders)
  287. _SET_INT_LIST(lookup_gap , true, opt_lg )
  288. _SET_INT_LIST(thread_concurrency, true, opt_tc )
  289. #endif
  290. static
  291. enum cl_kernels select_kernel(const char * const arg)
  292. {
  293. if (!strcmp(arg, "diablo"))
  294. return KL_DIABLO;
  295. if (!strcmp(arg, "diakgcn"))
  296. return KL_DIAKGCN;
  297. if (!strcmp(arg, "poclbm"))
  298. return KL_POCLBM;
  299. if (!strcmp(arg, "phatk"))
  300. return KL_PHATK;
  301. #ifdef USE_SCRYPT
  302. if (!strcmp(arg, "scrypt"))
  303. return KL_SCRYPT;
  304. #endif
  305. return KL_NONE;
  306. }
  307. static
  308. bool _set_kernel(struct cgpu_info * const cgpu, const char * const _val)
  309. {
  310. const enum cl_kernels kern = select_kernel(_val);
  311. if (kern == KL_NONE)
  312. return false;
  313. cgpu->kernel = kern;
  314. return true;
  315. }
  316. const char *set_kernel(char *arg)
  317. {
  318. if (opt_scrypt)
  319. return "Cannot specify a kernel with scrypt";
  320. return _set_list(arg, "Invalid value passed to set_kernel", _set_kernel);
  321. }
  322. #endif
  323. #ifdef HAVE_ADL
  324. /* This function allows us to map an adl device to an opencl device for when
  325. * simple enumeration has failed to match them. */
  326. char *set_gpu_map(char *arg)
  327. {
  328. int val1 = 0, val2 = 0;
  329. char *nextptr;
  330. nextptr = strtok(arg, ",");
  331. if (nextptr == NULL)
  332. return "Invalid parameters for set gpu map";
  333. if (sscanf(arg, "%d:%d", &val1, &val2) != 2)
  334. return "Invalid description for map pair";
  335. if (val1 < 0 || val1 > MAX_GPUDEVICES || val2 < 0 || val2 > MAX_GPUDEVICES)
  336. return "Invalid value passed to set_gpu_map";
  337. gpus[val1].virtual_adl = val2;
  338. gpus[val1].mapped = true;
  339. while ((nextptr = strtok(NULL, ",")) != NULL) {
  340. if (sscanf(nextptr, "%d:%d", &val1, &val2) != 2)
  341. return "Invalid description for map pair";
  342. if (val1 < 0 || val1 > MAX_GPUDEVICES || val2 < 0 || val2 > MAX_GPUDEVICES)
  343. return "Invalid value passed to set_gpu_map";
  344. gpus[val1].virtual_adl = val2;
  345. gpus[val1].mapped = true;
  346. }
  347. return NULL;
  348. }
  349. static
  350. bool _set_gpu_engine(struct cgpu_info * const cgpu, const char * const _val)
  351. {
  352. int val1, val2;
  353. get_intrange(_val, &val1, &val2);
  354. if (val1 < 0 || val1 > 9999 || val2 < 0 || val2 > 9999)
  355. return false;
  356. cgpu->min_engine = val1;
  357. cgpu->gpu_engine = val2;
  358. return true;
  359. }
  360. const char *set_gpu_engine(char *arg)
  361. {
  362. return _set_list(arg, "Invalid value passed to set_gpu_engine", _set_gpu_engine);
  363. }
  364. static
  365. bool _set_gpu_fan(struct cgpu_info * const cgpu, const char * const _val)
  366. {
  367. int val1, val2;
  368. get_intrange(_val, &val1, &val2);
  369. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100)
  370. return false;
  371. cgpu->min_fan = val1;
  372. cgpu->gpu_fan = val2;
  373. return true;
  374. }
  375. const char *set_gpu_fan(char *arg)
  376. {
  377. return _set_list(arg, "Invalid value passed to set_gpu_fan", _set_gpu_fan);
  378. }
  379. _SET_INT_LIST(gpu_memclock , (v >= 1 && v < 9999), gpu_memclock )
  380. _SET_INT_LIST(gpu_memdiff , (v >= -9999 && v <= 9999), gpu_memdiff )
  381. _SET_INT_LIST(gpu_powertune, (v >= -99 && v <= 99), gpu_powertune)
  382. _SET_INT_LIST(gpu_vddc , (v >= 0 && v < 9999), gpu_vddc )
  383. _SET_INT_LIST(temp_overheat, (v >= 0 && v < 200), adl.overtemp )
  384. #endif
  385. #ifdef HAVE_OPENCL
  386. static
  387. bool _set_intensity(struct cgpu_info * const cgpu, const char * const _val)
  388. {
  389. if (!strncasecmp(_val, "d", 1))
  390. cgpu->dynamic = true;
  391. else
  392. {
  393. const int v = atoi(_val);
  394. if (v < MIN_INTENSITY || v > MAX_GPU_INTENSITY)
  395. return false;
  396. cgpu->dynamic = false;
  397. cgpu->intensity = v;
  398. }
  399. return true;
  400. }
  401. const char *set_intensity(char *arg)
  402. {
  403. return _set_list(arg, "Invalid value passed to intensity", _set_intensity);
  404. }
  405. #endif
  406. #ifdef HAVE_OPENCL
  407. BFG_REGISTER_DRIVER(opencl_api)
  408. char *print_ndevs_and_exit(int *ndevs)
  409. {
  410. opt_log_output = true;
  411. opencl_api.drv_detect();
  412. clear_adl(*ndevs);
  413. applog(LOG_INFO, "%i GPU devices max detected", *ndevs);
  414. exit(*ndevs);
  415. }
  416. #endif
  417. struct cgpu_info gpus[MAX_GPUDEVICES]; /* Maximum number apparently possible */
  418. struct cgpu_info *cpus;
  419. #ifdef HAVE_OPENCL
  420. /* In dynamic mode, only the first thread of each device will be in use.
  421. * This potentially could start a thread that was stopped with the start-stop
  422. * options if one were to disable dynamic from the menu on a paused GPU */
  423. void pause_dynamic_threads(int gpu)
  424. {
  425. struct cgpu_info *cgpu = &gpus[gpu];
  426. int i;
  427. for (i = 1; i < cgpu->threads; i++) {
  428. struct thr_info *thr;
  429. thr = cgpu->thr[i];
  430. if (!thr->pause && cgpu->dynamic) {
  431. applog(LOG_WARNING, "Disabling extra threads due to dynamic mode.");
  432. applog(LOG_WARNING, "Tune dynamic intensity with --gpu-dyninterval");
  433. }
  434. thr->pause = cgpu->dynamic;
  435. if (!cgpu->dynamic && cgpu->deven != DEV_DISABLED)
  436. mt_enable(thr);
  437. }
  438. }
  439. struct device_drv opencl_api;
  440. #endif /* HAVE_OPENCL */
  441. #if defined(HAVE_OPENCL) && defined(HAVE_CURSES)
  442. static
  443. void opencl_wlogprint_status(struct cgpu_info *cgpu)
  444. {
  445. struct thr_info *thr;
  446. int i;
  447. char checkin[40];
  448. double displayed_rolling;
  449. bool mhash_base = !(cgpu->rolling < 1);
  450. char logline[255];
  451. strcpy(logline, ""); // In case it has no data
  452. tailsprintf(logline, sizeof(logline), "I:%s%d ", (cgpu->dynamic ? "d" : ""), cgpu->intensity);
  453. #ifdef HAVE_ADL
  454. if (cgpu->has_adl) {
  455. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  456. float temp = 0, vddc = 0;
  457. if (gpu_stats(cgpu->device_id, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune)) {
  458. if (fanspeed != -1 || fanpercent != -1) {
  459. tailsprintf(logline, sizeof(logline), "F: ");
  460. if (fanspeed > 9999)
  461. fanspeed = 9999;
  462. if (fanpercent != -1)
  463. {
  464. tailsprintf(logline, sizeof(logline), "%d%% ", fanpercent);
  465. if (fanspeed != -1)
  466. tailsprintf(logline, sizeof(logline), "(%d RPM) ", fanspeed);
  467. }
  468. else
  469. tailsprintf(logline, sizeof(logline), "%d RPM ", fanspeed);
  470. tailsprintf(logline, sizeof(logline), " ");
  471. }
  472. if (engineclock != -1)
  473. tailsprintf(logline, sizeof(logline), "E: %d MHz ", engineclock);
  474. if (memclock != -1)
  475. tailsprintf(logline, sizeof(logline), "M: %d MHz ", memclock);
  476. if (vddc != -1)
  477. tailsprintf(logline, sizeof(logline), "V: %.3fV ", vddc);
  478. if (activity != -1)
  479. tailsprintf(logline, sizeof(logline), "A: %d%% ", activity);
  480. if (powertune != -1)
  481. tailsprintf(logline, sizeof(logline), "P: %d%%", powertune);
  482. }
  483. }
  484. #endif
  485. wlogprint("%s\n", logline);
  486. wlogprint("Last initialised: %s\n", cgpu->init);
  487. for (i = 0; i < mining_threads; i++) {
  488. thr = get_thread(i);
  489. if (thr->cgpu != cgpu)
  490. continue;
  491. get_datestamp(checkin, sizeof(checkin), time(NULL) - timer_elapsed(&thr->last, NULL));
  492. displayed_rolling = thr->rolling;
  493. if (!mhash_base)
  494. displayed_rolling *= 1000;
  495. snprintf(logline, sizeof(logline), "Thread %d: %.1f %sh/s %s ", i, displayed_rolling, mhash_base ? "M" : "K" , cgpu->deven != DEV_DISABLED ? "Enabled" : "Disabled");
  496. switch (cgpu->status) {
  497. default:
  498. case LIFE_WELL:
  499. tailsprintf(logline, sizeof(logline), "ALIVE");
  500. break;
  501. case LIFE_SICK:
  502. tailsprintf(logline, sizeof(logline), "SICK reported in %s", checkin);
  503. break;
  504. case LIFE_DEAD:
  505. tailsprintf(logline, sizeof(logline), "DEAD reported in %s", checkin);
  506. break;
  507. case LIFE_INIT:
  508. case LIFE_NOSTART:
  509. tailsprintf(logline, sizeof(logline), "Never started");
  510. break;
  511. }
  512. if (thr->pause)
  513. tailsprintf(logline, sizeof(logline), " paused");
  514. wlogprint("%s\n", logline);
  515. }
  516. }
  517. static
  518. void opencl_tui_wlogprint_choices(struct cgpu_info *cgpu)
  519. {
  520. wlogprint("[I]ntensity [R]estart GPU ");
  521. #ifdef HAVE_ADL
  522. if (cgpu->has_adl)
  523. wlogprint("[C]hange settings ");
  524. #endif
  525. }
  526. static
  527. const char *opencl_tui_handle_choice(struct cgpu_info *cgpu, int input)
  528. {
  529. switch (input)
  530. {
  531. case 'i': case 'I':
  532. {
  533. int intensity;
  534. char *intvar;
  535. if (opt_scrypt) {
  536. intvar = curses_input("Set GPU scan intensity (d or "
  537. MIN_SCRYPT_INTENSITY_STR " -> "
  538. MAX_SCRYPT_INTENSITY_STR ")");
  539. } else {
  540. intvar = curses_input("Set GPU scan intensity (d or "
  541. MIN_SHA_INTENSITY_STR " -> "
  542. MAX_SHA_INTENSITY_STR ")");
  543. }
  544. if (!intvar)
  545. return "Invalid intensity\n";
  546. if (!strncasecmp(intvar, "d", 1)) {
  547. cgpu->dynamic = true;
  548. pause_dynamic_threads(cgpu->device_id);
  549. free(intvar);
  550. return "Dynamic mode enabled\n";
  551. }
  552. intensity = atoi(intvar);
  553. free(intvar);
  554. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY)
  555. return "Invalid intensity (out of range)\n";
  556. cgpu->dynamic = false;
  557. cgpu->intensity = intensity;
  558. pause_dynamic_threads(cgpu->device_id);
  559. return "Intensity changed\n";
  560. }
  561. case 'r': case 'R':
  562. reinit_device(cgpu);
  563. return "Attempting to restart\n";
  564. case 'c': case 'C':
  565. {
  566. char logline[256];
  567. clear_logwin();
  568. get_statline3(logline, sizeof(logline), cgpu, true, true);
  569. wattron(logwin, A_BOLD);
  570. wlogprint("%s", logline);
  571. wattroff(logwin, A_BOLD);
  572. wlogprint("\n");
  573. change_gpusettings(cgpu->device_id);
  574. return ""; // Force refresh
  575. }
  576. }
  577. return NULL;
  578. }
  579. #endif
  580. #ifdef HAVE_OPENCL
  581. static _clState *clStates[MAX_GPUDEVICES];
  582. #define CL_SET_BLKARG(blkvar) status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->blkvar)
  583. #define CL_SET_ARG(var) status |= clSetKernelArg(*kernel, num++, sizeof(var), (void *)&var)
  584. #define CL_SET_VARG(args, var) status |= clSetKernelArg(*kernel, num++, args * sizeof(uint), (void *)var)
  585. static cl_int queue_poclbm_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  586. {
  587. cl_kernel *kernel = &clState->kernel;
  588. unsigned int num = 0;
  589. cl_int status = 0;
  590. CL_SET_BLKARG(ctx_a);
  591. CL_SET_BLKARG(ctx_b);
  592. CL_SET_BLKARG(ctx_c);
  593. CL_SET_BLKARG(ctx_d);
  594. CL_SET_BLKARG(ctx_e);
  595. CL_SET_BLKARG(ctx_f);
  596. CL_SET_BLKARG(ctx_g);
  597. CL_SET_BLKARG(ctx_h);
  598. CL_SET_BLKARG(cty_b);
  599. CL_SET_BLKARG(cty_c);
  600. CL_SET_BLKARG(cty_f);
  601. CL_SET_BLKARG(cty_g);
  602. CL_SET_BLKARG(cty_h);
  603. if (!clState->goffset) {
  604. cl_uint vwidth = clState->vwidth;
  605. uint *nonces = alloca(sizeof(uint) * vwidth);
  606. unsigned int i;
  607. for (i = 0; i < vwidth; i++)
  608. nonces[i] = blk->nonce + (i * threads);
  609. CL_SET_VARG(vwidth, nonces);
  610. }
  611. CL_SET_BLKARG(fW0);
  612. CL_SET_BLKARG(fW1);
  613. CL_SET_BLKARG(fW2);
  614. CL_SET_BLKARG(fW3);
  615. CL_SET_BLKARG(fW15);
  616. CL_SET_BLKARG(fW01r);
  617. CL_SET_BLKARG(D1A);
  618. CL_SET_BLKARG(C1addK5);
  619. CL_SET_BLKARG(B1addK6);
  620. CL_SET_BLKARG(W16addK16);
  621. CL_SET_BLKARG(W17addK17);
  622. CL_SET_BLKARG(PreVal4addT1);
  623. CL_SET_BLKARG(PreVal0);
  624. CL_SET_ARG(clState->outputBuffer);
  625. return status;
  626. }
  627. static cl_int queue_phatk_kernel(_clState *clState, dev_blk_ctx *blk,
  628. __maybe_unused cl_uint threads)
  629. {
  630. cl_kernel *kernel = &clState->kernel;
  631. cl_uint vwidth = clState->vwidth;
  632. unsigned int i, num = 0;
  633. cl_int status = 0;
  634. uint *nonces;
  635. CL_SET_BLKARG(ctx_a);
  636. CL_SET_BLKARG(ctx_b);
  637. CL_SET_BLKARG(ctx_c);
  638. CL_SET_BLKARG(ctx_d);
  639. CL_SET_BLKARG(ctx_e);
  640. CL_SET_BLKARG(ctx_f);
  641. CL_SET_BLKARG(ctx_g);
  642. CL_SET_BLKARG(ctx_h);
  643. CL_SET_BLKARG(cty_b);
  644. CL_SET_BLKARG(cty_c);
  645. CL_SET_BLKARG(cty_d);
  646. CL_SET_BLKARG(cty_f);
  647. CL_SET_BLKARG(cty_g);
  648. CL_SET_BLKARG(cty_h);
  649. nonces = alloca(sizeof(uint) * vwidth);
  650. for (i = 0; i < vwidth; i++)
  651. nonces[i] = blk->nonce + i;
  652. CL_SET_VARG(vwidth, nonces);
  653. CL_SET_BLKARG(W16);
  654. CL_SET_BLKARG(W17);
  655. CL_SET_BLKARG(PreVal4_2);
  656. CL_SET_BLKARG(PreVal0);
  657. CL_SET_BLKARG(PreW18);
  658. CL_SET_BLKARG(PreW19);
  659. CL_SET_BLKARG(PreW31);
  660. CL_SET_BLKARG(PreW32);
  661. CL_SET_ARG(clState->outputBuffer);
  662. return status;
  663. }
  664. static cl_int queue_diakgcn_kernel(_clState *clState, dev_blk_ctx *blk,
  665. __maybe_unused cl_uint threads)
  666. {
  667. cl_kernel *kernel = &clState->kernel;
  668. unsigned int num = 0;
  669. cl_int status = 0;
  670. if (!clState->goffset) {
  671. cl_uint vwidth = clState->vwidth;
  672. uint *nonces = alloca(sizeof(uint) * vwidth);
  673. unsigned int i;
  674. for (i = 0; i < vwidth; i++)
  675. nonces[i] = blk->nonce + i;
  676. CL_SET_VARG(vwidth, nonces);
  677. }
  678. CL_SET_BLKARG(PreVal0);
  679. CL_SET_BLKARG(PreVal4_2);
  680. CL_SET_BLKARG(cty_h);
  681. CL_SET_BLKARG(D1A);
  682. CL_SET_BLKARG(cty_b);
  683. CL_SET_BLKARG(cty_c);
  684. CL_SET_BLKARG(cty_f);
  685. CL_SET_BLKARG(cty_g);
  686. CL_SET_BLKARG(C1addK5);
  687. CL_SET_BLKARG(B1addK6);
  688. CL_SET_BLKARG(PreVal0addK7);
  689. CL_SET_BLKARG(W16addK16);
  690. CL_SET_BLKARG(W17addK17);
  691. CL_SET_BLKARG(PreW18);
  692. CL_SET_BLKARG(PreW19);
  693. CL_SET_BLKARG(W16);
  694. CL_SET_BLKARG(W17);
  695. CL_SET_BLKARG(PreW31);
  696. CL_SET_BLKARG(PreW32);
  697. CL_SET_BLKARG(ctx_a);
  698. CL_SET_BLKARG(ctx_b);
  699. CL_SET_BLKARG(ctx_c);
  700. CL_SET_BLKARG(ctx_d);
  701. CL_SET_BLKARG(ctx_e);
  702. CL_SET_BLKARG(ctx_f);
  703. CL_SET_BLKARG(ctx_g);
  704. CL_SET_BLKARG(ctx_h);
  705. CL_SET_BLKARG(zeroA);
  706. CL_SET_BLKARG(zeroB);
  707. CL_SET_BLKARG(oneA);
  708. CL_SET_BLKARG(twoA);
  709. CL_SET_BLKARG(threeA);
  710. CL_SET_BLKARG(fourA);
  711. CL_SET_BLKARG(fiveA);
  712. CL_SET_BLKARG(sixA);
  713. CL_SET_BLKARG(sevenA);
  714. CL_SET_ARG(clState->outputBuffer);
  715. return status;
  716. }
  717. static cl_int queue_diablo_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  718. {
  719. cl_kernel *kernel = &clState->kernel;
  720. unsigned int num = 0;
  721. cl_int status = 0;
  722. if (!clState->goffset) {
  723. cl_uint vwidth = clState->vwidth;
  724. uint *nonces = alloca(sizeof(uint) * vwidth);
  725. unsigned int i;
  726. for (i = 0; i < vwidth; i++)
  727. nonces[i] = blk->nonce + (i * threads);
  728. CL_SET_VARG(vwidth, nonces);
  729. }
  730. CL_SET_BLKARG(PreVal0);
  731. CL_SET_BLKARG(PreVal0addK7);
  732. CL_SET_BLKARG(PreVal4addT1);
  733. CL_SET_BLKARG(PreW18);
  734. CL_SET_BLKARG(PreW19);
  735. CL_SET_BLKARG(W16);
  736. CL_SET_BLKARG(W17);
  737. CL_SET_BLKARG(W16addK16);
  738. CL_SET_BLKARG(W17addK17);
  739. CL_SET_BLKARG(PreW31);
  740. CL_SET_BLKARG(PreW32);
  741. CL_SET_BLKARG(D1A);
  742. CL_SET_BLKARG(cty_b);
  743. CL_SET_BLKARG(cty_c);
  744. CL_SET_BLKARG(cty_h);
  745. CL_SET_BLKARG(cty_f);
  746. CL_SET_BLKARG(cty_g);
  747. CL_SET_BLKARG(C1addK5);
  748. CL_SET_BLKARG(B1addK6);
  749. CL_SET_BLKARG(ctx_a);
  750. CL_SET_BLKARG(ctx_b);
  751. CL_SET_BLKARG(ctx_c);
  752. CL_SET_BLKARG(ctx_d);
  753. CL_SET_BLKARG(ctx_e);
  754. CL_SET_BLKARG(ctx_f);
  755. CL_SET_BLKARG(ctx_g);
  756. CL_SET_BLKARG(ctx_h);
  757. CL_SET_ARG(clState->outputBuffer);
  758. return status;
  759. }
  760. #ifdef USE_SCRYPT
  761. static cl_int queue_scrypt_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_unused cl_uint threads)
  762. {
  763. unsigned char *midstate = blk->work->midstate;
  764. cl_kernel *kernel = &clState->kernel;
  765. unsigned int num = 0;
  766. cl_uint le_target;
  767. cl_int status = 0;
  768. le_target = *(cl_uint *)(blk->work->target + 28);
  769. clState->cldata = blk->work->data;
  770. status = clEnqueueWriteBuffer(clState->commandQueue, clState->CLbuffer0, true, 0, 80, clState->cldata, 0, NULL,NULL);
  771. CL_SET_ARG(clState->CLbuffer0);
  772. CL_SET_ARG(clState->outputBuffer);
  773. CL_SET_ARG(clState->padbuffer8);
  774. CL_SET_VARG(4, &midstate[0]);
  775. CL_SET_VARG(4, &midstate[16]);
  776. CL_SET_ARG(le_target);
  777. return status;
  778. }
  779. #endif
  780. static void set_threads_hashes(unsigned int vectors,int64_t *hashes, size_t *globalThreads,
  781. unsigned int minthreads, __maybe_unused int *intensity)
  782. {
  783. unsigned int threads = 0;
  784. while (threads < minthreads) {
  785. threads = 1 << ((opt_scrypt ? 0 : 15) + *intensity);
  786. if (threads < minthreads) {
  787. if (likely(*intensity < MAX_INTENSITY))
  788. (*intensity)++;
  789. else
  790. threads = minthreads;
  791. }
  792. }
  793. *globalThreads = threads;
  794. *hashes = threads * vectors;
  795. }
  796. #endif /* HAVE_OPENCL */
  797. #ifdef HAVE_OPENCL
  798. /* We have only one thread that ever re-initialises GPUs, thus if any GPU
  799. * init command fails due to a completely wedged GPU, the thread will never
  800. * return, unable to harm other GPUs. If it does return, it means we only had
  801. * a soft failure and then the reinit_gpu thread is ready to tackle another
  802. * GPU */
  803. void *reinit_gpu(void *userdata)
  804. {
  805. struct thr_info *mythr = userdata;
  806. struct cgpu_info *cgpu, *sel_cgpu;
  807. struct thr_info *thr;
  808. char name[256];
  809. int thr_id;
  810. int i;
  811. pthread_detach(pthread_self());
  812. RenameThread("reinit_gpu");
  813. select_cgpu:
  814. sel_cgpu =
  815. cgpu = tq_pop(mythr->q, NULL);
  816. if (!cgpu)
  817. goto out;
  818. if (clDevicesNum() != nDevs) {
  819. applog(LOG_WARNING, "Hardware not reporting same number of active devices, will not attempt to restart GPU");
  820. goto out;
  821. }
  822. for (i = 0; i < cgpu->threads; ++i)
  823. {
  824. thr = cgpu->thr[i];
  825. thr_id = thr->id;
  826. thr->rolling = thr->cgpu->rolling = 0;
  827. /* Reports the last time we tried to revive a sick GPU */
  828. cgtime(&thr->sick);
  829. if (!pthread_cancel(thr->pth)) {
  830. applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
  831. } else
  832. applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
  833. }
  834. for (i = 0; i < cgpu->threads; ++i)
  835. {
  836. int virtual_gpu;
  837. thr = cgpu->thr[i];
  838. thr_id = thr->id;
  839. virtual_gpu = cgpu->virtual_gpu;
  840. /* Lose this ram cause we may get stuck here! */
  841. //tq_freeze(thr->q);
  842. thr->q = tq_new();
  843. if (!thr->q)
  844. quithere(1, "Failed to tq_new");
  845. /* Lose this ram cause we may dereference in the dying thread! */
  846. //free(clState);
  847. applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
  848. clStates[thr_id] = initCl(virtual_gpu, name, sizeof(name));
  849. if (!clStates[thr_id]) {
  850. applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
  851. goto select_cgpu;
  852. }
  853. applog(LOG_INFO, "initCl() finished. Found %s", name);
  854. if (unlikely(thr_info_create(thr, NULL, miner_thread, thr))) {
  855. applog(LOG_ERR, "thread %d create failed", thr_id);
  856. return NULL;
  857. }
  858. applog(LOG_WARNING, "Thread %d restarted", thr_id);
  859. }
  860. get_now_datestamp(sel_cgpu->init, sizeof(sel_cgpu->init));
  861. proc_enable(cgpu);
  862. goto select_cgpu;
  863. out:
  864. return NULL;
  865. }
  866. #else
  867. void *reinit_gpu(__maybe_unused void *userdata)
  868. {
  869. return NULL;
  870. }
  871. #endif
  872. #ifdef HAVE_OPENCL
  873. struct device_drv opencl_api;
  874. static int opencl_autodetect()
  875. {
  876. RUNONCE(0);
  877. #ifndef WIN32
  878. if (!getenv("DISPLAY")) {
  879. applog(LOG_DEBUG, "DISPLAY not set, setting :0 just in case");
  880. setenv("DISPLAY", ":0", 1);
  881. }
  882. #endif
  883. if (!load_opencl_symbols()) {
  884. nDevs = 0;
  885. return 0;
  886. }
  887. int i;
  888. nDevs = clDevicesNum();
  889. if (nDevs < 0) {
  890. applog(LOG_ERR, "clDevicesNum returned error, no GPUs usable");
  891. nDevs = 0;
  892. }
  893. if (!nDevs)
  894. return 0;
  895. /* If opt_g_threads is not set, use default 1 thread on scrypt and
  896. * 2 for regular mining */
  897. if (opt_g_threads == -1) {
  898. if (opt_scrypt)
  899. opt_g_threads = 1;
  900. else
  901. opt_g_threads = 2;
  902. }
  903. #ifdef HAVE_SENSORS
  904. struct opencl_device_data *data;
  905. const sensors_chip_name *cn;
  906. int c = 0;
  907. sensors_init(NULL);
  908. sensors_chip_name cnm;
  909. if (sensors_parse_chip_name("radeon-*", &cnm))
  910. c = -1;
  911. #endif
  912. for (i = 0; i < nDevs; ++i) {
  913. struct cgpu_info *cgpu;
  914. cgpu = &gpus[i];
  915. cgpu->devtype = "GPU";
  916. cgpu->deven = DEV_ENABLED;
  917. cgpu->drv = &opencl_api;
  918. cgpu->device_id = i;
  919. cgpu->threads = opt_g_threads;
  920. cgpu->virtual_gpu = i;
  921. #ifdef HAVE_SENSORS
  922. cn = (c == -1) ? NULL : sensors_get_detected_chips(&cnm, &c);
  923. cgpu->device_data = data = malloc(sizeof(*data));
  924. *data = (struct opencl_device_data){
  925. .sensor = cn,
  926. };
  927. #endif
  928. add_cgpu(cgpu);
  929. }
  930. if (!opt_noadl)
  931. init_adl(nDevs);
  932. return nDevs;
  933. }
  934. static void opencl_detect()
  935. {
  936. int flags = 0;
  937. if (!opt_scrypt)
  938. flags |= GDF_DEFAULT_NOAUTO;
  939. generic_detect(&opencl_api, NULL, opencl_autodetect, flags);
  940. }
  941. static void reinit_opencl_device(struct cgpu_info *gpu)
  942. {
  943. tq_push(control_thr[gpur_thr_id].q, gpu);
  944. }
  945. // FIXME: Legacy (called by TUI) for side effects
  946. static
  947. bool override_opencl_statline_temp(char *buf, size_t bufsz, struct cgpu_info *gpu, __maybe_unused bool per_processor)
  948. {
  949. #ifdef HAVE_SENSORS
  950. struct opencl_device_data *data = gpu->device_data;
  951. if (data->sensor)
  952. {
  953. const sensors_chip_name *cn = data->sensor;
  954. const sensors_feature *feat;
  955. for (int f = 0; (feat = sensors_get_features(cn, &f)); )
  956. {
  957. const sensors_subfeature *subf;
  958. subf = sensors_get_subfeature(cn, feat, SENSORS_SUBFEATURE_TEMP_INPUT);
  959. if (!(subf && subf->flags & SENSORS_MODE_R))
  960. continue;
  961. double val;
  962. int rc = sensors_get_value(cn, subf->number, &val);
  963. if (rc)
  964. continue;
  965. gpu->temp = val;
  966. return false;
  967. }
  968. }
  969. #endif
  970. #ifdef HAVE_ADL
  971. if (gpu->has_adl) {
  972. int gpuid = gpu->device_id;
  973. gpu_temp(gpuid);
  974. gpu_fanspeed(gpuid);
  975. }
  976. #endif
  977. return false;
  978. }
  979. static struct api_data*
  980. get_opencl_api_extra_device_status(struct cgpu_info *gpu)
  981. {
  982. struct api_data*root = NULL;
  983. float gt, gv;
  984. int ga, gf, gp, gc, gm, pt;
  985. #ifdef HAVE_ADL
  986. if (!gpu_stats(gpu->device_id, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  987. #endif
  988. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  989. root = api_add_int(root, "Fan Speed", &gf, true);
  990. root = api_add_int(root, "Fan Percent", &gp, true);
  991. root = api_add_int(root, "GPU Clock", &gc, true);
  992. root = api_add_int(root, "Memory Clock", &gm, true);
  993. root = api_add_volts(root, "GPU Voltage", &gv, true);
  994. root = api_add_int(root, "GPU Activity", &ga, true);
  995. root = api_add_int(root, "Powertune", &pt, true);
  996. char intensity[20];
  997. if (gpu->dynamic)
  998. strcpy(intensity, "D");
  999. else
  1000. sprintf(intensity, "%d", gpu->intensity);
  1001. root = api_add_string(root, "Intensity", intensity, true);
  1002. return root;
  1003. }
  1004. struct opencl_thread_data {
  1005. cl_int (*queue_kernel_parameters)(_clState *, dev_blk_ctx *, cl_uint);
  1006. uint32_t *res;
  1007. };
  1008. static uint32_t *blank_res;
  1009. static bool opencl_thread_prepare(struct thr_info *thr)
  1010. {
  1011. char name[256];
  1012. struct cgpu_info *cgpu = thr->cgpu;
  1013. int gpu = cgpu->device_id;
  1014. int virtual_gpu = cgpu->virtual_gpu;
  1015. int i = thr->id;
  1016. static bool failmessage = false;
  1017. int buffersize = opt_scrypt ? SCRYPT_BUFFERSIZE : BUFFERSIZE;
  1018. if (!blank_res)
  1019. blank_res = calloc(buffersize, 1);
  1020. if (!blank_res) {
  1021. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1022. return false;
  1023. }
  1024. strcpy(name, "");
  1025. applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
  1026. clStates[i] = initCl(virtual_gpu, name, sizeof(name));
  1027. if (!clStates[i]) {
  1028. #ifdef HAVE_CURSES
  1029. if (use_curses)
  1030. enable_curses();
  1031. #endif
  1032. applog(LOG_ERR, "Failed to init GPU thread %d, disabling device %d", i, gpu);
  1033. if (!failmessage) {
  1034. applog(LOG_ERR, "Restarting the GPU from the menu will not fix this.");
  1035. applog(LOG_ERR, "Try restarting BFGMiner.");
  1036. failmessage = true;
  1037. #ifdef HAVE_CURSES
  1038. char *buf;
  1039. if (use_curses) {
  1040. buf = curses_input("Press enter to continue");
  1041. if (buf)
  1042. free(buf);
  1043. }
  1044. #endif
  1045. }
  1046. cgpu->deven = DEV_DISABLED;
  1047. cgpu->status = LIFE_NOSTART;
  1048. dev_error(cgpu, REASON_DEV_NOSTART);
  1049. return false;
  1050. }
  1051. if (!cgpu->name)
  1052. cgpu->name = strdup(name);
  1053. if (!cgpu->kname)
  1054. {
  1055. switch (clStates[i]->chosen_kernel) {
  1056. case KL_DIABLO:
  1057. cgpu->kname = "diablo";
  1058. break;
  1059. case KL_DIAKGCN:
  1060. cgpu->kname = "diakgcn";
  1061. break;
  1062. case KL_PHATK:
  1063. cgpu->kname = "phatk";
  1064. break;
  1065. #ifdef USE_SCRYPT
  1066. case KL_SCRYPT:
  1067. cgpu->kname = "scrypt";
  1068. break;
  1069. #endif
  1070. case KL_POCLBM:
  1071. cgpu->kname = "poclbm";
  1072. break;
  1073. default:
  1074. break;
  1075. }
  1076. }
  1077. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1078. get_now_datestamp(cgpu->init, sizeof(cgpu->init));
  1079. have_opencl = true;
  1080. return true;
  1081. }
  1082. static bool opencl_thread_init(struct thr_info *thr)
  1083. {
  1084. const int thr_id = thr->id;
  1085. struct cgpu_info *gpu = thr->cgpu;
  1086. struct opencl_thread_data *thrdata;
  1087. _clState *clState = clStates[thr_id];
  1088. cl_int status = 0;
  1089. thrdata = calloc(1, sizeof(*thrdata));
  1090. thr->cgpu_data = thrdata;
  1091. int buffersize = opt_scrypt ? SCRYPT_BUFFERSIZE : BUFFERSIZE;
  1092. if (!thrdata) {
  1093. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1094. return false;
  1095. }
  1096. switch (clState->chosen_kernel) {
  1097. case KL_POCLBM:
  1098. thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
  1099. break;
  1100. case KL_PHATK:
  1101. thrdata->queue_kernel_parameters = &queue_phatk_kernel;
  1102. break;
  1103. case KL_DIAKGCN:
  1104. thrdata->queue_kernel_parameters = &queue_diakgcn_kernel;
  1105. break;
  1106. #ifdef USE_SCRYPT
  1107. case KL_SCRYPT:
  1108. thrdata->queue_kernel_parameters = &queue_scrypt_kernel;
  1109. gpu->min_nonce_diff = 1./0x10000;
  1110. break;
  1111. #endif
  1112. default:
  1113. case KL_DIABLO:
  1114. thrdata->queue_kernel_parameters = &queue_diablo_kernel;
  1115. break;
  1116. }
  1117. thrdata->res = calloc(buffersize, 1);
  1118. if (!thrdata->res) {
  1119. free(thrdata);
  1120. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1121. return false;
  1122. }
  1123. status |= clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  1124. buffersize, blank_res, 0, NULL, NULL);
  1125. if (unlikely(status != CL_SUCCESS)) {
  1126. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1127. return false;
  1128. }
  1129. gpu->status = LIFE_WELL;
  1130. gpu->device_last_well = time(NULL);
  1131. return true;
  1132. }
  1133. static bool opencl_prepare_work(struct thr_info __maybe_unused *thr, struct work *work)
  1134. {
  1135. #ifdef USE_SCRYPT
  1136. if (opt_scrypt)
  1137. work->blk.work = work;
  1138. else
  1139. #endif
  1140. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1141. return true;
  1142. }
  1143. extern int opt_dynamic_interval;
  1144. static int64_t opencl_scanhash(struct thr_info *thr, struct work *work,
  1145. int64_t __maybe_unused max_nonce)
  1146. {
  1147. const int thr_id = thr->id;
  1148. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1149. struct cgpu_info *gpu = thr->cgpu;
  1150. _clState *clState = clStates[thr_id];
  1151. const cl_kernel *kernel = &clState->kernel;
  1152. const int dynamic_us = opt_dynamic_interval * 1000;
  1153. cl_int status;
  1154. size_t globalThreads[1];
  1155. size_t localThreads[1] = { clState->wsize };
  1156. int64_t hashes;
  1157. int found = opt_scrypt ? SCRYPT_FOUND : FOUND;
  1158. int buffersize = opt_scrypt ? SCRYPT_BUFFERSIZE : BUFFERSIZE;
  1159. /* Windows' timer resolution is only 15ms so oversample 5x */
  1160. if (gpu->dynamic && (++gpu->intervals * dynamic_us) > 70000) {
  1161. struct timeval tv_gpuend;
  1162. double gpu_us;
  1163. cgtime(&tv_gpuend);
  1164. gpu_us = us_tdiff(&tv_gpuend, &gpu->tv_gpustart) / gpu->intervals;
  1165. if (gpu_us > dynamic_us) {
  1166. if (gpu->intensity > MIN_INTENSITY)
  1167. --gpu->intensity;
  1168. } else if (gpu_us < dynamic_us / 2) {
  1169. if (gpu->intensity < MAX_INTENSITY)
  1170. ++gpu->intensity;
  1171. }
  1172. memcpy(&(gpu->tv_gpustart), &tv_gpuend, sizeof(struct timeval));
  1173. gpu->intervals = 0;
  1174. }
  1175. set_threads_hashes(clState->vwidth, &hashes, globalThreads, localThreads[0], &gpu->intensity);
  1176. if (hashes > gpu->max_hashes)
  1177. gpu->max_hashes = hashes;
  1178. status = thrdata->queue_kernel_parameters(clState, &work->blk, globalThreads[0]);
  1179. if (unlikely(status != CL_SUCCESS)) {
  1180. applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
  1181. return -1;
  1182. }
  1183. if (clState->goffset) {
  1184. size_t global_work_offset[1];
  1185. global_work_offset[0] = work->blk.nonce;
  1186. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, global_work_offset,
  1187. globalThreads, localThreads, 0, NULL, NULL);
  1188. } else
  1189. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1190. globalThreads, localThreads, 0, NULL, NULL);
  1191. if (unlikely(status != CL_SUCCESS)) {
  1192. applog(LOG_ERR, "Error %d: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)", status);
  1193. return -1;
  1194. }
  1195. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1196. buffersize, thrdata->res, 0, NULL, NULL);
  1197. if (unlikely(status != CL_SUCCESS)) {
  1198. applog(LOG_ERR, "Error: clEnqueueReadBuffer failed error %d. (clEnqueueReadBuffer)", status);
  1199. return -1;
  1200. }
  1201. /* The amount of work scanned can fluctuate when intensity changes
  1202. * and since we do this one cycle behind, we increment the work more
  1203. * than enough to prevent repeating work */
  1204. work->blk.nonce += gpu->max_hashes;
  1205. /* This finish flushes the readbuffer set with CL_FALSE in clEnqueueReadBuffer */
  1206. clFinish(clState->commandQueue);
  1207. /* FOUND entry is used as a counter to say how many nonces exist */
  1208. if (thrdata->res[found]) {
  1209. /* Clear the buffer again */
  1210. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1211. buffersize, blank_res, 0, NULL, NULL);
  1212. if (unlikely(status != CL_SUCCESS)) {
  1213. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1214. return -1;
  1215. }
  1216. applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
  1217. postcalc_hash_async(thr, work, thrdata->res);
  1218. memset(thrdata->res, 0, buffersize);
  1219. /* This finish flushes the writebuffer set with CL_FALSE in clEnqueueWriteBuffer */
  1220. clFinish(clState->commandQueue);
  1221. }
  1222. return hashes;
  1223. }
  1224. static void opencl_thread_shutdown(struct thr_info *thr)
  1225. {
  1226. const int thr_id = thr->id;
  1227. _clState *clState = clStates[thr_id];
  1228. clReleaseKernel(clState->kernel);
  1229. clReleaseProgram(clState->program);
  1230. clReleaseCommandQueue(clState->commandQueue);
  1231. clReleaseContext(clState->context);
  1232. }
  1233. struct device_drv opencl_api = {
  1234. .dname = "opencl",
  1235. .name = "OCL",
  1236. .probe_priority = 110,
  1237. .supported_algos = POW_SHA256D | POW_SCRYPT,
  1238. .drv_detect = opencl_detect,
  1239. .reinit_device = reinit_opencl_device,
  1240. .override_statline_temp2 = override_opencl_statline_temp,
  1241. #ifdef HAVE_CURSES
  1242. .proc_wlogprint_status = opencl_wlogprint_status,
  1243. .proc_tui_wlogprint_choices = opencl_tui_wlogprint_choices,
  1244. .proc_tui_handle_choice = opencl_tui_handle_choice,
  1245. #endif
  1246. .get_api_extra_device_status = get_opencl_api_extra_device_status,
  1247. .thread_prepare = opencl_thread_prepare,
  1248. .thread_init = opencl_thread_init,
  1249. .prepare_work = opencl_prepare_work,
  1250. .scanhash = opencl_scanhash,
  1251. .thread_shutdown = opencl_thread_shutdown,
  1252. };
  1253. #endif