driver-opencl.c 46 KB

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