driver-opencl.c 35 KB

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