driver-opencl.c 38 KB

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