driver-opencl.c 53 KB

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