driver-opencl.c 50 KB

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