driver-opencl.c 37 KB

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