driver-opencl.c 48 KB

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