device-gpu.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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. if (!thr->pause && cgpu->dynamic) {
  345. applog(LOG_WARNING, "Disabling extra threads due to dynamic mode.");
  346. applog(LOG_WARNING, "Tune dynamic intensity with --gpu-dyninterval");
  347. }
  348. thr->pause = cgpu->dynamic;
  349. if (!cgpu->dynamic && cgpu->enabled)
  350. tq_push(thr->q, &ping);
  351. }
  352. }
  353. struct device_api opencl_api;
  354. void manage_gpu(void)
  355. {
  356. struct thr_info *thr;
  357. int selected, gpu, i;
  358. char checkin[40];
  359. char input;
  360. if (!opt_g_threads)
  361. return;
  362. opt_loginput = true;
  363. immedok(logwin, true);
  364. clear_logwin();
  365. retry:
  366. for (gpu = 0; gpu < nDevs; gpu++) {
  367. struct cgpu_info *cgpu = &gpus[gpu];
  368. wlog("GPU %d: %.1f / %.1f Mh/s | A:%d R:%d HW:%d U:%.2f/m I:%d\n",
  369. gpu, cgpu->rolling, cgpu->total_mhashes / total_secs,
  370. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  371. cgpu->utility, cgpu->intensity);
  372. #ifdef HAVE_ADL
  373. if (gpus[gpu].has_adl) {
  374. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  375. float temp = 0, vddc = 0;
  376. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune)) {
  377. char logline[255];
  378. strcpy(logline, ""); // In case it has no data
  379. if (temp != -1)
  380. sprintf(logline, "%.1f C ", temp);
  381. if (fanspeed != -1 || fanpercent != -1) {
  382. tailsprintf(logline, "F: ");
  383. if (fanpercent != -1)
  384. tailsprintf(logline, "%d%% ", fanpercent);
  385. if (fanspeed != -1)
  386. tailsprintf(logline, "(%d RPM) ", fanspeed);
  387. tailsprintf(logline, " ");
  388. }
  389. if (engineclock != -1)
  390. tailsprintf(logline, "E: %d MHz ", engineclock);
  391. if (memclock != -1)
  392. tailsprintf(logline, "M: %d Mhz ", memclock);
  393. if (vddc != -1)
  394. tailsprintf(logline, "V: %.3fV ", vddc);
  395. if (activity != -1)
  396. tailsprintf(logline, "A: %d%% ", activity);
  397. if (powertune != -1)
  398. tailsprintf(logline, "P: %d%%", powertune);
  399. tailsprintf(logline, "\n");
  400. wlog(logline);
  401. }
  402. }
  403. #endif
  404. wlog("Last initialised: %s\n", cgpu->init);
  405. wlog("Intensity: ");
  406. if (gpus[gpu].dynamic)
  407. wlog("Dynamic (only one thread in use)\n");
  408. else
  409. wlog("%d\n", gpus[gpu].intensity);
  410. for (i = 0; i < mining_threads; i++) {
  411. thr = &thr_info[i];
  412. if (thr->cgpu != cgpu)
  413. continue;
  414. get_datestamp(checkin, &thr->last);
  415. wlog("Thread %d: %.1f Mh/s %s ", i, thr->rolling, cgpu->enabled ? "Enabled" : "Disabled");
  416. switch (cgpu->status) {
  417. default:
  418. case LIFE_WELL:
  419. wlog("ALIVE");
  420. break;
  421. case LIFE_SICK:
  422. wlog("SICK reported in %s", checkin);
  423. break;
  424. case LIFE_DEAD:
  425. wlog("DEAD reported in %s", checkin);
  426. break;
  427. case LIFE_NOSTART:
  428. wlog("Never started");
  429. break;
  430. }
  431. if (thr->pause)
  432. wlog(" paused");
  433. wlog("\n");
  434. }
  435. wlog("\n");
  436. }
  437. wlogprint("[E]nable [D]isable [I]ntensity [R]estart GPU %s\n",adl_active ? "[C]hange settings" : "");
  438. wlogprint("Or press any other key to continue\n");
  439. input = getch();
  440. if (nDevs == 1)
  441. selected = 0;
  442. else
  443. selected = -1;
  444. if (!strncasecmp(&input, "e", 1)) {
  445. struct cgpu_info *cgpu;
  446. if (selected)
  447. selected = curses_int("Select GPU to enable");
  448. if (selected < 0 || selected >= nDevs) {
  449. wlogprint("Invalid selection\n");
  450. goto retry;
  451. }
  452. if (gpus[selected].enabled) {
  453. wlogprint("Device already enabled\n");
  454. goto retry;
  455. }
  456. gpus[selected].enabled = true;
  457. for (i = 0; i < mining_threads; ++i) {
  458. thr = &thr_info[i];
  459. cgpu = thr->cgpu;
  460. if (cgpu->api != &opencl_api)
  461. continue;
  462. if (dev_from_id(i) != selected)
  463. continue;
  464. if (cgpu->status != LIFE_WELL) {
  465. wlogprint("Must restart device before enabling it");
  466. gpus[selected].enabled = false;
  467. goto retry;
  468. }
  469. applog(LOG_DEBUG, "Pushing ping to thread %d", thr->id);
  470. tq_push(thr->q, &ping);
  471. }
  472. goto retry;
  473. } if (!strncasecmp(&input, "d", 1)) {
  474. if (selected)
  475. selected = curses_int("Select GPU to disable");
  476. if (selected < 0 || selected >= nDevs) {
  477. wlogprint("Invalid selection\n");
  478. goto retry;
  479. }
  480. if (!gpus[selected].enabled) {
  481. wlogprint("Device already disabled\n");
  482. goto retry;
  483. }
  484. gpus[selected].enabled = false;
  485. goto retry;
  486. } else if (!strncasecmp(&input, "i", 1)) {
  487. int intensity;
  488. char *intvar;
  489. if (selected)
  490. selected = curses_int("Select GPU to change intensity on");
  491. if (selected < 0 || selected >= nDevs) {
  492. wlogprint("Invalid selection\n");
  493. goto retry;
  494. }
  495. intvar = curses_input("Set GPU scan intensity (d or " _MIN_INTENSITY_STR " -> " _MAX_INTENSITY_STR ")");
  496. if (!intvar) {
  497. wlogprint("Invalid input\n");
  498. goto retry;
  499. }
  500. if (!strncasecmp(intvar, "d", 1)) {
  501. wlogprint("Dynamic mode enabled on gpu %d\n", selected);
  502. gpus[selected].dynamic = true;
  503. pause_dynamic_threads(selected);
  504. free(intvar);
  505. goto retry;
  506. }
  507. intensity = atoi(intvar);
  508. free(intvar);
  509. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  510. wlogprint("Invalid selection\n");
  511. goto retry;
  512. }
  513. gpus[selected].dynamic = false;
  514. gpus[selected].intensity = intensity;
  515. wlogprint("Intensity on gpu %d set to %d\n", selected, intensity);
  516. pause_dynamic_threads(selected);
  517. goto retry;
  518. } else if (!strncasecmp(&input, "r", 1)) {
  519. if (selected)
  520. selected = curses_int("Select GPU to attempt to restart");
  521. if (selected < 0 || selected >= nDevs) {
  522. wlogprint("Invalid selection\n");
  523. goto retry;
  524. }
  525. wlogprint("Attempting to restart threads of GPU %d\n", selected);
  526. reinit_device(&gpus[selected]);
  527. goto retry;
  528. } else if (adl_active && (!strncasecmp(&input, "c", 1))) {
  529. if (selected)
  530. selected = curses_int("Select GPU to change settings on");
  531. if (selected < 0 || selected >= nDevs) {
  532. wlogprint("Invalid selection\n");
  533. goto retry;
  534. }
  535. change_gpusettings(selected);
  536. goto retry;
  537. } else
  538. clear_logwin();
  539. immedok(logwin, false);
  540. opt_loginput = false;
  541. }
  542. #else
  543. void manage_gpu(void)
  544. {
  545. }
  546. #endif
  547. #ifdef HAVE_OPENCL
  548. static _clState *clStates[MAX_GPUDEVICES];
  549. #define CL_SET_BLKARG(blkvar) status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->blkvar)
  550. #define CL_SET_ARG(var) status |= clSetKernelArg(*kernel, num++, sizeof(var), (void *)&var)
  551. #define CL_SET_VARG(args, var) status |= clSetKernelArg(*kernel, num++, args * sizeof(uint), (void *)var)
  552. static cl_int queue_poclbm_kernel(_clState *clState, dev_blk_ctx *blk)
  553. {
  554. cl_uint vwidth = clState->preferred_vwidth;
  555. cl_kernel *kernel = &clState->kernel;
  556. unsigned int i, num = 0;
  557. cl_int status = 0;
  558. uint *nonces;
  559. CL_SET_BLKARG(ctx_a);
  560. CL_SET_BLKARG(ctx_b);
  561. CL_SET_BLKARG(ctx_c);
  562. CL_SET_BLKARG(ctx_d);
  563. CL_SET_BLKARG(ctx_e);
  564. CL_SET_BLKARG(ctx_f);
  565. CL_SET_BLKARG(ctx_g);
  566. CL_SET_BLKARG(ctx_h);
  567. CL_SET_BLKARG(cty_b);
  568. CL_SET_BLKARG(cty_c);
  569. CL_SET_BLKARG(cty_f);
  570. CL_SET_BLKARG(cty_g);
  571. CL_SET_BLKARG(cty_h);
  572. nonces = alloca(sizeof(uint) * vwidth);
  573. for (i = 0; i < vwidth; i++)
  574. nonces[i] = blk->nonce + i;
  575. CL_SET_VARG(vwidth, nonces);
  576. CL_SET_BLKARG(fW0);
  577. CL_SET_BLKARG(fW1);
  578. CL_SET_BLKARG(fW2);
  579. CL_SET_BLKARG(fW3);
  580. CL_SET_BLKARG(fW15);
  581. CL_SET_BLKARG(fW01r);
  582. CL_SET_BLKARG(fcty_e2);
  583. CL_SET_BLKARG(D1A);
  584. CL_SET_BLKARG(C1addK5);
  585. CL_SET_BLKARG(B1addK6);
  586. CL_SET_BLKARG(W16addK16);
  587. CL_SET_BLKARG(W17addK17);
  588. CL_SET_BLKARG(PreVal4addT1);
  589. CL_SET_BLKARG(PreVal0);
  590. CL_SET_ARG(clState->outputBuffer);
  591. return status;
  592. }
  593. static cl_int queue_phatk_kernel(_clState *clState, dev_blk_ctx *blk)
  594. {
  595. cl_uint vwidth = clState->preferred_vwidth;
  596. cl_kernel *kernel = &clState->kernel;
  597. unsigned int i, num = 0;
  598. cl_int status = 0;
  599. uint *nonces;
  600. CL_SET_BLKARG(ctx_a);
  601. CL_SET_BLKARG(ctx_b);
  602. CL_SET_BLKARG(ctx_c);
  603. CL_SET_BLKARG(ctx_d);
  604. CL_SET_BLKARG(ctx_e);
  605. CL_SET_BLKARG(ctx_f);
  606. CL_SET_BLKARG(ctx_g);
  607. CL_SET_BLKARG(ctx_h);
  608. CL_SET_BLKARG(cty_b);
  609. CL_SET_BLKARG(cty_c);
  610. CL_SET_BLKARG(cty_d);
  611. CL_SET_BLKARG(cty_f);
  612. CL_SET_BLKARG(cty_g);
  613. CL_SET_BLKARG(cty_h);
  614. nonces = alloca(sizeof(uint) * vwidth);
  615. for (i = 0; i < vwidth; i++)
  616. nonces[i] = blk->nonce + i;
  617. CL_SET_VARG(vwidth, nonces);
  618. CL_SET_BLKARG(W16);
  619. CL_SET_BLKARG(W17);
  620. CL_SET_BLKARG(PreVal4_2);
  621. CL_SET_BLKARG(PreVal0);
  622. CL_SET_BLKARG(PreW18);
  623. CL_SET_BLKARG(PreW19);
  624. CL_SET_BLKARG(PreW31);
  625. CL_SET_BLKARG(PreW32);
  626. CL_SET_ARG(clState->outputBuffer);
  627. return status;
  628. }
  629. static cl_int queue_diakgcn_kernel(_clState *clState, dev_blk_ctx *blk)
  630. {
  631. cl_uint vwidth = clState->preferred_vwidth;
  632. cl_kernel *kernel = &clState->kernel;
  633. unsigned int i, num = 0;
  634. cl_int status = 0;
  635. uint *nonces;
  636. nonces = alloca(sizeof(uint) * vwidth);
  637. for (i = 0; i < vwidth; i++)
  638. nonces[i] = blk->nonce + i;
  639. CL_SET_VARG(vwidth, nonces);
  640. CL_SET_BLKARG(PreVal0);
  641. CL_SET_BLKARG(PreVal4_2);
  642. CL_SET_BLKARG(cty_h);
  643. CL_SET_BLKARG(D1A);
  644. CL_SET_BLKARG(cty_b);
  645. CL_SET_BLKARG(cty_c);
  646. CL_SET_BLKARG(cty_f);
  647. CL_SET_BLKARG(cty_g);
  648. CL_SET_BLKARG(C1addK5);
  649. CL_SET_BLKARG(B1addK6);
  650. CL_SET_BLKARG(PreVal0addK7);
  651. CL_SET_BLKARG(W16addK16);
  652. CL_SET_BLKARG(W17addK17);
  653. CL_SET_BLKARG(PreW18);
  654. CL_SET_BLKARG(PreW19);
  655. CL_SET_BLKARG(W16);
  656. CL_SET_BLKARG(W17);
  657. CL_SET_BLKARG(PreW31);
  658. CL_SET_BLKARG(PreW32);
  659. CL_SET_BLKARG(ctx_a);
  660. CL_SET_BLKARG(ctx_b);
  661. CL_SET_BLKARG(ctx_c);
  662. CL_SET_BLKARG(ctx_d);
  663. CL_SET_BLKARG(ctx_e);
  664. CL_SET_BLKARG(ctx_f);
  665. CL_SET_BLKARG(ctx_g);
  666. CL_SET_BLKARG(ctx_h);
  667. CL_SET_BLKARG(zeroA);
  668. CL_SET_BLKARG(zeroB);
  669. CL_SET_BLKARG(oneA);
  670. CL_SET_BLKARG(twoA);
  671. CL_SET_BLKARG(threeA);
  672. CL_SET_BLKARG(fourA);
  673. CL_SET_BLKARG(fiveA);
  674. CL_SET_BLKARG(sixA);
  675. CL_SET_BLKARG(sevenA);
  676. CL_SET_ARG(clState->outputBuffer);
  677. return status;
  678. }
  679. static cl_int queue_diablo_kernel(_clState *clState, dev_blk_ctx *blk)
  680. {
  681. cl_kernel *kernel = &clState->kernel;
  682. cl_int status = 0;
  683. int num = 0;
  684. CL_SET_BLKARG(nonce);
  685. CL_SET_BLKARG(PreVal0);
  686. CL_SET_BLKARG(PreVal4_2);
  687. CL_SET_BLKARG(PreW18);
  688. CL_SET_BLKARG(PreW19);
  689. CL_SET_BLKARG(W16);
  690. CL_SET_BLKARG(W17);
  691. CL_SET_BLKARG(PreW31);
  692. CL_SET_BLKARG(PreW32);
  693. CL_SET_BLKARG(cty_d);
  694. CL_SET_BLKARG(cty_b);
  695. CL_SET_BLKARG(cty_c);
  696. CL_SET_BLKARG(cty_h);
  697. CL_SET_BLKARG(cty_f);
  698. CL_SET_BLKARG(cty_g);
  699. CL_SET_BLKARG(C1addK5);
  700. CL_SET_BLKARG(B1addK6);
  701. CL_SET_BLKARG(ctx_a);
  702. CL_SET_BLKARG(ctx_b);
  703. CL_SET_BLKARG(ctx_c);
  704. CL_SET_BLKARG(ctx_d);
  705. CL_SET_BLKARG(ctx_e);
  706. CL_SET_BLKARG(ctx_f);
  707. CL_SET_BLKARG(ctx_g);
  708. CL_SET_BLKARG(ctx_h);
  709. CL_SET_ARG(clState->outputBuffer);
  710. return status;
  711. }
  712. static void set_threads_hashes(unsigned int vectors, unsigned int *threads,
  713. unsigned int *hashes, size_t *globalThreads,
  714. unsigned int minthreads, int intensity)
  715. {
  716. *threads = 1 << (15 + intensity);
  717. if (*threads < minthreads)
  718. *threads = minthreads;
  719. *globalThreads = *threads;
  720. *hashes = *threads * vectors;
  721. }
  722. #endif /* HAVE_OPENCL */
  723. #ifdef HAVE_OPENCL
  724. /* We have only one thread that ever re-initialises GPUs, thus if any GPU
  725. * init command fails due to a completely wedged GPU, the thread will never
  726. * return, unable to harm other GPUs. If it does return, it means we only had
  727. * a soft failure and then the reinit_gpu thread is ready to tackle another
  728. * GPU */
  729. void *reinit_gpu(void *userdata)
  730. {
  731. struct thr_info *mythr = userdata;
  732. struct cgpu_info *cgpu;
  733. struct thr_info *thr;
  734. struct timeval now;
  735. char name[256];
  736. int thr_id;
  737. int gpu;
  738. pthread_detach(pthread_self());
  739. select_cgpu:
  740. cgpu = tq_pop(mythr->q, NULL);
  741. if (!cgpu)
  742. goto out;
  743. if (clDevicesNum() != nDevs) {
  744. applog(LOG_WARNING, "Hardware not reporting same number of active devices, will not attempt to restart GPU");
  745. goto out;
  746. }
  747. gpu = cgpu->device_id;
  748. cgpu->enabled = false;
  749. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  750. thr = &thr_info[thr_id];
  751. cgpu = thr->cgpu;
  752. if (cgpu->api != &opencl_api)
  753. continue;
  754. if (dev_from_id(thr_id) != gpu)
  755. continue;
  756. thr = &thr_info[thr_id];
  757. if (!thr) {
  758. applog(LOG_WARNING, "No reference to thread %d exists", thr_id);
  759. continue;
  760. }
  761. thr->rolling = thr->cgpu->rolling = 0;
  762. /* Reports the last time we tried to revive a sick GPU */
  763. gettimeofday(&thr->sick, NULL);
  764. if (!pthread_cancel(thr->pth)) {
  765. applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
  766. } else
  767. applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
  768. }
  769. cgpu->enabled = true;
  770. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  771. int virtual_gpu;
  772. thr = &thr_info[thr_id];
  773. cgpu = thr->cgpu;
  774. if (cgpu->api != &opencl_api)
  775. continue;
  776. if (dev_from_id(thr_id) != gpu)
  777. continue;
  778. virtual_gpu = cgpu->virtual_gpu;
  779. /* Lose this ram cause we may get stuck here! */
  780. //tq_freeze(thr->q);
  781. thr->q = tq_new();
  782. if (!thr->q)
  783. quit(1, "Failed to tq_new in reinit_gpu");
  784. /* Lose this ram cause we may dereference in the dying thread! */
  785. //free(clState);
  786. applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
  787. clStates[thr_id] = initCl(virtual_gpu, name, sizeof(name));
  788. if (!clStates[thr_id]) {
  789. applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
  790. goto select_cgpu;
  791. }
  792. applog(LOG_INFO, "initCl() finished. Found %s", name);
  793. if (unlikely(thr_info_create(thr, NULL, miner_thread, thr))) {
  794. applog(LOG_ERR, "thread %d create failed", thr_id);
  795. return NULL;
  796. }
  797. applog(LOG_WARNING, "Thread %d restarted", thr_id);
  798. }
  799. gettimeofday(&now, NULL);
  800. get_datestamp(cgpu->init, &now);
  801. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  802. thr = &thr_info[thr_id];
  803. cgpu = thr->cgpu;
  804. if (cgpu->api != &opencl_api)
  805. continue;
  806. if (dev_from_id(thr_id) != gpu)
  807. continue;
  808. tq_push(thr->q, &ping);
  809. }
  810. goto select_cgpu;
  811. out:
  812. return NULL;
  813. }
  814. #else
  815. void *reinit_gpu(void *userdata)
  816. {
  817. return NULL;
  818. }
  819. #endif
  820. #ifdef HAVE_OPENCL
  821. struct device_api opencl_api;
  822. static void opencl_detect()
  823. {
  824. int i;
  825. nDevs = clDevicesNum();
  826. if (nDevs < 0) {
  827. applog(LOG_ERR, "clDevicesNum returned error, no GPUs usable");
  828. nDevs = 0;
  829. }
  830. if (MAX_DEVICES - total_devices < nDevs)
  831. nDevs = MAX_DEVICES - total_devices;
  832. if (!nDevs)
  833. return;
  834. if (opt_kernel) {
  835. if (strcmp(opt_kernel, "poclbm") &&
  836. strcmp(opt_kernel, "phatk") &&
  837. strcmp(opt_kernel, "diakgcn") &&
  838. strcmp(opt_kernel, "diablo"))
  839. quit(1, "Invalid kernel name specified - must be poclbm or phatk");
  840. if (!strcmp(opt_kernel, "diakgcn"))
  841. chosen_kernel = KL_DIAKGCN;
  842. else if (!strcmp(opt_kernel, "poclbm"))
  843. chosen_kernel = KL_POCLBM;
  844. else if (!strcmp(opt_kernel, "diablo"))
  845. chosen_kernel = KL_DIABLO;
  846. else
  847. chosen_kernel = KL_PHATK;
  848. } else
  849. chosen_kernel = KL_NONE;
  850. for (i = 0; i < nDevs; ++i) {
  851. struct cgpu_info *cgpu;
  852. cgpu = devices[total_devices++] = &gpus[i];
  853. cgpu->enabled = true;
  854. cgpu->api = &opencl_api;
  855. cgpu->device_id = i;
  856. cgpu->threads = opt_g_threads;
  857. cgpu->virtual_gpu = i;
  858. }
  859. if (!opt_noadl)
  860. init_adl(nDevs);
  861. }
  862. static void reinit_opencl_device(struct cgpu_info *gpu)
  863. {
  864. tq_push(thr_info[gpur_thr_id].q, gpu);
  865. }
  866. #ifdef HAVE_ADL
  867. static void get_opencl_statline_before(char *buf, struct cgpu_info *gpu)
  868. {
  869. if (gpu->has_adl) {
  870. int gpuid = gpu->device_id;
  871. float gt = gpu_temp(gpuid);
  872. int gf = gpu_fanspeed(gpuid);
  873. int gp;
  874. if (gt != -1)
  875. tailsprintf(buf, "%5.1fC ", gt);
  876. else
  877. tailsprintf(buf, " ", gt);
  878. if (gf != -1)
  879. tailsprintf(buf, "%4dRPM ", gf);
  880. else if ((gp = gpu_fanpercent(gpuid)) != -1)
  881. tailsprintf(buf, "%3d%% ", gp);
  882. else
  883. tailsprintf(buf, " ");
  884. tailsprintf(buf, "| ");
  885. }
  886. }
  887. #endif
  888. static void get_opencl_statline(char *buf, struct cgpu_info *gpu)
  889. {
  890. tailsprintf(buf, " I:%2d", gpu->intensity);
  891. }
  892. struct opencl_thread_data {
  893. cl_int (*queue_kernel_parameters)(_clState *, dev_blk_ctx *);
  894. uint32_t *res;
  895. struct work *last_work;
  896. struct work _last_work;
  897. };
  898. static uint32_t *blank_res;
  899. static bool opencl_thread_prepare(struct thr_info *thr)
  900. {
  901. char name[256];
  902. struct timeval now;
  903. struct cgpu_info *cgpu = thr->cgpu;
  904. int gpu = cgpu->device_id;
  905. int virtual_gpu = cgpu->virtual_gpu;
  906. int i = thr->id;
  907. static bool failmessage = false;
  908. if (!blank_res)
  909. blank_res = calloc(BUFFERSIZE, 1);
  910. if (!blank_res) {
  911. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  912. return false;
  913. }
  914. applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
  915. clStates[i] = initCl(virtual_gpu, name, sizeof(name));
  916. if (!clStates[i]) {
  917. if (use_curses)
  918. enable_curses();
  919. applog(LOG_ERR, "Failed to init GPU thread %d, disabling device %d", i, gpu);
  920. if (!failmessage) {
  921. char *buf;
  922. applog(LOG_ERR, "Restarting the GPU from the menu will not fix this.");
  923. applog(LOG_ERR, "Try restarting cgminer.");
  924. failmessage = true;
  925. if (use_curses) {
  926. buf = curses_input("Press enter to continue");
  927. if (buf)
  928. free(buf);
  929. }
  930. }
  931. cgpu->enabled = false;
  932. cgpu->status = LIFE_NOSTART;
  933. return false;
  934. }
  935. applog(LOG_INFO, "initCl() finished. Found %s", name);
  936. gettimeofday(&now, NULL);
  937. get_datestamp(cgpu->init, &now);
  938. have_opencl = true;
  939. return true;
  940. }
  941. static bool opencl_thread_init(struct thr_info *thr)
  942. {
  943. const int thr_id = thr->id;
  944. struct cgpu_info *gpu = thr->cgpu;
  945. struct opencl_thread_data *thrdata;
  946. _clState *clState = clStates[thr_id];
  947. cl_int status;
  948. thrdata = calloc(1, sizeof(*thrdata));
  949. thr->cgpu_data = thrdata;
  950. if (!thrdata) {
  951. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  952. return false;
  953. }
  954. switch (clState->chosen_kernel) {
  955. case KL_POCLBM:
  956. thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
  957. break;
  958. case KL_PHATK:
  959. default:
  960. thrdata->queue_kernel_parameters = &queue_phatk_kernel;
  961. break;
  962. case KL_DIAKGCN:
  963. thrdata->queue_kernel_parameters = &queue_diakgcn_kernel;
  964. break;
  965. case KL_DIABLO:
  966. thrdata->queue_kernel_parameters = &queue_diablo_kernel;
  967. break;
  968. }
  969. thrdata->res = calloc(BUFFERSIZE, 1);
  970. if (!thrdata->res) {
  971. free(thrdata);
  972. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  973. return false;
  974. }
  975. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  976. BUFFERSIZE, blank_res, 0, NULL, NULL);
  977. if (unlikely(status != CL_SUCCESS)) {
  978. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  979. return false;
  980. }
  981. gpu->status = LIFE_WELL;
  982. return true;
  983. }
  984. static void opencl_free_work(struct thr_info *thr, struct work *work)
  985. {
  986. const int thr_id = thr->id;
  987. struct opencl_thread_data *thrdata = thr->cgpu_data;
  988. _clState *clState = clStates[thr_id];
  989. clFinish(clState->commandQueue);
  990. if (thrdata->res[FOUND]) {
  991. thrdata->last_work = &thrdata->_last_work;
  992. memcpy(thrdata->last_work, work, sizeof(*thrdata->last_work));
  993. }
  994. }
  995. static bool opencl_prepare_work(struct thr_info __maybe_unused *thr, struct work *work)
  996. {
  997. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  998. return true;
  999. }
  1000. extern int opt_dynamic_interval;
  1001. static uint64_t opencl_scanhash(struct thr_info *thr, struct work *work,
  1002. uint64_t __maybe_unused max_nonce)
  1003. {
  1004. const int thr_id = thr->id;
  1005. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1006. struct cgpu_info *gpu = thr->cgpu;
  1007. _clState *clState = clStates[thr_id];
  1008. const cl_kernel *kernel = &clState->kernel;
  1009. double gpu_ms_average = 7;
  1010. cl_int status;
  1011. size_t globalThreads[1];
  1012. size_t localThreads[1] = { clState->work_size };
  1013. unsigned int threads;
  1014. unsigned int hashes;
  1015. struct timeval tv_gpustart, tv_gpuend, diff;
  1016. suseconds_t gpu_us;
  1017. gettimeofday(&tv_gpustart, NULL);
  1018. timeval_subtract(&diff, &tv_gpustart, &tv_gpuend);
  1019. /* This finish flushes the readbuffer set with CL_FALSE later */
  1020. clFinish(clState->commandQueue);
  1021. gettimeofday(&tv_gpuend, NULL);
  1022. timeval_subtract(&diff, &tv_gpuend, &tv_gpustart);
  1023. gpu_us = diff.tv_sec * 1000000 + diff.tv_usec;
  1024. decay_time(&gpu_ms_average, gpu_us / 1000);
  1025. if (gpu->dynamic) {
  1026. /* Try to not let the GPU be out for longer than 6ms, but
  1027. * increase intensity when the system is idle, unless
  1028. * dynamic is disabled. */
  1029. if (gpu_ms_average > opt_dynamic_interval) {
  1030. if (gpu->intensity > MIN_INTENSITY)
  1031. --gpu->intensity;
  1032. } else if (gpu_ms_average < ((opt_dynamic_interval / 2) ? : 1)) {
  1033. if (gpu->intensity < MAX_INTENSITY)
  1034. ++gpu->intensity;
  1035. }
  1036. }
  1037. set_threads_hashes(clState->preferred_vwidth, &threads, &hashes, globalThreads,
  1038. localThreads[0], gpu->intensity);
  1039. if (hashes > gpu->max_hashes)
  1040. gpu->max_hashes = hashes;
  1041. status = thrdata->queue_kernel_parameters(clState, &work->blk);
  1042. if (unlikely(status != CL_SUCCESS)) {
  1043. applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
  1044. return 0;
  1045. }
  1046. /* MAXBUFFERS entry is used as a flag to say nonces exist */
  1047. if (thrdata->res[FOUND]) {
  1048. /* Clear the buffer again */
  1049. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1050. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1051. if (unlikely(status != CL_SUCCESS)) {
  1052. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1053. return 0;
  1054. }
  1055. if (unlikely(thrdata->last_work)) {
  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. applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
  1061. postcalc_hash_async(thr, work, thrdata->res);
  1062. }
  1063. memset(thrdata->res, 0, BUFFERSIZE);
  1064. clFinish(clState->commandQueue);
  1065. }
  1066. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1067. globalThreads, localThreads, 0, NULL, NULL);
  1068. if (unlikely(status != CL_SUCCESS)) {
  1069. applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)");
  1070. return 0;
  1071. }
  1072. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1073. BUFFERSIZE, thrdata->res, 0, NULL, NULL);
  1074. if (unlikely(status != CL_SUCCESS)) {
  1075. applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)");
  1076. return 0;
  1077. }
  1078. /* The amount of work scanned can fluctuate when intensity changes
  1079. * and since we do this one cycle behind, we increment the work more
  1080. * than enough to prevent repeating work */
  1081. work->blk.nonce += gpu->max_hashes;
  1082. return hashes;
  1083. }
  1084. static void opencl_thread_shutdown(struct thr_info *thr)
  1085. {
  1086. const int thr_id = thr->id;
  1087. _clState *clState = clStates[thr_id];
  1088. clReleaseCommandQueue(clState->commandQueue);
  1089. clReleaseKernel(clState->kernel);
  1090. clReleaseProgram(clState->program);
  1091. clReleaseContext(clState->context);
  1092. }
  1093. struct device_api opencl_api = {
  1094. .name = "GPU",
  1095. .api_detect = opencl_detect,
  1096. .reinit_device = reinit_opencl_device,
  1097. #ifdef HAVE_ADL
  1098. .get_statline_before = get_opencl_statline_before,
  1099. #endif
  1100. .get_statline = get_opencl_statline,
  1101. .thread_prepare = opencl_thread_prepare,
  1102. .thread_init = opencl_thread_init,
  1103. .free_work = opencl_free_work,
  1104. .prepare_work = opencl_prepare_work,
  1105. .scanhash = opencl_scanhash,
  1106. .thread_shutdown = opencl_thread_shutdown,
  1107. };
  1108. #endif