driver-opencl.c 48 KB

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