driver-opencl.c 34 KB

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