driver-opencl.c 36 KB

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