driver-opencl.c 57 KB

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