driver-opencl.c 45 KB

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