device-gpu.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  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. static cl_int queue_poclbm_kernel(_clState *clState, dev_blk_ctx *blk)
  569. {
  570. cl_kernel *kernel = &clState->kernel;
  571. cl_int status = 0;
  572. int num = 0;
  573. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_a);
  574. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_b);
  575. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_c);
  576. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_d);
  577. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_e);
  578. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_f);
  579. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_g);
  580. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_h);
  581. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_b);
  582. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_c);
  583. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_d);
  584. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_f);
  585. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_g);
  586. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_h);
  587. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->nonce);
  588. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW0);
  589. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW1);
  590. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW2);
  591. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW3);
  592. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW15);
  593. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW01r);
  594. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e);
  595. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e2);
  596. status |= clSetKernelArg(*kernel, num++, sizeof(clState->outputBuffer),
  597. (void *)&clState->outputBuffer);
  598. return status;
  599. }
  600. static cl_int queue_phatk_kernel(_clState *clState, dev_blk_ctx *blk)
  601. {
  602. cl_uint vwidth = clState->preferred_vwidth;
  603. cl_kernel *kernel = &clState->kernel;
  604. cl_int status = 0;
  605. int i, num = 0;
  606. uint *nonces;
  607. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_a);
  608. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_b);
  609. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_c);
  610. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_d);
  611. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_e);
  612. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_f);
  613. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_g);
  614. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_h);
  615. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_b);
  616. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_c);
  617. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_d);
  618. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_f);
  619. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_g);
  620. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_h);
  621. nonces = alloca(sizeof(uint) * vwidth);
  622. for (i = 0; i < vwidth; i++)
  623. nonces[i] = blk->nonce + i;
  624. status |= clSetKernelArg(*kernel, num++, vwidth * sizeof(uint), (void *)nonces);
  625. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->W16);
  626. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->W17);
  627. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->PreVal4_2);
  628. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->PreVal0);
  629. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->PreW18);
  630. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->PreW19);
  631. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->PreW31);
  632. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->PreW32);
  633. status |= clSetKernelArg(*kernel, num++, sizeof(clState->outputBuffer),
  634. (void *)&clState->outputBuffer);
  635. return status;
  636. }
  637. static cl_int queue_diakgcn_kernel(_clState *clState, dev_blk_ctx *blk)
  638. {
  639. cl_kernel *kernel = &clState->kernel;
  640. cl_int status = 0;
  641. int num = 0;
  642. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_a);
  643. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_b);
  644. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_c);
  645. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_d);
  646. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_e);
  647. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_f);
  648. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_g);
  649. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->ctx_h);
  650. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_b);
  651. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_c);
  652. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_d);
  653. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_f);
  654. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_g);
  655. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->cty_h);
  656. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->nonce);
  657. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW0);
  658. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW1);
  659. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW2);
  660. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW3);
  661. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW15);
  662. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fW01r);
  663. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e);
  664. status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->fcty_e2);
  665. status |= clSetKernelArg(*kernel, num++, sizeof(clState->outputBuffer),
  666. (void *)&clState->outputBuffer);
  667. return status;
  668. }
  669. static void set_threads_hashes(unsigned int vectors, unsigned int *threads,
  670. unsigned int *hashes, size_t *globalThreads,
  671. unsigned int minthreads, int intensity)
  672. {
  673. *threads = 1 << (15 + intensity);
  674. if (*threads < minthreads)
  675. *threads = minthreads;
  676. *globalThreads = *threads;
  677. *hashes = *threads * vectors;
  678. }
  679. #endif /* HAVE_OPENCL */
  680. #ifdef HAVE_OPENCL
  681. /* We have only one thread that ever re-initialises GPUs, thus if any GPU
  682. * init command fails due to a completely wedged GPU, the thread will never
  683. * return, unable to harm other GPUs. If it does return, it means we only had
  684. * a soft failure and then the reinit_gpu thread is ready to tackle another
  685. * GPU */
  686. void *reinit_gpu(void *userdata)
  687. {
  688. struct thr_info *mythr = userdata;
  689. struct cgpu_info *cgpu;
  690. struct thr_info *thr;
  691. struct timeval now;
  692. char name[256];
  693. int thr_id;
  694. int gpu;
  695. pthread_detach(pthread_self());
  696. select_cgpu:
  697. cgpu = tq_pop(mythr->q, NULL);
  698. if (!cgpu)
  699. goto out;
  700. if (clDevicesNum() != nDevs) {
  701. applog(LOG_WARNING, "Hardware not reporting same number of active devices, will not attempt to restart GPU");
  702. goto out;
  703. }
  704. gpu = cgpu->device_id;
  705. cgpu->enabled = false;
  706. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  707. thr = &thr_info[thr_id];
  708. cgpu = thr->cgpu;
  709. if (cgpu->api != &opencl_api)
  710. continue;
  711. if (dev_from_id(thr_id) != gpu)
  712. continue;
  713. thr = &thr_info[thr_id];
  714. if (!thr) {
  715. applog(LOG_WARNING, "No reference to thread %d exists", thr_id);
  716. continue;
  717. }
  718. thr->rolling = thr->cgpu->rolling = 0;
  719. /* Reports the last time we tried to revive a sick GPU */
  720. gettimeofday(&thr->sick, NULL);
  721. if (!pthread_cancel(thr->pth)) {
  722. applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
  723. } else
  724. applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
  725. }
  726. cgpu->enabled = true;
  727. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  728. int virtual_gpu;
  729. thr = &thr_info[thr_id];
  730. cgpu = thr->cgpu;
  731. if (cgpu->api != &opencl_api)
  732. continue;
  733. if (dev_from_id(thr_id) != gpu)
  734. continue;
  735. virtual_gpu = cgpu->virtual_gpu;
  736. /* Lose this ram cause we may get stuck here! */
  737. //tq_freeze(thr->q);
  738. thr->q = tq_new();
  739. if (!thr->q)
  740. quit(1, "Failed to tq_new in reinit_gpu");
  741. /* Lose this ram cause we may dereference in the dying thread! */
  742. //free(clState);
  743. applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
  744. clStates[thr_id] = initCl(virtual_gpu, name, sizeof(name));
  745. if (!clStates[thr_id]) {
  746. applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
  747. goto select_cgpu;
  748. }
  749. applog(LOG_INFO, "initCl() finished. Found %s", name);
  750. if (unlikely(thr_info_create(thr, NULL, miner_thread, thr))) {
  751. applog(LOG_ERR, "thread %d create failed", thr_id);
  752. return NULL;
  753. }
  754. applog(LOG_WARNING, "Thread %d restarted", thr_id);
  755. }
  756. gettimeofday(&now, NULL);
  757. get_datestamp(cgpu->init, &now);
  758. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  759. thr = &thr_info[thr_id];
  760. cgpu = thr->cgpu;
  761. if (cgpu->api != &opencl_api)
  762. continue;
  763. if (dev_from_id(thr_id) != gpu)
  764. continue;
  765. tq_push(thr->q, &ping);
  766. }
  767. goto select_cgpu;
  768. out:
  769. return NULL;
  770. }
  771. #else
  772. void *reinit_gpu(void *userdata)
  773. {
  774. return NULL;
  775. }
  776. #endif
  777. #ifdef HAVE_OPENCL
  778. struct device_api opencl_api;
  779. static void opencl_detect()
  780. {
  781. int i;
  782. nDevs = clDevicesNum();
  783. if (nDevs < 0) {
  784. applog(LOG_ERR, "clDevicesNum returned error, no GPUs usable");
  785. nDevs = 0;
  786. }
  787. if (MAX_DEVICES - total_devices < nDevs)
  788. nDevs = MAX_DEVICES - total_devices;
  789. if (!nDevs)
  790. return;
  791. if (opt_kernel) {
  792. if (strcmp(opt_kernel, "poclbm") && strcmp(opt_kernel, "phatk") && strcmp(opt_kernel, "diakgcn"))
  793. quit(1, "Invalid kernel name specified - must be poclbm, phatk or diakgcn");
  794. if (!strcmp(opt_kernel, "diakgcn"))
  795. chosen_kernel = KL_DIAKGCN;
  796. else if (!strcmp(opt_kernel, "poclbm"))
  797. chosen_kernel = KL_POCLBM;
  798. else
  799. chosen_kernel = KL_PHATK;
  800. } else
  801. chosen_kernel = KL_NONE;
  802. for (i = 0; i < nDevs; ++i) {
  803. struct cgpu_info *cgpu;
  804. cgpu = devices[total_devices++] = &gpus[i];
  805. cgpu->enabled = true;
  806. cgpu->api = &opencl_api;
  807. cgpu->device_id = i;
  808. cgpu->threads = opt_g_threads;
  809. cgpu->virtual_gpu = i;
  810. }
  811. if (!opt_noadl)
  812. init_adl(nDevs);
  813. }
  814. static void reinit_opencl_device(struct cgpu_info *gpu)
  815. {
  816. tq_push(thr_info[gpur_thr_id].q, gpu);
  817. }
  818. #ifdef HAVE_ADL
  819. static void get_opencl_statline_before(char *buf, struct cgpu_info *gpu)
  820. {
  821. if (gpu->has_adl) {
  822. int gpuid = gpu->device_id;
  823. float gt = gpu_temp(gpuid);
  824. int gf = gpu_fanspeed(gpuid);
  825. int gp;
  826. if (gt != -1)
  827. tailsprintf(buf, "%5.1fC ", gt);
  828. else
  829. tailsprintf(buf, " ", gt);
  830. if (gf != -1)
  831. tailsprintf(buf, "%4dRPM ", gf);
  832. else if ((gp = gpu_fanpercent(gpuid)) != -1)
  833. tailsprintf(buf, "%3d%% ", gp);
  834. else
  835. tailsprintf(buf, " ");
  836. tailsprintf(buf, "| ");
  837. }
  838. }
  839. #endif
  840. static void get_opencl_statline(char *buf, struct cgpu_info *gpu)
  841. {
  842. tailsprintf(buf, " I:%2d", gpu->intensity);
  843. }
  844. struct opencl_thread_data {
  845. cl_int (*queue_kernel_parameters)(_clState *, dev_blk_ctx *);
  846. uint32_t *res;
  847. struct work *last_work;
  848. struct work _last_work;
  849. };
  850. static uint32_t *blank_res;
  851. static bool opencl_thread_prepare(struct thr_info *thr)
  852. {
  853. char name[256];
  854. struct timeval now;
  855. struct cgpu_info *cgpu = thr->cgpu;
  856. int gpu = cgpu->device_id;
  857. int virtual_gpu = cgpu->virtual_gpu;
  858. int i = thr->id;
  859. static bool failmessage = false;
  860. if (!blank_res)
  861. blank_res = calloc(BUFFERSIZE, 1);
  862. if (!blank_res) {
  863. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  864. return false;
  865. }
  866. applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
  867. clStates[i] = initCl(virtual_gpu, name, sizeof(name));
  868. if (!clStates[i]) {
  869. if (use_curses)
  870. enable_curses();
  871. applog(LOG_ERR, "Failed to init GPU thread %d, disabling device %d", i, gpu);
  872. if (!failmessage) {
  873. char *buf;
  874. applog(LOG_ERR, "Restarting the GPU from the menu will not fix this.");
  875. applog(LOG_ERR, "Try restarting cgminer.");
  876. failmessage = true;
  877. if (use_curses) {
  878. buf = curses_input("Press enter to continue");
  879. if (buf)
  880. free(buf);
  881. }
  882. }
  883. cgpu->enabled = false;
  884. cgpu->status = LIFE_NOSTART;
  885. return false;
  886. }
  887. applog(LOG_INFO, "initCl() finished. Found %s", name);
  888. gettimeofday(&now, NULL);
  889. get_datestamp(cgpu->init, &now);
  890. have_opencl = true;
  891. return true;
  892. }
  893. static bool opencl_thread_init(struct thr_info *thr)
  894. {
  895. const int thr_id = thr->id;
  896. struct cgpu_info *gpu = thr->cgpu;
  897. struct opencl_thread_data *thrdata;
  898. thrdata = calloc(1, sizeof(*thrdata));
  899. thr->cgpu_data = thrdata;
  900. if (!thrdata) {
  901. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  902. return false;
  903. }
  904. switch (chosen_kernel) {
  905. case KL_POCLBM:
  906. thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
  907. break;
  908. case KL_PHATK:
  909. default:
  910. thrdata->queue_kernel_parameters = &queue_phatk_kernel;
  911. break;
  912. case KL_DIAKGCN:
  913. thrdata->queue_kernel_parameters = &queue_diakgcn_kernel;
  914. break;
  915. }
  916. thrdata->res = calloc(BUFFERSIZE, 1);
  917. if (!thrdata->res) {
  918. free(thrdata);
  919. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  920. return false;
  921. }
  922. _clState *clState = clStates[thr_id];
  923. cl_int status;
  924. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  925. BUFFERSIZE, blank_res, 0, NULL, NULL);
  926. if (unlikely(status != CL_SUCCESS)) {
  927. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  928. return false;
  929. }
  930. gpu->status = LIFE_WELL;
  931. return true;
  932. }
  933. static void opencl_free_work(struct thr_info *thr, struct work *work)
  934. {
  935. const int thr_id = thr->id;
  936. struct opencl_thread_data *thrdata = thr->cgpu_data;
  937. _clState *clState = clStates[thr_id];
  938. clFinish(clState->commandQueue);
  939. if (thrdata->res[FOUND]) {
  940. thrdata->last_work = &thrdata->_last_work;
  941. memcpy(thrdata->last_work, work, sizeof(*thrdata->last_work));
  942. }
  943. }
  944. static bool opencl_prepare_work(struct thr_info __maybe_unused *thr, struct work *work)
  945. {
  946. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  947. return true;
  948. }
  949. static uint64_t opencl_scanhash(struct thr_info *thr, struct work *work,
  950. uint64_t __maybe_unused max_nonce)
  951. {
  952. const int thr_id = thr->id;
  953. struct opencl_thread_data *thrdata = thr->cgpu_data;
  954. struct cgpu_info *gpu = thr->cgpu;
  955. _clState *clState = clStates[thr_id];
  956. const cl_kernel *kernel = &clState->kernel;
  957. double gpu_ms_average = 7;
  958. cl_int status;
  959. size_t globalThreads[1];
  960. size_t localThreads[1] = { clState->work_size };
  961. unsigned int threads;
  962. unsigned int hashes;
  963. struct timeval tv_gpustart, tv_gpuend, diff;
  964. suseconds_t gpu_us;
  965. gettimeofday(&tv_gpustart, NULL);
  966. timeval_subtract(&diff, &tv_gpustart, &tv_gpuend);
  967. /* This finish flushes the readbuffer set with CL_FALSE later */
  968. clFinish(clState->commandQueue);
  969. gettimeofday(&tv_gpuend, NULL);
  970. timeval_subtract(&diff, &tv_gpuend, &tv_gpustart);
  971. gpu_us = diff.tv_sec * 1000000 + diff.tv_usec;
  972. decay_time(&gpu_ms_average, gpu_us / 1000);
  973. if (gpu->dynamic) {
  974. /* Try to not let the GPU be out for longer than 6ms, but
  975. * increase intensity when the system is idle, unless
  976. * dynamic is disabled. */
  977. if (gpu_ms_average > 7) {
  978. if (gpu->intensity > MIN_INTENSITY)
  979. --gpu->intensity;
  980. } else if (gpu_ms_average < 3) {
  981. if (gpu->intensity < MAX_INTENSITY)
  982. ++gpu->intensity;
  983. }
  984. }
  985. set_threads_hashes(clState->preferred_vwidth, &threads, &hashes, globalThreads,
  986. localThreads[0], gpu->intensity);
  987. status = thrdata->queue_kernel_parameters(clState, &work->blk);
  988. if (unlikely(status != CL_SUCCESS)) {
  989. applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
  990. return 0;
  991. }
  992. /* MAXBUFFERS entry is used as a flag to say nonces exist */
  993. if (thrdata->res[FOUND]) {
  994. /* Clear the buffer again */
  995. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  996. BUFFERSIZE, blank_res, 0, NULL, NULL);
  997. if (unlikely(status != CL_SUCCESS)) {
  998. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  999. return 0;
  1000. }
  1001. if (unlikely(thrdata->last_work)) {
  1002. if (opt_debug)
  1003. applog(LOG_DEBUG, "GPU %d found something in last work?", gpu->device_id);
  1004. postcalc_hash_async(thr, thrdata->last_work, thrdata->res);
  1005. thrdata->last_work = NULL;
  1006. } else {
  1007. if (opt_debug)
  1008. applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
  1009. postcalc_hash_async(thr, work, thrdata->res);
  1010. }
  1011. memset(thrdata->res, 0, BUFFERSIZE);
  1012. clFinish(clState->commandQueue);
  1013. }
  1014. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1015. globalThreads, localThreads, 0, NULL, NULL);
  1016. if (unlikely(status != CL_SUCCESS)) {
  1017. applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)");
  1018. return 0;
  1019. }
  1020. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1021. BUFFERSIZE, thrdata->res, 0, NULL, NULL);
  1022. if (unlikely(status != CL_SUCCESS)) {
  1023. applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)");
  1024. return 0;
  1025. }
  1026. work->blk.nonce += hashes;
  1027. return hashes;
  1028. }
  1029. static void opencl_thread_shutdown(struct thr_info *thr)
  1030. {
  1031. const int thr_id = thr->id;
  1032. _clState *clState = clStates[thr_id];
  1033. clReleaseCommandQueue(clState->commandQueue);
  1034. clReleaseKernel(clState->kernel);
  1035. clReleaseProgram(clState->program);
  1036. clReleaseContext(clState->context);
  1037. }
  1038. struct device_api opencl_api = {
  1039. .name = "GPU",
  1040. .api_detect = opencl_detect,
  1041. .reinit_device = reinit_opencl_device,
  1042. #ifdef HAVE_ADL
  1043. .get_statline_before = get_opencl_statline_before,
  1044. #endif
  1045. .get_statline = get_opencl_statline,
  1046. .thread_prepare = opencl_thread_prepare,
  1047. .thread_init = opencl_thread_init,
  1048. .free_work = opencl_free_work,
  1049. .prepare_work = opencl_prepare_work,
  1050. .scanhash = opencl_scanhash,
  1051. .thread_shutdown = opencl_thread_shutdown,
  1052. };
  1053. #endif