driver-opencl.c 46 KB

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