driver-opencl.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  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. cl_uint vwidth = clState->vwidth;
  633. unsigned int i, num = 0;
  634. cl_int status = 0;
  635. uint *nonces;
  636. CL_SET_BLKARG(ctx_a);
  637. CL_SET_BLKARG(ctx_b);
  638. CL_SET_BLKARG(ctx_c);
  639. CL_SET_BLKARG(ctx_d);
  640. CL_SET_BLKARG(ctx_e);
  641. CL_SET_BLKARG(ctx_f);
  642. CL_SET_BLKARG(ctx_g);
  643. CL_SET_BLKARG(ctx_h);
  644. CL_SET_BLKARG(cty_b);
  645. CL_SET_BLKARG(cty_c);
  646. CL_SET_BLKARG(cty_f);
  647. CL_SET_BLKARG(cty_g);
  648. CL_SET_BLKARG(cty_h);
  649. nonces = alloca(sizeof(uint) * vwidth);
  650. for (i = 0; i < vwidth; i++)
  651. nonces[i] = blk->nonce + (i * threads);
  652. CL_SET_VARG(vwidth, nonces);
  653. CL_SET_BLKARG(fW0);
  654. CL_SET_BLKARG(fW1);
  655. CL_SET_BLKARG(fW2);
  656. CL_SET_BLKARG(fW3);
  657. CL_SET_BLKARG(fW15);
  658. CL_SET_BLKARG(fW01r);
  659. CL_SET_BLKARG(fcty_e2);
  660. CL_SET_BLKARG(D1A);
  661. CL_SET_BLKARG(C1addK5);
  662. CL_SET_BLKARG(B1addK6);
  663. CL_SET_BLKARG(W16addK16);
  664. CL_SET_BLKARG(W17addK17);
  665. CL_SET_BLKARG(PreVal4addT1);
  666. CL_SET_BLKARG(PreVal0);
  667. CL_SET_ARG(clState->outputBuffer);
  668. return status;
  669. }
  670. static cl_int queue_phatk_kernel(_clState *clState, dev_blk_ctx *blk,
  671. __maybe_unused cl_uint threads)
  672. {
  673. cl_kernel *kernel = &clState->kernel;
  674. cl_uint vwidth = clState->vwidth;
  675. unsigned int i, num = 0;
  676. cl_int status = 0;
  677. uint *nonces;
  678. CL_SET_BLKARG(ctx_a);
  679. CL_SET_BLKARG(ctx_b);
  680. CL_SET_BLKARG(ctx_c);
  681. CL_SET_BLKARG(ctx_d);
  682. CL_SET_BLKARG(ctx_e);
  683. CL_SET_BLKARG(ctx_f);
  684. CL_SET_BLKARG(ctx_g);
  685. CL_SET_BLKARG(ctx_h);
  686. CL_SET_BLKARG(cty_b);
  687. CL_SET_BLKARG(cty_c);
  688. CL_SET_BLKARG(cty_d);
  689. CL_SET_BLKARG(cty_f);
  690. CL_SET_BLKARG(cty_g);
  691. CL_SET_BLKARG(cty_h);
  692. nonces = alloca(sizeof(uint) * vwidth);
  693. for (i = 0; i < vwidth; i++)
  694. nonces[i] = blk->nonce + i;
  695. CL_SET_VARG(vwidth, nonces);
  696. CL_SET_BLKARG(W16);
  697. CL_SET_BLKARG(W17);
  698. CL_SET_BLKARG(PreVal4_2);
  699. CL_SET_BLKARG(PreVal0);
  700. CL_SET_BLKARG(PreW18);
  701. CL_SET_BLKARG(PreW19);
  702. CL_SET_BLKARG(PreW31);
  703. CL_SET_BLKARG(PreW32);
  704. CL_SET_ARG(clState->outputBuffer);
  705. return status;
  706. }
  707. static cl_int queue_diakgcn_kernel(_clState *clState, dev_blk_ctx *blk,
  708. __maybe_unused cl_uint threads)
  709. {
  710. cl_kernel *kernel = &clState->kernel;
  711. cl_uint vwidth = clState->vwidth;
  712. unsigned int i, num = 0;
  713. cl_int status = 0;
  714. uint *nonces;
  715. nonces = alloca(sizeof(uint) * vwidth);
  716. for (i = 0; i < vwidth; i++)
  717. nonces[i] = blk->nonce + i;
  718. CL_SET_VARG(vwidth, nonces);
  719. CL_SET_BLKARG(PreVal0);
  720. CL_SET_BLKARG(PreVal4_2);
  721. CL_SET_BLKARG(cty_h);
  722. CL_SET_BLKARG(D1A);
  723. CL_SET_BLKARG(cty_b);
  724. CL_SET_BLKARG(cty_c);
  725. CL_SET_BLKARG(cty_f);
  726. CL_SET_BLKARG(cty_g);
  727. CL_SET_BLKARG(C1addK5);
  728. CL_SET_BLKARG(B1addK6);
  729. CL_SET_BLKARG(PreVal0addK7);
  730. CL_SET_BLKARG(W16addK16);
  731. CL_SET_BLKARG(W17addK17);
  732. CL_SET_BLKARG(PreW18);
  733. CL_SET_BLKARG(PreW19);
  734. CL_SET_BLKARG(W16);
  735. CL_SET_BLKARG(W17);
  736. CL_SET_BLKARG(PreW31);
  737. CL_SET_BLKARG(PreW32);
  738. CL_SET_BLKARG(ctx_a);
  739. CL_SET_BLKARG(ctx_b);
  740. CL_SET_BLKARG(ctx_c);
  741. CL_SET_BLKARG(ctx_d);
  742. CL_SET_BLKARG(ctx_e);
  743. CL_SET_BLKARG(ctx_f);
  744. CL_SET_BLKARG(ctx_g);
  745. CL_SET_BLKARG(ctx_h);
  746. CL_SET_BLKARG(zeroA);
  747. CL_SET_BLKARG(zeroB);
  748. CL_SET_BLKARG(oneA);
  749. CL_SET_BLKARG(twoA);
  750. CL_SET_BLKARG(threeA);
  751. CL_SET_BLKARG(fourA);
  752. CL_SET_BLKARG(fiveA);
  753. CL_SET_BLKARG(sixA);
  754. CL_SET_BLKARG(sevenA);
  755. CL_SET_ARG(clState->outputBuffer);
  756. return status;
  757. }
  758. static cl_int queue_diablo_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  759. {
  760. cl_kernel *kernel = &clState->kernel;
  761. cl_uint vwidth = clState->vwidth;
  762. unsigned int i, num = 0;
  763. cl_int status = 0;
  764. uint *nonces;
  765. nonces = alloca(sizeof(uint) * vwidth);
  766. for (i = 0; i < vwidth; i++)
  767. nonces[i] = blk->nonce + (i * threads);
  768. CL_SET_VARG(vwidth, nonces);
  769. CL_SET_BLKARG(PreVal0);
  770. CL_SET_BLKARG(PreVal0addK7);
  771. CL_SET_BLKARG(PreVal4addT1);
  772. CL_SET_BLKARG(PreW18);
  773. CL_SET_BLKARG(PreW19);
  774. CL_SET_BLKARG(W16);
  775. CL_SET_BLKARG(W17);
  776. CL_SET_BLKARG(W16addK16);
  777. CL_SET_BLKARG(W17addK17);
  778. CL_SET_BLKARG(PreW31);
  779. CL_SET_BLKARG(PreW32);
  780. CL_SET_BLKARG(D1A);
  781. CL_SET_BLKARG(cty_b);
  782. CL_SET_BLKARG(cty_c);
  783. CL_SET_BLKARG(cty_h);
  784. CL_SET_BLKARG(cty_f);
  785. CL_SET_BLKARG(cty_g);
  786. CL_SET_BLKARG(C1addK5);
  787. CL_SET_BLKARG(B1addK6);
  788. CL_SET_BLKARG(ctx_a);
  789. CL_SET_BLKARG(ctx_b);
  790. CL_SET_BLKARG(ctx_c);
  791. CL_SET_BLKARG(ctx_d);
  792. CL_SET_BLKARG(ctx_e);
  793. CL_SET_BLKARG(ctx_f);
  794. CL_SET_BLKARG(ctx_g);
  795. CL_SET_BLKARG(ctx_h);
  796. CL_SET_ARG(clState->outputBuffer);
  797. return status;
  798. }
  799. static void set_threads_hashes(unsigned int vectors, unsigned int *threads,
  800. unsigned int *hashes, size_t *globalThreads,
  801. unsigned int minthreads, int intensity)
  802. {
  803. *threads = 1 << (15 + intensity);
  804. if (*threads < minthreads)
  805. *threads = minthreads;
  806. *globalThreads = *threads;
  807. *hashes = *threads * vectors;
  808. }
  809. #endif /* HAVE_OPENCL */
  810. #ifdef HAVE_OPENCL
  811. /* We have only one thread that ever re-initialises GPUs, thus if any GPU
  812. * init command fails due to a completely wedged GPU, the thread will never
  813. * return, unable to harm other GPUs. If it does return, it means we only had
  814. * a soft failure and then the reinit_gpu thread is ready to tackle another
  815. * GPU */
  816. void *reinit_gpu(void *userdata)
  817. {
  818. struct thr_info *mythr = userdata;
  819. struct cgpu_info *cgpu;
  820. struct thr_info *thr;
  821. struct timeval now;
  822. char name[256];
  823. int thr_id;
  824. int gpu;
  825. pthread_detach(pthread_self());
  826. select_cgpu:
  827. cgpu = tq_pop(mythr->q, NULL);
  828. if (!cgpu)
  829. goto out;
  830. if (clDevicesNum() != nDevs) {
  831. applog(LOG_WARNING, "Hardware not reporting same number of active devices, will not attempt to restart GPU");
  832. goto out;
  833. }
  834. gpu = cgpu->device_id;
  835. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  836. thr = &thr_info[thr_id];
  837. cgpu = thr->cgpu;
  838. if (cgpu->api != &opencl_api)
  839. continue;
  840. if (dev_from_id(thr_id) != gpu)
  841. continue;
  842. thr = &thr_info[thr_id];
  843. if (!thr) {
  844. applog(LOG_WARNING, "No reference to thread %d exists", thr_id);
  845. continue;
  846. }
  847. thr->rolling = thr->cgpu->rolling = 0;
  848. /* Reports the last time we tried to revive a sick GPU */
  849. gettimeofday(&thr->sick, NULL);
  850. if (!pthread_cancel(thr->pth)) {
  851. applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
  852. } else
  853. applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
  854. }
  855. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  856. int virtual_gpu;
  857. thr = &thr_info[thr_id];
  858. cgpu = thr->cgpu;
  859. if (cgpu->api != &opencl_api)
  860. continue;
  861. if (dev_from_id(thr_id) != gpu)
  862. continue;
  863. virtual_gpu = cgpu->virtual_gpu;
  864. /* Lose this ram cause we may get stuck here! */
  865. //tq_freeze(thr->q);
  866. thr->q = tq_new();
  867. if (!thr->q)
  868. quit(1, "Failed to tq_new in reinit_gpu");
  869. /* Lose this ram cause we may dereference in the dying thread! */
  870. //free(clState);
  871. applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
  872. clStates[thr_id] = initCl(virtual_gpu, name, sizeof(name));
  873. if (!clStates[thr_id]) {
  874. applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
  875. goto select_cgpu;
  876. }
  877. applog(LOG_INFO, "initCl() finished. Found %s", name);
  878. if (unlikely(thr_info_create(thr, NULL, miner_thread, thr))) {
  879. applog(LOG_ERR, "thread %d create failed", thr_id);
  880. return NULL;
  881. }
  882. applog(LOG_WARNING, "Thread %d restarted", thr_id);
  883. }
  884. gettimeofday(&now, NULL);
  885. get_datestamp(cgpu->init, &now);
  886. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  887. thr = &thr_info[thr_id];
  888. cgpu = thr->cgpu;
  889. if (cgpu->api != &opencl_api)
  890. continue;
  891. if (dev_from_id(thr_id) != gpu)
  892. continue;
  893. tq_push(thr->q, &ping);
  894. }
  895. goto select_cgpu;
  896. out:
  897. return NULL;
  898. }
  899. #else
  900. void *reinit_gpu(void *userdata)
  901. {
  902. return NULL;
  903. }
  904. #endif
  905. #ifdef HAVE_OPENCL
  906. struct device_api opencl_api;
  907. static void opencl_detect()
  908. {
  909. int i;
  910. nDevs = clDevicesNum();
  911. if (nDevs < 0) {
  912. applog(LOG_ERR, "clDevicesNum returned error, no GPUs usable");
  913. nDevs = 0;
  914. }
  915. if (MAX_DEVICES - total_devices < nDevs)
  916. nDevs = MAX_DEVICES - total_devices;
  917. if (!nDevs)
  918. return;
  919. for (i = 0; i < nDevs; ++i) {
  920. struct cgpu_info *cgpu;
  921. cgpu = devices[total_devices++] = &gpus[i];
  922. cgpu->deven = DEV_ENABLED;
  923. cgpu->api = &opencl_api;
  924. cgpu->device_id = i;
  925. cgpu->threads = opt_g_threads;
  926. cgpu->virtual_gpu = i;
  927. }
  928. if (!opt_noadl)
  929. init_adl(nDevs);
  930. }
  931. static void reinit_opencl_device(struct cgpu_info *gpu)
  932. {
  933. tq_push(thr_info[gpur_thr_id].q, gpu);
  934. }
  935. #ifdef HAVE_ADL
  936. static void get_opencl_statline_before(char *buf, struct cgpu_info *gpu)
  937. {
  938. if (gpu->has_adl) {
  939. int gpuid = gpu->device_id;
  940. float gt = gpu_temp(gpuid);
  941. int gf = gpu_fanspeed(gpuid);
  942. int gp;
  943. if (gt != -1)
  944. tailsprintf(buf, "%5.1fC ", gt);
  945. else
  946. tailsprintf(buf, " ", gt);
  947. if (gf != -1)
  948. tailsprintf(buf, "%4dRPM ", gf);
  949. else if ((gp = gpu_fanpercent(gpuid)) != -1)
  950. tailsprintf(buf, "%3d%% ", gp);
  951. else
  952. tailsprintf(buf, " ");
  953. tailsprintf(buf, "| ");
  954. }
  955. }
  956. #endif
  957. static void get_opencl_statline(char *buf, struct cgpu_info *gpu)
  958. {
  959. tailsprintf(buf, " I:%2d", gpu->intensity);
  960. }
  961. struct opencl_thread_data {
  962. cl_int (*queue_kernel_parameters)(_clState *, dev_blk_ctx *, cl_uint);
  963. uint32_t *res;
  964. struct work *last_work;
  965. struct work _last_work;
  966. };
  967. static uint32_t *blank_res;
  968. static bool opencl_thread_prepare(struct thr_info *thr)
  969. {
  970. char name[256];
  971. struct timeval now;
  972. struct cgpu_info *cgpu = thr->cgpu;
  973. int gpu = cgpu->device_id;
  974. int virtual_gpu = cgpu->virtual_gpu;
  975. int i = thr->id;
  976. static bool failmessage = false;
  977. if (!blank_res)
  978. blank_res = calloc(BUFFERSIZE, 1);
  979. if (!blank_res) {
  980. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  981. return false;
  982. }
  983. applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
  984. clStates[i] = initCl(virtual_gpu, name, sizeof(name));
  985. if (!clStates[i]) {
  986. if (use_curses)
  987. enable_curses();
  988. applog(LOG_ERR, "Failed to init GPU thread %d, disabling device %d", i, gpu);
  989. if (!failmessage) {
  990. char *buf;
  991. applog(LOG_ERR, "Restarting the GPU from the menu will not fix this.");
  992. applog(LOG_ERR, "Try restarting cgminer.");
  993. failmessage = true;
  994. #ifdef HAVE_CURSES
  995. if (use_curses) {
  996. buf = curses_input("Press enter to continue");
  997. if (buf)
  998. free(buf);
  999. }
  1000. #endif
  1001. }
  1002. cgpu->deven = DEV_DISABLED;
  1003. cgpu->status = LIFE_NOSTART;
  1004. return false;
  1005. }
  1006. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1007. gettimeofday(&now, NULL);
  1008. get_datestamp(cgpu->init, &now);
  1009. have_opencl = true;
  1010. return true;
  1011. }
  1012. static bool opencl_thread_init(struct thr_info *thr)
  1013. {
  1014. const int thr_id = thr->id;
  1015. struct cgpu_info *gpu = thr->cgpu;
  1016. struct opencl_thread_data *thrdata;
  1017. _clState *clState = clStates[thr_id];
  1018. cl_int status;
  1019. thrdata = calloc(1, sizeof(*thrdata));
  1020. thr->cgpu_data = thrdata;
  1021. if (!thrdata) {
  1022. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1023. return false;
  1024. }
  1025. switch (clState->chosen_kernel) {
  1026. case KL_POCLBM:
  1027. thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
  1028. break;
  1029. case KL_PHATK:
  1030. thrdata->queue_kernel_parameters = &queue_phatk_kernel;
  1031. break;
  1032. case KL_DIAKGCN:
  1033. thrdata->queue_kernel_parameters = &queue_diakgcn_kernel;
  1034. break;
  1035. default:
  1036. case KL_DIABLO:
  1037. thrdata->queue_kernel_parameters = &queue_diablo_kernel;
  1038. break;
  1039. }
  1040. thrdata->res = calloc(BUFFERSIZE, 1);
  1041. if (!thrdata->res) {
  1042. free(thrdata);
  1043. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1044. return false;
  1045. }
  1046. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  1047. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1048. if (unlikely(status != CL_SUCCESS)) {
  1049. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1050. return false;
  1051. }
  1052. gpu->status = LIFE_WELL;
  1053. return true;
  1054. }
  1055. static void opencl_free_work(struct thr_info *thr, struct work *work)
  1056. {
  1057. const int thr_id = thr->id;
  1058. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1059. _clState *clState = clStates[thr_id];
  1060. clFinish(clState->commandQueue);
  1061. if (thrdata->res[FOUND]) {
  1062. thrdata->last_work = &thrdata->_last_work;
  1063. memcpy(thrdata->last_work, work, sizeof(*thrdata->last_work));
  1064. }
  1065. }
  1066. static bool opencl_prepare_work(struct thr_info __maybe_unused *thr, struct work *work)
  1067. {
  1068. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1069. return true;
  1070. }
  1071. extern int opt_dynamic_interval;
  1072. static uint64_t opencl_scanhash(struct thr_info *thr, struct work *work,
  1073. uint64_t __maybe_unused max_nonce)
  1074. {
  1075. const int thr_id = thr->id;
  1076. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1077. struct cgpu_info *gpu = thr->cgpu;
  1078. _clState *clState = clStates[thr_id];
  1079. const cl_kernel *kernel = &clState->kernel;
  1080. double gpu_ms_average = 7;
  1081. cl_int status;
  1082. size_t globalThreads[1];
  1083. size_t localThreads[1] = { clState->wsize };
  1084. unsigned int threads;
  1085. unsigned int hashes;
  1086. struct timeval tv_gpustart, tv_gpuend, diff;
  1087. suseconds_t gpu_us;
  1088. gettimeofday(&tv_gpustart, NULL);
  1089. timeval_subtract(&diff, &tv_gpustart, &tv_gpuend);
  1090. /* This finish flushes the readbuffer set with CL_FALSE later */
  1091. clFinish(clState->commandQueue);
  1092. gettimeofday(&tv_gpuend, NULL);
  1093. timeval_subtract(&diff, &tv_gpuend, &tv_gpustart);
  1094. gpu_us = diff.tv_sec * 1000000 + diff.tv_usec;
  1095. decay_time(&gpu_ms_average, gpu_us / 1000);
  1096. if (gpu->dynamic) {
  1097. /* Try to not let the GPU be out for longer than 6ms, but
  1098. * increase intensity when the system is idle, unless
  1099. * dynamic is disabled. */
  1100. if (gpu_ms_average > opt_dynamic_interval) {
  1101. if (gpu->intensity > MIN_INTENSITY)
  1102. --gpu->intensity;
  1103. } else if (gpu_ms_average < ((opt_dynamic_interval / 2) ? : 1)) {
  1104. if (gpu->intensity < MAX_INTENSITY)
  1105. ++gpu->intensity;
  1106. }
  1107. }
  1108. set_threads_hashes(clState->vwidth, &threads, &hashes, globalThreads,
  1109. localThreads[0], gpu->intensity);
  1110. if (hashes > gpu->max_hashes)
  1111. gpu->max_hashes = hashes;
  1112. status = thrdata->queue_kernel_parameters(clState, &work->blk, globalThreads[0]);
  1113. if (unlikely(status != CL_SUCCESS)) {
  1114. applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
  1115. return 0;
  1116. }
  1117. /* MAXBUFFERS entry is used as a flag to say nonces exist */
  1118. if (thrdata->res[FOUND]) {
  1119. /* Clear the buffer again */
  1120. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1121. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1122. if (unlikely(status != CL_SUCCESS)) {
  1123. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1124. return 0;
  1125. }
  1126. if (unlikely(thrdata->last_work)) {
  1127. applog(LOG_DEBUG, "GPU %d found something in last work?", gpu->device_id);
  1128. postcalc_hash_async(thr, thrdata->last_work, thrdata->res);
  1129. thrdata->last_work = NULL;
  1130. } else {
  1131. applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
  1132. postcalc_hash_async(thr, work, thrdata->res);
  1133. }
  1134. memset(thrdata->res, 0, BUFFERSIZE);
  1135. clFinish(clState->commandQueue);
  1136. }
  1137. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1138. globalThreads, localThreads, 0, NULL, NULL);
  1139. if (unlikely(status != CL_SUCCESS)) {
  1140. applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)");
  1141. return 0;
  1142. }
  1143. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1144. BUFFERSIZE, thrdata->res, 0, NULL, NULL);
  1145. if (unlikely(status != CL_SUCCESS)) {
  1146. applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)");
  1147. return 0;
  1148. }
  1149. /* The amount of work scanned can fluctuate when intensity changes
  1150. * and since we do this one cycle behind, we increment the work more
  1151. * than enough to prevent repeating work */
  1152. work->blk.nonce += gpu->max_hashes;
  1153. return hashes;
  1154. }
  1155. static void opencl_thread_shutdown(struct thr_info *thr)
  1156. {
  1157. const int thr_id = thr->id;
  1158. _clState *clState = clStates[thr_id];
  1159. clReleaseCommandQueue(clState->commandQueue);
  1160. clReleaseKernel(clState->kernel);
  1161. clReleaseProgram(clState->program);
  1162. clReleaseContext(clState->context);
  1163. }
  1164. struct device_api opencl_api = {
  1165. .dname = "opencl",
  1166. .name = "GPU",
  1167. .api_detect = opencl_detect,
  1168. .reinit_device = reinit_opencl_device,
  1169. #ifdef HAVE_ADL
  1170. .get_statline_before = get_opencl_statline_before,
  1171. #endif
  1172. .get_statline = get_opencl_statline,
  1173. .thread_prepare = opencl_thread_prepare,
  1174. .thread_init = opencl_thread_init,
  1175. .free_work = opencl_free_work,
  1176. .prepare_work = opencl_prepare_work,
  1177. .scanhash = opencl_scanhash,
  1178. .thread_shutdown = opencl_thread_shutdown,
  1179. };
  1180. #endif