driver-opencl.c 37 KB

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