driver-opencl.c 34 KB

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