device-gpu.c 33 KB

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