driver-opencl.c 53 KB

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