driver-opencl.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  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. #endif
  424. void write_config_opencl(FILE * const fcfg)
  425. {
  426. int i;
  427. #ifdef HAVE_OPENCL
  428. if (nDevs) {
  429. /* Write GPU device values */
  430. fputs(",\n\"intensity\" : \"", fcfg);
  431. for(i = 0; i < nDevs; i++)
  432. {
  433. struct cgpu_info * const cgpu = &gpus[i];
  434. struct opencl_device_data * const data = cgpu->device_data;
  435. if (i > 0)
  436. fputc(',', fcfg);
  437. if (data->dynamic)
  438. fputc('d', fcfg);
  439. else
  440. fprintf(fcfg, "%d", data->intensity);
  441. }
  442. fputs("\",\n\"vectors\" : \"", fcfg);
  443. for(i = 0; i < nDevs; i++)
  444. {
  445. struct cgpu_info * const cgpu = &gpus[i];
  446. struct opencl_device_data * const data = cgpu->device_data;
  447. fprintf(fcfg, "%s%d", i > 0 ? "," : "",
  448. data->vwidth);
  449. }
  450. fputs("\",\n\"worksize\" : \"", fcfg);
  451. for(i = 0; i < nDevs; i++)
  452. {
  453. struct cgpu_info * const cgpu = &gpus[i];
  454. struct opencl_device_data * const data = cgpu->device_data;
  455. fprintf(fcfg, "%s%d", i > 0 ? "," : "",
  456. (int)data->work_size);
  457. }
  458. fputs("\",\n\"kernel\" : \"", fcfg);
  459. for(i = 0; i < nDevs; i++) {
  460. struct cgpu_info * const cgpu = &gpus[i];
  461. struct opencl_device_data * const data = cgpu->device_data;
  462. fprintf(fcfg, "%s", i > 0 ? "," : "");
  463. switch (data->kernel) {
  464. case KL_NONE: // Shouldn't happen
  465. break;
  466. case KL_POCLBM:
  467. fprintf(fcfg, "poclbm");
  468. break;
  469. case KL_PHATK:
  470. fprintf(fcfg, "phatk");
  471. break;
  472. case KL_DIAKGCN:
  473. fprintf(fcfg, "diakgcn");
  474. break;
  475. case KL_DIABLO:
  476. fprintf(fcfg, "diablo");
  477. break;
  478. case KL_SCRYPT:
  479. fprintf(fcfg, "scrypt");
  480. break;
  481. }
  482. }
  483. #ifdef USE_SCRYPT
  484. fputs("\",\n\"lookup-gap\" : \"", fcfg);
  485. for(i = 0; i < nDevs; i++)
  486. {
  487. struct cgpu_info * const cgpu = &gpus[i];
  488. struct opencl_device_data * const data = cgpu->device_data;
  489. fprintf(fcfg, "%s%d", i > 0 ? "," : "",
  490. (int)data->opt_lg);
  491. }
  492. fputs("\",\n\"thread-concurrency\" : \"", fcfg);
  493. for(i = 0; i < nDevs; i++)
  494. {
  495. struct cgpu_info * const cgpu = &gpus[i];
  496. struct opencl_device_data * const data = cgpu->device_data;
  497. fprintf(fcfg, "%s%d", i > 0 ? "," : "",
  498. (int)data->opt_tc);
  499. }
  500. fputs("\",\n\"shaders\" : \"", fcfg);
  501. for(i = 0; i < nDevs; i++)
  502. {
  503. struct cgpu_info * const cgpu = &gpus[i];
  504. struct opencl_device_data * const data = cgpu->device_data;
  505. fprintf(fcfg, "%s%d", i > 0 ? "," : "",
  506. (int)data->shaders);
  507. }
  508. #endif
  509. #ifdef HAVE_ADL
  510. fputs("\",\n\"gpu-engine\" : \"", fcfg);
  511. for(i = 0; i < nDevs; i++)
  512. {
  513. struct cgpu_info * const cgpu = &gpus[i];
  514. struct opencl_device_data * const data = cgpu->device_data;
  515. fprintf(fcfg, "%s%d-%d", i > 0 ? "," : "",
  516. data->min_engine, data->gpu_engine);
  517. }
  518. fputs("\",\n\"gpu-fan\" : \"", fcfg);
  519. for(i = 0; i < nDevs; i++)
  520. {
  521. struct cgpu_info * const cgpu = &gpus[i];
  522. struct opencl_device_data * const data = cgpu->device_data;
  523. fprintf(fcfg, "%s%d-%d", i > 0 ? "," : "",
  524. data->min_fan, data->gpu_fan);
  525. }
  526. fputs("\",\n\"gpu-memclock\" : \"", fcfg);
  527. for(i = 0; i < nDevs; i++)
  528. {
  529. struct cgpu_info * const cgpu = &gpus[i];
  530. struct opencl_device_data * const data = cgpu->device_data;
  531. fprintf(fcfg, "%s%d", i > 0 ? "," : "", data->gpu_memclock);
  532. }
  533. fputs("\",\n\"gpu-memdiff\" : \"", fcfg);
  534. for(i = 0; i < nDevs; i++)
  535. {
  536. struct cgpu_info * const cgpu = &gpus[i];
  537. struct opencl_device_data * const data = cgpu->device_data;
  538. fprintf(fcfg, "%s%d", i > 0 ? "," : "", data->gpu_memdiff);
  539. }
  540. fputs("\",\n\"gpu-powertune\" : \"", fcfg);
  541. for(i = 0; i < nDevs; i++)
  542. {
  543. struct cgpu_info * const cgpu = &gpus[i];
  544. struct opencl_device_data * const data = cgpu->device_data;
  545. fprintf(fcfg, "%s%d", i > 0 ? "," : "", data->gpu_powertune);
  546. }
  547. fputs("\",\n\"gpu-vddc\" : \"", 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%1.3f", i > 0 ? "," : "", data->gpu_vddc);
  553. }
  554. fputs("\",\n\"temp-overheat\" : \"", fcfg);
  555. for(i = 0; i < nDevs; i++)
  556. {
  557. struct cgpu_info * const cgpu = &gpus[i];
  558. struct opencl_device_data * const data = cgpu->device_data;
  559. fprintf(fcfg, "%s%d", i > 0 ? "," : "", data->adl.overtemp);
  560. }
  561. #endif
  562. fputs("\"", fcfg);
  563. }
  564. #endif
  565. #ifdef HAVE_ADL
  566. if (opt_reorder)
  567. fprintf(fcfg, ",\n\"gpu-reorder\" : true");
  568. #endif
  569. }
  570. #ifdef HAVE_OPENCL
  571. BFG_REGISTER_DRIVER(opencl_api)
  572. char *print_ndevs_and_exit(int *ndevs)
  573. {
  574. opt_log_output = true;
  575. opencl_api.drv_detect();
  576. clear_adl(*ndevs);
  577. applog(LOG_INFO, "%i GPU devices max detected", *ndevs);
  578. exit(*ndevs);
  579. }
  580. #endif
  581. struct cgpu_info gpus[MAX_GPUDEVICES]; /* Maximum number apparently possible */
  582. struct cgpu_info *cpus;
  583. #ifdef HAVE_OPENCL
  584. /* In dynamic mode, only the first thread of each device will be in use.
  585. * This potentially could start a thread that was stopped with the start-stop
  586. * options if one were to disable dynamic from the menu on a paused GPU */
  587. void pause_dynamic_threads(int gpu)
  588. {
  589. struct cgpu_info *cgpu = &gpus[gpu];
  590. struct opencl_device_data * const data = cgpu->device_data;
  591. int i;
  592. for (i = 1; i < cgpu->threads; i++) {
  593. struct thr_info *thr;
  594. thr = cgpu->thr[i];
  595. if (!thr->pause && data->dynamic) {
  596. applog(LOG_WARNING, "Disabling extra threads due to dynamic mode.");
  597. applog(LOG_WARNING, "Tune dynamic intensity with --gpu-dyninterval");
  598. }
  599. thr->pause = data->dynamic;
  600. if (!data->dynamic && cgpu->deven != DEV_DISABLED)
  601. mt_enable(thr);
  602. }
  603. }
  604. struct device_drv opencl_api;
  605. #endif /* HAVE_OPENCL */
  606. #if defined(HAVE_OPENCL) && defined(HAVE_CURSES)
  607. static
  608. void opencl_wlogprint_status(struct cgpu_info *cgpu)
  609. {
  610. struct opencl_device_data * const data = cgpu->device_data;
  611. struct thr_info *thr;
  612. int i;
  613. char checkin[40];
  614. double displayed_rolling;
  615. bool mhash_base = !(cgpu->rolling < 1);
  616. char logline[255];
  617. strcpy(logline, ""); // In case it has no data
  618. tailsprintf(logline, sizeof(logline), "I:%s%d ", (data->dynamic ? "d" : ""), data->intensity);
  619. #ifdef HAVE_ADL
  620. if (data->has_adl) {
  621. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  622. float temp = 0, vddc = 0;
  623. if (gpu_stats(cgpu->device_id, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune)) {
  624. if (fanspeed != -1 || fanpercent != -1) {
  625. tailsprintf(logline, sizeof(logline), "F: ");
  626. if (fanspeed > 9999)
  627. fanspeed = 9999;
  628. if (fanpercent != -1)
  629. {
  630. tailsprintf(logline, sizeof(logline), "%d%% ", fanpercent);
  631. if (fanspeed != -1)
  632. tailsprintf(logline, sizeof(logline), "(%d RPM) ", fanspeed);
  633. }
  634. else
  635. tailsprintf(logline, sizeof(logline), "%d RPM ", fanspeed);
  636. tailsprintf(logline, sizeof(logline), " ");
  637. }
  638. if (engineclock != -1)
  639. tailsprintf(logline, sizeof(logline), "E: %d MHz ", engineclock);
  640. if (memclock != -1)
  641. tailsprintf(logline, sizeof(logline), "M: %d MHz ", memclock);
  642. if (vddc != -1)
  643. tailsprintf(logline, sizeof(logline), "V: %.3fV ", vddc);
  644. if (activity != -1)
  645. tailsprintf(logline, sizeof(logline), "A: %d%% ", activity);
  646. if (powertune != -1)
  647. tailsprintf(logline, sizeof(logline), "P: %d%%", powertune);
  648. }
  649. }
  650. #endif
  651. wlogprint("%s\n", logline);
  652. wlogprint("Last initialised: %s\n", cgpu->init);
  653. for (i = 0; i < mining_threads; i++) {
  654. thr = get_thread(i);
  655. if (thr->cgpu != cgpu)
  656. continue;
  657. get_datestamp(checkin, sizeof(checkin), time(NULL) - timer_elapsed(&thr->last, NULL));
  658. displayed_rolling = thr->rolling;
  659. if (!mhash_base)
  660. displayed_rolling *= 1000;
  661. snprintf(logline, sizeof(logline), "Thread %d: %.1f %sh/s %s ", i, displayed_rolling, mhash_base ? "M" : "K" , cgpu->deven != DEV_DISABLED ? "Enabled" : "Disabled");
  662. switch (cgpu->status) {
  663. default:
  664. case LIFE_WELL:
  665. tailsprintf(logline, sizeof(logline), "ALIVE");
  666. break;
  667. case LIFE_SICK:
  668. tailsprintf(logline, sizeof(logline), "SICK reported in %s", checkin);
  669. break;
  670. case LIFE_DEAD:
  671. tailsprintf(logline, sizeof(logline), "DEAD reported in %s", checkin);
  672. break;
  673. case LIFE_INIT:
  674. case LIFE_NOSTART:
  675. tailsprintf(logline, sizeof(logline), "Never started");
  676. break;
  677. }
  678. if (thr->pause)
  679. tailsprintf(logline, sizeof(logline), " paused");
  680. wlogprint("%s\n", logline);
  681. }
  682. }
  683. static
  684. void opencl_tui_wlogprint_choices(struct cgpu_info *cgpu)
  685. {
  686. wlogprint("[I]ntensity [R]estart GPU ");
  687. #ifdef HAVE_ADL
  688. struct opencl_device_data * const data = cgpu->device_data;
  689. if (data->has_adl)
  690. wlogprint("[C]hange settings ");
  691. #endif
  692. }
  693. static
  694. const char *opencl_tui_handle_choice(struct cgpu_info *cgpu, int input)
  695. {
  696. struct opencl_device_data * const data = cgpu->device_data;
  697. switch (input)
  698. {
  699. case 'i': case 'I':
  700. {
  701. int intensity;
  702. char *intvar;
  703. if (opt_scrypt) {
  704. intvar = curses_input("Set GPU scan intensity (d or "
  705. MIN_SCRYPT_INTENSITY_STR " -> "
  706. MAX_SCRYPT_INTENSITY_STR ")");
  707. } else {
  708. intvar = curses_input("Set GPU scan intensity (d or "
  709. MIN_SHA_INTENSITY_STR " -> "
  710. MAX_SHA_INTENSITY_STR ")");
  711. }
  712. if (!intvar)
  713. return "Invalid intensity\n";
  714. if (!strncasecmp(intvar, "d", 1)) {
  715. data->dynamic = true;
  716. pause_dynamic_threads(cgpu->device_id);
  717. free(intvar);
  718. return "Dynamic mode enabled\n";
  719. }
  720. intensity = atoi(intvar);
  721. free(intvar);
  722. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY)
  723. return "Invalid intensity (out of range)\n";
  724. data->dynamic = false;
  725. data->intensity = intensity;
  726. pause_dynamic_threads(cgpu->device_id);
  727. return "Intensity changed\n";
  728. }
  729. case 'r': case 'R':
  730. reinit_device(cgpu);
  731. return "Attempting to restart\n";
  732. case 'c': case 'C':
  733. {
  734. char logline[256];
  735. clear_logwin();
  736. get_statline3(logline, sizeof(logline), cgpu, true, true);
  737. wattron(logwin, A_BOLD);
  738. wlogprint("%s", logline);
  739. wattroff(logwin, A_BOLD);
  740. wlogprint("\n");
  741. change_gpusettings(cgpu->device_id);
  742. return ""; // Force refresh
  743. }
  744. }
  745. return NULL;
  746. }
  747. #endif
  748. #ifdef HAVE_OPENCL
  749. static _clState *clStates[MAX_GPUDEVICES];
  750. #define CL_SET_BLKARG(blkvar) status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->blkvar)
  751. #define CL_SET_ARG(var) status |= clSetKernelArg(*kernel, num++, sizeof(var), (void *)&var)
  752. #define CL_SET_VARG(args, var) status |= clSetKernelArg(*kernel, num++, args * sizeof(uint), (void *)var)
  753. static
  754. struct opencl_work_data *_opencl_work_data(struct work * const work)
  755. {
  756. if (work->device_data)
  757. return work->device_data;
  758. return (work->device_data = calloc(1, sizeof(struct opencl_work_data)));
  759. }
  760. static
  761. cl_int queue_poclbm_kernel(_clState * const clState, struct work * const work, const cl_uint threads)
  762. {
  763. struct opencl_work_data * const blk = _opencl_work_data(work);
  764. cl_kernel *kernel = &clState->kernel;
  765. unsigned int num = 0;
  766. cl_int status = 0;
  767. CL_SET_BLKARG(ctx_a);
  768. CL_SET_BLKARG(ctx_b);
  769. CL_SET_BLKARG(ctx_c);
  770. CL_SET_BLKARG(ctx_d);
  771. CL_SET_BLKARG(ctx_e);
  772. CL_SET_BLKARG(ctx_f);
  773. CL_SET_BLKARG(ctx_g);
  774. CL_SET_BLKARG(ctx_h);
  775. CL_SET_BLKARG(cty_b);
  776. CL_SET_BLKARG(cty_c);
  777. CL_SET_BLKARG(cty_f);
  778. CL_SET_BLKARG(cty_g);
  779. CL_SET_BLKARG(cty_h);
  780. if (!clState->goffset) {
  781. cl_uint vwidth = clState->vwidth;
  782. uint *nonces = alloca(sizeof(uint) * vwidth);
  783. unsigned int i;
  784. for (i = 0; i < vwidth; i++)
  785. nonces[i] = work->blk.nonce + (i * threads);
  786. CL_SET_VARG(vwidth, nonces);
  787. }
  788. CL_SET_BLKARG(fW0);
  789. CL_SET_BLKARG(fW1);
  790. CL_SET_BLKARG(fW2);
  791. CL_SET_BLKARG(fW3);
  792. CL_SET_BLKARG(fW15);
  793. CL_SET_BLKARG(fW01r);
  794. CL_SET_BLKARG(D1A);
  795. CL_SET_BLKARG(C1addK5);
  796. CL_SET_BLKARG(B1addK6);
  797. CL_SET_BLKARG(W16addK16);
  798. CL_SET_BLKARG(W17addK17);
  799. CL_SET_BLKARG(PreVal4addT1);
  800. CL_SET_BLKARG(PreVal0);
  801. CL_SET_ARG(clState->outputBuffer);
  802. return status;
  803. }
  804. static
  805. cl_int queue_phatk_kernel(_clState * const clState, struct work * const work, __maybe_unused const cl_uint threads)
  806. {
  807. struct opencl_work_data * const blk = _opencl_work_data(work);
  808. cl_kernel *kernel = &clState->kernel;
  809. cl_uint vwidth = clState->vwidth;
  810. unsigned int i, num = 0;
  811. cl_int status = 0;
  812. uint *nonces;
  813. CL_SET_BLKARG(ctx_a);
  814. CL_SET_BLKARG(ctx_b);
  815. CL_SET_BLKARG(ctx_c);
  816. CL_SET_BLKARG(ctx_d);
  817. CL_SET_BLKARG(ctx_e);
  818. CL_SET_BLKARG(ctx_f);
  819. CL_SET_BLKARG(ctx_g);
  820. CL_SET_BLKARG(ctx_h);
  821. CL_SET_BLKARG(cty_b);
  822. CL_SET_BLKARG(cty_c);
  823. CL_SET_BLKARG(cty_d);
  824. CL_SET_BLKARG(cty_f);
  825. CL_SET_BLKARG(cty_g);
  826. CL_SET_BLKARG(cty_h);
  827. nonces = alloca(sizeof(uint) * vwidth);
  828. for (i = 0; i < vwidth; i++)
  829. nonces[i] = work->blk.nonce + i;
  830. CL_SET_VARG(vwidth, nonces);
  831. CL_SET_BLKARG(W16);
  832. CL_SET_BLKARG(W17);
  833. CL_SET_BLKARG(PreVal4_2);
  834. CL_SET_BLKARG(PreVal0);
  835. CL_SET_BLKARG(PreW18);
  836. CL_SET_BLKARG(PreW19);
  837. CL_SET_BLKARG(PreW31);
  838. CL_SET_BLKARG(PreW32);
  839. CL_SET_ARG(clState->outputBuffer);
  840. return status;
  841. }
  842. static
  843. cl_int queue_diakgcn_kernel(_clState * const clState, struct work * const work, __maybe_unused const cl_uint threads)
  844. {
  845. struct opencl_work_data * const blk = _opencl_work_data(work);
  846. cl_kernel *kernel = &clState->kernel;
  847. unsigned int num = 0;
  848. cl_int status = 0;
  849. if (!clState->goffset) {
  850. cl_uint vwidth = clState->vwidth;
  851. uint *nonces = alloca(sizeof(uint) * vwidth);
  852. unsigned int i;
  853. for (i = 0; i < vwidth; i++)
  854. nonces[i] = work->blk.nonce + i;
  855. CL_SET_VARG(vwidth, nonces);
  856. }
  857. CL_SET_BLKARG(PreVal0);
  858. CL_SET_BLKARG(PreVal4_2);
  859. CL_SET_BLKARG(cty_h);
  860. CL_SET_BLKARG(D1A);
  861. CL_SET_BLKARG(cty_b);
  862. CL_SET_BLKARG(cty_c);
  863. CL_SET_BLKARG(cty_f);
  864. CL_SET_BLKARG(cty_g);
  865. CL_SET_BLKARG(C1addK5);
  866. CL_SET_BLKARG(B1addK6);
  867. CL_SET_BLKARG(PreVal0addK7);
  868. CL_SET_BLKARG(W16addK16);
  869. CL_SET_BLKARG(W17addK17);
  870. CL_SET_BLKARG(PreW18);
  871. CL_SET_BLKARG(PreW19);
  872. CL_SET_BLKARG(W16);
  873. CL_SET_BLKARG(W17);
  874. CL_SET_BLKARG(PreW31);
  875. CL_SET_BLKARG(PreW32);
  876. CL_SET_BLKARG(ctx_a);
  877. CL_SET_BLKARG(ctx_b);
  878. CL_SET_BLKARG(ctx_c);
  879. CL_SET_BLKARG(ctx_d);
  880. CL_SET_BLKARG(ctx_e);
  881. CL_SET_BLKARG(ctx_f);
  882. CL_SET_BLKARG(ctx_g);
  883. CL_SET_BLKARG(ctx_h);
  884. CL_SET_BLKARG(zeroA);
  885. CL_SET_BLKARG(zeroB);
  886. CL_SET_BLKARG(oneA);
  887. CL_SET_BLKARG(twoA);
  888. CL_SET_BLKARG(threeA);
  889. CL_SET_BLKARG(fourA);
  890. CL_SET_BLKARG(fiveA);
  891. CL_SET_BLKARG(sixA);
  892. CL_SET_BLKARG(sevenA);
  893. CL_SET_ARG(clState->outputBuffer);
  894. return status;
  895. }
  896. static
  897. cl_int queue_diablo_kernel(_clState * const clState, struct work * const work, const cl_uint threads)
  898. {
  899. struct opencl_work_data * const blk = _opencl_work_data(work);
  900. cl_kernel *kernel = &clState->kernel;
  901. unsigned int num = 0;
  902. cl_int status = 0;
  903. if (!clState->goffset) {
  904. cl_uint vwidth = clState->vwidth;
  905. uint *nonces = alloca(sizeof(uint) * vwidth);
  906. unsigned int i;
  907. for (i = 0; i < vwidth; i++)
  908. nonces[i] = work->blk.nonce + (i * threads);
  909. CL_SET_VARG(vwidth, nonces);
  910. }
  911. CL_SET_BLKARG(PreVal0);
  912. CL_SET_BLKARG(PreVal0addK7);
  913. CL_SET_BLKARG(PreVal4addT1);
  914. CL_SET_BLKARG(PreW18);
  915. CL_SET_BLKARG(PreW19);
  916. CL_SET_BLKARG(W16);
  917. CL_SET_BLKARG(W17);
  918. CL_SET_BLKARG(W16addK16);
  919. CL_SET_BLKARG(W17addK17);
  920. CL_SET_BLKARG(PreW31);
  921. CL_SET_BLKARG(PreW32);
  922. CL_SET_BLKARG(D1A);
  923. CL_SET_BLKARG(cty_b);
  924. CL_SET_BLKARG(cty_c);
  925. CL_SET_BLKARG(cty_h);
  926. CL_SET_BLKARG(cty_f);
  927. CL_SET_BLKARG(cty_g);
  928. CL_SET_BLKARG(C1addK5);
  929. CL_SET_BLKARG(B1addK6);
  930. CL_SET_BLKARG(ctx_a);
  931. CL_SET_BLKARG(ctx_b);
  932. CL_SET_BLKARG(ctx_c);
  933. CL_SET_BLKARG(ctx_d);
  934. CL_SET_BLKARG(ctx_e);
  935. CL_SET_BLKARG(ctx_f);
  936. CL_SET_BLKARG(ctx_g);
  937. CL_SET_BLKARG(ctx_h);
  938. CL_SET_ARG(clState->outputBuffer);
  939. return status;
  940. }
  941. #ifdef USE_SCRYPT
  942. static
  943. cl_int queue_scrypt_kernel(_clState * const clState, struct work * const work, __maybe_unused const cl_uint threads)
  944. {
  945. unsigned char *midstate = work->midstate;
  946. cl_kernel *kernel = &clState->kernel;
  947. unsigned int num = 0;
  948. cl_uint le_target;
  949. cl_int status = 0;
  950. le_target = *(cl_uint *)(work->target + 28);
  951. clState->cldata = work->data;
  952. status = clEnqueueWriteBuffer(clState->commandQueue, clState->CLbuffer0, true, 0, 80, clState->cldata, 0, NULL,NULL);
  953. CL_SET_ARG(clState->CLbuffer0);
  954. CL_SET_ARG(clState->outputBuffer);
  955. CL_SET_ARG(clState->padbuffer8);
  956. CL_SET_VARG(4, &midstate[0]);
  957. CL_SET_VARG(4, &midstate[16]);
  958. CL_SET_ARG(le_target);
  959. return status;
  960. }
  961. #endif
  962. static void set_threads_hashes(unsigned int vectors,int64_t *hashes, size_t *globalThreads,
  963. unsigned int minthreads, __maybe_unused int *intensity)
  964. {
  965. unsigned int threads = 0;
  966. while (threads < minthreads) {
  967. threads = 1 << ((opt_scrypt ? 0 : 15) + *intensity);
  968. if (threads < minthreads) {
  969. if (likely(*intensity < MAX_INTENSITY))
  970. (*intensity)++;
  971. else
  972. threads = minthreads;
  973. }
  974. }
  975. *globalThreads = threads;
  976. *hashes = threads * vectors;
  977. }
  978. #endif /* HAVE_OPENCL */
  979. #ifdef HAVE_OPENCL
  980. /* We have only one thread that ever re-initialises GPUs, thus if any GPU
  981. * init command fails due to a completely wedged GPU, the thread will never
  982. * return, unable to harm other GPUs. If it does return, it means we only had
  983. * a soft failure and then the reinit_gpu thread is ready to tackle another
  984. * GPU */
  985. void *reinit_gpu(void *userdata)
  986. {
  987. struct thr_info *mythr = userdata;
  988. struct cgpu_info *cgpu, *sel_cgpu;
  989. struct thr_info *thr;
  990. char name[256];
  991. int thr_id;
  992. int i;
  993. pthread_detach(pthread_self());
  994. RenameThread("reinit_gpu");
  995. select_cgpu:
  996. sel_cgpu =
  997. cgpu = tq_pop(mythr->q, NULL);
  998. if (!cgpu)
  999. goto out;
  1000. struct opencl_device_data * const data = cgpu->device_data;
  1001. if (clDevicesNum() != nDevs) {
  1002. applog(LOG_WARNING, "Hardware not reporting same number of active devices, will not attempt to restart GPU");
  1003. goto out;
  1004. }
  1005. for (i = 0; i < cgpu->threads; ++i)
  1006. {
  1007. thr = cgpu->thr[i];
  1008. thr_id = thr->id;
  1009. thr->rolling = thr->cgpu->rolling = 0;
  1010. /* Reports the last time we tried to revive a sick GPU */
  1011. cgtime(&thr->sick);
  1012. if (!pthread_cancel(thr->pth)) {
  1013. applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
  1014. } else
  1015. applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
  1016. }
  1017. for (i = 0; i < cgpu->threads; ++i)
  1018. {
  1019. int virtual_gpu;
  1020. thr = cgpu->thr[i];
  1021. thr_id = thr->id;
  1022. virtual_gpu = data->virtual_gpu;
  1023. /* Lose this ram cause we may get stuck here! */
  1024. //tq_freeze(thr->q);
  1025. thr->q = tq_new();
  1026. if (!thr->q)
  1027. quithere(1, "Failed to tq_new");
  1028. /* Lose this ram cause we may dereference in the dying thread! */
  1029. //free(clState);
  1030. applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
  1031. clStates[thr_id] = initCl(virtual_gpu, name, sizeof(name));
  1032. if (!clStates[thr_id]) {
  1033. applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
  1034. goto select_cgpu;
  1035. }
  1036. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1037. if (unlikely(thr_info_create(thr, NULL, miner_thread, thr))) {
  1038. applog(LOG_ERR, "thread %d create failed", thr_id);
  1039. return NULL;
  1040. }
  1041. applog(LOG_WARNING, "Thread %d restarted", thr_id);
  1042. }
  1043. get_now_datestamp(sel_cgpu->init, sizeof(sel_cgpu->init));
  1044. proc_enable(cgpu);
  1045. goto select_cgpu;
  1046. out:
  1047. return NULL;
  1048. }
  1049. #else
  1050. void *reinit_gpu(__maybe_unused void *userdata)
  1051. {
  1052. return NULL;
  1053. }
  1054. #endif
  1055. #ifdef HAVE_OPENCL
  1056. struct device_drv opencl_api;
  1057. static int opencl_autodetect()
  1058. {
  1059. RUNONCE(0);
  1060. #ifndef WIN32
  1061. if (!getenv("DISPLAY")) {
  1062. applog(LOG_DEBUG, "DISPLAY not set, setting :0 just in case");
  1063. setenv("DISPLAY", ":0", 1);
  1064. }
  1065. #endif
  1066. if (!load_opencl_symbols()) {
  1067. nDevs = 0;
  1068. return 0;
  1069. }
  1070. int i;
  1071. nDevs = clDevicesNum();
  1072. if (nDevs < 0) {
  1073. applog(LOG_ERR, "clDevicesNum returned error, no GPUs usable");
  1074. nDevs = 0;
  1075. }
  1076. if (!nDevs)
  1077. return 0;
  1078. /* If opt_g_threads is not set, use default 1 thread on scrypt and
  1079. * 2 for regular mining */
  1080. if (opt_g_threads == -1) {
  1081. if (opt_scrypt)
  1082. opt_g_threads = 1;
  1083. else
  1084. opt_g_threads = 2;
  1085. }
  1086. #ifdef HAVE_SENSORS
  1087. const sensors_chip_name *cn;
  1088. int c = 0;
  1089. sensors_init(NULL);
  1090. sensors_chip_name cnm;
  1091. if (sensors_parse_chip_name("radeon-*", &cnm))
  1092. c = -1;
  1093. #endif
  1094. for (i = 0; i < nDevs; ++i) {
  1095. struct cgpu_info *cgpu;
  1096. cgpu = &gpus[i];
  1097. struct opencl_device_data * const data = cgpu->device_data;
  1098. cgpu->devtype = "GPU";
  1099. cgpu->deven = DEV_ENABLED;
  1100. cgpu->drv = &opencl_api;
  1101. cgpu->device_id = i;
  1102. cgpu->threads = opt_g_threads;
  1103. data->virtual_gpu = i;
  1104. #ifdef HAVE_SENSORS
  1105. cn = (c == -1) ? NULL : sensors_get_detected_chips(&cnm, &c);
  1106. data->sensor = cn;
  1107. #endif
  1108. add_cgpu(cgpu);
  1109. }
  1110. if (!opt_noadl)
  1111. init_adl(nDevs);
  1112. return nDevs;
  1113. }
  1114. static void opencl_detect()
  1115. {
  1116. int flags = 0;
  1117. if (!opt_scrypt)
  1118. flags |= GDF_DEFAULT_NOAUTO;
  1119. generic_detect(&opencl_api, NULL, opencl_autodetect, flags);
  1120. }
  1121. static void reinit_opencl_device(struct cgpu_info *gpu)
  1122. {
  1123. #ifdef HAVE_ADL
  1124. struct opencl_device_data * const data = gpu->device_data;
  1125. if (adl_active && data->has_adl && gpu_activity(gpu->device_id) > 50)
  1126. {
  1127. applogr(, LOG_ERR, "%s: Still showing activity (suggests a hard hang); cancelling reinitialise.",
  1128. gpu->dev_repr);
  1129. }
  1130. #endif
  1131. tq_push(control_thr[gpur_thr_id].q, gpu);
  1132. }
  1133. // FIXME: Legacy (called by TUI) for side effects
  1134. static
  1135. bool override_opencl_statline_temp(char *buf, size_t bufsz, struct cgpu_info *gpu, __maybe_unused bool per_processor)
  1136. {
  1137. __maybe_unused struct opencl_device_data * const data = gpu->device_data;
  1138. #ifdef HAVE_SENSORS
  1139. if (data->sensor)
  1140. {
  1141. const sensors_chip_name *cn = data->sensor;
  1142. const sensors_feature *feat;
  1143. for (int f = 0; (feat = sensors_get_features(cn, &f)); )
  1144. {
  1145. const sensors_subfeature *subf;
  1146. subf = sensors_get_subfeature(cn, feat, SENSORS_SUBFEATURE_TEMP_INPUT);
  1147. if (!(subf && subf->flags & SENSORS_MODE_R))
  1148. continue;
  1149. double val;
  1150. int rc = sensors_get_value(cn, subf->number, &val);
  1151. if (rc)
  1152. continue;
  1153. gpu->temp = val;
  1154. return false;
  1155. }
  1156. }
  1157. #endif
  1158. #ifdef HAVE_ADL
  1159. if (data->has_adl) {
  1160. int gpuid = gpu->device_id;
  1161. gpu_temp(gpuid);
  1162. gpu_fanspeed(gpuid);
  1163. }
  1164. #endif
  1165. return false;
  1166. }
  1167. static
  1168. void opencl_watchdog(struct cgpu_info * const cgpu, __maybe_unused const struct timeval * const tv_now)
  1169. {
  1170. #ifdef HAVE_ADL
  1171. struct opencl_device_data * const data = cgpu->device_data;
  1172. const int gpu = cgpu->device_id;
  1173. enum dev_enable *denable = &cgpu->deven;
  1174. if (adl_active && data->has_adl)
  1175. gpu_autotune(gpu, denable);
  1176. if (opt_debug && data->has_adl) {
  1177. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  1178. float temp = 0, vddc = 0;
  1179. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune))
  1180. applog(LOG_DEBUG, "%.1f C F: %d%%(%dRPM) E: %dMHz M: %dMHz V: %.3fV A: %d%% P: %d%%",
  1181. temp, fanpercent, fanspeed, engineclock, memclock, vddc, activity, powertune);
  1182. }
  1183. #endif
  1184. }
  1185. static struct api_data*
  1186. get_opencl_api_extra_device_status(struct cgpu_info *gpu)
  1187. {
  1188. struct opencl_device_data * const data = gpu->device_data;
  1189. struct api_data*root = NULL;
  1190. float gt, gv;
  1191. int ga, gf, gp, gc, gm, pt;
  1192. #ifdef HAVE_ADL
  1193. if (!gpu_stats(gpu->device_id, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  1194. #endif
  1195. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  1196. root = api_add_int(root, "Fan Speed", &gf, true);
  1197. root = api_add_int(root, "Fan Percent", &gp, true);
  1198. root = api_add_int(root, "GPU Clock", &gc, true);
  1199. root = api_add_int(root, "Memory Clock", &gm, true);
  1200. root = api_add_volts(root, "GPU Voltage", &gv, true);
  1201. root = api_add_int(root, "GPU Activity", &ga, true);
  1202. root = api_add_int(root, "Powertune", &pt, true);
  1203. char intensity[20];
  1204. if (data->dynamic)
  1205. strcpy(intensity, "D");
  1206. else
  1207. sprintf(intensity, "%d", data->intensity);
  1208. root = api_add_string(root, "Intensity", intensity, true);
  1209. return root;
  1210. }
  1211. struct opencl_thread_data {
  1212. cl_int (*queue_kernel_parameters)(_clState *, struct work *, cl_uint);
  1213. uint32_t *res;
  1214. };
  1215. static uint32_t *blank_res;
  1216. static bool opencl_thread_prepare(struct thr_info *thr)
  1217. {
  1218. char name[256];
  1219. struct cgpu_info *cgpu = thr->cgpu;
  1220. struct opencl_device_data * const data = cgpu->device_data;
  1221. int gpu = cgpu->device_id;
  1222. int virtual_gpu = data->virtual_gpu;
  1223. int i = thr->id;
  1224. static bool failmessage = false;
  1225. int buffersize = opt_scrypt ? SCRYPT_BUFFERSIZE : BUFFERSIZE;
  1226. if (!blank_res)
  1227. blank_res = calloc(buffersize, 1);
  1228. if (!blank_res) {
  1229. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1230. return false;
  1231. }
  1232. strcpy(name, "");
  1233. applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
  1234. clStates[i] = initCl(virtual_gpu, name, sizeof(name));
  1235. if (!clStates[i]) {
  1236. #ifdef HAVE_CURSES
  1237. if (use_curses)
  1238. enable_curses();
  1239. #endif
  1240. applog(LOG_ERR, "Failed to init GPU thread %d, disabling device %d", i, gpu);
  1241. if (!failmessage) {
  1242. applog(LOG_ERR, "Restarting the GPU from the menu will not fix this.");
  1243. applog(LOG_ERR, "Try restarting BFGMiner.");
  1244. failmessage = true;
  1245. #ifdef HAVE_CURSES
  1246. char *buf;
  1247. if (use_curses) {
  1248. buf = curses_input("Press enter to continue");
  1249. if (buf)
  1250. free(buf);
  1251. }
  1252. #endif
  1253. }
  1254. cgpu->deven = DEV_DISABLED;
  1255. cgpu->status = LIFE_NOSTART;
  1256. dev_error(cgpu, REASON_DEV_NOSTART);
  1257. return false;
  1258. }
  1259. if (!cgpu->name)
  1260. cgpu->name = strdup(name);
  1261. if (!cgpu->kname)
  1262. {
  1263. switch (clStates[i]->chosen_kernel) {
  1264. case KL_DIABLO:
  1265. cgpu->kname = "diablo";
  1266. break;
  1267. case KL_DIAKGCN:
  1268. cgpu->kname = "diakgcn";
  1269. break;
  1270. case KL_PHATK:
  1271. cgpu->kname = "phatk";
  1272. break;
  1273. #ifdef USE_SCRYPT
  1274. case KL_SCRYPT:
  1275. cgpu->kname = "scrypt";
  1276. break;
  1277. #endif
  1278. case KL_POCLBM:
  1279. cgpu->kname = "poclbm";
  1280. break;
  1281. default:
  1282. break;
  1283. }
  1284. }
  1285. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1286. get_now_datestamp(cgpu->init, sizeof(cgpu->init));
  1287. have_opencl = true;
  1288. return true;
  1289. }
  1290. static bool opencl_thread_init(struct thr_info *thr)
  1291. {
  1292. const int thr_id = thr->id;
  1293. struct cgpu_info *gpu = thr->cgpu;
  1294. struct opencl_thread_data *thrdata;
  1295. _clState *clState = clStates[thr_id];
  1296. cl_int status = 0;
  1297. thrdata = calloc(1, sizeof(*thrdata));
  1298. thr->cgpu_data = thrdata;
  1299. int buffersize = opt_scrypt ? SCRYPT_BUFFERSIZE : BUFFERSIZE;
  1300. if (!thrdata) {
  1301. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1302. return false;
  1303. }
  1304. switch (clState->chosen_kernel) {
  1305. case KL_POCLBM:
  1306. thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
  1307. break;
  1308. case KL_PHATK:
  1309. thrdata->queue_kernel_parameters = &queue_phatk_kernel;
  1310. break;
  1311. case KL_DIAKGCN:
  1312. thrdata->queue_kernel_parameters = &queue_diakgcn_kernel;
  1313. break;
  1314. #ifdef USE_SCRYPT
  1315. case KL_SCRYPT:
  1316. thrdata->queue_kernel_parameters = &queue_scrypt_kernel;
  1317. gpu->min_nonce_diff = 1./0x10000;
  1318. break;
  1319. #endif
  1320. default:
  1321. case KL_DIABLO:
  1322. thrdata->queue_kernel_parameters = &queue_diablo_kernel;
  1323. break;
  1324. }
  1325. thrdata->res = calloc(buffersize, 1);
  1326. if (!thrdata->res) {
  1327. free(thrdata);
  1328. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1329. return false;
  1330. }
  1331. status |= clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  1332. buffersize, blank_res, 0, NULL, NULL);
  1333. if (unlikely(status != CL_SUCCESS)) {
  1334. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1335. return false;
  1336. }
  1337. gpu->status = LIFE_WELL;
  1338. gpu->device_last_well = time(NULL);
  1339. return true;
  1340. }
  1341. static bool opencl_prepare_work(struct thr_info __maybe_unused *thr, struct work *work)
  1342. {
  1343. #ifdef USE_SCRYPT
  1344. if (!opt_scrypt)
  1345. #endif
  1346. {
  1347. struct opencl_work_data * const blk = _opencl_work_data(work);
  1348. precalc_hash(blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1349. }
  1350. return true;
  1351. }
  1352. extern int opt_dynamic_interval;
  1353. static int64_t opencl_scanhash(struct thr_info *thr, struct work *work,
  1354. int64_t __maybe_unused max_nonce)
  1355. {
  1356. const int thr_id = thr->id;
  1357. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1358. struct cgpu_info *gpu = thr->cgpu;
  1359. struct opencl_device_data * const data = gpu->device_data;
  1360. _clState *clState = clStates[thr_id];
  1361. const cl_kernel *kernel = &clState->kernel;
  1362. const int dynamic_us = opt_dynamic_interval * 1000;
  1363. cl_int status;
  1364. size_t globalThreads[1];
  1365. size_t localThreads[1] = { clState->wsize };
  1366. int64_t hashes;
  1367. int found = opt_scrypt ? SCRYPT_FOUND : FOUND;
  1368. int buffersize = opt_scrypt ? SCRYPT_BUFFERSIZE : BUFFERSIZE;
  1369. /* Windows' timer resolution is only 15ms so oversample 5x */
  1370. if (data->dynamic && (++data->intervals * dynamic_us) > 70000) {
  1371. struct timeval tv_gpuend;
  1372. double gpu_us;
  1373. cgtime(&tv_gpuend);
  1374. gpu_us = us_tdiff(&tv_gpuend, &data->tv_gpustart) / data->intervals;
  1375. if (gpu_us > dynamic_us) {
  1376. if (data->intensity > MIN_INTENSITY)
  1377. --data->intensity;
  1378. } else if (gpu_us < dynamic_us / 2) {
  1379. if (data->intensity < MAX_INTENSITY)
  1380. ++data->intensity;
  1381. }
  1382. memcpy(&(data->tv_gpustart), &tv_gpuend, sizeof(struct timeval));
  1383. data->intervals = 0;
  1384. }
  1385. set_threads_hashes(clState->vwidth, &hashes, globalThreads, localThreads[0], &data->intensity);
  1386. if (hashes > gpu->max_hashes)
  1387. gpu->max_hashes = hashes;
  1388. status = thrdata->queue_kernel_parameters(clState, work, globalThreads[0]);
  1389. if (unlikely(status != CL_SUCCESS)) {
  1390. applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
  1391. return -1;
  1392. }
  1393. if (clState->goffset) {
  1394. size_t global_work_offset[1];
  1395. global_work_offset[0] = work->blk.nonce;
  1396. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, global_work_offset,
  1397. globalThreads, localThreads, 0, NULL, NULL);
  1398. } else
  1399. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1400. globalThreads, localThreads, 0, NULL, NULL);
  1401. if (unlikely(status != CL_SUCCESS)) {
  1402. applog(LOG_ERR, "Error %d: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)", status);
  1403. return -1;
  1404. }
  1405. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1406. buffersize, thrdata->res, 0, NULL, NULL);
  1407. if (unlikely(status != CL_SUCCESS)) {
  1408. applog(LOG_ERR, "Error: clEnqueueReadBuffer failed error %d. (clEnqueueReadBuffer)", status);
  1409. return -1;
  1410. }
  1411. /* The amount of work scanned can fluctuate when intensity changes
  1412. * and since we do this one cycle behind, we increment the work more
  1413. * than enough to prevent repeating work */
  1414. work->blk.nonce += gpu->max_hashes;
  1415. /* This finish flushes the readbuffer set with CL_FALSE in clEnqueueReadBuffer */
  1416. clFinish(clState->commandQueue);
  1417. /* FOUND entry is used as a counter to say how many nonces exist */
  1418. if (thrdata->res[found]) {
  1419. /* Clear the buffer again */
  1420. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1421. buffersize, blank_res, 0, NULL, NULL);
  1422. if (unlikely(status != CL_SUCCESS)) {
  1423. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1424. return -1;
  1425. }
  1426. applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
  1427. postcalc_hash_async(thr, work, thrdata->res);
  1428. memset(thrdata->res, 0, buffersize);
  1429. /* This finish flushes the writebuffer set with CL_FALSE in clEnqueueWriteBuffer */
  1430. clFinish(clState->commandQueue);
  1431. }
  1432. return hashes;
  1433. }
  1434. static void opencl_thread_shutdown(struct thr_info *thr)
  1435. {
  1436. const int thr_id = thr->id;
  1437. _clState *clState = clStates[thr_id];
  1438. clReleaseKernel(clState->kernel);
  1439. clReleaseProgram(clState->program);
  1440. clReleaseCommandQueue(clState->commandQueue);
  1441. clReleaseContext(clState->context);
  1442. }
  1443. struct device_drv opencl_api = {
  1444. .dname = "opencl",
  1445. .name = "OCL",
  1446. .probe_priority = 110,
  1447. .supported_algos = POW_SHA256D | POW_SCRYPT,
  1448. .drv_detect = opencl_detect,
  1449. .reinit_device = reinit_opencl_device,
  1450. .watchdog = opencl_watchdog,
  1451. .override_statline_temp2 = override_opencl_statline_temp,
  1452. #ifdef HAVE_CURSES
  1453. .proc_wlogprint_status = opencl_wlogprint_status,
  1454. .proc_tui_wlogprint_choices = opencl_tui_wlogprint_choices,
  1455. .proc_tui_handle_choice = opencl_tui_handle_choice,
  1456. #endif
  1457. .get_api_extra_device_status = get_opencl_api_extra_device_status,
  1458. .thread_prepare = opencl_thread_prepare,
  1459. .thread_init = opencl_thread_init,
  1460. .prepare_work = opencl_prepare_work,
  1461. .scanhash = opencl_scanhash,
  1462. .thread_shutdown = opencl_thread_shutdown,
  1463. };
  1464. #endif