device-gpu.c 30 KB

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