device-gpu.c 31 KB

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