driver-opencl.c 46 KB

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