driver-opencl.c 38 KB

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