driver-opencl.c 38 KB

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