driver-opencl.c 46 KB

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