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