device-gpu.c 33 KB

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