driver-opencl.c 47 KB

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