device-gpu.c 30 KB

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