driver-opencl.c 38 KB

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