driver-opencl.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  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, thread_no = 0;
  439. for (i = 0; i < mining_threads; i++) {
  440. struct thr_info *thr = &thr_info[i];
  441. if (thr->cgpu != cgpu)
  442. continue;
  443. if (!thread_no++)
  444. continue;
  445. if (!thr->pause && cgpu->dynamic) {
  446. applog(LOG_WARNING, "Disabling extra threads due to dynamic mode.");
  447. applog(LOG_WARNING, "Tune dynamic intensity with --gpu-dyninterval");
  448. }
  449. thr->pause = cgpu->dynamic;
  450. if (!cgpu->dynamic && cgpu->deven != DEV_DISABLED)
  451. tq_push(thr->q, &ping);
  452. }
  453. }
  454. struct device_api opencl_api;
  455. #endif /* HAVE_OPENCL */
  456. #if defined(HAVE_OPENCL) && defined(HAVE_CURSES)
  457. void manage_gpu(void)
  458. {
  459. struct thr_info *thr;
  460. int selected, gpu, i;
  461. char checkin[40];
  462. char input;
  463. if (!opt_g_threads)
  464. return;
  465. opt_loginput = true;
  466. immedok(logwin, true);
  467. clear_logwin();
  468. retry:
  469. for (gpu = 0; gpu < nDevs; gpu++) {
  470. struct cgpu_info *cgpu = &gpus[gpu];
  471. wlog("GPU %d: %.1f / %.1f Mh/s | A:%d R:%d HW:%d U:%.2f/m I:%d\n",
  472. gpu, cgpu->rolling, cgpu->total_mhashes / total_secs,
  473. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  474. cgpu->utility, cgpu->intensity);
  475. #ifdef HAVE_ADL
  476. if (gpus[gpu].has_adl) {
  477. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  478. float temp = 0, vddc = 0;
  479. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune)) {
  480. char logline[255];
  481. strcpy(logline, ""); // In case it has no data
  482. if (temp != -1)
  483. sprintf(logline, "%.1f C ", temp);
  484. if (fanspeed != -1 || fanpercent != -1) {
  485. tailsprintf(logline, "F: ");
  486. if (fanpercent != -1)
  487. tailsprintf(logline, "%d%% ", fanpercent);
  488. if (fanspeed != -1)
  489. tailsprintf(logline, "(%d RPM) ", fanspeed);
  490. tailsprintf(logline, " ");
  491. }
  492. if (engineclock != -1)
  493. tailsprintf(logline, "E: %d MHz ", engineclock);
  494. if (memclock != -1)
  495. tailsprintf(logline, "M: %d Mhz ", memclock);
  496. if (vddc != -1)
  497. tailsprintf(logline, "V: %.3fV ", vddc);
  498. if (activity != -1)
  499. tailsprintf(logline, "A: %d%% ", activity);
  500. if (powertune != -1)
  501. tailsprintf(logline, "P: %d%%", powertune);
  502. tailsprintf(logline, "\n");
  503. wlog(logline);
  504. }
  505. }
  506. #endif
  507. wlog("Last initialised: %s\n", cgpu->init);
  508. wlog("Intensity: ");
  509. if (gpus[gpu].dynamic)
  510. wlog("Dynamic (only one thread in use)\n");
  511. else
  512. wlog("%d\n", gpus[gpu].intensity);
  513. for (i = 0; i < mining_threads; i++) {
  514. thr = &thr_info[i];
  515. if (thr->cgpu != cgpu)
  516. continue;
  517. get_datestamp(checkin, &thr->last);
  518. wlog("Thread %d: %.1f Mh/s %s ", i, thr->rolling, cgpu->deven != DEV_DISABLED ? "Enabled" : "Disabled");
  519. switch (cgpu->status) {
  520. default:
  521. case LIFE_WELL:
  522. wlog("ALIVE");
  523. break;
  524. case LIFE_SICK:
  525. wlog("SICK reported in %s", checkin);
  526. break;
  527. case LIFE_DEAD:
  528. wlog("DEAD reported in %s", checkin);
  529. break;
  530. case LIFE_NOSTART:
  531. wlog("Never started");
  532. break;
  533. }
  534. if (thr->pause)
  535. wlog(" paused");
  536. wlog("\n");
  537. }
  538. wlog("\n");
  539. }
  540. wlogprint("[E]nable [D]isable [I]ntensity [R]estart GPU %s\n",adl_active ? "[C]hange settings" : "");
  541. wlogprint("Or press any other key to continue\n");
  542. input = getch();
  543. if (nDevs == 1)
  544. selected = 0;
  545. else
  546. selected = -1;
  547. if (INPUTEQ(e)) {
  548. struct cgpu_info *cgpu;
  549. if (selected)
  550. selected = curses_int("Select GPU to enable");
  551. if (selected < 0 || selected >= nDevs) {
  552. wlogprint("Invalid selection\n");
  553. goto retry;
  554. }
  555. if (gpus[selected].deven != DEV_DISABLED) {
  556. wlogprint("Device already enabled\n");
  557. goto retry;
  558. }
  559. gpus[selected].deven = DEV_ENABLED;
  560. for (i = 0; i < mining_threads; ++i) {
  561. thr = &thr_info[i];
  562. cgpu = thr->cgpu;
  563. if (cgpu->api != &opencl_api)
  564. continue;
  565. if (dev_from_id(i) != selected)
  566. continue;
  567. if (cgpu->status != LIFE_WELL) {
  568. wlogprint("Must restart device before enabling it");
  569. goto retry;
  570. }
  571. applog(LOG_DEBUG, "Pushing ping to thread %d", thr->id);
  572. tq_push(thr->q, &ping);
  573. }
  574. goto retry;
  575. } if (INPUTEQ(d)) {
  576. if (selected)
  577. selected = curses_int("Select GPU to disable");
  578. if (selected < 0 || selected >= nDevs) {
  579. wlogprint("Invalid selection\n");
  580. goto retry;
  581. }
  582. if (gpus[selected].deven == DEV_DISABLED) {
  583. wlogprint("Device already disabled\n");
  584. goto retry;
  585. }
  586. gpus[selected].deven = DEV_DISABLED;
  587. goto retry;
  588. } else if (INPUTEQ(i)) {
  589. int intensity;
  590. char *intvar;
  591. if (selected)
  592. selected = curses_int("Select GPU to change intensity on");
  593. if (selected < 0 || selected >= nDevs) {
  594. wlogprint("Invalid selection\n");
  595. goto retry;
  596. }
  597. intvar = curses_input("Set GPU scan intensity (d or " _MIN_INTENSITY_STR " -> " _MAX_INTENSITY_STR ")");
  598. if (!intvar) {
  599. wlogprint("Invalid input\n");
  600. goto retry;
  601. }
  602. if (!strncasecmp(intvar, "d", 1)) {
  603. wlogprint("Dynamic mode enabled on gpu %d\n", selected);
  604. gpus[selected].dynamic = true;
  605. pause_dynamic_threads(selected);
  606. free(intvar);
  607. goto retry;
  608. }
  609. intensity = atoi(intvar);
  610. free(intvar);
  611. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  612. wlogprint("Invalid selection\n");
  613. goto retry;
  614. }
  615. gpus[selected].dynamic = false;
  616. gpus[selected].intensity = intensity;
  617. wlogprint("Intensity on gpu %d set to %d\n", selected, intensity);
  618. pause_dynamic_threads(selected);
  619. goto retry;
  620. } else if (INPUTEQ(r)) {
  621. if (selected)
  622. selected = curses_int("Select GPU to attempt to restart");
  623. if (selected < 0 || selected >= nDevs) {
  624. wlogprint("Invalid selection\n");
  625. goto retry;
  626. }
  627. wlogprint("Attempting to restart threads of GPU %d\n", selected);
  628. reinit_device(&gpus[selected]);
  629. goto retry;
  630. } else if (adl_active && (INPUTEQ(c))) {
  631. if (selected)
  632. selected = curses_int("Select GPU to change settings on");
  633. if (selected < 0 || selected >= nDevs) {
  634. wlogprint("Invalid selection\n");
  635. goto retry;
  636. }
  637. change_gpusettings(selected);
  638. goto retry;
  639. } else
  640. clear_logwin();
  641. immedok(logwin, false);
  642. opt_loginput = false;
  643. }
  644. #else
  645. void manage_gpu(void)
  646. {
  647. }
  648. #endif
  649. #ifdef HAVE_OPENCL
  650. static _clState *clStates[MAX_GPUDEVICES];
  651. #define CL_SET_BLKARG(blkvar) status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->blkvar)
  652. #define CL_SET_ARG(var) status |= clSetKernelArg(*kernel, num++, sizeof(var), (void *)&var)
  653. #define CL_SET_VARG(args, var) status |= clSetKernelArg(*kernel, num++, args * sizeof(uint), (void *)var)
  654. static cl_int queue_poclbm_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  655. {
  656. cl_kernel *kernel = &clState->kernel;
  657. unsigned int num = 0;
  658. cl_int status = 0;
  659. CL_SET_BLKARG(ctx_a);
  660. CL_SET_BLKARG(ctx_b);
  661. CL_SET_BLKARG(ctx_c);
  662. CL_SET_BLKARG(ctx_d);
  663. CL_SET_BLKARG(ctx_e);
  664. CL_SET_BLKARG(ctx_f);
  665. CL_SET_BLKARG(ctx_g);
  666. CL_SET_BLKARG(ctx_h);
  667. CL_SET_BLKARG(cty_b);
  668. CL_SET_BLKARG(cty_c);
  669. CL_SET_BLKARG(cty_f);
  670. CL_SET_BLKARG(cty_g);
  671. CL_SET_BLKARG(cty_h);
  672. if (!clState->goffset) {
  673. cl_uint vwidth = clState->vwidth;
  674. uint *nonces = alloca(sizeof(uint) * vwidth);
  675. unsigned int i;
  676. for (i = 0; i < vwidth; i++)
  677. nonces[i] = blk->nonce + (i * threads);
  678. CL_SET_VARG(vwidth, nonces);
  679. }
  680. CL_SET_BLKARG(fW0);
  681. CL_SET_BLKARG(fW1);
  682. CL_SET_BLKARG(fW2);
  683. CL_SET_BLKARG(fW3);
  684. CL_SET_BLKARG(fW15);
  685. CL_SET_BLKARG(fW01r);
  686. CL_SET_BLKARG(D1A);
  687. CL_SET_BLKARG(C1addK5);
  688. CL_SET_BLKARG(B1addK6);
  689. CL_SET_BLKARG(W16addK16);
  690. CL_SET_BLKARG(W17addK17);
  691. CL_SET_BLKARG(PreVal4addT1);
  692. CL_SET_BLKARG(PreVal0);
  693. CL_SET_ARG(clState->outputBuffer);
  694. return status;
  695. }
  696. static cl_int queue_phatk_kernel(_clState *clState, dev_blk_ctx *blk,
  697. __maybe_unused cl_uint threads)
  698. {
  699. cl_kernel *kernel = &clState->kernel;
  700. cl_uint vwidth = clState->vwidth;
  701. unsigned int i, num = 0;
  702. cl_int status = 0;
  703. uint *nonces;
  704. CL_SET_BLKARG(ctx_a);
  705. CL_SET_BLKARG(ctx_b);
  706. CL_SET_BLKARG(ctx_c);
  707. CL_SET_BLKARG(ctx_d);
  708. CL_SET_BLKARG(ctx_e);
  709. CL_SET_BLKARG(ctx_f);
  710. CL_SET_BLKARG(ctx_g);
  711. CL_SET_BLKARG(ctx_h);
  712. CL_SET_BLKARG(cty_b);
  713. CL_SET_BLKARG(cty_c);
  714. CL_SET_BLKARG(cty_d);
  715. CL_SET_BLKARG(cty_f);
  716. CL_SET_BLKARG(cty_g);
  717. CL_SET_BLKARG(cty_h);
  718. nonces = alloca(sizeof(uint) * vwidth);
  719. for (i = 0; i < vwidth; i++)
  720. nonces[i] = blk->nonce + i;
  721. CL_SET_VARG(vwidth, nonces);
  722. CL_SET_BLKARG(W16);
  723. CL_SET_BLKARG(W17);
  724. CL_SET_BLKARG(PreVal4_2);
  725. CL_SET_BLKARG(PreVal0);
  726. CL_SET_BLKARG(PreW18);
  727. CL_SET_BLKARG(PreW19);
  728. CL_SET_BLKARG(PreW31);
  729. CL_SET_BLKARG(PreW32);
  730. CL_SET_ARG(clState->outputBuffer);
  731. return status;
  732. }
  733. static cl_int queue_diakgcn_kernel(_clState *clState, dev_blk_ctx *blk,
  734. __maybe_unused cl_uint threads)
  735. {
  736. cl_kernel *kernel = &clState->kernel;
  737. unsigned int num = 0;
  738. cl_int status = 0;
  739. if (!clState->goffset) {
  740. cl_uint vwidth = clState->vwidth;
  741. uint *nonces = alloca(sizeof(uint) * vwidth);
  742. unsigned int i;
  743. for (i = 0; i < vwidth; i++)
  744. nonces[i] = blk->nonce + i;
  745. CL_SET_VARG(vwidth, nonces);
  746. }
  747. CL_SET_BLKARG(PreVal0);
  748. CL_SET_BLKARG(PreVal4_2);
  749. CL_SET_BLKARG(cty_h);
  750. CL_SET_BLKARG(D1A);
  751. CL_SET_BLKARG(cty_b);
  752. CL_SET_BLKARG(cty_c);
  753. CL_SET_BLKARG(cty_f);
  754. CL_SET_BLKARG(cty_g);
  755. CL_SET_BLKARG(C1addK5);
  756. CL_SET_BLKARG(B1addK6);
  757. CL_SET_BLKARG(PreVal0addK7);
  758. CL_SET_BLKARG(W16addK16);
  759. CL_SET_BLKARG(W17addK17);
  760. CL_SET_BLKARG(PreW18);
  761. CL_SET_BLKARG(PreW19);
  762. CL_SET_BLKARG(W16);
  763. CL_SET_BLKARG(W17);
  764. CL_SET_BLKARG(PreW31);
  765. CL_SET_BLKARG(PreW32);
  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(zeroA);
  775. CL_SET_BLKARG(zeroB);
  776. CL_SET_BLKARG(oneA);
  777. CL_SET_BLKARG(twoA);
  778. CL_SET_BLKARG(threeA);
  779. CL_SET_BLKARG(fourA);
  780. CL_SET_BLKARG(fiveA);
  781. CL_SET_BLKARG(sixA);
  782. CL_SET_BLKARG(sevenA);
  783. CL_SET_ARG(clState->outputBuffer);
  784. return status;
  785. }
  786. static cl_int queue_diablo_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  787. {
  788. cl_kernel *kernel = &clState->kernel;
  789. unsigned int num = 0;
  790. cl_int status = 0;
  791. if (!clState->goffset) {
  792. cl_uint vwidth = clState->vwidth;
  793. uint *nonces = alloca(sizeof(uint) * vwidth);
  794. unsigned int i;
  795. for (i = 0; i < vwidth; i++)
  796. nonces[i] = blk->nonce + (i * threads);
  797. CL_SET_VARG(vwidth, nonces);
  798. }
  799. CL_SET_BLKARG(PreVal0);
  800. CL_SET_BLKARG(PreVal0addK7);
  801. CL_SET_BLKARG(PreVal4addT1);
  802. CL_SET_BLKARG(PreW18);
  803. CL_SET_BLKARG(PreW19);
  804. CL_SET_BLKARG(W16);
  805. CL_SET_BLKARG(W17);
  806. CL_SET_BLKARG(W16addK16);
  807. CL_SET_BLKARG(W17addK17);
  808. CL_SET_BLKARG(PreW31);
  809. CL_SET_BLKARG(PreW32);
  810. CL_SET_BLKARG(D1A);
  811. CL_SET_BLKARG(cty_b);
  812. CL_SET_BLKARG(cty_c);
  813. CL_SET_BLKARG(cty_h);
  814. CL_SET_BLKARG(cty_f);
  815. CL_SET_BLKARG(cty_g);
  816. CL_SET_BLKARG(C1addK5);
  817. CL_SET_BLKARG(B1addK6);
  818. CL_SET_BLKARG(ctx_a);
  819. CL_SET_BLKARG(ctx_b);
  820. CL_SET_BLKARG(ctx_c);
  821. CL_SET_BLKARG(ctx_d);
  822. CL_SET_BLKARG(ctx_e);
  823. CL_SET_BLKARG(ctx_f);
  824. CL_SET_BLKARG(ctx_g);
  825. CL_SET_BLKARG(ctx_h);
  826. CL_SET_ARG(clState->outputBuffer);
  827. return status;
  828. }
  829. static void set_threads_hashes(unsigned int vectors, unsigned int *threads,
  830. unsigned int *hashes, size_t *globalThreads,
  831. unsigned int minthreads, int intensity)
  832. {
  833. *threads = 1 << (15 + intensity);
  834. if (*threads < minthreads)
  835. *threads = minthreads;
  836. *globalThreads = *threads;
  837. *hashes = *threads * vectors;
  838. }
  839. #endif /* HAVE_OPENCL */
  840. #ifdef HAVE_OPENCL
  841. /* We have only one thread that ever re-initialises GPUs, thus if any GPU
  842. * init command fails due to a completely wedged GPU, the thread will never
  843. * return, unable to harm other GPUs. If it does return, it means we only had
  844. * a soft failure and then the reinit_gpu thread is ready to tackle another
  845. * GPU */
  846. void *reinit_gpu(void *userdata)
  847. {
  848. struct thr_info *mythr = userdata;
  849. struct cgpu_info *cgpu;
  850. struct thr_info *thr;
  851. struct timeval now;
  852. char name[256];
  853. int thr_id;
  854. int gpu;
  855. pthread_detach(pthread_self());
  856. select_cgpu:
  857. cgpu = tq_pop(mythr->q, NULL);
  858. if (!cgpu)
  859. goto out;
  860. if (clDevicesNum() != nDevs) {
  861. applog(LOG_WARNING, "Hardware not reporting same number of active devices, will not attempt to restart GPU");
  862. goto out;
  863. }
  864. gpu = cgpu->device_id;
  865. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  866. thr = &thr_info[thr_id];
  867. cgpu = thr->cgpu;
  868. if (cgpu->api != &opencl_api)
  869. continue;
  870. if (dev_from_id(thr_id) != gpu)
  871. continue;
  872. thr = &thr_info[thr_id];
  873. if (!thr) {
  874. applog(LOG_WARNING, "No reference to thread %d exists", thr_id);
  875. continue;
  876. }
  877. thr->rolling = thr->cgpu->rolling = 0;
  878. /* Reports the last time we tried to revive a sick GPU */
  879. gettimeofday(&thr->sick, NULL);
  880. if (!pthread_cancel(thr->pth)) {
  881. applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
  882. } else
  883. applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
  884. }
  885. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  886. int virtual_gpu;
  887. thr = &thr_info[thr_id];
  888. cgpu = thr->cgpu;
  889. if (cgpu->api != &opencl_api)
  890. continue;
  891. if (dev_from_id(thr_id) != gpu)
  892. continue;
  893. virtual_gpu = cgpu->virtual_gpu;
  894. /* Lose this ram cause we may get stuck here! */
  895. //tq_freeze(thr->q);
  896. thr->q = tq_new();
  897. if (!thr->q)
  898. quit(1, "Failed to tq_new in reinit_gpu");
  899. /* Lose this ram cause we may dereference in the dying thread! */
  900. //free(clState);
  901. applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
  902. clStates[thr_id] = initCl(virtual_gpu, name, sizeof(name));
  903. if (!clStates[thr_id]) {
  904. applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
  905. goto select_cgpu;
  906. }
  907. applog(LOG_INFO, "initCl() finished. Found %s", name);
  908. if (unlikely(thr_info_create(thr, NULL, miner_thread, thr))) {
  909. applog(LOG_ERR, "thread %d create failed", thr_id);
  910. return NULL;
  911. }
  912. applog(LOG_WARNING, "Thread %d restarted", thr_id);
  913. }
  914. gettimeofday(&now, NULL);
  915. get_datestamp(cgpu->init, &now);
  916. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  917. thr = &thr_info[thr_id];
  918. cgpu = thr->cgpu;
  919. if (cgpu->api != &opencl_api)
  920. continue;
  921. if (dev_from_id(thr_id) != gpu)
  922. continue;
  923. tq_push(thr->q, &ping);
  924. }
  925. goto select_cgpu;
  926. out:
  927. return NULL;
  928. }
  929. #else
  930. void *reinit_gpu(void *userdata)
  931. {
  932. return NULL;
  933. }
  934. #endif
  935. #ifdef HAVE_OPENCL
  936. struct device_api opencl_api;
  937. static void opencl_detect()
  938. {
  939. int i;
  940. nDevs = clDevicesNum();
  941. if (nDevs < 0) {
  942. applog(LOG_ERR, "clDevicesNum returned error, no GPUs usable");
  943. nDevs = 0;
  944. }
  945. if (MAX_DEVICES - total_devices < nDevs)
  946. nDevs = MAX_DEVICES - total_devices;
  947. if (!nDevs)
  948. return;
  949. for (i = 0; i < nDevs; ++i) {
  950. struct cgpu_info *cgpu;
  951. cgpu = &gpus[i];
  952. cgpu->deven = DEV_ENABLED;
  953. cgpu->api = &opencl_api;
  954. cgpu->device_id = i;
  955. cgpu->threads = opt_g_threads;
  956. cgpu->virtual_gpu = i;
  957. add_cgpu(cgpu);
  958. }
  959. if (!opt_noadl)
  960. init_adl(nDevs);
  961. }
  962. static void reinit_opencl_device(struct cgpu_info *gpu)
  963. {
  964. tq_push(thr_info[gpur_thr_id].q, gpu);
  965. }
  966. #ifdef HAVE_ADL
  967. static void get_opencl_statline_before(char *buf, struct cgpu_info *gpu)
  968. {
  969. if (gpu->has_adl) {
  970. int gpuid = gpu->device_id;
  971. float gt = gpu_temp(gpuid);
  972. int gf = gpu_fanspeed(gpuid);
  973. int gp;
  974. if (gt != -1)
  975. tailsprintf(buf, "%5.1fC ", gt);
  976. else
  977. tailsprintf(buf, " ", gt);
  978. if (gf != -1)
  979. tailsprintf(buf, "%4dRPM ", gf);
  980. else if ((gp = gpu_fanpercent(gpuid)) != -1)
  981. tailsprintf(buf, "%3d%% ", gp);
  982. else
  983. tailsprintf(buf, " ");
  984. tailsprintf(buf, "| ");
  985. }
  986. }
  987. #endif
  988. static void get_opencl_statline(char *buf, struct cgpu_info *gpu)
  989. {
  990. tailsprintf(buf, " I:%2d", gpu->intensity);
  991. }
  992. struct opencl_thread_data {
  993. cl_int (*queue_kernel_parameters)(_clState *, dev_blk_ctx *, cl_uint);
  994. uint32_t *res;
  995. struct work *last_work;
  996. struct work _last_work;
  997. };
  998. static uint32_t *blank_res;
  999. static bool opencl_thread_prepare(struct thr_info *thr)
  1000. {
  1001. char name[256];
  1002. struct timeval now;
  1003. struct cgpu_info *cgpu = thr->cgpu;
  1004. int gpu = cgpu->device_id;
  1005. int virtual_gpu = cgpu->virtual_gpu;
  1006. int i = thr->id;
  1007. static bool failmessage = false;
  1008. if (!blank_res)
  1009. blank_res = calloc(BUFFERSIZE, 1);
  1010. if (!blank_res) {
  1011. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1012. return false;
  1013. }
  1014. strcpy(name, "");
  1015. applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
  1016. clStates[i] = initCl(virtual_gpu, name, sizeof(name));
  1017. if (!clStates[i]) {
  1018. if (use_curses)
  1019. enable_curses();
  1020. applog(LOG_ERR, "Failed to init GPU thread %d, disabling device %d", i, gpu);
  1021. if (!failmessage) {
  1022. char *buf;
  1023. applog(LOG_ERR, "Restarting the GPU from the menu will not fix this.");
  1024. applog(LOG_ERR, "Try restarting cgminer.");
  1025. failmessage = true;
  1026. #ifdef HAVE_CURSES
  1027. if (use_curses) {
  1028. buf = curses_input("Press enter to continue");
  1029. if (buf)
  1030. free(buf);
  1031. }
  1032. #endif
  1033. }
  1034. cgpu->deven = DEV_DISABLED;
  1035. cgpu->status = LIFE_NOSTART;
  1036. cgpu->device_last_not_well = time(NULL);
  1037. cgpu->device_not_well_reason = REASON_DEV_NOSTART;
  1038. cgpu->dev_nostart_count++;
  1039. return false;
  1040. }
  1041. if (!cgpu->name)
  1042. cgpu->name = strdup(name);
  1043. if (!cgpu->kname)
  1044. {
  1045. switch (clStates[i]->chosen_kernel) {
  1046. case KL_DIABLO:
  1047. cgpu->kname = "diablo";
  1048. break;
  1049. case KL_DIAKGCN:
  1050. cgpu->kname = "diakgcn";
  1051. break;
  1052. case KL_PHATK:
  1053. cgpu->kname = "phatk";
  1054. break;
  1055. case KL_POCLBM:
  1056. cgpu->kname = "poclbm";
  1057. default:
  1058. break;
  1059. }
  1060. }
  1061. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1062. gettimeofday(&now, NULL);
  1063. get_datestamp(cgpu->init, &now);
  1064. have_opencl = true;
  1065. return true;
  1066. }
  1067. static bool opencl_thread_init(struct thr_info *thr)
  1068. {
  1069. const int thr_id = thr->id;
  1070. struct cgpu_info *gpu = thr->cgpu;
  1071. struct opencl_thread_data *thrdata;
  1072. _clState *clState = clStates[thr_id];
  1073. cl_int status;
  1074. thrdata = calloc(1, sizeof(*thrdata));
  1075. thr->cgpu_data = thrdata;
  1076. if (!thrdata) {
  1077. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1078. return false;
  1079. }
  1080. switch (clState->chosen_kernel) {
  1081. case KL_POCLBM:
  1082. thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
  1083. break;
  1084. case KL_PHATK:
  1085. thrdata->queue_kernel_parameters = &queue_phatk_kernel;
  1086. break;
  1087. case KL_DIAKGCN:
  1088. thrdata->queue_kernel_parameters = &queue_diakgcn_kernel;
  1089. break;
  1090. default:
  1091. case KL_DIABLO:
  1092. thrdata->queue_kernel_parameters = &queue_diablo_kernel;
  1093. break;
  1094. }
  1095. thrdata->res = calloc(BUFFERSIZE, 1);
  1096. if (!thrdata->res) {
  1097. free(thrdata);
  1098. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1099. return false;
  1100. }
  1101. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  1102. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1103. if (unlikely(status != CL_SUCCESS)) {
  1104. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1105. return false;
  1106. }
  1107. gpu->status = LIFE_WELL;
  1108. gpu->device_last_well = time(NULL);
  1109. return true;
  1110. }
  1111. static void opencl_free_work(struct thr_info *thr, struct work *work)
  1112. {
  1113. const int thr_id = thr->id;
  1114. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1115. _clState *clState = clStates[thr_id];
  1116. clFinish(clState->commandQueue);
  1117. if (thrdata->res[FOUND]) {
  1118. thrdata->last_work = &thrdata->_last_work;
  1119. memcpy(thrdata->last_work, work, sizeof(*thrdata->last_work));
  1120. }
  1121. }
  1122. static bool opencl_prepare_work(struct thr_info __maybe_unused *thr, struct work *work)
  1123. {
  1124. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1125. return true;
  1126. }
  1127. extern int opt_dynamic_interval;
  1128. static uint64_t opencl_scanhash(struct thr_info *thr, struct work *work,
  1129. uint64_t __maybe_unused max_nonce)
  1130. {
  1131. const int thr_id = thr->id;
  1132. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1133. struct cgpu_info *gpu = thr->cgpu;
  1134. _clState *clState = clStates[thr_id];
  1135. const cl_kernel *kernel = &clState->kernel;
  1136. double gpu_ms_average = 7;
  1137. cl_int status;
  1138. size_t globalThreads[1];
  1139. size_t localThreads[1] = { clState->wsize };
  1140. unsigned int threads;
  1141. unsigned int hashes;
  1142. struct timeval tv_gpustart, tv_gpuend, diff;
  1143. suseconds_t gpu_us;
  1144. gettimeofday(&tv_gpustart, NULL);
  1145. timeval_subtract(&diff, &tv_gpustart, &tv_gpuend);
  1146. /* This finish flushes the readbuffer set with CL_FALSE later */
  1147. clFinish(clState->commandQueue);
  1148. gettimeofday(&tv_gpuend, NULL);
  1149. timeval_subtract(&diff, &tv_gpuend, &tv_gpustart);
  1150. gpu_us = diff.tv_sec * 1000000 + diff.tv_usec;
  1151. decay_time(&gpu_ms_average, gpu_us / 1000);
  1152. if (gpu->dynamic) {
  1153. /* Try to not let the GPU be out for longer than 6ms, but
  1154. * increase intensity when the system is idle, unless
  1155. * dynamic is disabled. */
  1156. if (gpu_ms_average > opt_dynamic_interval) {
  1157. if (gpu->intensity > MIN_INTENSITY)
  1158. --gpu->intensity;
  1159. } else if (gpu_ms_average < ((opt_dynamic_interval / 2) ? : 1)) {
  1160. if (gpu->intensity < MAX_INTENSITY)
  1161. ++gpu->intensity;
  1162. }
  1163. }
  1164. set_threads_hashes(clState->vwidth, &threads, &hashes, globalThreads,
  1165. localThreads[0], gpu->intensity);
  1166. if (hashes > gpu->max_hashes)
  1167. gpu->max_hashes = hashes;
  1168. status = thrdata->queue_kernel_parameters(clState, &work->blk, globalThreads[0]);
  1169. if (unlikely(status != CL_SUCCESS)) {
  1170. applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
  1171. return 0;
  1172. }
  1173. /* MAXBUFFERS entry is used as a flag to say nonces exist */
  1174. if (thrdata->res[FOUND]) {
  1175. /* Clear the buffer again */
  1176. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1177. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1178. if (unlikely(status != CL_SUCCESS)) {
  1179. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1180. return 0;
  1181. }
  1182. if (unlikely(thrdata->last_work)) {
  1183. applog(LOG_DEBUG, "GPU %d found something in last work?", gpu->device_id);
  1184. postcalc_hash_async(thr, thrdata->last_work, thrdata->res);
  1185. thrdata->last_work = NULL;
  1186. } else {
  1187. applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
  1188. postcalc_hash_async(thr, work, thrdata->res);
  1189. }
  1190. memset(thrdata->res, 0, BUFFERSIZE);
  1191. clFinish(clState->commandQueue);
  1192. }
  1193. if (clState->goffset) {
  1194. size_t global_work_offset[1];
  1195. global_work_offset[0] = work->blk.nonce;
  1196. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, global_work_offset,
  1197. globalThreads, localThreads, 0, NULL, NULL);
  1198. } else
  1199. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1200. globalThreads, localThreads, 0, NULL, NULL);
  1201. if (unlikely(status != CL_SUCCESS)) {
  1202. applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)");
  1203. return 0;
  1204. }
  1205. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1206. BUFFERSIZE, thrdata->res, 0, NULL, NULL);
  1207. if (unlikely(status != CL_SUCCESS)) {
  1208. applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)");
  1209. return 0;
  1210. }
  1211. /* The amount of work scanned can fluctuate when intensity changes
  1212. * and since we do this one cycle behind, we increment the work more
  1213. * than enough to prevent repeating work */
  1214. work->blk.nonce += gpu->max_hashes;
  1215. return hashes;
  1216. }
  1217. static void opencl_thread_shutdown(struct thr_info *thr)
  1218. {
  1219. const int thr_id = thr->id;
  1220. _clState *clState = clStates[thr_id];
  1221. clReleaseCommandQueue(clState->commandQueue);
  1222. clReleaseKernel(clState->kernel);
  1223. clReleaseProgram(clState->program);
  1224. clReleaseContext(clState->context);
  1225. }
  1226. struct device_api opencl_api = {
  1227. .dname = "opencl",
  1228. .name = "GPU",
  1229. .api_detect = opencl_detect,
  1230. .reinit_device = reinit_opencl_device,
  1231. #ifdef HAVE_ADL
  1232. .get_statline_before = get_opencl_statline_before,
  1233. #endif
  1234. .get_statline = get_opencl_statline,
  1235. .thread_prepare = opencl_thread_prepare,
  1236. .thread_init = opencl_thread_init,
  1237. .free_work = opencl_free_work,
  1238. .prepare_work = opencl_prepare_work,
  1239. .scanhash = opencl_scanhash,
  1240. .thread_shutdown = opencl_thread_shutdown,
  1241. };
  1242. #endif