driver-opencl.c 47 KB

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