driver-opencl.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698
  1. /*
  2. * Copyright 2011-2012 Con Kolivas
  3. * Copyright 2011-2012 Luke Dashjr
  4. * Copyright 2010 Jeff Garzik
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include "config.h"
  12. #ifdef HAVE_CURSES
  13. #include <curses.h>
  14. #endif
  15. #ifndef WIN32
  16. #include <dlfcn.h>
  17. #else
  18. #include <windows.h>
  19. #endif
  20. #include <string.h>
  21. #include <stdbool.h>
  22. #include <stdint.h>
  23. #include <sys/types.h>
  24. #ifndef WIN32
  25. #include <sys/resource.h>
  26. #endif
  27. #include <ccan/opt/opt.h>
  28. #define OMIT_OPENCL_API
  29. #include "compat.h"
  30. #include "miner.h"
  31. #include "driver-opencl.h"
  32. #include "findnonce.h"
  33. #include "ocl.h"
  34. #include "adl.h"
  35. /* TODO: cleanup externals ********************/
  36. /* Platform API */
  37. CL_API_ENTRY cl_int CL_API_CALL
  38. (*clGetPlatformIDs)(cl_uint /* num_entries */,
  39. cl_platform_id * /* platforms */,
  40. cl_uint * /* num_platforms */) CL_API_SUFFIX__VERSION_1_0;
  41. CL_API_ENTRY cl_int CL_API_CALL
  42. (*clGetPlatformInfo)(cl_platform_id /* platform */,
  43. cl_platform_info /* param_name */,
  44. size_t /* param_value_size */,
  45. void * /* param_value */,
  46. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  47. /* Device APIs */
  48. CL_API_ENTRY cl_int CL_API_CALL
  49. (*clGetDeviceIDs)(cl_platform_id /* platform */,
  50. cl_device_type /* device_type */,
  51. cl_uint /* num_entries */,
  52. cl_device_id * /* devices */,
  53. cl_uint * /* num_devices */) CL_API_SUFFIX__VERSION_1_0;
  54. CL_API_ENTRY cl_int CL_API_CALL
  55. (*clGetDeviceInfo)(cl_device_id /* device */,
  56. cl_device_info /* param_name */,
  57. size_t /* param_value_size */,
  58. void * /* param_value */,
  59. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  60. /* Context APIs */
  61. CL_API_ENTRY cl_context CL_API_CALL
  62. (*clCreateContextFromType)(const cl_context_properties * /* properties */,
  63. cl_device_type /* device_type */,
  64. void (CL_CALLBACK * /* pfn_notify*/ )(const char *, const void *, size_t, void *),
  65. void * /* user_data */,
  66. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  67. CL_API_ENTRY cl_int CL_API_CALL
  68. (*clReleaseContext)(cl_context /* context */) CL_API_SUFFIX__VERSION_1_0;
  69. /* Command Queue APIs */
  70. CL_API_ENTRY cl_command_queue CL_API_CALL
  71. (*clCreateCommandQueue)(cl_context /* context */,
  72. cl_device_id /* device */,
  73. cl_command_queue_properties /* properties */,
  74. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  75. CL_API_ENTRY cl_int CL_API_CALL
  76. (*clReleaseCommandQueue)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  77. /* Memory Object APIs */
  78. CL_API_ENTRY cl_mem CL_API_CALL
  79. (*clCreateBuffer)(cl_context /* context */,
  80. cl_mem_flags /* flags */,
  81. size_t /* size */,
  82. void * /* host_ptr */,
  83. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  84. /* Program Object APIs */
  85. CL_API_ENTRY cl_program CL_API_CALL
  86. (*clCreateProgramWithSource)(cl_context /* context */,
  87. cl_uint /* count */,
  88. const char ** /* strings */,
  89. const size_t * /* lengths */,
  90. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  91. CL_API_ENTRY cl_program CL_API_CALL
  92. (*clCreateProgramWithBinary)(cl_context /* context */,
  93. cl_uint /* num_devices */,
  94. const cl_device_id * /* device_list */,
  95. const size_t * /* lengths */,
  96. const unsigned char ** /* binaries */,
  97. cl_int * /* binary_status */,
  98. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  99. CL_API_ENTRY cl_int CL_API_CALL
  100. (*clReleaseProgram)(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0;
  101. CL_API_ENTRY cl_int CL_API_CALL
  102. (*clBuildProgram)(cl_program /* program */,
  103. cl_uint /* num_devices */,
  104. const cl_device_id * /* device_list */,
  105. const char * /* options */,
  106. void (CL_CALLBACK * /* pfn_notify */)(cl_program /* program */, void * /* user_data */),
  107. void * /* user_data */) CL_API_SUFFIX__VERSION_1_0;
  108. CL_API_ENTRY cl_int CL_API_CALL
  109. (*clGetProgramInfo)(cl_program /* program */,
  110. cl_program_info /* param_name */,
  111. size_t /* param_value_size */,
  112. void * /* param_value */,
  113. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  114. CL_API_ENTRY cl_int CL_API_CALL
  115. (*clGetProgramBuildInfo)(cl_program /* program */,
  116. cl_device_id /* device */,
  117. cl_program_build_info /* param_name */,
  118. size_t /* param_value_size */,
  119. void * /* param_value */,
  120. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  121. /* Kernel Object APIs */
  122. CL_API_ENTRY cl_kernel CL_API_CALL
  123. (*clCreateKernel)(cl_program /* program */,
  124. const char * /* kernel_name */,
  125. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  126. CL_API_ENTRY cl_int CL_API_CALL
  127. (*clReleaseKernel)(cl_kernel /* kernel */) CL_API_SUFFIX__VERSION_1_0;
  128. CL_API_ENTRY cl_int CL_API_CALL
  129. (*clSetKernelArg)(cl_kernel /* kernel */,
  130. cl_uint /* arg_index */,
  131. size_t /* arg_size */,
  132. const void * /* arg_value */) CL_API_SUFFIX__VERSION_1_0;
  133. /* Flush and Finish APIs */
  134. CL_API_ENTRY cl_int CL_API_CALL
  135. (*clFinish)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  136. /* Enqueued Commands APIs */
  137. CL_API_ENTRY cl_int CL_API_CALL
  138. (*clEnqueueReadBuffer)(cl_command_queue /* command_queue */,
  139. cl_mem /* buffer */,
  140. cl_bool /* blocking_read */,
  141. size_t /* offset */,
  142. size_t /* size */,
  143. void * /* ptr */,
  144. cl_uint /* num_events_in_wait_list */,
  145. const cl_event * /* event_wait_list */,
  146. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  147. CL_API_ENTRY cl_int CL_API_CALL
  148. (*clEnqueueWriteBuffer)(cl_command_queue /* command_queue */,
  149. cl_mem /* buffer */,
  150. cl_bool /* blocking_write */,
  151. size_t /* offset */,
  152. size_t /* size */,
  153. const void * /* ptr */,
  154. cl_uint /* num_events_in_wait_list */,
  155. const cl_event * /* event_wait_list */,
  156. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  157. CL_API_ENTRY cl_int CL_API_CALL
  158. (*clEnqueueNDRangeKernel)(cl_command_queue /* command_queue */,
  159. cl_kernel /* kernel */,
  160. cl_uint /* work_dim */,
  161. const size_t * /* global_work_offset */,
  162. const size_t * /* global_work_size */,
  163. const size_t * /* local_work_size */,
  164. cl_uint /* num_events_in_wait_list */,
  165. const cl_event * /* event_wait_list */,
  166. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  167. #ifdef WIN32
  168. #define dlsym (void*)GetProcAddress
  169. #define dlclose FreeLibrary
  170. #endif
  171. #define LOAD_OCL_SYM(sym) do { \
  172. if (!(sym = dlsym(cl, #sym))) { \
  173. applog(LOG_ERR, "Failed to load OpenCL symbol " #sym ", no GPUs usable"); \
  174. dlclose(cl); \
  175. return false; \
  176. } \
  177. } while(0)
  178. static bool
  179. load_opencl_symbols() {
  180. #ifndef WIN32
  181. void *cl = dlopen("libOpenCL.so", RTLD_LAZY);
  182. #else
  183. HMODULE cl = LoadLibrary("OpenCL.dll");
  184. #endif
  185. if (!cl)
  186. {
  187. applog(LOG_ERR, "Failed to load OpenCL library, no GPUs usable");
  188. return false;
  189. }
  190. LOAD_OCL_SYM(clGetPlatformIDs);
  191. LOAD_OCL_SYM(clGetPlatformInfo);
  192. LOAD_OCL_SYM(clGetDeviceIDs);
  193. LOAD_OCL_SYM(clGetDeviceInfo);
  194. LOAD_OCL_SYM(clCreateContextFromType);
  195. LOAD_OCL_SYM(clReleaseContext);
  196. LOAD_OCL_SYM(clCreateCommandQueue);
  197. LOAD_OCL_SYM(clReleaseCommandQueue);
  198. LOAD_OCL_SYM(clCreateBuffer);
  199. LOAD_OCL_SYM(clCreateProgramWithSource);
  200. LOAD_OCL_SYM(clCreateProgramWithBinary);
  201. LOAD_OCL_SYM(clReleaseProgram);
  202. LOAD_OCL_SYM(clBuildProgram);
  203. LOAD_OCL_SYM(clGetProgramInfo);
  204. LOAD_OCL_SYM(clGetProgramBuildInfo);
  205. LOAD_OCL_SYM(clCreateKernel);
  206. LOAD_OCL_SYM(clReleaseKernel);
  207. LOAD_OCL_SYM(clSetKernelArg);
  208. LOAD_OCL_SYM(clFinish);
  209. LOAD_OCL_SYM(clEnqueueReadBuffer);
  210. LOAD_OCL_SYM(clEnqueueWriteBuffer);
  211. LOAD_OCL_SYM(clEnqueueNDRangeKernel);
  212. return true;
  213. }
  214. #ifdef HAVE_CURSES
  215. extern WINDOW *mainwin, *statuswin, *logwin;
  216. extern void enable_curses(void);
  217. #endif
  218. extern int mining_threads;
  219. extern double total_secs;
  220. extern int opt_g_threads;
  221. extern bool ping;
  222. extern bool opt_loginput;
  223. extern char *opt_kernel_path;
  224. extern int gpur_thr_id;
  225. extern bool opt_noadl;
  226. extern bool have_opencl;
  227. extern void *miner_thread(void *userdata);
  228. extern int dev_from_id(int thr_id);
  229. extern void tailsprintf(char *f, const char *fmt, ...);
  230. extern void wlog(const char *f, ...);
  231. extern void decay_time(double *f, double fadd);
  232. /**********************************************/
  233. #ifdef HAVE_ADL
  234. extern float gpu_temp(int gpu);
  235. extern int gpu_fanspeed(int gpu);
  236. extern int gpu_fanpercent(int gpu);
  237. #endif
  238. #ifdef HAVE_OPENCL
  239. char *set_vector(char *arg)
  240. {
  241. int i, val = 0, device = 0;
  242. char *nextptr;
  243. nextptr = strtok(arg, ",");
  244. if (nextptr == NULL)
  245. return "Invalid parameters for set vector";
  246. val = atoi(nextptr);
  247. if (val != 1 && val != 2 && val != 4)
  248. return "Invalid value passed to set_vector";
  249. gpus[device++].vwidth = val;
  250. while ((nextptr = strtok(NULL, ",")) != NULL) {
  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. }
  256. if (device == 1) {
  257. for (i = device; i < MAX_GPUDEVICES; i++)
  258. gpus[i].vwidth = gpus[0].vwidth;
  259. }
  260. return NULL;
  261. }
  262. char *set_worksize(char *arg)
  263. {
  264. int i, val = 0, device = 0;
  265. char *nextptr;
  266. nextptr = strtok(arg, ",");
  267. if (nextptr == NULL)
  268. return "Invalid parameters for set work size";
  269. val = atoi(nextptr);
  270. if (val < 1 || val > 9999)
  271. return "Invalid value passed to set_worksize";
  272. gpus[device++].work_size = val;
  273. while ((nextptr = strtok(NULL, ",")) != NULL) {
  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. }
  279. if (device == 1) {
  280. for (i = device; i < MAX_GPUDEVICES; i++)
  281. gpus[i].work_size = gpus[0].work_size;
  282. }
  283. return NULL;
  284. }
  285. static enum cl_kernels select_kernel(char *arg)
  286. {
  287. if (!strcmp(arg, "diablo"))
  288. return KL_DIABLO;
  289. if (!strcmp(arg, "diakgcn"))
  290. return KL_DIAKGCN;
  291. if (!strcmp(arg, "poclbm"))
  292. return KL_POCLBM;
  293. if (!strcmp(arg, "phatk"))
  294. return KL_PHATK;
  295. return KL_NONE;
  296. }
  297. char *set_kernel(char *arg)
  298. {
  299. enum cl_kernels kern;
  300. int i, device = 0;
  301. char *nextptr;
  302. nextptr = strtok(arg, ",");
  303. if (nextptr == NULL)
  304. return "Invalid parameters for set kernel";
  305. kern = select_kernel(nextptr);
  306. if (kern == KL_NONE)
  307. return "Invalid parameter to set_kernel";
  308. gpus[device++].kernel = kern;
  309. while ((nextptr = strtok(NULL, ",")) != NULL) {
  310. kern = select_kernel(nextptr);
  311. if (kern == KL_NONE)
  312. return "Invalid parameter to set_kernel";
  313. gpus[device++].kernel = kern;
  314. }
  315. if (device == 1) {
  316. for (i = device; i < MAX_GPUDEVICES; i++)
  317. gpus[i].kernel = gpus[0].kernel;
  318. }
  319. return NULL;
  320. }
  321. #endif
  322. #ifdef HAVE_ADL
  323. /* This function allows us to map an adl device to an opencl device for when
  324. * simple enumeration has failed to match them. */
  325. char *set_gpu_map(char *arg)
  326. {
  327. int val1 = 0, val2 = 0;
  328. char *nextptr;
  329. nextptr = strtok(arg, ",");
  330. if (nextptr == NULL)
  331. return "Invalid parameters for set gpu map";
  332. if (sscanf(arg, "%d:%d", &val1, &val2) != 2)
  333. return "Invalid description for map pair";
  334. if (val1 < 0 || val1 > MAX_GPUDEVICES || val2 < 0 || val2 > MAX_GPUDEVICES)
  335. return "Invalid value passed to set_gpu_map";
  336. gpus[val1].virtual_adl = val2;
  337. gpus[val1].mapped = true;
  338. while ((nextptr = strtok(NULL, ",")) != NULL) {
  339. if (sscanf(nextptr, "%d:%d", &val1, &val2) != 2)
  340. return "Invalid description for map pair";
  341. if (val1 < 0 || val1 > MAX_GPUDEVICES || val2 < 0 || val2 > MAX_GPUDEVICES)
  342. return "Invalid value passed to set_gpu_map";
  343. gpus[val1].virtual_adl = val2;
  344. gpus[val1].mapped = true;
  345. }
  346. return NULL;
  347. }
  348. void get_intrange(char *arg, int *val1, int *val2)
  349. {
  350. if (sscanf(arg, "%d-%d", val1, val2) == 1) {
  351. *val2 = *val1;
  352. *val1 = 0;
  353. }
  354. }
  355. char *set_gpu_engine(char *arg)
  356. {
  357. int i, val1 = 0, val2 = 0, device = 0;
  358. char *nextptr;
  359. nextptr = strtok(arg, ",");
  360. if (nextptr == NULL)
  361. return "Invalid parameters for set gpu engine";
  362. get_intrange(nextptr, &val1, &val2);
  363. if (val1 < 0 || val1 > 9999 || val2 < 0 || val2 > 9999)
  364. return "Invalid value passed to set_gpu_engine";
  365. gpus[device].min_engine = val1;
  366. gpus[device].gpu_engine = val2;
  367. device++;
  368. while ((nextptr = strtok(NULL, ",")) != NULL) {
  369. get_intrange(nextptr, &val1, &val2);
  370. if (val1 < 0 || val1 > 9999 || val2 < 0 || val2 > 9999)
  371. return "Invalid value passed to set_gpu_engine";
  372. gpus[device].min_engine = val1;
  373. gpus[device].gpu_engine = val2;
  374. device++;
  375. }
  376. if (device == 1) {
  377. for (i = 1; i < MAX_GPUDEVICES; i++) {
  378. gpus[i].min_engine = gpus[0].min_engine;
  379. gpus[i].gpu_engine = gpus[0].gpu_engine;
  380. }
  381. }
  382. return NULL;
  383. }
  384. char *set_gpu_fan(char *arg)
  385. {
  386. int i, val1 = 0, val2 = 0, device = 0;
  387. char *nextptr;
  388. nextptr = strtok(arg, ",");
  389. if (nextptr == NULL)
  390. return "Invalid parameters for set gpu fan";
  391. get_intrange(nextptr, &val1, &val2);
  392. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100)
  393. return "Invalid value passed to set_gpu_fan";
  394. gpus[device].min_fan = val1;
  395. gpus[device].gpu_fan = val2;
  396. device++;
  397. while ((nextptr = strtok(NULL, ",")) != NULL) {
  398. get_intrange(nextptr, &val1, &val2);
  399. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100)
  400. return "Invalid value passed to set_gpu_fan";
  401. gpus[device].min_fan = val1;
  402. gpus[device].gpu_fan = val2;
  403. device++;
  404. }
  405. if (device == 1) {
  406. for (i = 1; i < MAX_GPUDEVICES; i++) {
  407. gpus[i].min_fan = gpus[0].min_fan;
  408. gpus[i].gpu_fan = gpus[0].gpu_fan;
  409. }
  410. }
  411. return NULL;
  412. }
  413. char *set_gpu_memclock(char *arg)
  414. {
  415. int i, val = 0, device = 0;
  416. char *nextptr;
  417. nextptr = strtok(arg, ",");
  418. if (nextptr == NULL)
  419. return "Invalid parameters for set gpu memclock";
  420. val = atoi(nextptr);
  421. if (val < 0 || val >= 9999)
  422. return "Invalid value passed to set_gpu_memclock";
  423. gpus[device++].gpu_memclock = val;
  424. while ((nextptr = strtok(NULL, ",")) != NULL) {
  425. val = atoi(nextptr);
  426. if (val < 0 || val >= 9999)
  427. return "Invalid value passed to set_gpu_memclock";
  428. gpus[device++].gpu_memclock = val;
  429. }
  430. if (device == 1) {
  431. for (i = device; i < MAX_GPUDEVICES; i++)
  432. gpus[i].gpu_memclock = gpus[0].gpu_memclock;
  433. }
  434. return NULL;
  435. }
  436. char *set_gpu_memdiff(char *arg)
  437. {
  438. int i, val = 0, device = 0;
  439. char *nextptr;
  440. nextptr = strtok(arg, ",");
  441. if (nextptr == NULL)
  442. return "Invalid parameters for set gpu memdiff";
  443. val = atoi(nextptr);
  444. if (val < -9999 || val > 9999)
  445. return "Invalid value passed to set_gpu_memdiff";
  446. gpus[device++].gpu_memdiff = val;
  447. while ((nextptr = strtok(NULL, ",")) != NULL) {
  448. val = atoi(nextptr);
  449. if (val < -9999 || val > 9999)
  450. return "Invalid value passed to set_gpu_memdiff";
  451. gpus[device++].gpu_memdiff = val;
  452. }
  453. if (device == 1) {
  454. for (i = device; i < MAX_GPUDEVICES; i++)
  455. gpus[i].gpu_memdiff = gpus[0].gpu_memdiff;
  456. }
  457. return NULL;
  458. }
  459. char *set_gpu_powertune(char *arg)
  460. {
  461. int i, val = 0, device = 0;
  462. char *nextptr;
  463. nextptr = strtok(arg, ",");
  464. if (nextptr == NULL)
  465. return "Invalid parameters for set gpu powertune";
  466. val = atoi(nextptr);
  467. if (val < -99 || val > 99)
  468. return "Invalid value passed to set_gpu_powertune";
  469. gpus[device++].gpu_powertune = val;
  470. while ((nextptr = strtok(NULL, ",")) != NULL) {
  471. val = atoi(nextptr);
  472. if (val < -99 || val > 99)
  473. return "Invalid value passed to set_gpu_powertune";
  474. gpus[device++].gpu_powertune = val;
  475. }
  476. if (device == 1) {
  477. for (i = device; i < MAX_GPUDEVICES; i++)
  478. gpus[i].gpu_powertune = gpus[0].gpu_powertune;
  479. }
  480. return NULL;
  481. }
  482. char *set_gpu_vddc(char *arg)
  483. {
  484. int i, device = 0;
  485. float val = 0;
  486. char *nextptr;
  487. nextptr = strtok(arg, ",");
  488. if (nextptr == NULL)
  489. return "Invalid parameters for set gpu vddc";
  490. val = atof(nextptr);
  491. if (val < 0 || val >= 9999)
  492. return "Invalid value passed to set_gpu_vddc";
  493. gpus[device++].gpu_vddc = val;
  494. while ((nextptr = strtok(NULL, ",")) != NULL) {
  495. val = atof(nextptr);
  496. if (val < 0 || val >= 9999)
  497. return "Invalid value passed to set_gpu_vddc";
  498. gpus[device++].gpu_vddc = val;
  499. }
  500. if (device == 1) {
  501. for (i = device; i < MAX_GPUDEVICES; i++)
  502. gpus[i].gpu_vddc = gpus[0].gpu_vddc;
  503. }
  504. return NULL;
  505. }
  506. char *set_temp_overheat(char *arg)
  507. {
  508. int i, val = 0, device = 0, *to;
  509. char *nextptr;
  510. nextptr = strtok(arg, ",");
  511. if (nextptr == NULL)
  512. return "Invalid parameters for set temp overheat";
  513. val = atoi(nextptr);
  514. if (val < 0 || val > 200)
  515. return "Invalid value passed to set temp overheat";
  516. to = &gpus[device++].adl.overtemp;
  517. *to = val;
  518. while ((nextptr = strtok(NULL, ",")) != NULL) {
  519. val = atoi(nextptr);
  520. if (val < 0 || val > 200)
  521. return "Invalid value passed to set temp overheat";
  522. to = &gpus[device++].adl.overtemp;
  523. *to = val;
  524. }
  525. if (device == 1) {
  526. for (i = device; i < MAX_GPUDEVICES; i++) {
  527. to = &gpus[i].adl.overtemp;
  528. *to = val;
  529. }
  530. }
  531. return NULL;
  532. }
  533. char *set_temp_target(char *arg)
  534. {
  535. int i, val = 0, device = 0, *tt;
  536. char *nextptr;
  537. nextptr = strtok(arg, ",");
  538. if (nextptr == NULL)
  539. return "Invalid parameters for set temp target";
  540. val = atoi(nextptr);
  541. if (val < 0 || val > 200)
  542. return "Invalid value passed to set temp target";
  543. tt = &gpus[device++].adl.targettemp;
  544. *tt = val;
  545. while ((nextptr = strtok(NULL, ",")) != NULL) {
  546. val = atoi(nextptr);
  547. if (val < 0 || val > 200)
  548. return "Invalid value passed to set temp target";
  549. tt = &gpus[device++].adl.targettemp;
  550. *tt = val;
  551. }
  552. if (device == 1) {
  553. for (i = device; i < MAX_GPUDEVICES; i++) {
  554. tt = &gpus[i].adl.targettemp;
  555. *tt = val;
  556. }
  557. }
  558. return NULL;
  559. }
  560. #endif
  561. #ifdef HAVE_OPENCL
  562. char *set_intensity(char *arg)
  563. {
  564. int i, device = 0, *tt;
  565. char *nextptr, val = 0;
  566. nextptr = strtok(arg, ",");
  567. if (nextptr == NULL)
  568. return "Invalid parameters for set intensity";
  569. if (!strncasecmp(nextptr, "d", 1))
  570. gpus[device].dynamic = true;
  571. else {
  572. gpus[device].dynamic = false;
  573. val = atoi(nextptr);
  574. if (val < MIN_INTENSITY || val > MAX_INTENSITY)
  575. return "Invalid value passed to set intensity";
  576. tt = &gpus[device].intensity;
  577. *tt = val;
  578. }
  579. device++;
  580. while ((nextptr = strtok(NULL, ",")) != NULL) {
  581. if (!strncasecmp(nextptr, "d", 1))
  582. gpus[device].dynamic = true;
  583. else {
  584. gpus[device].dynamic = false;
  585. val = atoi(nextptr);
  586. if (val < MIN_INTENSITY || val > MAX_INTENSITY)
  587. return "Invalid value passed to set intensity";
  588. tt = &gpus[device].intensity;
  589. *tt = val;
  590. }
  591. device++;
  592. }
  593. if (device == 1) {
  594. for (i = device; i < MAX_GPUDEVICES; i++) {
  595. gpus[i].dynamic = gpus[0].dynamic;
  596. gpus[i].intensity = gpus[0].intensity;
  597. }
  598. }
  599. return NULL;
  600. }
  601. #endif
  602. #ifdef HAVE_OPENCL
  603. struct device_api opencl_api;
  604. char *print_ndevs_and_exit(int *ndevs)
  605. {
  606. opt_log_output = true;
  607. opencl_api.api_detect();
  608. clear_adl(*ndevs);
  609. applog(LOG_INFO, "%i GPU devices max detected", *ndevs);
  610. exit(*ndevs);
  611. }
  612. #endif
  613. struct cgpu_info gpus[MAX_GPUDEVICES]; /* Maximum number apparently possible */
  614. struct cgpu_info *cpus;
  615. #ifdef HAVE_OPENCL
  616. /* In dynamic mode, only the first thread of each device will be in use.
  617. * This potentially could start a thread that was stopped with the start-stop
  618. * options if one were to disable dynamic from the menu on a paused GPU */
  619. void pause_dynamic_threads(int gpu)
  620. {
  621. struct cgpu_info *cgpu = &gpus[gpu];
  622. int i, thread_no = 0;
  623. for (i = 0; i < mining_threads; i++) {
  624. struct thr_info *thr = &thr_info[i];
  625. if (thr->cgpu != cgpu)
  626. continue;
  627. if (!thread_no++)
  628. continue;
  629. if (!thr->pause && cgpu->dynamic) {
  630. applog(LOG_WARNING, "Disabling extra threads due to dynamic mode.");
  631. applog(LOG_WARNING, "Tune dynamic intensity with --gpu-dyninterval");
  632. }
  633. thr->pause = cgpu->dynamic;
  634. if (!cgpu->dynamic && cgpu->deven != DEV_DISABLED)
  635. tq_push(thr->q, &ping);
  636. }
  637. }
  638. struct device_api opencl_api;
  639. #endif /* HAVE_OPENCL */
  640. #if defined(HAVE_OPENCL) && defined(HAVE_CURSES)
  641. void manage_gpu(void)
  642. {
  643. struct thr_info *thr;
  644. int selected, gpu, i;
  645. char checkin[40];
  646. char input;
  647. if (!opt_g_threads)
  648. return;
  649. opt_loginput = true;
  650. immedok(logwin, true);
  651. clear_logwin();
  652. retry:
  653. for (gpu = 0; gpu < nDevs; gpu++) {
  654. struct cgpu_info *cgpu = &gpus[gpu];
  655. wlog("GPU %d: %.1f / %.1f Mh/s | A:%d R:%d HW:%d U:%.2f/m I:%d\n",
  656. gpu, cgpu->rolling, cgpu->total_mhashes / total_secs,
  657. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  658. cgpu->utility, cgpu->intensity);
  659. #ifdef HAVE_ADL
  660. if (gpus[gpu].has_adl) {
  661. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  662. float temp = 0, vddc = 0;
  663. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune)) {
  664. char logline[255];
  665. strcpy(logline, ""); // In case it has no data
  666. if (temp != -1)
  667. sprintf(logline, "%.1f C ", temp);
  668. if (fanspeed != -1 || fanpercent != -1) {
  669. tailsprintf(logline, "F: ");
  670. if (fanpercent != -1)
  671. tailsprintf(logline, "%d%% ", fanpercent);
  672. if (fanspeed != -1)
  673. tailsprintf(logline, "(%d RPM) ", fanspeed);
  674. tailsprintf(logline, " ");
  675. }
  676. if (engineclock != -1)
  677. tailsprintf(logline, "E: %d MHz ", engineclock);
  678. if (memclock != -1)
  679. tailsprintf(logline, "M: %d Mhz ", memclock);
  680. if (vddc != -1)
  681. tailsprintf(logline, "V: %.3fV ", vddc);
  682. if (activity != -1)
  683. tailsprintf(logline, "A: %d%% ", activity);
  684. if (powertune != -1)
  685. tailsprintf(logline, "P: %d%%", powertune);
  686. tailsprintf(logline, "\n");
  687. wlog(logline);
  688. }
  689. }
  690. #endif
  691. wlog("Last initialised: %s\n", cgpu->init);
  692. wlog("Intensity: ");
  693. if (gpus[gpu].dynamic)
  694. wlog("Dynamic (only one thread in use)\n");
  695. else
  696. wlog("%d\n", gpus[gpu].intensity);
  697. for (i = 0; i < mining_threads; i++) {
  698. thr = &thr_info[i];
  699. if (thr->cgpu != cgpu)
  700. continue;
  701. get_datestamp(checkin, &thr->last);
  702. wlog("Thread %d: %.1f Mh/s %s ", i, thr->rolling, cgpu->deven != DEV_DISABLED ? "Enabled" : "Disabled");
  703. switch (cgpu->status) {
  704. default:
  705. case LIFE_WELL:
  706. wlog("ALIVE");
  707. break;
  708. case LIFE_SICK:
  709. wlog("SICK reported in %s", checkin);
  710. break;
  711. case LIFE_DEAD:
  712. wlog("DEAD reported in %s", checkin);
  713. break;
  714. case LIFE_NOSTART:
  715. wlog("Never started");
  716. break;
  717. }
  718. if (thr->pause)
  719. wlog(" paused");
  720. wlog("\n");
  721. }
  722. wlog("\n");
  723. }
  724. wlogprint("[E]nable [D]isable [I]ntensity [R]estart GPU %s\n",adl_active ? "[C]hange settings" : "");
  725. wlogprint("Or press any other key to continue\n");
  726. input = getch();
  727. if (nDevs == 1)
  728. selected = 0;
  729. else
  730. selected = -1;
  731. if (!strncasecmp(&input, "e", 1)) {
  732. struct cgpu_info *cgpu;
  733. if (selected)
  734. selected = curses_int("Select GPU to enable");
  735. if (selected < 0 || selected >= nDevs) {
  736. wlogprint("Invalid selection\n");
  737. goto retry;
  738. }
  739. if (gpus[selected].deven != DEV_DISABLED) {
  740. wlogprint("Device already enabled\n");
  741. goto retry;
  742. }
  743. gpus[selected].deven = DEV_ENABLED;
  744. for (i = 0; i < mining_threads; ++i) {
  745. thr = &thr_info[i];
  746. cgpu = thr->cgpu;
  747. if (cgpu->api != &opencl_api)
  748. continue;
  749. if (dev_from_id(i) != selected)
  750. continue;
  751. if (cgpu->status != LIFE_WELL) {
  752. wlogprint("Must restart device before enabling it");
  753. goto retry;
  754. }
  755. applog(LOG_DEBUG, "Pushing ping to thread %d", thr->id);
  756. tq_push(thr->q, &ping);
  757. }
  758. goto retry;
  759. } if (!strncasecmp(&input, "d", 1)) {
  760. if (selected)
  761. selected = curses_int("Select GPU to disable");
  762. if (selected < 0 || selected >= nDevs) {
  763. wlogprint("Invalid selection\n");
  764. goto retry;
  765. }
  766. if (gpus[selected].deven == DEV_DISABLED) {
  767. wlogprint("Device already disabled\n");
  768. goto retry;
  769. }
  770. gpus[selected].deven = DEV_DISABLED;
  771. goto retry;
  772. } else if (!strncasecmp(&input, "i", 1)) {
  773. int intensity;
  774. char *intvar;
  775. if (selected)
  776. selected = curses_int("Select GPU to change intensity on");
  777. if (selected < 0 || selected >= nDevs) {
  778. wlogprint("Invalid selection\n");
  779. goto retry;
  780. }
  781. intvar = curses_input("Set GPU scan intensity (d or " _MIN_INTENSITY_STR " -> " _MAX_INTENSITY_STR ")");
  782. if (!intvar) {
  783. wlogprint("Invalid input\n");
  784. goto retry;
  785. }
  786. if (!strncasecmp(intvar, "d", 1)) {
  787. wlogprint("Dynamic mode enabled on gpu %d\n", selected);
  788. gpus[selected].dynamic = true;
  789. pause_dynamic_threads(selected);
  790. free(intvar);
  791. goto retry;
  792. }
  793. intensity = atoi(intvar);
  794. free(intvar);
  795. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  796. wlogprint("Invalid selection\n");
  797. goto retry;
  798. }
  799. gpus[selected].dynamic = false;
  800. gpus[selected].intensity = intensity;
  801. wlogprint("Intensity on gpu %d set to %d\n", selected, intensity);
  802. pause_dynamic_threads(selected);
  803. goto retry;
  804. } else if (!strncasecmp(&input, "r", 1)) {
  805. if (selected)
  806. selected = curses_int("Select GPU to attempt to restart");
  807. if (selected < 0 || selected >= nDevs) {
  808. wlogprint("Invalid selection\n");
  809. goto retry;
  810. }
  811. wlogprint("Attempting to restart threads of GPU %d\n", selected);
  812. reinit_device(&gpus[selected]);
  813. goto retry;
  814. } else if (adl_active && (!strncasecmp(&input, "c", 1))) {
  815. if (selected)
  816. selected = curses_int("Select GPU to change settings on");
  817. if (selected < 0 || selected >= nDevs) {
  818. wlogprint("Invalid selection\n");
  819. goto retry;
  820. }
  821. change_gpusettings(selected);
  822. goto retry;
  823. } else
  824. clear_logwin();
  825. immedok(logwin, false);
  826. opt_loginput = false;
  827. }
  828. #else
  829. void manage_gpu(void)
  830. {
  831. }
  832. #endif
  833. #ifdef HAVE_OPENCL
  834. static _clState *clStates[MAX_GPUDEVICES];
  835. #define CL_SET_BLKARG(blkvar) status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->blkvar)
  836. #define CL_SET_ARG(var) status |= clSetKernelArg(*kernel, num++, sizeof(var), (void *)&var)
  837. #define CL_SET_VARG(args, var) status |= clSetKernelArg(*kernel, num++, args * sizeof(uint), (void *)var)
  838. static cl_int queue_poclbm_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  839. {
  840. cl_kernel *kernel = &clState->kernel;
  841. unsigned int num = 0;
  842. cl_int status = 0;
  843. CL_SET_BLKARG(ctx_a);
  844. CL_SET_BLKARG(ctx_b);
  845. CL_SET_BLKARG(ctx_c);
  846. CL_SET_BLKARG(ctx_d);
  847. CL_SET_BLKARG(ctx_e);
  848. CL_SET_BLKARG(ctx_f);
  849. CL_SET_BLKARG(ctx_g);
  850. CL_SET_BLKARG(ctx_h);
  851. CL_SET_BLKARG(cty_b);
  852. CL_SET_BLKARG(cty_c);
  853. CL_SET_BLKARG(cty_f);
  854. CL_SET_BLKARG(cty_g);
  855. CL_SET_BLKARG(cty_h);
  856. if (!clState->goffset) {
  857. cl_uint vwidth = clState->vwidth;
  858. uint *nonces = alloca(sizeof(uint) * vwidth);
  859. unsigned int i;
  860. for (i = 0; i < vwidth; i++)
  861. nonces[i] = blk->nonce + (i * threads);
  862. CL_SET_VARG(vwidth, nonces);
  863. }
  864. CL_SET_BLKARG(fW0);
  865. CL_SET_BLKARG(fW1);
  866. CL_SET_BLKARG(fW2);
  867. CL_SET_BLKARG(fW3);
  868. CL_SET_BLKARG(fW15);
  869. CL_SET_BLKARG(fW01r);
  870. CL_SET_BLKARG(D1A);
  871. CL_SET_BLKARG(C1addK5);
  872. CL_SET_BLKARG(B1addK6);
  873. CL_SET_BLKARG(W16addK16);
  874. CL_SET_BLKARG(W17addK17);
  875. CL_SET_BLKARG(PreVal4addT1);
  876. CL_SET_BLKARG(PreVal0);
  877. CL_SET_ARG(clState->outputBuffer);
  878. return status;
  879. }
  880. static cl_int queue_phatk_kernel(_clState *clState, dev_blk_ctx *blk,
  881. __maybe_unused cl_uint threads)
  882. {
  883. cl_kernel *kernel = &clState->kernel;
  884. cl_uint vwidth = clState->vwidth;
  885. unsigned int i, num = 0;
  886. cl_int status = 0;
  887. uint *nonces;
  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_d);
  899. CL_SET_BLKARG(cty_f);
  900. CL_SET_BLKARG(cty_g);
  901. CL_SET_BLKARG(cty_h);
  902. nonces = alloca(sizeof(uint) * vwidth);
  903. for (i = 0; i < vwidth; i++)
  904. nonces[i] = blk->nonce + i;
  905. CL_SET_VARG(vwidth, nonces);
  906. CL_SET_BLKARG(W16);
  907. CL_SET_BLKARG(W17);
  908. CL_SET_BLKARG(PreVal4_2);
  909. CL_SET_BLKARG(PreVal0);
  910. CL_SET_BLKARG(PreW18);
  911. CL_SET_BLKARG(PreW19);
  912. CL_SET_BLKARG(PreW31);
  913. CL_SET_BLKARG(PreW32);
  914. CL_SET_ARG(clState->outputBuffer);
  915. return status;
  916. }
  917. static cl_int queue_diakgcn_kernel(_clState *clState, dev_blk_ctx *blk,
  918. __maybe_unused cl_uint threads)
  919. {
  920. cl_kernel *kernel = &clState->kernel;
  921. cl_uint vwidth = clState->vwidth;
  922. unsigned int i, num = 0;
  923. cl_int status = 0;
  924. uint *nonces;
  925. nonces = alloca(sizeof(uint) * vwidth);
  926. for (i = 0; i < vwidth; i++)
  927. nonces[i] = blk->nonce + i;
  928. CL_SET_VARG(vwidth, nonces);
  929. CL_SET_BLKARG(PreVal0);
  930. CL_SET_BLKARG(PreVal4_2);
  931. CL_SET_BLKARG(cty_h);
  932. CL_SET_BLKARG(D1A);
  933. CL_SET_BLKARG(cty_b);
  934. CL_SET_BLKARG(cty_c);
  935. CL_SET_BLKARG(cty_f);
  936. CL_SET_BLKARG(cty_g);
  937. CL_SET_BLKARG(C1addK5);
  938. CL_SET_BLKARG(B1addK6);
  939. CL_SET_BLKARG(PreVal0addK7);
  940. CL_SET_BLKARG(W16addK16);
  941. CL_SET_BLKARG(W17addK17);
  942. CL_SET_BLKARG(PreW18);
  943. CL_SET_BLKARG(PreW19);
  944. CL_SET_BLKARG(W16);
  945. CL_SET_BLKARG(W17);
  946. CL_SET_BLKARG(PreW31);
  947. CL_SET_BLKARG(PreW32);
  948. CL_SET_BLKARG(ctx_a);
  949. CL_SET_BLKARG(ctx_b);
  950. CL_SET_BLKARG(ctx_c);
  951. CL_SET_BLKARG(ctx_d);
  952. CL_SET_BLKARG(ctx_e);
  953. CL_SET_BLKARG(ctx_f);
  954. CL_SET_BLKARG(ctx_g);
  955. CL_SET_BLKARG(ctx_h);
  956. CL_SET_BLKARG(zeroA);
  957. CL_SET_BLKARG(zeroB);
  958. CL_SET_BLKARG(oneA);
  959. CL_SET_BLKARG(twoA);
  960. CL_SET_BLKARG(threeA);
  961. CL_SET_BLKARG(fourA);
  962. CL_SET_BLKARG(fiveA);
  963. CL_SET_BLKARG(sixA);
  964. CL_SET_BLKARG(sevenA);
  965. CL_SET_ARG(clState->outputBuffer);
  966. return status;
  967. }
  968. static cl_int queue_diablo_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  969. {
  970. cl_kernel *kernel = &clState->kernel;
  971. unsigned int num = 0;
  972. cl_int status = 0;
  973. if (!clState->goffset) {
  974. cl_uint vwidth = clState->vwidth;
  975. uint *nonces = alloca(sizeof(uint) * vwidth);
  976. unsigned int i;
  977. for (i = 0; i < vwidth; i++)
  978. nonces[i] = blk->nonce + (i * threads);
  979. CL_SET_VARG(vwidth, nonces);
  980. }
  981. CL_SET_BLKARG(PreVal0);
  982. CL_SET_BLKARG(PreVal0addK7);
  983. CL_SET_BLKARG(PreVal4addT1);
  984. CL_SET_BLKARG(PreW18);
  985. CL_SET_BLKARG(PreW19);
  986. CL_SET_BLKARG(W16);
  987. CL_SET_BLKARG(W17);
  988. CL_SET_BLKARG(W16addK16);
  989. CL_SET_BLKARG(W17addK17);
  990. CL_SET_BLKARG(PreW31);
  991. CL_SET_BLKARG(PreW32);
  992. CL_SET_BLKARG(D1A);
  993. CL_SET_BLKARG(cty_b);
  994. CL_SET_BLKARG(cty_c);
  995. CL_SET_BLKARG(cty_h);
  996. CL_SET_BLKARG(cty_f);
  997. CL_SET_BLKARG(cty_g);
  998. CL_SET_BLKARG(C1addK5);
  999. CL_SET_BLKARG(B1addK6);
  1000. CL_SET_BLKARG(ctx_a);
  1001. CL_SET_BLKARG(ctx_b);
  1002. CL_SET_BLKARG(ctx_c);
  1003. CL_SET_BLKARG(ctx_d);
  1004. CL_SET_BLKARG(ctx_e);
  1005. CL_SET_BLKARG(ctx_f);
  1006. CL_SET_BLKARG(ctx_g);
  1007. CL_SET_BLKARG(ctx_h);
  1008. CL_SET_ARG(clState->outputBuffer);
  1009. return status;
  1010. }
  1011. static void set_threads_hashes(unsigned int vectors, unsigned int *threads,
  1012. unsigned int *hashes, size_t *globalThreads,
  1013. unsigned int minthreads, int intensity)
  1014. {
  1015. *threads = 1 << (15 + intensity);
  1016. if (*threads < minthreads)
  1017. *threads = minthreads;
  1018. *globalThreads = *threads;
  1019. *hashes = *threads * vectors;
  1020. }
  1021. #endif /* HAVE_OPENCL */
  1022. #ifdef HAVE_OPENCL
  1023. /* We have only one thread that ever re-initialises GPUs, thus if any GPU
  1024. * init command fails due to a completely wedged GPU, the thread will never
  1025. * return, unable to harm other GPUs. If it does return, it means we only had
  1026. * a soft failure and then the reinit_gpu thread is ready to tackle another
  1027. * GPU */
  1028. void *reinit_gpu(void *userdata)
  1029. {
  1030. struct thr_info *mythr = userdata;
  1031. struct cgpu_info *cgpu;
  1032. struct thr_info *thr;
  1033. struct timeval now;
  1034. char name[256];
  1035. int thr_id;
  1036. int gpu;
  1037. pthread_detach(pthread_self());
  1038. select_cgpu:
  1039. cgpu = tq_pop(mythr->q, NULL);
  1040. if (!cgpu)
  1041. goto out;
  1042. if (clDevicesNum() != nDevs) {
  1043. applog(LOG_WARNING, "Hardware not reporting same number of active devices, will not attempt to restart GPU");
  1044. goto out;
  1045. }
  1046. gpu = cgpu->device_id;
  1047. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  1048. thr = &thr_info[thr_id];
  1049. cgpu = thr->cgpu;
  1050. if (cgpu->api != &opencl_api)
  1051. continue;
  1052. if (dev_from_id(thr_id) != gpu)
  1053. continue;
  1054. thr = &thr_info[thr_id];
  1055. if (!thr) {
  1056. applog(LOG_WARNING, "No reference to thread %d exists", thr_id);
  1057. continue;
  1058. }
  1059. thr->rolling = thr->cgpu->rolling = 0;
  1060. /* Reports the last time we tried to revive a sick GPU */
  1061. gettimeofday(&thr->sick, NULL);
  1062. if (!pthread_cancel(thr->pth)) {
  1063. applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
  1064. } else
  1065. applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
  1066. }
  1067. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  1068. int virtual_gpu;
  1069. thr = &thr_info[thr_id];
  1070. cgpu = thr->cgpu;
  1071. if (cgpu->api != &opencl_api)
  1072. continue;
  1073. if (dev_from_id(thr_id) != gpu)
  1074. continue;
  1075. virtual_gpu = cgpu->virtual_gpu;
  1076. /* Lose this ram cause we may get stuck here! */
  1077. //tq_freeze(thr->q);
  1078. thr->q = tq_new();
  1079. if (!thr->q)
  1080. quit(1, "Failed to tq_new in reinit_gpu");
  1081. /* Lose this ram cause we may dereference in the dying thread! */
  1082. //free(clState);
  1083. applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
  1084. clStates[thr_id] = initCl(virtual_gpu, name, sizeof(name));
  1085. if (!clStates[thr_id]) {
  1086. applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
  1087. goto select_cgpu;
  1088. }
  1089. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1090. if (unlikely(thr_info_create(thr, NULL, miner_thread, thr))) {
  1091. applog(LOG_ERR, "thread %d create failed", thr_id);
  1092. return NULL;
  1093. }
  1094. applog(LOG_WARNING, "Thread %d restarted", thr_id);
  1095. }
  1096. gettimeofday(&now, NULL);
  1097. get_datestamp(cgpu->init, &now);
  1098. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  1099. thr = &thr_info[thr_id];
  1100. cgpu = thr->cgpu;
  1101. if (cgpu->api != &opencl_api)
  1102. continue;
  1103. if (dev_from_id(thr_id) != gpu)
  1104. continue;
  1105. tq_push(thr->q, &ping);
  1106. }
  1107. goto select_cgpu;
  1108. out:
  1109. return NULL;
  1110. }
  1111. #else
  1112. void *reinit_gpu(void *userdata)
  1113. {
  1114. return NULL;
  1115. }
  1116. #endif
  1117. #ifdef HAVE_OPENCL
  1118. struct device_api opencl_api;
  1119. static void opencl_detect()
  1120. {
  1121. if (!load_opencl_symbols()) {
  1122. nDevs = 0;
  1123. return;
  1124. }
  1125. int i;
  1126. nDevs = clDevicesNum();
  1127. if (nDevs < 0) {
  1128. applog(LOG_ERR, "clDevicesNum returned error, no GPUs usable");
  1129. nDevs = 0;
  1130. }
  1131. if (MAX_DEVICES - total_devices < nDevs)
  1132. nDevs = MAX_DEVICES - total_devices;
  1133. if (!nDevs)
  1134. return;
  1135. for (i = 0; i < nDevs; ++i) {
  1136. struct cgpu_info *cgpu;
  1137. cgpu = &gpus[i];
  1138. cgpu->deven = DEV_ENABLED;
  1139. cgpu->api = &opencl_api;
  1140. cgpu->device_id = i;
  1141. cgpu->threads = opt_g_threads;
  1142. cgpu->virtual_gpu = i;
  1143. add_cgpu(cgpu);
  1144. }
  1145. if (!opt_noadl)
  1146. init_adl(nDevs);
  1147. }
  1148. static void reinit_opencl_device(struct cgpu_info *gpu)
  1149. {
  1150. tq_push(thr_info[gpur_thr_id].q, gpu);
  1151. }
  1152. #ifdef HAVE_ADL
  1153. static void get_opencl_statline_before(char *buf, struct cgpu_info *gpu)
  1154. {
  1155. if (gpu->has_adl) {
  1156. int gpuid = gpu->device_id;
  1157. float gt = gpu_temp(gpuid);
  1158. int gf = gpu_fanspeed(gpuid);
  1159. int gp;
  1160. if (gt != -1)
  1161. tailsprintf(buf, "%5.1fC ", gt);
  1162. else
  1163. tailsprintf(buf, " ", gt);
  1164. if (gf != -1)
  1165. tailsprintf(buf, "%4dRPM ", gf);
  1166. else if ((gp = gpu_fanpercent(gpuid)) != -1)
  1167. tailsprintf(buf, "%3d%% ", gp);
  1168. else
  1169. tailsprintf(buf, " ");
  1170. tailsprintf(buf, "| ");
  1171. }
  1172. }
  1173. #endif
  1174. static void get_opencl_statline(char *buf, struct cgpu_info *gpu)
  1175. {
  1176. tailsprintf(buf, " I:%2d", gpu->intensity);
  1177. }
  1178. struct opencl_thread_data {
  1179. cl_int (*queue_kernel_parameters)(_clState *, dev_blk_ctx *, cl_uint);
  1180. uint32_t *res;
  1181. struct work *last_work;
  1182. struct work _last_work;
  1183. };
  1184. static uint32_t *blank_res;
  1185. static bool opencl_thread_prepare(struct thr_info *thr)
  1186. {
  1187. char name[256];
  1188. struct timeval now;
  1189. struct cgpu_info *cgpu = thr->cgpu;
  1190. int gpu = cgpu->device_id;
  1191. int virtual_gpu = cgpu->virtual_gpu;
  1192. int i = thr->id;
  1193. static bool failmessage = false;
  1194. if (!blank_res)
  1195. blank_res = calloc(BUFFERSIZE, 1);
  1196. if (!blank_res) {
  1197. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1198. return false;
  1199. }
  1200. strcpy(name, "");
  1201. applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
  1202. clStates[i] = initCl(virtual_gpu, name, sizeof(name));
  1203. if (!clStates[i]) {
  1204. if (use_curses)
  1205. enable_curses();
  1206. applog(LOG_ERR, "Failed to init GPU thread %d, disabling device %d", i, gpu);
  1207. if (!failmessage) {
  1208. char *buf;
  1209. applog(LOG_ERR, "Restarting the GPU from the menu will not fix this.");
  1210. applog(LOG_ERR, "Try restarting cgminer.");
  1211. failmessage = true;
  1212. #ifdef HAVE_CURSES
  1213. if (use_curses) {
  1214. buf = curses_input("Press enter to continue");
  1215. if (buf)
  1216. free(buf);
  1217. }
  1218. #endif
  1219. }
  1220. cgpu->deven = DEV_DISABLED;
  1221. cgpu->status = LIFE_NOSTART;
  1222. cgpu->device_last_not_well = time(NULL);
  1223. cgpu->device_not_well_reason = REASON_DEV_NOSTART;
  1224. cgpu->dev_nostart_count++;
  1225. return false;
  1226. }
  1227. if (!cgpu->name)
  1228. cgpu->name = strdup(name);
  1229. if (!cgpu->kname)
  1230. {
  1231. switch (clStates[i]->chosen_kernel) {
  1232. case KL_DIABLO:
  1233. cgpu->kname = "diablo";
  1234. break;
  1235. case KL_DIAKGCN:
  1236. cgpu->kname = "diakgcn";
  1237. break;
  1238. case KL_PHATK:
  1239. cgpu->kname = "phatk";
  1240. break;
  1241. case KL_POCLBM:
  1242. cgpu->kname = "poclbm";
  1243. default:
  1244. break;
  1245. }
  1246. }
  1247. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1248. gettimeofday(&now, NULL);
  1249. get_datestamp(cgpu->init, &now);
  1250. have_opencl = true;
  1251. return true;
  1252. }
  1253. static bool opencl_thread_init(struct thr_info *thr)
  1254. {
  1255. const int thr_id = thr->id;
  1256. struct cgpu_info *gpu = thr->cgpu;
  1257. struct opencl_thread_data *thrdata;
  1258. _clState *clState = clStates[thr_id];
  1259. cl_int status;
  1260. thrdata = calloc(1, sizeof(*thrdata));
  1261. thr->cgpu_data = thrdata;
  1262. if (!thrdata) {
  1263. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1264. return false;
  1265. }
  1266. switch (clState->chosen_kernel) {
  1267. case KL_POCLBM:
  1268. thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
  1269. break;
  1270. case KL_PHATK:
  1271. thrdata->queue_kernel_parameters = &queue_phatk_kernel;
  1272. break;
  1273. case KL_DIAKGCN:
  1274. thrdata->queue_kernel_parameters = &queue_diakgcn_kernel;
  1275. break;
  1276. default:
  1277. case KL_DIABLO:
  1278. thrdata->queue_kernel_parameters = &queue_diablo_kernel;
  1279. break;
  1280. }
  1281. thrdata->res = calloc(BUFFERSIZE, 1);
  1282. if (!thrdata->res) {
  1283. free(thrdata);
  1284. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1285. return false;
  1286. }
  1287. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  1288. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1289. if (unlikely(status != CL_SUCCESS)) {
  1290. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1291. return false;
  1292. }
  1293. gpu->status = LIFE_WELL;
  1294. gpu->device_last_well = time(NULL);
  1295. return true;
  1296. }
  1297. static void opencl_free_work(struct thr_info *thr, struct work *work)
  1298. {
  1299. const int thr_id = thr->id;
  1300. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1301. _clState *clState = clStates[thr_id];
  1302. clFinish(clState->commandQueue);
  1303. if (thrdata->res[FOUND]) {
  1304. thrdata->last_work = &thrdata->_last_work;
  1305. memcpy(thrdata->last_work, work, sizeof(*thrdata->last_work));
  1306. }
  1307. }
  1308. static bool opencl_prepare_work(struct thr_info __maybe_unused *thr, struct work *work)
  1309. {
  1310. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1311. return true;
  1312. }
  1313. extern int opt_dynamic_interval;
  1314. static uint64_t opencl_scanhash(struct thr_info *thr, struct work *work,
  1315. uint64_t __maybe_unused max_nonce)
  1316. {
  1317. const int thr_id = thr->id;
  1318. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1319. struct cgpu_info *gpu = thr->cgpu;
  1320. _clState *clState = clStates[thr_id];
  1321. const cl_kernel *kernel = &clState->kernel;
  1322. double gpu_ms_average = 7;
  1323. cl_int status;
  1324. size_t globalThreads[1];
  1325. size_t localThreads[1] = { clState->wsize };
  1326. unsigned int threads;
  1327. unsigned int hashes;
  1328. struct timeval tv_gpustart, tv_gpuend, diff;
  1329. suseconds_t gpu_us;
  1330. gettimeofday(&tv_gpustart, NULL);
  1331. timeval_subtract(&diff, &tv_gpustart, &tv_gpuend);
  1332. /* This finish flushes the readbuffer set with CL_FALSE later */
  1333. clFinish(clState->commandQueue);
  1334. gettimeofday(&tv_gpuend, NULL);
  1335. timeval_subtract(&diff, &tv_gpuend, &tv_gpustart);
  1336. gpu_us = diff.tv_sec * 1000000 + diff.tv_usec;
  1337. decay_time(&gpu_ms_average, gpu_us / 1000);
  1338. if (gpu->dynamic) {
  1339. /* Try to not let the GPU be out for longer than 6ms, but
  1340. * increase intensity when the system is idle, unless
  1341. * dynamic is disabled. */
  1342. if (gpu_ms_average > opt_dynamic_interval) {
  1343. if (gpu->intensity > MIN_INTENSITY)
  1344. --gpu->intensity;
  1345. } else if (gpu_ms_average < ((opt_dynamic_interval / 2) ? : 1)) {
  1346. if (gpu->intensity < MAX_INTENSITY)
  1347. ++gpu->intensity;
  1348. }
  1349. }
  1350. set_threads_hashes(clState->vwidth, &threads, &hashes, globalThreads,
  1351. localThreads[0], gpu->intensity);
  1352. if (hashes > gpu->max_hashes)
  1353. gpu->max_hashes = hashes;
  1354. status = thrdata->queue_kernel_parameters(clState, &work->blk, globalThreads[0]);
  1355. if (unlikely(status != CL_SUCCESS)) {
  1356. applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
  1357. return 0;
  1358. }
  1359. /* MAXBUFFERS entry is used as a flag to say nonces exist */
  1360. if (thrdata->res[FOUND]) {
  1361. /* Clear the buffer again */
  1362. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1363. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1364. if (unlikely(status != CL_SUCCESS)) {
  1365. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1366. return 0;
  1367. }
  1368. if (unlikely(thrdata->last_work)) {
  1369. applog(LOG_DEBUG, "GPU %d found something in last work?", gpu->device_id);
  1370. postcalc_hash_async(thr, thrdata->last_work, thrdata->res);
  1371. thrdata->last_work = NULL;
  1372. } else {
  1373. applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
  1374. postcalc_hash_async(thr, work, thrdata->res);
  1375. }
  1376. memset(thrdata->res, 0, BUFFERSIZE);
  1377. clFinish(clState->commandQueue);
  1378. }
  1379. if (clState->goffset) {
  1380. size_t global_work_offset[1];
  1381. global_work_offset[0] = work->blk.nonce;
  1382. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, global_work_offset,
  1383. globalThreads, localThreads, 0, NULL, NULL);
  1384. } else
  1385. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1386. globalThreads, localThreads, 0, NULL, NULL);
  1387. if (unlikely(status != CL_SUCCESS)) {
  1388. applog(LOG_ERR, "Error: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)");
  1389. return 0;
  1390. }
  1391. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1392. BUFFERSIZE, thrdata->res, 0, NULL, NULL);
  1393. if (unlikely(status != CL_SUCCESS)) {
  1394. applog(LOG_ERR, "Error: clEnqueueReadBuffer failed. (clEnqueueReadBuffer)");
  1395. return 0;
  1396. }
  1397. /* The amount of work scanned can fluctuate when intensity changes
  1398. * and since we do this one cycle behind, we increment the work more
  1399. * than enough to prevent repeating work */
  1400. work->blk.nonce += gpu->max_hashes;
  1401. return hashes;
  1402. }
  1403. static void opencl_thread_shutdown(struct thr_info *thr)
  1404. {
  1405. const int thr_id = thr->id;
  1406. _clState *clState = clStates[thr_id];
  1407. clReleaseCommandQueue(clState->commandQueue);
  1408. clReleaseKernel(clState->kernel);
  1409. clReleaseProgram(clState->program);
  1410. clReleaseContext(clState->context);
  1411. }
  1412. struct device_api opencl_api = {
  1413. .dname = "opencl",
  1414. .name = "GPU",
  1415. .api_detect = opencl_detect,
  1416. .reinit_device = reinit_opencl_device,
  1417. #ifdef HAVE_ADL
  1418. .get_statline_before = get_opencl_statline_before,
  1419. #endif
  1420. .get_statline = get_opencl_statline,
  1421. .thread_prepare = opencl_thread_prepare,
  1422. .thread_init = opencl_thread_init,
  1423. .free_work = opencl_free_work,
  1424. .prepare_work = opencl_prepare_work,
  1425. .scanhash = opencl_scanhash,
  1426. .thread_shutdown = opencl_thread_shutdown,
  1427. };
  1428. #endif