device-gpu.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  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,
  668. __maybe_unused cl_uint threads)
  669. {
  670. cl_kernel *kernel = &clState->kernel;
  671. cl_uint vwidth = clState->vwidth;
  672. unsigned int i, num = 0;
  673. cl_int status = 0;
  674. uint *nonces;
  675. nonces = alloca(sizeof(uint) * vwidth);
  676. for (i = 0; i < vwidth; i++)
  677. nonces[i] = blk->nonce + i;
  678. CL_SET_VARG(vwidth, nonces);
  679. CL_SET_BLKARG(PreVal0);
  680. CL_SET_BLKARG(PreVal4_2);
  681. CL_SET_BLKARG(cty_h);
  682. CL_SET_BLKARG(D1A);
  683. CL_SET_BLKARG(cty_b);
  684. CL_SET_BLKARG(cty_c);
  685. CL_SET_BLKARG(cty_f);
  686. CL_SET_BLKARG(cty_g);
  687. CL_SET_BLKARG(C1addK5);
  688. CL_SET_BLKARG(B1addK6);
  689. CL_SET_BLKARG(PreVal0addK7);
  690. CL_SET_BLKARG(W16addK16);
  691. CL_SET_BLKARG(W17addK17);
  692. CL_SET_BLKARG(PreW18);
  693. CL_SET_BLKARG(PreW19);
  694. CL_SET_BLKARG(W16);
  695. CL_SET_BLKARG(W17);
  696. CL_SET_BLKARG(PreW31);
  697. CL_SET_BLKARG(PreW32);
  698. CL_SET_BLKARG(ctx_a);
  699. CL_SET_BLKARG(ctx_b);
  700. CL_SET_BLKARG(ctx_c);
  701. CL_SET_BLKARG(ctx_d);
  702. CL_SET_BLKARG(ctx_e);
  703. CL_SET_BLKARG(ctx_f);
  704. CL_SET_BLKARG(ctx_g);
  705. CL_SET_BLKARG(ctx_h);
  706. CL_SET_BLKARG(zeroA);
  707. CL_SET_BLKARG(zeroB);
  708. CL_SET_BLKARG(oneA);
  709. CL_SET_BLKARG(twoA);
  710. CL_SET_BLKARG(threeA);
  711. CL_SET_BLKARG(fourA);
  712. CL_SET_BLKARG(fiveA);
  713. CL_SET_BLKARG(sixA);
  714. CL_SET_BLKARG(sevenA);
  715. CL_SET_ARG(clState->outputBuffer);
  716. return status;
  717. }
  718. static cl_int queue_diablo_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  719. {
  720. cl_kernel *kernel = &clState->kernel;
  721. cl_uint vwidth = clState->vwidth;
  722. unsigned int i, num = 0;
  723. cl_int status = 0;
  724. uint *nonces;
  725. nonces = alloca(sizeof(uint) * vwidth);
  726. for (i = 0; i < vwidth; i++)
  727. nonces[i] = blk->nonce + (i * threads);
  728. CL_SET_VARG(vwidth, nonces);
  729. CL_SET_BLKARG(PreVal0);
  730. CL_SET_BLKARG(PreVal0addK7);
  731. CL_SET_BLKARG(PreVal4addT1);
  732. CL_SET_BLKARG(PreW18);
  733. CL_SET_BLKARG(PreW19);
  734. CL_SET_BLKARG(W16);
  735. CL_SET_BLKARG(W17);
  736. CL_SET_BLKARG(W16addK16);
  737. CL_SET_BLKARG(W17addK17);
  738. CL_SET_BLKARG(PreW31);
  739. CL_SET_BLKARG(PreW32);
  740. CL_SET_BLKARG(D1A);
  741. CL_SET_BLKARG(cty_b);
  742. CL_SET_BLKARG(cty_c);
  743. CL_SET_BLKARG(cty_h);
  744. CL_SET_BLKARG(cty_f);
  745. CL_SET_BLKARG(cty_g);
  746. CL_SET_BLKARG(C1addK5);
  747. CL_SET_BLKARG(B1addK6);
  748. CL_SET_BLKARG(ctx_a);
  749. CL_SET_BLKARG(ctx_b);
  750. CL_SET_BLKARG(ctx_c);
  751. CL_SET_BLKARG(ctx_d);
  752. CL_SET_BLKARG(ctx_e);
  753. CL_SET_BLKARG(ctx_f);
  754. CL_SET_BLKARG(ctx_g);
  755. CL_SET_BLKARG(ctx_h);
  756. CL_SET_ARG(clState->outputBuffer);
  757. return status;
  758. }
  759. static void set_threads_hashes(unsigned int vectors, unsigned int *threads,
  760. unsigned int *hashes, size_t *globalThreads,
  761. unsigned int minthreads, int intensity)
  762. {
  763. *threads = 1 << (15 + intensity);
  764. if (*threads < minthreads)
  765. *threads = minthreads;
  766. *globalThreads = *threads;
  767. *hashes = *threads * vectors;
  768. }
  769. #endif /* HAVE_OPENCL */
  770. #ifdef HAVE_OPENCL
  771. /* We have only one thread that ever re-initialises GPUs, thus if any GPU
  772. * init command fails due to a completely wedged GPU, the thread will never
  773. * return, unable to harm other GPUs. If it does return, it means we only had
  774. * a soft failure and then the reinit_gpu thread is ready to tackle another
  775. * GPU */
  776. void *reinit_gpu(void *userdata)
  777. {
  778. struct thr_info *mythr = userdata;
  779. struct cgpu_info *cgpu;
  780. struct thr_info *thr;
  781. struct timeval now;
  782. char name[256];
  783. int thr_id;
  784. int gpu;
  785. pthread_detach(pthread_self());
  786. select_cgpu:
  787. cgpu = tq_pop(mythr->q, NULL);
  788. if (!cgpu)
  789. goto out;
  790. if (clDevicesNum() != nDevs) {
  791. applog(LOG_WARNING, "Hardware not reporting same number of active devices, will not attempt to restart GPU");
  792. goto out;
  793. }
  794. gpu = cgpu->device_id;
  795. cgpu->deven = DEV_DISABLED;
  796. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  797. thr = &thr_info[thr_id];
  798. cgpu = thr->cgpu;
  799. if (cgpu->api != &opencl_api)
  800. continue;
  801. if (dev_from_id(thr_id) != gpu)
  802. continue;
  803. thr = &thr_info[thr_id];
  804. if (!thr) {
  805. applog(LOG_WARNING, "No reference to thread %d exists", thr_id);
  806. continue;
  807. }
  808. thr->rolling = thr->cgpu->rolling = 0;
  809. /* Reports the last time we tried to revive a sick GPU */
  810. gettimeofday(&thr->sick, NULL);
  811. if (!pthread_cancel(thr->pth)) {
  812. applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
  813. } else
  814. applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
  815. }
  816. cgpu->deven = DEV_ENABLED;
  817. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  818. int virtual_gpu;
  819. thr = &thr_info[thr_id];
  820. cgpu = thr->cgpu;
  821. if (cgpu->api != &opencl_api)
  822. continue;
  823. if (dev_from_id(thr_id) != gpu)
  824. continue;
  825. virtual_gpu = cgpu->virtual_gpu;
  826. /* Lose this ram cause we may get stuck here! */
  827. //tq_freeze(thr->q);
  828. thr->q = tq_new();
  829. if (!thr->q)
  830. quit(1, "Failed to tq_new in reinit_gpu");
  831. /* Lose this ram cause we may dereference in the dying thread! */
  832. //free(clState);
  833. applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
  834. clStates[thr_id] = initCl(virtual_gpu, name, sizeof(name));
  835. if (!clStates[thr_id]) {
  836. applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
  837. goto select_cgpu;
  838. }
  839. applog(LOG_INFO, "initCl() finished. Found %s", name);
  840. if (unlikely(thr_info_create(thr, NULL, miner_thread, thr))) {
  841. applog(LOG_ERR, "thread %d create failed", thr_id);
  842. return NULL;
  843. }
  844. applog(LOG_WARNING, "Thread %d restarted", thr_id);
  845. }
  846. gettimeofday(&now, NULL);
  847. get_datestamp(cgpu->init, &now);
  848. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  849. thr = &thr_info[thr_id];
  850. cgpu = thr->cgpu;
  851. if (cgpu->api != &opencl_api)
  852. continue;
  853. if (dev_from_id(thr_id) != gpu)
  854. continue;
  855. tq_push(thr->q, &ping);
  856. }
  857. goto select_cgpu;
  858. out:
  859. return NULL;
  860. }
  861. #else
  862. void *reinit_gpu(void *userdata)
  863. {
  864. return NULL;
  865. }
  866. #endif
  867. #ifdef HAVE_OPENCL
  868. struct device_api opencl_api;
  869. static void opencl_detect()
  870. {
  871. int i;
  872. nDevs = clDevicesNum();
  873. if (nDevs < 0) {
  874. applog(LOG_ERR, "clDevicesNum returned error, no GPUs usable");
  875. nDevs = 0;
  876. }
  877. if (MAX_DEVICES - total_devices < nDevs)
  878. nDevs = MAX_DEVICES - total_devices;
  879. if (!nDevs)
  880. return;
  881. if (opt_kernel) {
  882. if (strcmp(opt_kernel, "poclbm") &&
  883. strcmp(opt_kernel, "phatk") &&
  884. strcmp(opt_kernel, "diakgcn") &&
  885. strcmp(opt_kernel, "diablo"))
  886. quit(1, "Invalid kernel name specified - must be diablo, poclbm, phatk or diakgcn");
  887. if (!strcmp(opt_kernel, "diakgcn"))
  888. chosen_kernel = KL_DIAKGCN;
  889. else if (!strcmp(opt_kernel, "poclbm"))
  890. chosen_kernel = KL_POCLBM;
  891. else if (!strcmp(opt_kernel, "diablo"))
  892. chosen_kernel = KL_DIABLO;
  893. else
  894. chosen_kernel = KL_PHATK;
  895. } else
  896. chosen_kernel = KL_NONE;
  897. for (i = 0; i < nDevs; ++i) {
  898. struct cgpu_info *cgpu;
  899. cgpu = devices[total_devices++] = &gpus[i];
  900. cgpu->deven = DEV_ENABLED;
  901. cgpu->api = &opencl_api;
  902. cgpu->device_id = i;
  903. cgpu->threads = opt_g_threads;
  904. cgpu->virtual_gpu = i;
  905. }
  906. if (!opt_noadl)
  907. init_adl(nDevs);
  908. }
  909. static void reinit_opencl_device(struct cgpu_info *gpu)
  910. {
  911. tq_push(thr_info[gpur_thr_id].q, gpu);
  912. }
  913. #ifdef HAVE_ADL
  914. static void get_opencl_statline_before(char *buf, struct cgpu_info *gpu)
  915. {
  916. if (gpu->has_adl) {
  917. int gpuid = gpu->device_id;
  918. float gt = gpu_temp(gpuid);
  919. int gf = gpu_fanspeed(gpuid);
  920. int gp;
  921. if (gt != -1)
  922. tailsprintf(buf, "%5.1fC ", gt);
  923. else
  924. tailsprintf(buf, " ", gt);
  925. if (gf != -1)
  926. tailsprintf(buf, "%4dRPM ", gf);
  927. else if ((gp = gpu_fanpercent(gpuid)) != -1)
  928. tailsprintf(buf, "%3d%% ", gp);
  929. else
  930. tailsprintf(buf, " ");
  931. tailsprintf(buf, "| ");
  932. }
  933. }
  934. #endif
  935. static void get_opencl_statline(char *buf, struct cgpu_info *gpu)
  936. {
  937. tailsprintf(buf, " I:%2d", gpu->intensity);
  938. }
  939. struct opencl_thread_data {
  940. cl_int (*queue_kernel_parameters)(_clState *, dev_blk_ctx *, cl_uint);
  941. uint32_t *res;
  942. struct work *last_work;
  943. struct work _last_work;
  944. };
  945. static uint32_t *blank_res;
  946. static bool opencl_thread_prepare(struct thr_info *thr)
  947. {
  948. char name[256];
  949. struct timeval now;
  950. struct cgpu_info *cgpu = thr->cgpu;
  951. int gpu = cgpu->device_id;
  952. int virtual_gpu = cgpu->virtual_gpu;
  953. int i = thr->id;
  954. static bool failmessage = false;
  955. if (!blank_res)
  956. blank_res = calloc(BUFFERSIZE, 1);
  957. if (!blank_res) {
  958. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  959. return false;
  960. }
  961. applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
  962. clStates[i] = initCl(virtual_gpu, name, sizeof(name));
  963. if (!clStates[i]) {
  964. if (use_curses)
  965. enable_curses();
  966. applog(LOG_ERR, "Failed to init GPU thread %d, disabling device %d", i, gpu);
  967. if (!failmessage) {
  968. char *buf;
  969. applog(LOG_ERR, "Restarting the GPU from the menu will not fix this.");
  970. applog(LOG_ERR, "Try restarting cgminer.");
  971. failmessage = true;
  972. if (use_curses) {
  973. buf = curses_input("Press enter to continue");
  974. if (buf)
  975. free(buf);
  976. }
  977. }
  978. cgpu->deven = DEV_DISABLED;
  979. cgpu->status = LIFE_NOSTART;
  980. return false;
  981. }
  982. applog(LOG_INFO, "initCl() finished. Found %s", name);
  983. gettimeofday(&now, NULL);
  984. get_datestamp(cgpu->init, &now);
  985. have_opencl = true;
  986. return true;
  987. }
  988. static bool opencl_thread_init(struct thr_info *thr)
  989. {
  990. const int thr_id = thr->id;
  991. struct cgpu_info *gpu = thr->cgpu;
  992. struct opencl_thread_data *thrdata;
  993. _clState *clState = clStates[thr_id];
  994. cl_int status;
  995. thrdata = calloc(1, sizeof(*thrdata));
  996. thr->cgpu_data = thrdata;
  997. if (!thrdata) {
  998. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  999. return false;
  1000. }
  1001. switch (clState->chosen_kernel) {
  1002. case KL_POCLBM:
  1003. thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
  1004. break;
  1005. case KL_PHATK:
  1006. thrdata->queue_kernel_parameters = &queue_phatk_kernel;
  1007. break;
  1008. case KL_DIAKGCN:
  1009. thrdata->queue_kernel_parameters = &queue_diakgcn_kernel;
  1010. break;
  1011. default:
  1012. case KL_DIABLO:
  1013. thrdata->queue_kernel_parameters = &queue_diablo_kernel;
  1014. break;
  1015. }
  1016. thrdata->res = calloc(BUFFERSIZE, 1);
  1017. if (!thrdata->res) {
  1018. free(thrdata);
  1019. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1020. return false;
  1021. }
  1022. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  1023. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1024. if (unlikely(status != CL_SUCCESS)) {
  1025. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1026. return false;
  1027. }
  1028. gpu->status = LIFE_WELL;
  1029. return true;
  1030. }
  1031. static void opencl_free_work(struct thr_info *thr, struct work *work)
  1032. {
  1033. const int thr_id = thr->id;
  1034. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1035. _clState *clState = clStates[thr_id];
  1036. clFinish(clState->commandQueue);
  1037. if (thrdata->res[FOUND]) {
  1038. thrdata->last_work = &thrdata->_last_work;
  1039. memcpy(thrdata->last_work, work, sizeof(*thrdata->last_work));
  1040. }
  1041. }
  1042. static bool opencl_prepare_work(struct thr_info __maybe_unused *thr, struct work *work)
  1043. {
  1044. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1045. return true;
  1046. }
  1047. extern int opt_dynamic_interval;
  1048. static uint64_t opencl_scanhash(struct thr_info *thr, struct work *work,
  1049. uint64_t __maybe_unused max_nonce)
  1050. {
  1051. const int thr_id = thr->id;
  1052. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1053. struct cgpu_info *gpu = thr->cgpu;
  1054. _clState *clState = clStates[thr_id];
  1055. const cl_kernel *kernel = &clState->kernel;
  1056. double gpu_ms_average = 7;
  1057. cl_int status;
  1058. size_t globalThreads[1];
  1059. size_t localThreads[1] = { clState->wsize };
  1060. unsigned int threads;
  1061. unsigned int hashes;
  1062. struct timeval tv_gpustart, tv_gpuend, diff;
  1063. suseconds_t gpu_us;
  1064. gettimeofday(&tv_gpustart, NULL);
  1065. timeval_subtract(&diff, &tv_gpustart, &tv_gpuend);
  1066. /* This finish flushes the readbuffer set with CL_FALSE later */
  1067. clFinish(clState->commandQueue);
  1068. gettimeofday(&tv_gpuend, NULL);
  1069. timeval_subtract(&diff, &tv_gpuend, &tv_gpustart);
  1070. gpu_us = diff.tv_sec * 1000000 + diff.tv_usec;
  1071. decay_time(&gpu_ms_average, gpu_us / 1000);
  1072. if (gpu->dynamic) {
  1073. /* Try to not let the GPU be out for longer than 6ms, but
  1074. * increase intensity when the system is idle, unless
  1075. * dynamic is disabled. */
  1076. if (gpu_ms_average > opt_dynamic_interval) {
  1077. if (gpu->intensity > MIN_INTENSITY)
  1078. --gpu->intensity;
  1079. } else if (gpu_ms_average < ((opt_dynamic_interval / 2) ? : 1)) {
  1080. if (gpu->intensity < MAX_INTENSITY)
  1081. ++gpu->intensity;
  1082. }
  1083. }
  1084. set_threads_hashes(clState->vwidth, &threads, &hashes, globalThreads,
  1085. localThreads[0], gpu->intensity);
  1086. if (hashes > gpu->max_hashes)
  1087. gpu->max_hashes = hashes;
  1088. status = thrdata->queue_kernel_parameters(clState, &work->blk, globalThreads[0]);
  1089. if (unlikely(status != CL_SUCCESS)) {
  1090. applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
  1091. return 0;
  1092. }
  1093. /* MAXBUFFERS entry is used as a flag to say nonces exist */
  1094. if (thrdata->res[FOUND]) {
  1095. /* Clear the buffer again */
  1096. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1097. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1098. if (unlikely(status != CL_SUCCESS)) {
  1099. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1100. return 0;
  1101. }
  1102. if (unlikely(thrdata->last_work)) {
  1103. applog(LOG_DEBUG, "GPU %d found something in last work?", gpu->device_id);
  1104. postcalc_hash_async(thr, thrdata->last_work, thrdata->res);
  1105. thrdata->last_work = NULL;
  1106. } else {
  1107. applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
  1108. postcalc_hash_async(thr, work, thrdata->res);
  1109. }
  1110. memset(thrdata->res, 0, BUFFERSIZE);
  1111. clFinish(clState->commandQueue);
  1112. }
  1113. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1114. globalThreads, localThreads, 0, NULL, NULL);
  1115. if (unlikely(status != CL_SUCCESS)) {
  1116. applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)");
  1117. return 0;
  1118. }
  1119. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1120. BUFFERSIZE, thrdata->res, 0, NULL, NULL);
  1121. if (unlikely(status != CL_SUCCESS)) {
  1122. applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)");
  1123. return 0;
  1124. }
  1125. /* The amount of work scanned can fluctuate when intensity changes
  1126. * and since we do this one cycle behind, we increment the work more
  1127. * than enough to prevent repeating work */
  1128. work->blk.nonce += gpu->max_hashes;
  1129. return hashes;
  1130. }
  1131. static void opencl_thread_shutdown(struct thr_info *thr)
  1132. {
  1133. const int thr_id = thr->id;
  1134. _clState *clState = clStates[thr_id];
  1135. clReleaseCommandQueue(clState->commandQueue);
  1136. clReleaseKernel(clState->kernel);
  1137. clReleaseProgram(clState->program);
  1138. clReleaseContext(clState->context);
  1139. }
  1140. struct device_api opencl_api = {
  1141. .name = "GPU",
  1142. .api_detect = opencl_detect,
  1143. .reinit_device = reinit_opencl_device,
  1144. #ifdef HAVE_ADL
  1145. .get_statline_before = get_opencl_statline_before,
  1146. #endif
  1147. .get_statline = get_opencl_statline,
  1148. .thread_prepare = opencl_thread_prepare,
  1149. .thread_init = opencl_thread_init,
  1150. .free_work = opencl_free_work,
  1151. .prepare_work = opencl_prepare_work,
  1152. .scanhash = opencl_scanhash,
  1153. .thread_shutdown = opencl_thread_shutdown,
  1154. };
  1155. #endif