driver-opencl.c 35 KB

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