driver-opencl.c 48 KB

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