driver-opencl.c 46 KB

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