device-gpu.c 33 KB

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