device-gpu.c 31 KB

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