driver-opencl.c 56 KB

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