driver-opencl.c 53 KB

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