driver-opencl.c 38 KB

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