device-gpu.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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 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. enable_curses();
  837. applog(LOG_ERR, "Failed to init GPU thread %d, disabling device %d", i, gpu);
  838. if (!failmessage) {
  839. char *buf;
  840. applog(LOG_ERR, "Restarting the GPU from the menu will not fix this.");
  841. applog(LOG_ERR, "Try restarting cgminer.");
  842. failmessage = true;
  843. buf = curses_input("Press enter to continue");
  844. if (buf)
  845. free(buf);
  846. }
  847. cgpu->enabled = false;
  848. cgpu->status = LIFE_NOSTART;
  849. return false;
  850. }
  851. applog(LOG_INFO, "initCl() finished. Found %s", name);
  852. gettimeofday(&now, NULL);
  853. get_datestamp(cgpu->init, &now);
  854. have_opencl = true;
  855. return true;
  856. }
  857. static bool opencl_thread_init(struct thr_info *thr)
  858. {
  859. const int thr_id = thr->id;
  860. struct cgpu_info *gpu = thr->cgpu;
  861. struct opencl_thread_data *thrdata;
  862. thrdata = calloc(1, sizeof(*thrdata));
  863. thr->cgpu_data = thrdata;
  864. if (!thrdata) {
  865. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  866. return false;
  867. }
  868. switch (chosen_kernel) {
  869. case KL_POCLBM:
  870. thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
  871. break;
  872. case KL_PHATK:
  873. default:
  874. thrdata->queue_kernel_parameters = &queue_phatk_kernel;
  875. break;
  876. }
  877. thrdata->res = calloc(BUFFERSIZE, 1);
  878. if (!thrdata->res) {
  879. free(thrdata);
  880. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  881. return false;
  882. }
  883. _clState *clState = clStates[thr_id];
  884. cl_int status;
  885. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  886. BUFFERSIZE, blank_res, 0, NULL, NULL);
  887. if (unlikely(status != CL_SUCCESS)) {
  888. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  889. return false;
  890. }
  891. gpu->status = LIFE_WELL;
  892. return true;
  893. }
  894. static void opencl_free_work(struct thr_info *thr, struct work *work)
  895. {
  896. const int thr_id = thr->id;
  897. struct opencl_thread_data *thrdata = thr->cgpu_data;
  898. _clState *clState = clStates[thr_id];
  899. clFinish(clState->commandQueue);
  900. if (thrdata->res[FOUND]) {
  901. thrdata->last_work = &thrdata->_last_work;
  902. memcpy(thrdata->last_work, work, sizeof(*thrdata->last_work));
  903. }
  904. }
  905. static bool opencl_prepare_work(struct thr_info *thr, struct work *work)
  906. {
  907. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  908. return true;
  909. }
  910. static uint64_t opencl_scanhash(struct thr_info *thr, struct work *work, uint64_t max_nonce)
  911. {
  912. const int thr_id = thr->id;
  913. struct opencl_thread_data *thrdata = thr->cgpu_data;
  914. struct cgpu_info *gpu = thr->cgpu;
  915. _clState *clState = clStates[thr_id];
  916. const cl_kernel *kernel = &clState->kernel;
  917. double gpu_ms_average = 7;
  918. cl_int status;
  919. size_t globalThreads[1];
  920. size_t localThreads[1] = { clState->work_size };
  921. unsigned int threads;
  922. unsigned int hashes;
  923. struct timeval tv_gpustart, tv_gpuend, diff;
  924. suseconds_t gpu_us;
  925. gettimeofday(&tv_gpustart, NULL);
  926. timeval_subtract(&diff, &tv_gpustart, &tv_gpuend);
  927. /* This finish flushes the readbuffer set with CL_FALSE later */
  928. clFinish(clState->commandQueue);
  929. gettimeofday(&tv_gpuend, NULL);
  930. timeval_subtract(&diff, &tv_gpuend, &tv_gpustart);
  931. gpu_us = diff.tv_sec * 1000000 + diff.tv_usec;
  932. decay_time(&gpu_ms_average, gpu_us / 1000);
  933. if (gpu->dynamic) {
  934. /* Try to not let the GPU be out for longer than 6ms, but
  935. * increase intensity when the system is idle, unless
  936. * dynamic is disabled. */
  937. if (gpu_ms_average > 7) {
  938. if (gpu->intensity > MIN_INTENSITY)
  939. --gpu->intensity;
  940. } else if (gpu_ms_average < 3) {
  941. if (gpu->intensity < MAX_INTENSITY)
  942. ++gpu->intensity;
  943. }
  944. }
  945. set_threads_hashes(clState->preferred_vwidth, &threads, &hashes, globalThreads,
  946. localThreads[0], gpu->intensity);
  947. status = thrdata->queue_kernel_parameters(clState, &work->blk);
  948. if (unlikely(status != CL_SUCCESS)) {
  949. applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
  950. return 0;
  951. }
  952. /* MAXBUFFERS entry is used as a flag to say nonces exist */
  953. if (thrdata->res[FOUND]) {
  954. /* Clear the buffer again */
  955. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  956. BUFFERSIZE, blank_res, 0, NULL, NULL);
  957. if (unlikely(status != CL_SUCCESS)) {
  958. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  959. return 0;
  960. }
  961. if (unlikely(thrdata->last_work)) {
  962. if (opt_debug)
  963. applog(LOG_DEBUG, "GPU %d found something in last work?", gpu->device_id);
  964. postcalc_hash_async(thr, thrdata->last_work, thrdata->res);
  965. thrdata->last_work = NULL;
  966. } else {
  967. if (opt_debug)
  968. applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
  969. postcalc_hash_async(thr, work, thrdata->res);
  970. }
  971. memset(thrdata->res, 0, BUFFERSIZE);
  972. clFinish(clState->commandQueue);
  973. }
  974. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  975. globalThreads, localThreads, 0, NULL, NULL);
  976. if (unlikely(status != CL_SUCCESS)) {
  977. applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)");
  978. return 0;
  979. }
  980. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  981. BUFFERSIZE, thrdata->res, 0, NULL, NULL);
  982. if (unlikely(status != CL_SUCCESS)) {
  983. applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)");
  984. return 0;
  985. }
  986. work->blk.nonce += hashes;
  987. return hashes;
  988. }
  989. static void opencl_thread_shutdown(struct thr_info *thr)
  990. {
  991. const int thr_id = thr->id;
  992. _clState *clState = clStates[thr_id];
  993. clReleaseCommandQueue(clState->commandQueue);
  994. clReleaseKernel(clState->kernel);
  995. clReleaseProgram(clState->program);
  996. clReleaseContext(clState->context);
  997. }
  998. struct device_api opencl_api = {
  999. .name = "GPU",
  1000. .api_detect = opencl_detect,
  1001. .reinit_device = reinit_opencl_device,
  1002. #ifdef HAVE_ADL
  1003. .get_statline_before = get_opencl_statline_before,
  1004. #endif
  1005. .get_statline = get_opencl_statline,
  1006. .thread_prepare = opencl_thread_prepare,
  1007. .thread_init = opencl_thread_init,
  1008. .free_work = opencl_free_work,
  1009. .prepare_work = opencl_prepare_work,
  1010. .scanhash = opencl_scanhash,
  1011. .thread_shutdown = opencl_thread_shutdown,
  1012. };
  1013. #endif