driver-opencl.c 35 KB

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