driver-opencl.c 46 KB

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