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 wlog(const char *f, ...);
  47. extern void decay_time(double *f, double fadd);
  48. /**********************************************/
  49. #ifdef HAVE_OPENCL
  50. struct device_drv opencl_drv;
  51. #endif
  52. #ifdef HAVE_ADL
  53. extern float gpu_temp(int gpu);
  54. extern int gpu_fanspeed(int gpu);
  55. extern int gpu_fanpercent(int gpu);
  56. #endif
  57. #ifdef HAVE_OPENCL
  58. char *set_vector(char *arg)
  59. {
  60. int i, val = 0, device = 0;
  61. char *nextptr;
  62. nextptr = strtok(arg, ",");
  63. if (nextptr == NULL)
  64. return "Invalid parameters for set vector";
  65. val = atoi(nextptr);
  66. if (val != 1 && val != 2 && val != 4)
  67. return "Invalid value passed to set_vector";
  68. gpus[device++].vwidth = val;
  69. while ((nextptr = strtok(NULL, ",")) != NULL) {
  70. val = atoi(nextptr);
  71. if (val != 1 && val != 2 && val != 4)
  72. return "Invalid value passed to set_vector";
  73. gpus[device++].vwidth = val;
  74. }
  75. if (device == 1) {
  76. for (i = device; i < MAX_GPUDEVICES; i++)
  77. gpus[i].vwidth = gpus[0].vwidth;
  78. }
  79. return NULL;
  80. }
  81. char *set_worksize(char *arg)
  82. {
  83. int i, val = 0, device = 0;
  84. char *nextptr;
  85. nextptr = strtok(arg, ",");
  86. if (nextptr == NULL)
  87. return "Invalid parameters for set work size";
  88. val = atoi(nextptr);
  89. if (val < 1 || val > 9999)
  90. return "Invalid value passed to set_worksize";
  91. gpus[device++].work_size = val;
  92. while ((nextptr = strtok(NULL, ",")) != NULL) {
  93. val = atoi(nextptr);
  94. if (val < 1 || val > 9999)
  95. return "Invalid value passed to set_worksize";
  96. gpus[device++].work_size = val;
  97. }
  98. if (device == 1) {
  99. for (i = device; i < MAX_GPUDEVICES; i++)
  100. gpus[i].work_size = gpus[0].work_size;
  101. }
  102. return NULL;
  103. }
  104. #ifdef USE_SCRYPT
  105. char *set_shaders(char *arg)
  106. {
  107. int i, val = 0, device = 0;
  108. char *nextptr;
  109. nextptr = strtok(arg, ",");
  110. if (nextptr == NULL)
  111. return "Invalid parameters for set lookup gap";
  112. val = atoi(nextptr);
  113. gpus[device++].shaders = val;
  114. while ((nextptr = strtok(NULL, ",")) != NULL) {
  115. val = atoi(nextptr);
  116. gpus[device++].shaders = val;
  117. }
  118. if (device == 1) {
  119. for (i = device; i < MAX_GPUDEVICES; i++)
  120. gpus[i].shaders = gpus[0].shaders;
  121. }
  122. return NULL;
  123. }
  124. char *set_lookup_gap(char *arg)
  125. {
  126. int i, val = 0, device = 0;
  127. char *nextptr;
  128. nextptr = strtok(arg, ",");
  129. if (nextptr == NULL)
  130. return "Invalid parameters for set lookup gap";
  131. val = atoi(nextptr);
  132. gpus[device++].opt_lg = val;
  133. while ((nextptr = strtok(NULL, ",")) != NULL) {
  134. val = atoi(nextptr);
  135. gpus[device++].opt_lg = val;
  136. }
  137. if (device == 1) {
  138. for (i = device; i < MAX_GPUDEVICES; i++)
  139. gpus[i].opt_lg = gpus[0].opt_lg;
  140. }
  141. return NULL;
  142. }
  143. char *set_thread_concurrency(char *arg)
  144. {
  145. int i, val = 0, device = 0;
  146. char *nextptr;
  147. nextptr = strtok(arg, ",");
  148. if (nextptr == NULL)
  149. return "Invalid parameters for set thread concurrency";
  150. val = atoi(nextptr);
  151. gpus[device++].opt_tc = val;
  152. while ((nextptr = strtok(NULL, ",")) != NULL) {
  153. val = atoi(nextptr);
  154. gpus[device++].opt_tc = val;
  155. }
  156. if (device == 1) {
  157. for (i = device; i < MAX_GPUDEVICES; i++)
  158. gpus[i].opt_tc = gpus[0].opt_tc;
  159. }
  160. return NULL;
  161. }
  162. #endif
  163. static enum cl_kernels select_kernel(char *arg)
  164. {
  165. if (!strcmp(arg, "diablo"))
  166. return KL_DIABLO;
  167. if (!strcmp(arg, "diakgcn"))
  168. return KL_DIAKGCN;
  169. if (!strcmp(arg, "poclbm"))
  170. return KL_POCLBM;
  171. if (!strcmp(arg, "phatk"))
  172. return KL_PHATK;
  173. #ifdef USE_SCRYPT
  174. if (!strcmp(arg, "scrypt"))
  175. return KL_SCRYPT;
  176. #endif
  177. return KL_NONE;
  178. }
  179. char *set_kernel(char *arg)
  180. {
  181. enum cl_kernels kern;
  182. int i, device = 0;
  183. char *nextptr;
  184. if (opt_scrypt)
  185. return "Cannot specify a kernel with scrypt";
  186. nextptr = strtok(arg, ",");
  187. if (nextptr == NULL)
  188. return "Invalid parameters for set kernel";
  189. kern = select_kernel(nextptr);
  190. if (kern == KL_NONE)
  191. return "Invalid parameter to set_kernel";
  192. gpus[device++].kernel = kern;
  193. while ((nextptr = strtok(NULL, ",")) != NULL) {
  194. kern = select_kernel(nextptr);
  195. if (kern == KL_NONE)
  196. return "Invalid parameter to set_kernel";
  197. gpus[device++].kernel = kern;
  198. }
  199. if (device == 1) {
  200. for (i = device; i < MAX_GPUDEVICES; i++)
  201. gpus[i].kernel = gpus[0].kernel;
  202. }
  203. return NULL;
  204. }
  205. #endif
  206. #ifdef HAVE_ADL
  207. /* This function allows us to map an adl device to an opencl device for when
  208. * simple enumeration has failed to match them. */
  209. char *set_gpu_map(char *arg)
  210. {
  211. int val1 = 0, val2 = 0;
  212. char *nextptr;
  213. nextptr = strtok(arg, ",");
  214. if (nextptr == NULL)
  215. return "Invalid parameters for set gpu map";
  216. if (sscanf(arg, "%d:%d", &val1, &val2) != 2)
  217. return "Invalid description for map pair";
  218. if (val1 < 0 || val1 > MAX_GPUDEVICES || val2 < 0 || val2 > MAX_GPUDEVICES)
  219. return "Invalid value passed to set_gpu_map";
  220. gpus[val1].virtual_adl = val2;
  221. gpus[val1].mapped = true;
  222. while ((nextptr = strtok(NULL, ",")) != NULL) {
  223. if (sscanf(nextptr, "%d:%d", &val1, &val2) != 2)
  224. return "Invalid description for map pair";
  225. if (val1 < 0 || val1 > MAX_GPUDEVICES || val2 < 0 || val2 > MAX_GPUDEVICES)
  226. return "Invalid value passed to set_gpu_map";
  227. gpus[val1].virtual_adl = val2;
  228. gpus[val1].mapped = true;
  229. }
  230. return NULL;
  231. }
  232. char *set_gpu_engine(char *arg)
  233. {
  234. int i, val1 = 0, val2 = 0, device = 0;
  235. char *nextptr;
  236. nextptr = strtok(arg, ",");
  237. if (nextptr == NULL)
  238. return "Invalid parameters for set gpu engine";
  239. get_intrange(nextptr, &val1, &val2);
  240. if (val1 < 0 || val1 > 9999 || val2 < 0 || val2 > 9999)
  241. return "Invalid value passed to set_gpu_engine";
  242. gpus[device].min_engine = val1;
  243. gpus[device].gpu_engine = val2;
  244. device++;
  245. while ((nextptr = strtok(NULL, ",")) != NULL) {
  246. get_intrange(nextptr, &val1, &val2);
  247. if (val1 < 0 || val1 > 9999 || val2 < 0 || val2 > 9999)
  248. return "Invalid value passed to set_gpu_engine";
  249. gpus[device].min_engine = val1;
  250. gpus[device].gpu_engine = val2;
  251. device++;
  252. }
  253. if (device == 1) {
  254. for (i = 1; i < MAX_GPUDEVICES; i++) {
  255. gpus[i].min_engine = gpus[0].min_engine;
  256. gpus[i].gpu_engine = gpus[0].gpu_engine;
  257. }
  258. }
  259. return NULL;
  260. }
  261. char *set_gpu_fan(char *arg)
  262. {
  263. int i, val1 = 0, val2 = 0, device = 0;
  264. char *nextptr;
  265. nextptr = strtok(arg, ",");
  266. if (nextptr == NULL)
  267. return "Invalid parameters for set gpu fan";
  268. get_intrange(nextptr, &val1, &val2);
  269. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100)
  270. return "Invalid value passed to set_gpu_fan";
  271. gpus[device].min_fan = val1;
  272. gpus[device].gpu_fan = val2;
  273. device++;
  274. while ((nextptr = strtok(NULL, ",")) != NULL) {
  275. get_intrange(nextptr, &val1, &val2);
  276. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100)
  277. return "Invalid value passed to set_gpu_fan";
  278. gpus[device].min_fan = val1;
  279. gpus[device].gpu_fan = val2;
  280. device++;
  281. }
  282. if (device == 1) {
  283. for (i = 1; i < MAX_GPUDEVICES; i++) {
  284. gpus[i].min_fan = gpus[0].min_fan;
  285. gpus[i].gpu_fan = gpus[0].gpu_fan;
  286. }
  287. }
  288. return NULL;
  289. }
  290. char *set_gpu_memclock(char *arg)
  291. {
  292. int i, val = 0, device = 0;
  293. char *nextptr;
  294. nextptr = strtok(arg, ",");
  295. if (nextptr == NULL)
  296. return "Invalid parameters for set gpu memclock";
  297. val = atoi(nextptr);
  298. if (val < 0 || val >= 9999)
  299. return "Invalid value passed to set_gpu_memclock";
  300. gpus[device++].gpu_memclock = val;
  301. while ((nextptr = strtok(NULL, ",")) != NULL) {
  302. val = atoi(nextptr);
  303. if (val < 0 || val >= 9999)
  304. return "Invalid value passed to set_gpu_memclock";
  305. gpus[device++].gpu_memclock = val;
  306. }
  307. if (device == 1) {
  308. for (i = device; i < MAX_GPUDEVICES; i++)
  309. gpus[i].gpu_memclock = gpus[0].gpu_memclock;
  310. }
  311. return NULL;
  312. }
  313. char *set_gpu_memdiff(char *arg)
  314. {
  315. int i, val = 0, device = 0;
  316. char *nextptr;
  317. nextptr = strtok(arg, ",");
  318. if (nextptr == NULL)
  319. return "Invalid parameters for set gpu memdiff";
  320. val = atoi(nextptr);
  321. if (val < -9999 || val > 9999)
  322. return "Invalid value passed to set_gpu_memdiff";
  323. gpus[device++].gpu_memdiff = val;
  324. while ((nextptr = strtok(NULL, ",")) != NULL) {
  325. val = atoi(nextptr);
  326. if (val < -9999 || val > 9999)
  327. return "Invalid value passed to set_gpu_memdiff";
  328. gpus[device++].gpu_memdiff = val;
  329. }
  330. if (device == 1) {
  331. for (i = device; i < MAX_GPUDEVICES; i++)
  332. gpus[i].gpu_memdiff = gpus[0].gpu_memdiff;
  333. }
  334. return NULL;
  335. }
  336. char *set_gpu_powertune(char *arg)
  337. {
  338. int i, val = 0, device = 0;
  339. char *nextptr;
  340. nextptr = strtok(arg, ",");
  341. if (nextptr == NULL)
  342. return "Invalid parameters for set gpu powertune";
  343. val = atoi(nextptr);
  344. if (val < -99 || val > 99)
  345. return "Invalid value passed to set_gpu_powertune";
  346. gpus[device++].gpu_powertune = val;
  347. while ((nextptr = strtok(NULL, ",")) != NULL) {
  348. val = atoi(nextptr);
  349. if (val < -99 || val > 99)
  350. return "Invalid value passed to set_gpu_powertune";
  351. gpus[device++].gpu_powertune = val;
  352. }
  353. if (device == 1) {
  354. for (i = device; i < MAX_GPUDEVICES; i++)
  355. gpus[i].gpu_powertune = gpus[0].gpu_powertune;
  356. }
  357. return NULL;
  358. }
  359. char *set_gpu_vddc(char *arg)
  360. {
  361. int i, device = 0;
  362. float val = 0;
  363. char *nextptr;
  364. nextptr = strtok(arg, ",");
  365. if (nextptr == NULL)
  366. return "Invalid parameters for set gpu vddc";
  367. val = atof(nextptr);
  368. if (val < 0 || val >= 9999)
  369. return "Invalid value passed to set_gpu_vddc";
  370. gpus[device++].gpu_vddc = val;
  371. while ((nextptr = strtok(NULL, ",")) != NULL) {
  372. val = atof(nextptr);
  373. if (val < 0 || val >= 9999)
  374. return "Invalid value passed to set_gpu_vddc";
  375. gpus[device++].gpu_vddc = val;
  376. }
  377. if (device == 1) {
  378. for (i = device; i < MAX_GPUDEVICES; i++)
  379. gpus[i].gpu_vddc = gpus[0].gpu_vddc;
  380. }
  381. return NULL;
  382. }
  383. char *set_temp_overheat(char *arg)
  384. {
  385. int i, val = 0, device = 0, *to;
  386. char *nextptr;
  387. nextptr = strtok(arg, ",");
  388. if (nextptr == NULL)
  389. return "Invalid parameters for set temp overheat";
  390. val = atoi(nextptr);
  391. if (val < 0 || val > 200)
  392. return "Invalid value passed to set temp overheat";
  393. to = &gpus[device++].adl.overtemp;
  394. *to = val;
  395. while ((nextptr = strtok(NULL, ",")) != NULL) {
  396. val = atoi(nextptr);
  397. if (val < 0 || val > 200)
  398. return "Invalid value passed to set temp overheat";
  399. to = &gpus[device++].adl.overtemp;
  400. *to = val;
  401. }
  402. if (device == 1) {
  403. for (i = device; i < MAX_GPUDEVICES; i++) {
  404. to = &gpus[i].adl.overtemp;
  405. *to = val;
  406. }
  407. }
  408. return NULL;
  409. }
  410. char *set_temp_target(char *arg)
  411. {
  412. int i, val = 0, device = 0, *tt;
  413. char *nextptr;
  414. nextptr = strtok(arg, ",");
  415. if (nextptr == NULL)
  416. return "Invalid parameters for set temp target";
  417. val = atoi(nextptr);
  418. if (val < 0 || val > 200)
  419. return "Invalid value passed to set temp target";
  420. tt = &gpus[device++].adl.targettemp;
  421. *tt = val;
  422. while ((nextptr = strtok(NULL, ",")) != NULL) {
  423. val = atoi(nextptr);
  424. if (val < 0 || val > 200)
  425. return "Invalid value passed to set temp target";
  426. tt = &gpus[device++].adl.targettemp;
  427. *tt = val;
  428. }
  429. if (device == 1) {
  430. for (i = device; i < MAX_GPUDEVICES; i++) {
  431. tt = &gpus[i].adl.targettemp;
  432. *tt = val;
  433. }
  434. }
  435. return NULL;
  436. }
  437. #endif
  438. #ifdef HAVE_OPENCL
  439. char *set_intensity(char *arg)
  440. {
  441. int i, device = 0, *tt;
  442. char *nextptr, val = 0;
  443. nextptr = strtok(arg, ",");
  444. if (nextptr == NULL)
  445. return "Invalid parameters for set intensity";
  446. if (!strncasecmp(nextptr, "d", 1))
  447. gpus[device].dynamic = true;
  448. else {
  449. gpus[device].dynamic = false;
  450. val = atoi(nextptr);
  451. if (val < MIN_INTENSITY || val > MAX_INTENSITY)
  452. return "Invalid value passed to set intensity";
  453. tt = &gpus[device].intensity;
  454. *tt = val;
  455. }
  456. device++;
  457. while ((nextptr = strtok(NULL, ",")) != NULL) {
  458. if (!strncasecmp(nextptr, "d", 1))
  459. gpus[device].dynamic = true;
  460. else {
  461. gpus[device].dynamic = false;
  462. val = atoi(nextptr);
  463. if (val < MIN_INTENSITY || val > MAX_INTENSITY)
  464. return "Invalid value passed to set intensity";
  465. tt = &gpus[device].intensity;
  466. *tt = val;
  467. }
  468. device++;
  469. }
  470. if (device == 1) {
  471. for (i = device; i < MAX_GPUDEVICES; i++) {
  472. gpus[i].dynamic = gpus[0].dynamic;
  473. gpus[i].intensity = gpus[0].intensity;
  474. }
  475. }
  476. return NULL;
  477. }
  478. void print_ndevs(int *ndevs)
  479. {
  480. opt_log_output = true;
  481. opencl_drv.drv_detect();
  482. clear_adl(*ndevs);
  483. applog(LOG_INFO, "%i GPU devices max detected", *ndevs);
  484. }
  485. #endif
  486. struct cgpu_info gpus[MAX_GPUDEVICES]; /* Maximum number apparently possible */
  487. struct cgpu_info *cpus;
  488. #ifdef HAVE_OPENCL
  489. /* In dynamic mode, only the first thread of each device will be in use.
  490. * This potentially could start a thread that was stopped with the start-stop
  491. * options if one were to disable dynamic from the menu on a paused GPU */
  492. void pause_dynamic_threads(int gpu)
  493. {
  494. struct cgpu_info *cgpu = &gpus[gpu];
  495. int i;
  496. for (i = 1; i < cgpu->threads; i++) {
  497. struct thr_info *thr;
  498. thr = get_thread(i);
  499. if (!thr->pause && cgpu->dynamic) {
  500. applog(LOG_WARNING, "Disabling extra threads due to dynamic mode.");
  501. applog(LOG_WARNING, "Tune dynamic intensity with --gpu-dyninterval");
  502. }
  503. thr->pause = cgpu->dynamic;
  504. if (!cgpu->dynamic && cgpu->deven != DEV_DISABLED)
  505. cgsem_post(&thr->sem);
  506. }
  507. }
  508. #endif /* HAVE_OPENCL */
  509. #if defined(HAVE_OPENCL) && defined(HAVE_CURSES)
  510. void manage_gpu(void)
  511. {
  512. struct thr_info *thr;
  513. int selected, gpu, i;
  514. char checkin[40];
  515. char input;
  516. if (!opt_g_threads)
  517. return;
  518. opt_loginput = true;
  519. immedok(logwin, true);
  520. clear_logwin();
  521. retry:
  522. for (gpu = 0; gpu < nDevs; gpu++) {
  523. struct cgpu_info *cgpu = &gpus[gpu];
  524. double displayed_rolling, displayed_total;
  525. bool mhash_base = true;
  526. displayed_rolling = cgpu->rolling;
  527. displayed_total = cgpu->total_mhashes / total_secs;
  528. if (displayed_rolling < 1) {
  529. displayed_rolling *= 1000;
  530. displayed_total *= 1000;
  531. mhash_base = false;
  532. }
  533. wlog("GPU %d: %.1f / %.1f %sh/s | A:%d R:%d HW:%d U:%.2f/m I:%d\n",
  534. gpu, displayed_rolling, displayed_total, mhash_base ? "M" : "K",
  535. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  536. cgpu->utility, cgpu->intensity);
  537. #ifdef HAVE_ADL
  538. if (gpus[gpu].has_adl) {
  539. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  540. float temp = 0, vddc = 0;
  541. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune)) {
  542. char logline[255];
  543. strcpy(logline, ""); // In case it has no data
  544. if (temp != -1)
  545. sprintf(logline, "%.1f C ", temp);
  546. if (fanspeed != -1 || fanpercent != -1) {
  547. tailsprintf(logline, "F: ");
  548. if (fanpercent != -1)
  549. tailsprintf(logline, "%d%% ", fanpercent);
  550. if (fanspeed != -1)
  551. tailsprintf(logline, "(%d RPM) ", fanspeed);
  552. tailsprintf(logline, " ");
  553. }
  554. if (engineclock != -1)
  555. tailsprintf(logline, "E: %d MHz ", engineclock);
  556. if (memclock != -1)
  557. tailsprintf(logline, "M: %d Mhz ", memclock);
  558. if (vddc != -1)
  559. tailsprintf(logline, "V: %.3fV ", vddc);
  560. if (activity != -1)
  561. tailsprintf(logline, "A: %d%% ", activity);
  562. if (powertune != -1)
  563. tailsprintf(logline, "P: %d%%", powertune);
  564. tailsprintf(logline, "\n");
  565. wlog(logline);
  566. }
  567. }
  568. #endif
  569. wlog("Last initialised: %s\n", cgpu->init);
  570. wlog("Intensity: ");
  571. if (gpus[gpu].dynamic)
  572. wlog("Dynamic (only one thread in use)\n");
  573. else
  574. wlog("%d\n", gpus[gpu].intensity);
  575. for (i = 0; i < mining_threads; i++) {
  576. thr = get_thread(i);
  577. if (thr->cgpu != cgpu)
  578. continue;
  579. get_datestamp(checkin, &thr->last);
  580. displayed_rolling = thr->rolling;
  581. if (!mhash_base)
  582. displayed_rolling *= 1000;
  583. wlog("Thread %d: %.1f %sh/s %s ", i, displayed_rolling, mhash_base ? "M" : "K" , cgpu->deven != DEV_DISABLED ? "Enabled" : "Disabled");
  584. switch (cgpu->status) {
  585. default:
  586. case LIFE_WELL:
  587. wlog("ALIVE");
  588. break;
  589. case LIFE_SICK:
  590. wlog("SICK reported in %s", checkin);
  591. break;
  592. case LIFE_DEAD:
  593. wlog("DEAD reported in %s", checkin);
  594. break;
  595. case LIFE_INIT:
  596. case LIFE_NOSTART:
  597. wlog("Never started");
  598. break;
  599. }
  600. if (thr->pause)
  601. wlog(" paused");
  602. wlog("\n");
  603. }
  604. wlog("\n");
  605. }
  606. wlogprint("[E]nable [D]isable [I]ntensity [R]estart GPU %s\n",adl_active ? "[C]hange settings" : "");
  607. wlogprint("Or press any other key to continue\n");
  608. logwin_update();
  609. input = getch();
  610. if (nDevs == 1)
  611. selected = 0;
  612. else
  613. selected = -1;
  614. if (!strncasecmp(&input, "e", 1)) {
  615. struct cgpu_info *cgpu;
  616. if (selected)
  617. selected = curses_int("Select GPU to enable");
  618. if (selected < 0 || selected >= nDevs) {
  619. wlogprint("Invalid selection\n");
  620. goto retry;
  621. }
  622. if (gpus[selected].deven != DEV_DISABLED) {
  623. wlogprint("Device already enabled\n");
  624. goto retry;
  625. }
  626. gpus[selected].deven = DEV_ENABLED;
  627. for (i = 0; i < mining_threads; ++i) {
  628. thr = get_thread(i);
  629. cgpu = thr->cgpu;
  630. if (cgpu->drv->drv_id != DRIVER_OPENCL)
  631. continue;
  632. if (dev_from_id(i) != selected)
  633. continue;
  634. if (cgpu->status != LIFE_WELL) {
  635. wlogprint("Must restart device before enabling it");
  636. goto retry;
  637. }
  638. applog(LOG_DEBUG, "Pushing sem post to thread %d", thr->id);
  639. cgsem_post(&thr->sem);
  640. }
  641. goto retry;
  642. } if (!strncasecmp(&input, "d", 1)) {
  643. if (selected)
  644. selected = curses_int("Select GPU to disable");
  645. if (selected < 0 || selected >= nDevs) {
  646. wlogprint("Invalid selection\n");
  647. goto retry;
  648. }
  649. if (gpus[selected].deven == DEV_DISABLED) {
  650. wlogprint("Device already disabled\n");
  651. goto retry;
  652. }
  653. gpus[selected].deven = DEV_DISABLED;
  654. goto retry;
  655. } else if (!strncasecmp(&input, "i", 1)) {
  656. int intensity;
  657. char *intvar;
  658. if (selected)
  659. selected = curses_int("Select GPU to change intensity on");
  660. if (selected < 0 || selected >= nDevs) {
  661. wlogprint("Invalid selection\n");
  662. goto retry;
  663. }
  664. intvar = curses_input("Set GPU scan intensity (d or " _MIN_INTENSITY_STR " -> " _MAX_INTENSITY_STR ")");
  665. if (!intvar) {
  666. wlogprint("Invalid input\n");
  667. goto retry;
  668. }
  669. if (!strncasecmp(intvar, "d", 1)) {
  670. wlogprint("Dynamic mode enabled on gpu %d\n", selected);
  671. gpus[selected].dynamic = true;
  672. pause_dynamic_threads(selected);
  673. free(intvar);
  674. goto retry;
  675. }
  676. intensity = atoi(intvar);
  677. free(intvar);
  678. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  679. wlogprint("Invalid selection\n");
  680. goto retry;
  681. }
  682. gpus[selected].dynamic = false;
  683. gpus[selected].intensity = intensity;
  684. wlogprint("Intensity on gpu %d set to %d\n", selected, intensity);
  685. pause_dynamic_threads(selected);
  686. goto retry;
  687. } else if (!strncasecmp(&input, "r", 1)) {
  688. if (selected)
  689. selected = curses_int("Select GPU to attempt to restart");
  690. if (selected < 0 || selected >= nDevs) {
  691. wlogprint("Invalid selection\n");
  692. goto retry;
  693. }
  694. wlogprint("Attempting to restart threads of GPU %d\n", selected);
  695. reinit_device(&gpus[selected]);
  696. goto retry;
  697. } else if (adl_active && (!strncasecmp(&input, "c", 1))) {
  698. if (selected)
  699. selected = curses_int("Select GPU to change settings on");
  700. if (selected < 0 || selected >= nDevs) {
  701. wlogprint("Invalid selection\n");
  702. goto retry;
  703. }
  704. change_gpusettings(selected);
  705. goto retry;
  706. } else
  707. clear_logwin();
  708. immedok(logwin, false);
  709. opt_loginput = false;
  710. }
  711. #else
  712. void manage_gpu(void)
  713. {
  714. }
  715. #endif
  716. #ifdef HAVE_OPENCL
  717. static _clState *clStates[MAX_GPUDEVICES];
  718. #define CL_SET_BLKARG(blkvar) status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->blkvar)
  719. #define CL_SET_ARG(var) status |= clSetKernelArg(*kernel, num++, sizeof(var), (void *)&var)
  720. #define CL_SET_VARG(args, var) status |= clSetKernelArg(*kernel, num++, args * sizeof(uint), (void *)var)
  721. static cl_int queue_poclbm_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  722. {
  723. cl_kernel *kernel = &clState->kernel;
  724. unsigned int num = 0;
  725. cl_int status = 0;
  726. CL_SET_BLKARG(ctx_a);
  727. CL_SET_BLKARG(ctx_b);
  728. CL_SET_BLKARG(ctx_c);
  729. CL_SET_BLKARG(ctx_d);
  730. CL_SET_BLKARG(ctx_e);
  731. CL_SET_BLKARG(ctx_f);
  732. CL_SET_BLKARG(ctx_g);
  733. CL_SET_BLKARG(ctx_h);
  734. CL_SET_BLKARG(cty_b);
  735. CL_SET_BLKARG(cty_c);
  736. CL_SET_BLKARG(cty_f);
  737. CL_SET_BLKARG(cty_g);
  738. CL_SET_BLKARG(cty_h);
  739. if (!clState->goffset) {
  740. cl_uint vwidth = clState->vwidth;
  741. uint *nonces = alloca(sizeof(uint) * vwidth);
  742. unsigned int i;
  743. for (i = 0; i < vwidth; i++)
  744. nonces[i] = blk->nonce + (i * threads);
  745. CL_SET_VARG(vwidth, nonces);
  746. }
  747. CL_SET_BLKARG(fW0);
  748. CL_SET_BLKARG(fW1);
  749. CL_SET_BLKARG(fW2);
  750. CL_SET_BLKARG(fW3);
  751. CL_SET_BLKARG(fW15);
  752. CL_SET_BLKARG(fW01r);
  753. CL_SET_BLKARG(D1A);
  754. CL_SET_BLKARG(C1addK5);
  755. CL_SET_BLKARG(B1addK6);
  756. CL_SET_BLKARG(W16addK16);
  757. CL_SET_BLKARG(W17addK17);
  758. CL_SET_BLKARG(PreVal4addT1);
  759. CL_SET_BLKARG(PreVal0);
  760. CL_SET_ARG(clState->outputBuffer);
  761. return status;
  762. }
  763. static cl_int queue_phatk_kernel(_clState *clState, dev_blk_ctx *blk,
  764. __maybe_unused cl_uint threads)
  765. {
  766. cl_kernel *kernel = &clState->kernel;
  767. cl_uint vwidth = clState->vwidth;
  768. unsigned int i, num = 0;
  769. cl_int status = 0;
  770. uint *nonces;
  771. CL_SET_BLKARG(ctx_a);
  772. CL_SET_BLKARG(ctx_b);
  773. CL_SET_BLKARG(ctx_c);
  774. CL_SET_BLKARG(ctx_d);
  775. CL_SET_BLKARG(ctx_e);
  776. CL_SET_BLKARG(ctx_f);
  777. CL_SET_BLKARG(ctx_g);
  778. CL_SET_BLKARG(ctx_h);
  779. CL_SET_BLKARG(cty_b);
  780. CL_SET_BLKARG(cty_c);
  781. CL_SET_BLKARG(cty_d);
  782. CL_SET_BLKARG(cty_f);
  783. CL_SET_BLKARG(cty_g);
  784. CL_SET_BLKARG(cty_h);
  785. nonces = alloca(sizeof(uint) * vwidth);
  786. for (i = 0; i < vwidth; i++)
  787. nonces[i] = blk->nonce + i;
  788. CL_SET_VARG(vwidth, nonces);
  789. CL_SET_BLKARG(W16);
  790. CL_SET_BLKARG(W17);
  791. CL_SET_BLKARG(PreVal4_2);
  792. CL_SET_BLKARG(PreVal0);
  793. CL_SET_BLKARG(PreW18);
  794. CL_SET_BLKARG(PreW19);
  795. CL_SET_BLKARG(PreW31);
  796. CL_SET_BLKARG(PreW32);
  797. CL_SET_ARG(clState->outputBuffer);
  798. return status;
  799. }
  800. static cl_int queue_diakgcn_kernel(_clState *clState, dev_blk_ctx *blk,
  801. __maybe_unused cl_uint threads)
  802. {
  803. cl_kernel *kernel = &clState->kernel;
  804. unsigned int num = 0;
  805. cl_int status = 0;
  806. if (!clState->goffset) {
  807. cl_uint vwidth = clState->vwidth;
  808. uint *nonces = alloca(sizeof(uint) * vwidth);
  809. unsigned int i;
  810. for (i = 0; i < vwidth; i++)
  811. nonces[i] = blk->nonce + i;
  812. CL_SET_VARG(vwidth, nonces);
  813. }
  814. CL_SET_BLKARG(PreVal0);
  815. CL_SET_BLKARG(PreVal4_2);
  816. CL_SET_BLKARG(cty_h);
  817. CL_SET_BLKARG(D1A);
  818. CL_SET_BLKARG(cty_b);
  819. CL_SET_BLKARG(cty_c);
  820. CL_SET_BLKARG(cty_f);
  821. CL_SET_BLKARG(cty_g);
  822. CL_SET_BLKARG(C1addK5);
  823. CL_SET_BLKARG(B1addK6);
  824. CL_SET_BLKARG(PreVal0addK7);
  825. CL_SET_BLKARG(W16addK16);
  826. CL_SET_BLKARG(W17addK17);
  827. CL_SET_BLKARG(PreW18);
  828. CL_SET_BLKARG(PreW19);
  829. CL_SET_BLKARG(W16);
  830. CL_SET_BLKARG(W17);
  831. CL_SET_BLKARG(PreW31);
  832. CL_SET_BLKARG(PreW32);
  833. CL_SET_BLKARG(ctx_a);
  834. CL_SET_BLKARG(ctx_b);
  835. CL_SET_BLKARG(ctx_c);
  836. CL_SET_BLKARG(ctx_d);
  837. CL_SET_BLKARG(ctx_e);
  838. CL_SET_BLKARG(ctx_f);
  839. CL_SET_BLKARG(ctx_g);
  840. CL_SET_BLKARG(ctx_h);
  841. CL_SET_BLKARG(zeroA);
  842. CL_SET_BLKARG(zeroB);
  843. CL_SET_BLKARG(oneA);
  844. CL_SET_BLKARG(twoA);
  845. CL_SET_BLKARG(threeA);
  846. CL_SET_BLKARG(fourA);
  847. CL_SET_BLKARG(fiveA);
  848. CL_SET_BLKARG(sixA);
  849. CL_SET_BLKARG(sevenA);
  850. CL_SET_ARG(clState->outputBuffer);
  851. return status;
  852. }
  853. static cl_int queue_diablo_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  854. {
  855. cl_kernel *kernel = &clState->kernel;
  856. unsigned int num = 0;
  857. cl_int status = 0;
  858. if (!clState->goffset) {
  859. cl_uint vwidth = clState->vwidth;
  860. uint *nonces = alloca(sizeof(uint) * vwidth);
  861. unsigned int i;
  862. for (i = 0; i < vwidth; i++)
  863. nonces[i] = blk->nonce + (i * threads);
  864. CL_SET_VARG(vwidth, nonces);
  865. }
  866. CL_SET_BLKARG(PreVal0);
  867. CL_SET_BLKARG(PreVal0addK7);
  868. CL_SET_BLKARG(PreVal4addT1);
  869. CL_SET_BLKARG(PreW18);
  870. CL_SET_BLKARG(PreW19);
  871. CL_SET_BLKARG(W16);
  872. CL_SET_BLKARG(W17);
  873. CL_SET_BLKARG(W16addK16);
  874. CL_SET_BLKARG(W17addK17);
  875. CL_SET_BLKARG(PreW31);
  876. CL_SET_BLKARG(PreW32);
  877. CL_SET_BLKARG(D1A);
  878. CL_SET_BLKARG(cty_b);
  879. CL_SET_BLKARG(cty_c);
  880. CL_SET_BLKARG(cty_h);
  881. CL_SET_BLKARG(cty_f);
  882. CL_SET_BLKARG(cty_g);
  883. CL_SET_BLKARG(C1addK5);
  884. CL_SET_BLKARG(B1addK6);
  885. CL_SET_BLKARG(ctx_a);
  886. CL_SET_BLKARG(ctx_b);
  887. CL_SET_BLKARG(ctx_c);
  888. CL_SET_BLKARG(ctx_d);
  889. CL_SET_BLKARG(ctx_e);
  890. CL_SET_BLKARG(ctx_f);
  891. CL_SET_BLKARG(ctx_g);
  892. CL_SET_BLKARG(ctx_h);
  893. CL_SET_ARG(clState->outputBuffer);
  894. return status;
  895. }
  896. #ifdef USE_SCRYPT
  897. static cl_int queue_scrypt_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_unused cl_uint threads)
  898. {
  899. unsigned char *midstate = blk->work->midstate;
  900. cl_kernel *kernel = &clState->kernel;
  901. unsigned int num = 0;
  902. cl_uint le_target;
  903. cl_int status = 0;
  904. le_target = *(cl_uint *)(blk->work->device_target + 28);
  905. clState->cldata = blk->work->data;
  906. status = clEnqueueWriteBuffer(clState->commandQueue, clState->CLbuffer0, true, 0, 80, clState->cldata, 0, NULL,NULL);
  907. CL_SET_ARG(clState->CLbuffer0);
  908. CL_SET_ARG(clState->outputBuffer);
  909. CL_SET_ARG(clState->padbuffer8);
  910. CL_SET_VARG(4, &midstate[0]);
  911. CL_SET_VARG(4, &midstate[16]);
  912. CL_SET_ARG(le_target);
  913. return status;
  914. }
  915. #endif
  916. static void set_threads_hashes(unsigned int vectors,int64_t *hashes, size_t *globalThreads,
  917. unsigned int minthreads, __maybe_unused int *intensity)
  918. {
  919. unsigned int threads = 0;
  920. while (threads < minthreads) {
  921. threads = 1 << ((opt_scrypt ? 0 : 15) + *intensity);
  922. if (threads < minthreads) {
  923. if (likely(*intensity < MAX_INTENSITY))
  924. (*intensity)++;
  925. else
  926. threads = minthreads;
  927. }
  928. }
  929. *globalThreads = threads;
  930. *hashes = threads * vectors;
  931. }
  932. #endif /* HAVE_OPENCL */
  933. #ifdef HAVE_OPENCL
  934. /* We have only one thread that ever re-initialises GPUs, thus if any GPU
  935. * init command fails due to a completely wedged GPU, the thread will never
  936. * return, unable to harm other GPUs. If it does return, it means we only had
  937. * a soft failure and then the reinit_gpu thread is ready to tackle another
  938. * GPU */
  939. void *reinit_gpu(void *userdata)
  940. {
  941. struct thr_info *mythr = userdata;
  942. struct cgpu_info *cgpu;
  943. struct thr_info *thr;
  944. struct timeval now;
  945. char name[256];
  946. int thr_id;
  947. int gpu;
  948. pthread_detach(pthread_self());
  949. select_cgpu:
  950. cgpu = tq_pop(mythr->q, NULL);
  951. if (!cgpu)
  952. goto out;
  953. if (clDevicesNum() != nDevs) {
  954. applog(LOG_WARNING, "Hardware not reporting same number of active devices, will not attempt to restart GPU");
  955. goto out;
  956. }
  957. gpu = cgpu->device_id;
  958. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  959. thr = get_thread(thr_id);
  960. cgpu = thr->cgpu;
  961. if (cgpu->drv->drv_id != DRIVER_OPENCL)
  962. continue;
  963. if (dev_from_id(thr_id) != gpu)
  964. continue;
  965. thr = get_thread(thr_id);
  966. if (!thr) {
  967. applog(LOG_WARNING, "No reference to thread %d exists", thr_id);
  968. continue;
  969. }
  970. thr->rolling = thr->cgpu->rolling = 0;
  971. /* Reports the last time we tried to revive a sick GPU */
  972. cgtime(&thr->sick);
  973. if (!pthread_cancel(thr->pth)) {
  974. applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
  975. } else
  976. applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
  977. }
  978. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  979. int virtual_gpu;
  980. thr = get_thread(thr_id);
  981. cgpu = thr->cgpu;
  982. if (cgpu->drv->drv_id != DRIVER_OPENCL)
  983. continue;
  984. if (dev_from_id(thr_id) != gpu)
  985. continue;
  986. virtual_gpu = cgpu->virtual_gpu;
  987. /* Lose this ram cause we may get stuck here! */
  988. //tq_freeze(thr->q);
  989. thr->q = tq_new();
  990. if (!thr->q)
  991. quit(1, "Failed to tq_new in reinit_gpu");
  992. /* Lose this ram cause we may dereference in the dying thread! */
  993. //free(clState);
  994. applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
  995. clStates[thr_id] = initCl(virtual_gpu, name, sizeof(name));
  996. if (!clStates[thr_id]) {
  997. applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
  998. goto select_cgpu;
  999. }
  1000. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1001. if (unlikely(thr_info_create(thr, NULL, miner_thread, thr))) {
  1002. applog(LOG_ERR, "thread %d create failed", thr_id);
  1003. return NULL;
  1004. }
  1005. applog(LOG_WARNING, "Thread %d restarted", thr_id);
  1006. }
  1007. cgtime(&now);
  1008. get_datestamp(cgpu->init, &now);
  1009. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  1010. thr = get_thread(thr_id);
  1011. cgpu = thr->cgpu;
  1012. if (cgpu->drv->drv_id != DRIVER_OPENCL)
  1013. continue;
  1014. if (dev_from_id(thr_id) != gpu)
  1015. continue;
  1016. cgsem_post(&thr->sem);
  1017. }
  1018. goto select_cgpu;
  1019. out:
  1020. return NULL;
  1021. }
  1022. #else
  1023. void *reinit_gpu(__maybe_unused void *userdata)
  1024. {
  1025. return NULL;
  1026. }
  1027. #endif
  1028. #ifdef HAVE_OPENCL
  1029. static void opencl_detect()
  1030. {
  1031. int i;
  1032. nDevs = clDevicesNum();
  1033. if (nDevs < 0) {
  1034. applog(LOG_ERR, "clDevicesNum returned error, no GPUs usable");
  1035. nDevs = 0;
  1036. }
  1037. if (!nDevs)
  1038. return;
  1039. /* If opt_g_threads is not set, use default 1 thread on scrypt and
  1040. * 2 for regular mining */
  1041. if (opt_g_threads == -1) {
  1042. if (opt_scrypt)
  1043. opt_g_threads = 1;
  1044. else
  1045. opt_g_threads = 2;
  1046. }
  1047. if (opt_scrypt)
  1048. opencl_drv.max_diff = 65536;
  1049. for (i = 0; i < nDevs; ++i) {
  1050. struct cgpu_info *cgpu;
  1051. cgpu = &gpus[i];
  1052. cgpu->deven = DEV_ENABLED;
  1053. cgpu->drv = &opencl_drv;
  1054. cgpu->device_id = i;
  1055. cgpu->threads = opt_g_threads;
  1056. cgpu->virtual_gpu = i;
  1057. add_cgpu(cgpu);
  1058. }
  1059. if (!opt_noadl)
  1060. init_adl(nDevs);
  1061. }
  1062. static void reinit_opencl_device(struct cgpu_info *gpu)
  1063. {
  1064. tq_push(control_thr[gpur_thr_id].q, gpu);
  1065. }
  1066. #ifdef HAVE_ADL
  1067. static void get_opencl_statline_before(char *buf, struct cgpu_info *gpu)
  1068. {
  1069. if (gpu->has_adl) {
  1070. int gpuid = gpu->device_id;
  1071. float gt = gpu_temp(gpuid);
  1072. int gf = gpu_fanspeed(gpuid);
  1073. int gp;
  1074. if (gt != -1)
  1075. tailsprintf(buf, "%5.1fC ", gt);
  1076. else
  1077. tailsprintf(buf, " ", gt);
  1078. if (gf != -1)
  1079. tailsprintf(buf, "%4dRPM ", 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