driver-opencl.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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, thread_no = 0;
  439. for (i = 0; i < mining_threads; i++) {
  440. struct thr_info *thr = &thr_info[i];
  441. if (thr->cgpu != cgpu)
  442. continue;
  443. if (!thread_no++)
  444. continue;
  445. if (!thr->pause && cgpu->dynamic) {
  446. applog(LOG_WARNING, "Disabling extra threads due to dynamic mode.");
  447. applog(LOG_WARNING, "Tune dynamic intensity with --gpu-dyninterval");
  448. }
  449. thr->pause = cgpu->dynamic;
  450. if (!cgpu->dynamic && cgpu->deven != DEV_DISABLED)
  451. tq_push(thr->q, &ping);
  452. }
  453. }
  454. struct device_api opencl_api;
  455. #endif /* HAVE_OPENCL */
  456. #if defined(HAVE_OPENCL) && defined(HAVE_CURSES)
  457. void manage_gpu(void)
  458. {
  459. struct thr_info *thr;
  460. int selected, gpu, i;
  461. char checkin[40];
  462. char input;
  463. if (!opt_g_threads)
  464. return;
  465. opt_loginput = true;
  466. immedok(logwin, true);
  467. clear_logwin();
  468. retry:
  469. for (gpu = 0; gpu < nDevs; gpu++) {
  470. struct cgpu_info *cgpu = &gpus[gpu];
  471. wlog("GPU %d: %.1f / %.1f Mh/s | A:%d R:%d HW:%d U:%.2f/m I:%d\n",
  472. gpu, cgpu->rolling, cgpu->total_mhashes / total_secs,
  473. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  474. cgpu->utility, cgpu->intensity);
  475. #ifdef HAVE_ADL
  476. if (gpus[gpu].has_adl) {
  477. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  478. float temp = 0, vddc = 0;
  479. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune)) {
  480. char logline[255];
  481. strcpy(logline, ""); // In case it has no data
  482. if (temp != -1)
  483. sprintf(logline, "%.1f C ", temp);
  484. if (fanspeed != -1 || fanpercent != -1) {
  485. tailsprintf(logline, "F: ");
  486. if (fanpercent != -1)
  487. tailsprintf(logline, "%d%% ", fanpercent);
  488. if (fanspeed != -1)
  489. tailsprintf(logline, "(%d RPM) ", fanspeed);
  490. tailsprintf(logline, " ");
  491. }
  492. if (engineclock != -1)
  493. tailsprintf(logline, "E: %d MHz ", engineclock);
  494. if (memclock != -1)
  495. tailsprintf(logline, "M: %d Mhz ", memclock);
  496. if (vddc != -1)
  497. tailsprintf(logline, "V: %.3fV ", vddc);
  498. if (activity != -1)
  499. tailsprintf(logline, "A: %d%% ", activity);
  500. if (powertune != -1)
  501. tailsprintf(logline, "P: %d%%", powertune);
  502. tailsprintf(logline, "\n");
  503. wlog(logline);
  504. }
  505. }
  506. #endif
  507. wlog("Last initialised: %s\n", cgpu->init);
  508. wlog("Intensity: ");
  509. if (gpus[gpu].dynamic)
  510. wlog("Dynamic (only one thread in use)\n");
  511. else
  512. wlog("%d\n", gpus[gpu].intensity);
  513. for (i = 0; i < mining_threads; i++) {
  514. thr = &thr_info[i];
  515. if (thr->cgpu != cgpu)
  516. continue;
  517. get_datestamp(checkin, &thr->last);
  518. wlog("Thread %d: %.1f Mh/s %s ", i, thr->rolling, cgpu->deven != DEV_DISABLED ? "Enabled" : "Disabled");
  519. switch (cgpu->status) {
  520. default:
  521. case LIFE_WELL:
  522. wlog("ALIVE");
  523. break;
  524. case LIFE_SICK:
  525. wlog("SICK reported in %s", checkin);
  526. break;
  527. case LIFE_DEAD:
  528. wlog("DEAD reported in %s", checkin);
  529. break;
  530. case LIFE_NOSTART:
  531. wlog("Never started");
  532. break;
  533. }
  534. if (thr->pause)
  535. wlog(" paused");
  536. wlog("\n");
  537. }
  538. wlog("\n");
  539. }
  540. wlogprint("[E]nable [D]isable [I]ntensity [R]estart GPU %s\n",adl_active ? "[C]hange settings" : "");
  541. wlogprint("Or press any other key to continue\n");
  542. input = getch();
  543. if (nDevs == 1)
  544. selected = 0;
  545. else
  546. selected = -1;
  547. if (!strncasecmp(&input, "e", 1)) {
  548. struct cgpu_info *cgpu;
  549. if (selected)
  550. selected = curses_int("Select GPU to enable");
  551. if (selected < 0 || selected >= nDevs) {
  552. wlogprint("Invalid selection\n");
  553. goto retry;
  554. }
  555. if (gpus[selected].deven != DEV_DISABLED) {
  556. wlogprint("Device already enabled\n");
  557. goto retry;
  558. }
  559. gpus[selected].deven = DEV_ENABLED;
  560. for (i = 0; i < mining_threads; ++i) {
  561. thr = &thr_info[i];
  562. cgpu = thr->cgpu;
  563. if (cgpu->api != &opencl_api)
  564. continue;
  565. if (dev_from_id(i) != selected)
  566. continue;
  567. if (cgpu->status != LIFE_WELL) {
  568. wlogprint("Must restart device before enabling it");
  569. goto retry;
  570. }
  571. applog(LOG_DEBUG, "Pushing ping to thread %d", thr->id);
  572. tq_push(thr->q, &ping);
  573. }
  574. goto retry;
  575. } if (!strncasecmp(&input, "d", 1)) {
  576. if (selected)
  577. selected = curses_int("Select GPU to disable");
  578. if (selected < 0 || selected >= nDevs) {
  579. wlogprint("Invalid selection\n");
  580. goto retry;
  581. }
  582. if (gpus[selected].deven == DEV_DISABLED) {
  583. wlogprint("Device already disabled\n");
  584. goto retry;
  585. }
  586. gpus[selected].deven = DEV_DISABLED;
  587. goto retry;
  588. } else if (!strncasecmp(&input, "i", 1)) {
  589. int intensity;
  590. char *intvar;
  591. if (selected)
  592. selected = curses_int("Select GPU to change intensity on");
  593. if (selected < 0 || selected >= nDevs) {
  594. wlogprint("Invalid selection\n");
  595. goto retry;
  596. }
  597. intvar = curses_input("Set GPU scan intensity (d or " _MIN_INTENSITY_STR " -> " _MAX_INTENSITY_STR ")");
  598. if (!intvar) {
  599. wlogprint("Invalid input\n");
  600. goto retry;
  601. }
  602. if (!strncasecmp(intvar, "d", 1)) {
  603. wlogprint("Dynamic mode enabled on gpu %d\n", selected);
  604. gpus[selected].dynamic = true;
  605. pause_dynamic_threads(selected);
  606. free(intvar);
  607. goto retry;
  608. }
  609. intensity = atoi(intvar);
  610. free(intvar);
  611. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  612. wlogprint("Invalid selection\n");
  613. goto retry;
  614. }
  615. gpus[selected].dynamic = false;
  616. gpus[selected].intensity = intensity;
  617. wlogprint("Intensity on gpu %d set to %d\n", selected, intensity);
  618. pause_dynamic_threads(selected);
  619. goto retry;
  620. } else if (!strncasecmp(&input, "r", 1)) {
  621. if (selected)
  622. selected = curses_int("Select GPU to attempt to restart");
  623. if (selected < 0 || selected >= nDevs) {
  624. wlogprint("Invalid selection\n");
  625. goto retry;
  626. }
  627. wlogprint("Attempting to restart threads of GPU %d\n", selected);
  628. reinit_device(&gpus[selected]);
  629. goto retry;
  630. } else if (adl_active && (!strncasecmp(&input, "c", 1))) {
  631. if (selected)
  632. selected = curses_int("Select GPU to change settings on");
  633. if (selected < 0 || selected >= nDevs) {
  634. wlogprint("Invalid selection\n");
  635. goto retry;
  636. }
  637. change_gpusettings(selected);
  638. goto retry;
  639. } else
  640. clear_logwin();
  641. immedok(logwin, false);
  642. opt_loginput = false;
  643. }
  644. #else
  645. void manage_gpu(void)
  646. {
  647. }
  648. #endif
  649. #ifdef HAVE_OPENCL
  650. static _clState *clStates[MAX_GPUDEVICES];
  651. #define CL_SET_BLKARG(blkvar) status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->blkvar)
  652. #define CL_SET_ARG(var) status |= clSetKernelArg(*kernel, num++, sizeof(var), (void *)&var)
  653. #define CL_SET_VARG(args, var) status |= clSetKernelArg(*kernel, num++, args * sizeof(uint), (void *)var)
  654. static cl_int queue_poclbm_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  655. {
  656. cl_kernel *kernel = &clState->kernel;
  657. unsigned int num = 0;
  658. cl_int status = 0;
  659. CL_SET_BLKARG(ctx_a);
  660. CL_SET_BLKARG(ctx_b);
  661. CL_SET_BLKARG(ctx_c);
  662. CL_SET_BLKARG(ctx_d);
  663. CL_SET_BLKARG(ctx_e);
  664. CL_SET_BLKARG(ctx_f);
  665. CL_SET_BLKARG(ctx_g);
  666. CL_SET_BLKARG(ctx_h);
  667. CL_SET_BLKARG(cty_b);
  668. CL_SET_BLKARG(cty_c);
  669. CL_SET_BLKARG(cty_f);
  670. CL_SET_BLKARG(cty_g);
  671. CL_SET_BLKARG(cty_h);
  672. if (!clState->goffset) {
  673. cl_uint vwidth = clState->vwidth;
  674. uint *nonces = alloca(sizeof(uint) * vwidth);
  675. unsigned int i;
  676. for (i = 0; i < vwidth; i++)
  677. nonces[i] = blk->nonce + (i * threads);
  678. CL_SET_VARG(vwidth, nonces);
  679. }
  680. CL_SET_BLKARG(fW0);
  681. CL_SET_BLKARG(fW1);
  682. CL_SET_BLKARG(fW2);
  683. CL_SET_BLKARG(fW3);
  684. CL_SET_BLKARG(fW15);
  685. CL_SET_BLKARG(fW01r);
  686. CL_SET_BLKARG(D1A);
  687. CL_SET_BLKARG(C1addK5);
  688. CL_SET_BLKARG(B1addK6);
  689. CL_SET_BLKARG(W16addK16);
  690. CL_SET_BLKARG(W17addK17);
  691. CL_SET_BLKARG(PreVal4addT1);
  692. CL_SET_BLKARG(PreVal0);
  693. CL_SET_ARG(clState->outputBuffer);
  694. return status;
  695. }
  696. static cl_int queue_phatk_kernel(_clState *clState, dev_blk_ctx *blk,
  697. __maybe_unused cl_uint threads)
  698. {
  699. cl_kernel *kernel = &clState->kernel;
  700. cl_uint vwidth = clState->vwidth;
  701. unsigned int i, num = 0;
  702. cl_int status = 0;
  703. uint *nonces;
  704. CL_SET_BLKARG(ctx_a);
  705. CL_SET_BLKARG(ctx_b);
  706. CL_SET_BLKARG(ctx_c);
  707. CL_SET_BLKARG(ctx_d);
  708. CL_SET_BLKARG(ctx_e);
  709. CL_SET_BLKARG(ctx_f);
  710. CL_SET_BLKARG(ctx_g);
  711. CL_SET_BLKARG(ctx_h);
  712. CL_SET_BLKARG(cty_b);
  713. CL_SET_BLKARG(cty_c);
  714. CL_SET_BLKARG(cty_d);
  715. CL_SET_BLKARG(cty_f);
  716. CL_SET_BLKARG(cty_g);
  717. CL_SET_BLKARG(cty_h);
  718. nonces = alloca(sizeof(uint) * vwidth);
  719. for (i = 0; i < vwidth; i++)
  720. nonces[i] = blk->nonce + i;
  721. CL_SET_VARG(vwidth, nonces);
  722. CL_SET_BLKARG(W16);
  723. CL_SET_BLKARG(W17);
  724. CL_SET_BLKARG(PreVal4_2);
  725. CL_SET_BLKARG(PreVal0);
  726. CL_SET_BLKARG(PreW18);
  727. CL_SET_BLKARG(PreW19);
  728. CL_SET_BLKARG(PreW31);
  729. CL_SET_BLKARG(PreW32);
  730. CL_SET_ARG(clState->outputBuffer);
  731. return status;
  732. }
  733. static cl_int queue_diakgcn_kernel(_clState *clState, dev_blk_ctx *blk,
  734. __maybe_unused cl_uint threads)
  735. {
  736. cl_kernel *kernel = &clState->kernel;
  737. cl_uint vwidth = clState->vwidth;
  738. unsigned int i, num = 0;
  739. cl_int status = 0;
  740. uint *nonces;
  741. nonces = alloca(sizeof(uint) * vwidth);
  742. for (i = 0; i < vwidth; i++)
  743. nonces[i] = blk->nonce + i;
  744. CL_SET_VARG(vwidth, nonces);
  745. CL_SET_BLKARG(PreVal0);
  746. CL_SET_BLKARG(PreVal4_2);
  747. CL_SET_BLKARG(cty_h);
  748. CL_SET_BLKARG(D1A);
  749. CL_SET_BLKARG(cty_b);
  750. CL_SET_BLKARG(cty_c);
  751. CL_SET_BLKARG(cty_f);
  752. CL_SET_BLKARG(cty_g);
  753. CL_SET_BLKARG(C1addK5);
  754. CL_SET_BLKARG(B1addK6);
  755. CL_SET_BLKARG(PreVal0addK7);
  756. CL_SET_BLKARG(W16addK16);
  757. CL_SET_BLKARG(W17addK17);
  758. CL_SET_BLKARG(PreW18);
  759. CL_SET_BLKARG(PreW19);
  760. CL_SET_BLKARG(W16);
  761. CL_SET_BLKARG(W17);
  762. CL_SET_BLKARG(PreW31);
  763. CL_SET_BLKARG(PreW32);
  764. CL_SET_BLKARG(ctx_a);
  765. CL_SET_BLKARG(ctx_b);
  766. CL_SET_BLKARG(ctx_c);
  767. CL_SET_BLKARG(ctx_d);
  768. CL_SET_BLKARG(ctx_e);
  769. CL_SET_BLKARG(ctx_f);
  770. CL_SET_BLKARG(ctx_g);
  771. CL_SET_BLKARG(ctx_h);
  772. CL_SET_BLKARG(zeroA);
  773. CL_SET_BLKARG(zeroB);
  774. CL_SET_BLKARG(oneA);
  775. CL_SET_BLKARG(twoA);
  776. CL_SET_BLKARG(threeA);
  777. CL_SET_BLKARG(fourA);
  778. CL_SET_BLKARG(fiveA);
  779. CL_SET_BLKARG(sixA);
  780. CL_SET_BLKARG(sevenA);
  781. CL_SET_ARG(clState->outputBuffer);
  782. return status;
  783. }
  784. static cl_int queue_diablo_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  785. {
  786. cl_kernel *kernel = &clState->kernel;
  787. unsigned int num = 0;
  788. cl_int status = 0;
  789. if (!clState->goffset) {
  790. cl_uint vwidth = clState->vwidth;
  791. uint *nonces = alloca(sizeof(uint) * vwidth);
  792. unsigned int i;
  793. for (i = 0; i < vwidth; i++)
  794. nonces[i] = blk->nonce + (i * threads);
  795. CL_SET_VARG(vwidth, nonces);
  796. }
  797. CL_SET_BLKARG(PreVal0);
  798. CL_SET_BLKARG(PreVal0addK7);
  799. CL_SET_BLKARG(PreVal4addT1);
  800. CL_SET_BLKARG(PreW18);
  801. CL_SET_BLKARG(PreW19);
  802. CL_SET_BLKARG(W16);
  803. CL_SET_BLKARG(W17);
  804. CL_SET_BLKARG(W16addK16);
  805. CL_SET_BLKARG(W17addK17);
  806. CL_SET_BLKARG(PreW31);
  807. CL_SET_BLKARG(PreW32);
  808. CL_SET_BLKARG(D1A);
  809. CL_SET_BLKARG(cty_b);
  810. CL_SET_BLKARG(cty_c);
  811. CL_SET_BLKARG(cty_h);
  812. CL_SET_BLKARG(cty_f);
  813. CL_SET_BLKARG(cty_g);
  814. CL_SET_BLKARG(C1addK5);
  815. CL_SET_BLKARG(B1addK6);
  816. CL_SET_BLKARG(ctx_a);
  817. CL_SET_BLKARG(ctx_b);
  818. CL_SET_BLKARG(ctx_c);
  819. CL_SET_BLKARG(ctx_d);
  820. CL_SET_BLKARG(ctx_e);
  821. CL_SET_BLKARG(ctx_f);
  822. CL_SET_BLKARG(ctx_g);
  823. CL_SET_BLKARG(ctx_h);
  824. CL_SET_ARG(clState->outputBuffer);
  825. return status;
  826. }
  827. static void set_threads_hashes(unsigned int vectors, unsigned int *threads,
  828. unsigned int *hashes, size_t *globalThreads,
  829. unsigned int minthreads, int intensity)
  830. {
  831. *threads = 1 << (15 + intensity);
  832. if (*threads < minthreads)
  833. *threads = minthreads;
  834. *globalThreads = *threads;
  835. *hashes = *threads * vectors;
  836. }
  837. #endif /* HAVE_OPENCL */
  838. #ifdef HAVE_OPENCL
  839. /* We have only one thread that ever re-initialises GPUs, thus if any GPU
  840. * init command fails due to a completely wedged GPU, the thread will never
  841. * return, unable to harm other GPUs. If it does return, it means we only had
  842. * a soft failure and then the reinit_gpu thread is ready to tackle another
  843. * GPU */
  844. void *reinit_gpu(void *userdata)
  845. {
  846. struct thr_info *mythr = userdata;
  847. struct cgpu_info *cgpu;
  848. struct thr_info *thr;
  849. struct timeval now;
  850. char name[256];
  851. int thr_id;
  852. int gpu;
  853. pthread_detach(pthread_self());
  854. select_cgpu:
  855. cgpu = tq_pop(mythr->q, NULL);
  856. if (!cgpu)
  857. goto out;
  858. if (clDevicesNum() != nDevs) {
  859. applog(LOG_WARNING, "Hardware not reporting same number of active devices, will not attempt to restart GPU");
  860. goto out;
  861. }
  862. gpu = cgpu->device_id;
  863. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  864. thr = &thr_info[thr_id];
  865. cgpu = thr->cgpu;
  866. if (cgpu->api != &opencl_api)
  867. continue;
  868. if (dev_from_id(thr_id) != gpu)
  869. continue;
  870. thr = &thr_info[thr_id];
  871. if (!thr) {
  872. applog(LOG_WARNING, "No reference to thread %d exists", thr_id);
  873. continue;
  874. }
  875. thr->rolling = thr->cgpu->rolling = 0;
  876. /* Reports the last time we tried to revive a sick GPU */
  877. gettimeofday(&thr->sick, NULL);
  878. if (!pthread_cancel(thr->pth)) {
  879. applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
  880. } else
  881. applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
  882. }
  883. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  884. int virtual_gpu;
  885. thr = &thr_info[thr_id];
  886. cgpu = thr->cgpu;
  887. if (cgpu->api != &opencl_api)
  888. continue;
  889. if (dev_from_id(thr_id) != gpu)
  890. continue;
  891. virtual_gpu = cgpu->virtual_gpu;
  892. /* Lose this ram cause we may get stuck here! */
  893. //tq_freeze(thr->q);
  894. thr->q = tq_new();
  895. if (!thr->q)
  896. quit(1, "Failed to tq_new in reinit_gpu");
  897. /* Lose this ram cause we may dereference in the dying thread! */
  898. //free(clState);
  899. applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
  900. clStates[thr_id] = initCl(virtual_gpu, name, sizeof(name));
  901. if (!clStates[thr_id]) {
  902. applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
  903. goto select_cgpu;
  904. }
  905. applog(LOG_INFO, "initCl() finished. Found %s", name);
  906. if (unlikely(thr_info_create(thr, NULL, miner_thread, thr))) {
  907. applog(LOG_ERR, "thread %d create failed", thr_id);
  908. return NULL;
  909. }
  910. applog(LOG_WARNING, "Thread %d restarted", thr_id);
  911. }
  912. gettimeofday(&now, NULL);
  913. get_datestamp(cgpu->init, &now);
  914. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  915. thr = &thr_info[thr_id];
  916. cgpu = thr->cgpu;
  917. if (cgpu->api != &opencl_api)
  918. continue;
  919. if (dev_from_id(thr_id) != gpu)
  920. continue;
  921. tq_push(thr->q, &ping);
  922. }
  923. goto select_cgpu;
  924. out:
  925. return NULL;
  926. }
  927. #else
  928. void *reinit_gpu(void *userdata)
  929. {
  930. return NULL;
  931. }
  932. #endif
  933. #ifdef HAVE_OPENCL
  934. struct device_api opencl_api;
  935. static void opencl_detect()
  936. {
  937. int i;
  938. nDevs = clDevicesNum();
  939. if (nDevs < 0) {
  940. applog(LOG_ERR, "clDevicesNum returned error, no GPUs usable");
  941. nDevs = 0;
  942. }
  943. if (MAX_DEVICES - total_devices < nDevs)
  944. nDevs = MAX_DEVICES - total_devices;
  945. if (!nDevs)
  946. return;
  947. for (i = 0; i < nDevs; ++i) {
  948. struct cgpu_info *cgpu;
  949. cgpu = &gpus[i];
  950. cgpu->deven = DEV_ENABLED;
  951. cgpu->api = &opencl_api;
  952. cgpu->device_id = i;
  953. cgpu->threads = opt_g_threads;
  954. cgpu->virtual_gpu = i;
  955. add_cgpu(cgpu);
  956. }
  957. if (!opt_noadl)
  958. init_adl(nDevs);
  959. }
  960. static void reinit_opencl_device(struct cgpu_info *gpu)
  961. {
  962. tq_push(thr_info[gpur_thr_id].q, gpu);
  963. }
  964. #ifdef HAVE_ADL
  965. static void get_opencl_statline_before(char *buf, struct cgpu_info *gpu)
  966. {
  967. if (gpu->has_adl) {
  968. int gpuid = gpu->device_id;
  969. float gt = gpu_temp(gpuid);
  970. int gf = gpu_fanspeed(gpuid);
  971. int gp;
  972. if (gt != -1)
  973. tailsprintf(buf, "%5.1fC ", gt);
  974. else
  975. tailsprintf(buf, " ", gt);
  976. if (gf != -1)
  977. tailsprintf(buf, "%4dRPM ", gf);
  978. else if ((gp = gpu_fanpercent(gpuid)) != -1)
  979. tailsprintf(buf, "%3d%% ", gp);
  980. else
  981. tailsprintf(buf, " ");
  982. tailsprintf(buf, "| ");
  983. }
  984. }
  985. #endif
  986. static void get_opencl_statline(char *buf, struct cgpu_info *gpu)
  987. {
  988. tailsprintf(buf, " I:%2d", gpu->intensity);
  989. }
  990. struct opencl_thread_data {
  991. cl_int (*queue_kernel_parameters)(_clState *, dev_blk_ctx *, cl_uint);
  992. uint32_t *res;
  993. struct work *last_work;
  994. struct work _last_work;
  995. };
  996. static uint32_t *blank_res;
  997. static bool opencl_thread_prepare(struct thr_info *thr)
  998. {
  999. char name[256];
  1000. struct timeval now;
  1001. struct cgpu_info *cgpu = thr->cgpu;
  1002. int gpu = cgpu->device_id;
  1003. int virtual_gpu = cgpu->virtual_gpu;
  1004. int i = thr->id;
  1005. static bool failmessage = false;
  1006. if (!blank_res)
  1007. blank_res = calloc(BUFFERSIZE, 1);
  1008. if (!blank_res) {
  1009. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1010. return false;
  1011. }
  1012. strcpy(name, "");
  1013. applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
  1014. clStates[i] = initCl(virtual_gpu, name, sizeof(name));
  1015. if (!clStates[i]) {
  1016. if (use_curses)
  1017. enable_curses();
  1018. applog(LOG_ERR, "Failed to init GPU thread %d, disabling device %d", i, gpu);
  1019. if (!failmessage) {
  1020. char *buf;
  1021. applog(LOG_ERR, "Restarting the GPU from the menu will not fix this.");
  1022. applog(LOG_ERR, "Try restarting cgminer.");
  1023. failmessage = true;
  1024. #ifdef HAVE_CURSES
  1025. if (use_curses) {
  1026. buf = curses_input("Press enter to continue");
  1027. if (buf)
  1028. free(buf);
  1029. }
  1030. #endif
  1031. }
  1032. cgpu->deven = DEV_DISABLED;
  1033. cgpu->status = LIFE_NOSTART;
  1034. cgpu->device_last_not_well = time(NULL);
  1035. cgpu->device_not_well_reason = REASON_DEV_NOSTART;
  1036. cgpu->dev_nostart_count++;
  1037. return false;
  1038. }
  1039. if (!cgpu->name)
  1040. cgpu->name = strdup(name);
  1041. if (!cgpu->kname)
  1042. {
  1043. switch (clStates[i]->chosen_kernel) {
  1044. case KL_DIABLO:
  1045. cgpu->kname = "diablo";
  1046. break;
  1047. case KL_DIAKGCN:
  1048. cgpu->kname = "diakgcn";
  1049. break;
  1050. case KL_PHATK:
  1051. cgpu->kname = "phatk";
  1052. break;
  1053. case KL_POCLBM:
  1054. cgpu->kname = "poclbm";
  1055. default:
  1056. break;
  1057. }
  1058. }
  1059. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1060. gettimeofday(&now, NULL);
  1061. get_datestamp(cgpu->init, &now);
  1062. have_opencl = true;
  1063. return true;
  1064. }
  1065. static bool opencl_thread_init(struct thr_info *thr)
  1066. {
  1067. const int thr_id = thr->id;
  1068. struct cgpu_info *gpu = thr->cgpu;
  1069. struct opencl_thread_data *thrdata;
  1070. _clState *clState = clStates[thr_id];
  1071. cl_int status;
  1072. thrdata = calloc(1, sizeof(*thrdata));
  1073. thr->cgpu_data = thrdata;
  1074. if (!thrdata) {
  1075. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1076. return false;
  1077. }
  1078. switch (clState->chosen_kernel) {
  1079. case KL_POCLBM:
  1080. thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
  1081. break;
  1082. case KL_PHATK:
  1083. thrdata->queue_kernel_parameters = &queue_phatk_kernel;
  1084. break;
  1085. case KL_DIAKGCN:
  1086. thrdata->queue_kernel_parameters = &queue_diakgcn_kernel;
  1087. break;
  1088. default:
  1089. case KL_DIABLO:
  1090. thrdata->queue_kernel_parameters = &queue_diablo_kernel;
  1091. break;
  1092. }
  1093. thrdata->res = calloc(BUFFERSIZE, 1);
  1094. if (!thrdata->res) {
  1095. free(thrdata);
  1096. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1097. return false;
  1098. }
  1099. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  1100. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1101. if (unlikely(status != CL_SUCCESS)) {
  1102. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1103. return false;
  1104. }
  1105. gpu->status = LIFE_WELL;
  1106. gpu->device_last_well = time(NULL);
  1107. return true;
  1108. }
  1109. static void opencl_free_work(struct thr_info *thr, struct work *work)
  1110. {
  1111. const int thr_id = thr->id;
  1112. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1113. _clState *clState = clStates[thr_id];
  1114. clFinish(clState->commandQueue);
  1115. if (thrdata->res[FOUND]) {
  1116. thrdata->last_work = &thrdata->_last_work;
  1117. memcpy(thrdata->last_work, work, sizeof(*thrdata->last_work));
  1118. }
  1119. }
  1120. static bool opencl_prepare_work(struct thr_info __maybe_unused *thr, struct work *work)
  1121. {
  1122. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1123. return true;
  1124. }
  1125. extern int opt_dynamic_interval;
  1126. static uint64_t opencl_scanhash(struct thr_info *thr, struct work *work,
  1127. uint64_t __maybe_unused max_nonce)
  1128. {
  1129. const int thr_id = thr->id;
  1130. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1131. struct cgpu_info *gpu = thr->cgpu;
  1132. _clState *clState = clStates[thr_id];
  1133. const cl_kernel *kernel = &clState->kernel;
  1134. double gpu_ms_average = 7;
  1135. cl_int status;
  1136. size_t globalThreads[1];
  1137. size_t localThreads[1] = { clState->wsize };
  1138. unsigned int threads;
  1139. unsigned int hashes;
  1140. struct timeval tv_gpustart, tv_gpuend, diff;
  1141. suseconds_t gpu_us;
  1142. gettimeofday(&tv_gpustart, NULL);
  1143. timeval_subtract(&diff, &tv_gpustart, &tv_gpuend);
  1144. /* This finish flushes the readbuffer set with CL_FALSE later */
  1145. clFinish(clState->commandQueue);
  1146. gettimeofday(&tv_gpuend, NULL);
  1147. timeval_subtract(&diff, &tv_gpuend, &tv_gpustart);
  1148. gpu_us = diff.tv_sec * 1000000 + diff.tv_usec;
  1149. decay_time(&gpu_ms_average, gpu_us / 1000);
  1150. if (gpu->dynamic) {
  1151. /* Try to not let the GPU be out for longer than 6ms, but
  1152. * increase intensity when the system is idle, unless
  1153. * dynamic is disabled. */
  1154. if (gpu_ms_average > opt_dynamic_interval) {
  1155. if (gpu->intensity > MIN_INTENSITY)
  1156. --gpu->intensity;
  1157. } else if (gpu_ms_average < ((opt_dynamic_interval / 2) ? : 1)) {
  1158. if (gpu->intensity < MAX_INTENSITY)
  1159. ++gpu->intensity;
  1160. }
  1161. }
  1162. set_threads_hashes(clState->vwidth, &threads, &hashes, globalThreads,
  1163. localThreads[0], gpu->intensity);
  1164. if (hashes > gpu->max_hashes)
  1165. gpu->max_hashes = hashes;
  1166. status = thrdata->queue_kernel_parameters(clState, &work->blk, globalThreads[0]);
  1167. if (unlikely(status != CL_SUCCESS)) {
  1168. applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
  1169. return 0;
  1170. }
  1171. /* MAXBUFFERS entry is used as a flag to say nonces exist */
  1172. if (thrdata->res[FOUND]) {
  1173. /* Clear the buffer again */
  1174. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1175. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1176. if (unlikely(status != CL_SUCCESS)) {
  1177. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1178. return 0;
  1179. }
  1180. if (unlikely(thrdata->last_work)) {
  1181. applog(LOG_DEBUG, "GPU %d found something in last work?", gpu->device_id);
  1182. postcalc_hash_async(thr, thrdata->last_work, thrdata->res);
  1183. thrdata->last_work = NULL;
  1184. } else {
  1185. applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
  1186. postcalc_hash_async(thr, work, thrdata->res);
  1187. }
  1188. memset(thrdata->res, 0, BUFFERSIZE);
  1189. clFinish(clState->commandQueue);
  1190. }
  1191. if (clState->goffset) {
  1192. size_t global_work_offset[1];
  1193. global_work_offset[0] = work->blk.nonce;
  1194. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, global_work_offset,
  1195. globalThreads, localThreads, 0, NULL, NULL);
  1196. } else
  1197. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1198. globalThreads, localThreads, 0, NULL, NULL);
  1199. if (unlikely(status != CL_SUCCESS)) {
  1200. applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)");
  1201. return 0;
  1202. }
  1203. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1204. BUFFERSIZE, thrdata->res, 0, NULL, NULL);
  1205. if (unlikely(status != CL_SUCCESS)) {
  1206. applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)");
  1207. return 0;
  1208. }
  1209. /* The amount of work scanned can fluctuate when intensity changes
  1210. * and since we do this one cycle behind, we increment the work more
  1211. * than enough to prevent repeating work */
  1212. work->blk.nonce += gpu->max_hashes;
  1213. return hashes;
  1214. }
  1215. static void opencl_thread_shutdown(struct thr_info *thr)
  1216. {
  1217. const int thr_id = thr->id;
  1218. _clState *clState = clStates[thr_id];
  1219. clReleaseCommandQueue(clState->commandQueue);
  1220. clReleaseKernel(clState->kernel);
  1221. clReleaseProgram(clState->program);
  1222. clReleaseContext(clState->context);
  1223. }
  1224. struct device_api opencl_api = {
  1225. .dname = "opencl",
  1226. .name = "GPU",
  1227. .api_detect = opencl_detect,
  1228. .reinit_device = reinit_opencl_device,
  1229. #ifdef HAVE_ADL
  1230. .get_statline_before = get_opencl_statline_before,
  1231. #endif
  1232. .get_statline = get_opencl_statline,
  1233. .thread_prepare = opencl_thread_prepare,
  1234. .thread_init = opencl_thread_init,
  1235. .free_work = opencl_free_work,
  1236. .prepare_work = opencl_prepare_work,
  1237. .scanhash = opencl_scanhash,
  1238. .thread_shutdown = opencl_thread_shutdown,
  1239. };
  1240. #endif