driver-opencl.c 38 KB

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