device-gpu.c 31 KB

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