driver-opencl.c 37 KB

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