driver-opencl.c 38 KB

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