driver-opencl.c 47 KB

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