driver-opencl.c 37 KB

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