driver-opencl.c 45 KB

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