driver-opencl.c 46 KB

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