driver-opencl.c 37 KB

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