driver-opencl.c 46 KB

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