driver-opencl.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874
  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 HAVE_CURSES
  13. // Must be before stdbool, since pdcurses typedefs bool :/
  14. #include <curses.h>
  15. #endif
  16. #ifndef WIN32
  17. #include <dlfcn.h>
  18. #else
  19. #include <windows.h>
  20. #endif
  21. #include <ctype.h>
  22. #include <math.h>
  23. #include <string.h>
  24. #include <stdbool.h>
  25. #include <stdint.h>
  26. #include <stdio.h>
  27. #include <sys/types.h>
  28. #ifndef WIN32
  29. #include <sys/resource.h>
  30. #endif
  31. #define OMIT_OPENCL_API
  32. #include "compat.h"
  33. #include "miner.h"
  34. #include "deviceapi.h"
  35. #include "driver-opencl.h"
  36. #include "findnonce.h"
  37. #include "ocl.h"
  38. #include "adl.h"
  39. #include "util.h"
  40. /* TODO: cleanup externals ********************/
  41. #ifdef HAVE_OPENCL
  42. /* Platform API */
  43. CL_API_ENTRY cl_int CL_API_CALL
  44. (*clGetPlatformIDs)(cl_uint /* num_entries */,
  45. cl_platform_id * /* platforms */,
  46. cl_uint * /* num_platforms */) CL_API_SUFFIX__VERSION_1_0;
  47. CL_API_ENTRY cl_int CL_API_CALL
  48. (*clGetPlatformInfo)(cl_platform_id /* platform */,
  49. cl_platform_info /* param_name */,
  50. size_t /* param_value_size */,
  51. void * /* param_value */,
  52. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  53. /* Device APIs */
  54. CL_API_ENTRY cl_int CL_API_CALL
  55. (*clGetDeviceIDs)(cl_platform_id /* platform */,
  56. cl_device_type /* device_type */,
  57. cl_uint /* num_entries */,
  58. cl_device_id * /* devices */,
  59. cl_uint * /* num_devices */) CL_API_SUFFIX__VERSION_1_0;
  60. CL_API_ENTRY cl_int CL_API_CALL
  61. (*clGetDeviceInfo)(cl_device_id /* device */,
  62. cl_device_info /* param_name */,
  63. size_t /* param_value_size */,
  64. void * /* param_value */,
  65. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  66. /* Context APIs */
  67. CL_API_ENTRY cl_context CL_API_CALL
  68. (*clCreateContextFromType)(const cl_context_properties * /* properties */,
  69. cl_device_type /* device_type */,
  70. void (CL_CALLBACK * /* pfn_notify*/ )(const char *, const void *, size_t, void *),
  71. void * /* user_data */,
  72. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  73. CL_API_ENTRY cl_int CL_API_CALL
  74. (*clReleaseContext)(cl_context /* context */) CL_API_SUFFIX__VERSION_1_0;
  75. /* Command Queue APIs */
  76. CL_API_ENTRY cl_command_queue CL_API_CALL
  77. (*clCreateCommandQueue)(cl_context /* context */,
  78. cl_device_id /* device */,
  79. cl_command_queue_properties /* properties */,
  80. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  81. CL_API_ENTRY cl_int CL_API_CALL
  82. (*clReleaseCommandQueue)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  83. /* Memory Object APIs */
  84. CL_API_ENTRY cl_mem CL_API_CALL
  85. (*clCreateBuffer)(cl_context /* context */,
  86. cl_mem_flags /* flags */,
  87. size_t /* size */,
  88. void * /* host_ptr */,
  89. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  90. /* Program Object APIs */
  91. CL_API_ENTRY cl_program CL_API_CALL
  92. (*clCreateProgramWithSource)(cl_context /* context */,
  93. cl_uint /* count */,
  94. const char ** /* strings */,
  95. const size_t * /* lengths */,
  96. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  97. CL_API_ENTRY cl_program CL_API_CALL
  98. (*clCreateProgramWithBinary)(cl_context /* context */,
  99. cl_uint /* num_devices */,
  100. const cl_device_id * /* device_list */,
  101. const size_t * /* lengths */,
  102. const unsigned char ** /* binaries */,
  103. cl_int * /* binary_status */,
  104. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  105. CL_API_ENTRY cl_int CL_API_CALL
  106. (*clReleaseProgram)(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0;
  107. CL_API_ENTRY cl_int CL_API_CALL
  108. (*clBuildProgram)(cl_program /* program */,
  109. cl_uint /* num_devices */,
  110. const cl_device_id * /* device_list */,
  111. const char * /* options */,
  112. void (CL_CALLBACK * /* pfn_notify */)(cl_program /* program */, void * /* user_data */),
  113. void * /* user_data */) CL_API_SUFFIX__VERSION_1_0;
  114. CL_API_ENTRY cl_int CL_API_CALL
  115. (*clGetProgramInfo)(cl_program /* program */,
  116. cl_program_info /* param_name */,
  117. size_t /* param_value_size */,
  118. void * /* param_value */,
  119. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  120. CL_API_ENTRY cl_int CL_API_CALL
  121. (*clGetProgramBuildInfo)(cl_program /* program */,
  122. cl_device_id /* device */,
  123. cl_program_build_info /* param_name */,
  124. size_t /* param_value_size */,
  125. void * /* param_value */,
  126. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  127. /* Kernel Object APIs */
  128. CL_API_ENTRY cl_kernel CL_API_CALL
  129. (*clCreateKernel)(cl_program /* program */,
  130. const char * /* kernel_name */,
  131. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  132. CL_API_ENTRY cl_int CL_API_CALL
  133. (*clReleaseKernel)(cl_kernel /* kernel */) CL_API_SUFFIX__VERSION_1_0;
  134. CL_API_ENTRY cl_int CL_API_CALL
  135. (*clSetKernelArg)(cl_kernel /* kernel */,
  136. cl_uint /* arg_index */,
  137. size_t /* arg_size */,
  138. const void * /* arg_value */) CL_API_SUFFIX__VERSION_1_0;
  139. /* Flush and Finish APIs */
  140. CL_API_ENTRY cl_int CL_API_CALL
  141. (*clFinish)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  142. /* Enqueued Commands APIs */
  143. CL_API_ENTRY cl_int CL_API_CALL
  144. (*clEnqueueReadBuffer)(cl_command_queue /* command_queue */,
  145. cl_mem /* buffer */,
  146. cl_bool /* blocking_read */,
  147. size_t /* offset */,
  148. size_t /* size */,
  149. void * /* ptr */,
  150. cl_uint /* num_events_in_wait_list */,
  151. const cl_event * /* event_wait_list */,
  152. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  153. CL_API_ENTRY cl_int CL_API_CALL
  154. (*clEnqueueWriteBuffer)(cl_command_queue /* command_queue */,
  155. cl_mem /* buffer */,
  156. cl_bool /* blocking_write */,
  157. size_t /* offset */,
  158. size_t /* size */,
  159. const void * /* ptr */,
  160. cl_uint /* num_events_in_wait_list */,
  161. const cl_event * /* event_wait_list */,
  162. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  163. CL_API_ENTRY cl_int CL_API_CALL
  164. (*clEnqueueNDRangeKernel)(cl_command_queue /* command_queue */,
  165. cl_kernel /* kernel */,
  166. cl_uint /* work_dim */,
  167. const size_t * /* global_work_offset */,
  168. const size_t * /* global_work_size */,
  169. const size_t * /* local_work_size */,
  170. cl_uint /* num_events_in_wait_list */,
  171. const cl_event * /* event_wait_list */,
  172. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  173. #ifdef WIN32
  174. #define dlsym (void*)GetProcAddress
  175. #define dlclose FreeLibrary
  176. #endif
  177. #define LOAD_OCL_SYM(sym) do { \
  178. if (!(sym = dlsym(cl, #sym))) { \
  179. applog(LOG_ERR, "Failed to load OpenCL symbol " #sym ", no GPUs usable"); \
  180. dlclose(cl); \
  181. return false; \
  182. } \
  183. } while(0)
  184. static bool
  185. load_opencl_symbols() {
  186. #if defined(__APPLE__)
  187. void *cl = dlopen("/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL", RTLD_LAZY);
  188. #elif !defined(WIN32)
  189. void *cl = dlopen("libOpenCL.so", RTLD_LAZY);
  190. #else
  191. HMODULE cl = LoadLibrary("OpenCL.dll");
  192. #endif
  193. if (!cl)
  194. {
  195. applog(LOG_ERR, "Failed to load OpenCL library, no GPUs usable");
  196. return false;
  197. }
  198. LOAD_OCL_SYM(clGetPlatformIDs);
  199. LOAD_OCL_SYM(clGetPlatformInfo);
  200. LOAD_OCL_SYM(clGetDeviceIDs);
  201. LOAD_OCL_SYM(clGetDeviceInfo);
  202. LOAD_OCL_SYM(clCreateContextFromType);
  203. LOAD_OCL_SYM(clReleaseContext);
  204. LOAD_OCL_SYM(clCreateCommandQueue);
  205. LOAD_OCL_SYM(clReleaseCommandQueue);
  206. LOAD_OCL_SYM(clCreateBuffer);
  207. LOAD_OCL_SYM(clCreateProgramWithSource);
  208. LOAD_OCL_SYM(clCreateProgramWithBinary);
  209. LOAD_OCL_SYM(clReleaseProgram);
  210. LOAD_OCL_SYM(clBuildProgram);
  211. LOAD_OCL_SYM(clGetProgramInfo);
  212. LOAD_OCL_SYM(clGetProgramBuildInfo);
  213. LOAD_OCL_SYM(clCreateKernel);
  214. LOAD_OCL_SYM(clReleaseKernel);
  215. LOAD_OCL_SYM(clSetKernelArg);
  216. LOAD_OCL_SYM(clFinish);
  217. LOAD_OCL_SYM(clEnqueueReadBuffer);
  218. LOAD_OCL_SYM(clEnqueueWriteBuffer);
  219. LOAD_OCL_SYM(clEnqueueNDRangeKernel);
  220. return true;
  221. }
  222. #endif
  223. #ifdef HAVE_CURSES
  224. extern WINDOW *mainwin, *statuswin, *logwin;
  225. extern void enable_curses(void);
  226. #endif
  227. extern int mining_threads;
  228. extern int opt_g_threads;
  229. extern bool ping;
  230. extern bool opt_loginput;
  231. extern char *opt_kernel_path;
  232. extern int gpur_thr_id;
  233. extern bool opt_noadl;
  234. extern bool have_opencl;
  235. static _clState *clStates[MAX_GPUDEVICES];
  236. extern void *miner_thread(void *userdata);
  237. extern int dev_from_id(int thr_id);
  238. extern void decay_time(double *f, double fadd);
  239. /**********************************************/
  240. #ifdef HAVE_ADL
  241. extern float gpu_temp(int gpu);
  242. extern int gpu_fanspeed(int gpu);
  243. extern int gpu_fanpercent(int gpu);
  244. #endif
  245. void opencl_early_init()
  246. {
  247. static struct opencl_device_data dataarray[MAX_GPUDEVICES];
  248. for (int i = 0; i < MAX_GPUDEVICES; ++i)
  249. {
  250. struct opencl_device_data * const data = &dataarray[i];
  251. *data = (struct opencl_device_data){
  252. .dynamic = true,
  253. };
  254. gpus[i] = (struct cgpu_info){
  255. .device_data = data,
  256. };
  257. }
  258. }
  259. static
  260. const char *_set_list(char * const arg, const char * const emsg, bool (*set_func)(struct cgpu_info *, const char *))
  261. {
  262. int i, device = 0;
  263. char *nextptr, buf[0x10];
  264. nextptr = strtok(arg, ",");
  265. if (nextptr == NULL)
  266. return emsg;
  267. if (!set_func(&gpus[device++], nextptr))
  268. return emsg;
  269. snprintf(buf, sizeof(buf), "%s", nextptr);
  270. while ((nextptr = strtok(NULL, ",")) != NULL)
  271. if (!set_func(&gpus[device++], nextptr))
  272. return emsg;
  273. if (device == 1) {
  274. for (i = device; i < MAX_GPUDEVICES; i++)
  275. set_func(&gpus[i], buf);
  276. }
  277. return NULL;
  278. }
  279. #define _SET_INTERFACE(PNAME) \
  280. static \
  281. const char *opencl_init_ ## PNAME (struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success) \
  282. { \
  283. if (!_set_ ## PNAME (proc, newvalue)) \
  284. return "Invalid value for " #PNAME; \
  285. return NULL; \
  286. } \
  287. // END OF _SET_INTERFACE
  288. #define _SET_INT_LIST2(PNAME, VCHECK, FIELD) \
  289. static \
  290. bool _set_ ## PNAME (struct cgpu_info * const cgpu, const char * const _val) \
  291. { \
  292. const int v = atoi(_val); \
  293. if (!(VCHECK)) \
  294. return false; \
  295. FIELD = v; \
  296. return true; \
  297. } \
  298. _SET_INTERFACE(PNAME) \
  299. const char *set_ ## PNAME(char *arg) \
  300. { \
  301. return _set_list(arg, "Invalid value passed to " #PNAME, _set_ ## PNAME); \
  302. } \
  303. // END OF _SET_INT_LIST
  304. #define _SET_INT_LIST(PNAME, VCHECK, FIELD) \
  305. _SET_INT_LIST2(PNAME, VCHECK, ((struct opencl_device_data *)cgpu->device_data)->FIELD)
  306. #ifdef HAVE_OPENCL
  307. _SET_INT_LIST(vector , (v == 1 || v == 2 || v == 4), vwidth )
  308. _SET_INT_LIST(worksize, (v >= 1 && v <= 9999) , work_size)
  309. #ifdef USE_SCRYPT
  310. _SET_INT_LIST(shaders , true, shaders)
  311. _SET_INT_LIST(lookup_gap , true, opt_lg )
  312. _SET_INT_LIST(thread_concurrency, true, opt_tc )
  313. #endif
  314. static
  315. enum cl_kernels select_kernel(const char * const arg)
  316. {
  317. if (!strcmp(arg, "diablo"))
  318. return KL_DIABLO;
  319. if (!strcmp(arg, "diakgcn"))
  320. return KL_DIAKGCN;
  321. if (!strcmp(arg, "poclbm"))
  322. return KL_POCLBM;
  323. if (!strcmp(arg, "phatk"))
  324. return KL_PHATK;
  325. #ifdef USE_SCRYPT
  326. if (!strcmp(arg, "scrypt"))
  327. return KL_SCRYPT;
  328. #endif
  329. return KL_NONE;
  330. }
  331. static
  332. bool _set_kernel(struct cgpu_info * const cgpu, const char * const _val)
  333. {
  334. const enum cl_kernels kern = select_kernel(_val);
  335. if (kern == KL_NONE)
  336. return false;
  337. struct opencl_device_data * const data = cgpu->device_data;
  338. data->kernel = kern;
  339. return true;
  340. }
  341. _SET_INTERFACE(kernel)
  342. const char *set_kernel(char *arg)
  343. {
  344. if (opt_scrypt)
  345. return "Cannot specify a kernel with scrypt";
  346. return _set_list(arg, "Invalid value passed to set_kernel", _set_kernel);
  347. }
  348. #endif
  349. static
  350. const char *opencl_init_binary(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  351. {
  352. struct opencl_device_data * const data = proc->device_data;
  353. if (!(strcasecmp(newvalue, "no") && strcasecmp(newvalue, "never") && strcasecmp(newvalue, "none")))
  354. data->opt_opencl_binaries = OBU_NONE;
  355. else
  356. if (!(strcasecmp(newvalue, "load") && strcasecmp(newvalue, "read")))
  357. data->opt_opencl_binaries = OBU_LOAD;
  358. else
  359. if (!(strcasecmp(newvalue, "save") && strcasecmp(newvalue, "write")))
  360. data->opt_opencl_binaries = OBU_SAVE;
  361. else
  362. if (!(strcasecmp(newvalue, "yes") && strcasecmp(newvalue, "always") && strcasecmp(newvalue, "both")))
  363. data->opt_opencl_binaries = OBU_LOADSAVE;
  364. else
  365. if (!(strcasecmp(newvalue, "default")))
  366. data->opt_opencl_binaries = OBU_DEFAULT;
  367. else
  368. return "Invalid value passed to opencl binary";
  369. return NULL;
  370. }
  371. #ifdef HAVE_ADL
  372. /* This function allows us to map an adl device to an opencl device for when
  373. * simple enumeration has failed to match them. */
  374. static
  375. const char *opencl_init_gpu_map(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  376. {
  377. struct opencl_device_data * const data = proc->device_data;
  378. data->virtual_adl = atoi(newvalue);
  379. data->mapped = true;
  380. return NULL;
  381. }
  382. char *set_gpu_map(char *arg)
  383. {
  384. struct opencl_device_data *data;
  385. int val1 = 0, val2 = 0;
  386. char *nextptr;
  387. nextptr = strtok(arg, ",");
  388. if (nextptr == NULL)
  389. return "Invalid parameters for set gpu map";
  390. if (sscanf(arg, "%d:%d", &val1, &val2) != 2)
  391. return "Invalid description for map pair";
  392. if (val1 < 0 || val1 > MAX_GPUDEVICES || val2 < 0 || val2 > MAX_GPUDEVICES)
  393. return "Invalid value passed to set_gpu_map";
  394. data = gpus[val1].device_data;
  395. data->virtual_adl = val2;
  396. data->mapped = true;
  397. while ((nextptr = strtok(NULL, ",")) != NULL) {
  398. if (sscanf(nextptr, "%d:%d", &val1, &val2) != 2)
  399. return "Invalid description for map pair";
  400. if (val1 < 0 || val1 > MAX_GPUDEVICES || val2 < 0 || val2 > MAX_GPUDEVICES)
  401. return "Invalid value passed to set_gpu_map";
  402. data = gpus[val1].device_data;
  403. data->virtual_adl = val2;
  404. data->mapped = true;
  405. }
  406. return NULL;
  407. }
  408. static
  409. bool _set_gpu_engine(struct cgpu_info * const cgpu, const char * const _val)
  410. {
  411. int val1, val2;
  412. get_intrange(_val, &val1, &val2);
  413. if (val1 < 0 || val1 > 9999 || val2 < 0 || val2 > 9999 || val2 < val1)
  414. return false;
  415. struct opencl_device_data * const data = cgpu->device_data;
  416. struct gpu_adl * const ga = &data->adl;
  417. data->min_engine = val1;
  418. data->gpu_engine = val2;
  419. ga->autoengine = (val1 != val2);
  420. return true;
  421. }
  422. _SET_INTERFACE(gpu_engine)
  423. const char *set_gpu_engine(char *arg)
  424. {
  425. return _set_list(arg, "Invalid value passed to set_gpu_engine", _set_gpu_engine);
  426. }
  427. static
  428. const char *opencl_set_gpu_engine(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  429. {
  430. struct opencl_device_data * const data = proc->device_data;
  431. struct gpu_adl * const ga = &data->adl;
  432. int val1, val2;
  433. get_intrange(newvalue, &val1, &val2);
  434. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  435. return "Invalid value for clock";
  436. if (val1 == val2)
  437. {
  438. if (set_engineclock(proc->device_id, val1))
  439. return "Failed to set gpu_engine";
  440. ga->autoengine = false;
  441. }
  442. else
  443. {
  444. // Ensure current clock is within range
  445. if (ga->lastengine < val1)
  446. {
  447. if (set_engineclock(proc->device_id, val1))
  448. return "Failed to set gpu_engine";
  449. }
  450. else
  451. if (ga->lastengine > val2)
  452. if (set_engineclock(proc->device_id, val2))
  453. return "Failed to set gpu_engine";
  454. data->min_engine = val1;
  455. data->gpu_engine = val2;
  456. ga->autoengine = true;
  457. }
  458. return NULL;
  459. }
  460. static
  461. bool _set_gpu_fan(struct cgpu_info * const cgpu, const char * const _val)
  462. {
  463. int val1, val2;
  464. get_intrange(_val, &val1, &val2);
  465. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  466. return false;
  467. struct opencl_device_data * const data = cgpu->device_data;
  468. struct gpu_adl * const ga = &data->adl;
  469. data->min_fan = val1;
  470. data->gpu_fan = val2;
  471. ga->autofan = (val1 != val2);
  472. return true;
  473. }
  474. _SET_INTERFACE(gpu_fan)
  475. const char *set_gpu_fan(char *arg)
  476. {
  477. return _set_list(arg, "Invalid value passed to set_gpu_fan", _set_gpu_fan);
  478. }
  479. static
  480. const char *opencl_set_gpu_fan(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  481. {
  482. struct opencl_device_data * const data = proc->device_data;
  483. struct gpu_adl * const ga = &data->adl;
  484. int val1, val2;
  485. get_intrange(newvalue, &val1, &val2);
  486. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100 || val2 < val1)
  487. return "Invalid value for fan";
  488. if (val1 == val2)
  489. {
  490. if (set_fanspeed(proc->device_id, val1))
  491. return "Failed to set gpu_fan";
  492. ga->autofan = false;
  493. }
  494. else
  495. {
  496. // Ensure current fan is within range
  497. if (ga->targetfan < val1)
  498. {
  499. if (set_fanspeed(proc->device_id, val1))
  500. return "Failed to set gpu_fan";
  501. }
  502. else
  503. if (ga->targetfan > val2)
  504. if (set_fanspeed(proc->device_id, val2))
  505. return "Failed to set gpu_fan";
  506. data->min_fan = val1;
  507. data->gpu_fan = val2;
  508. ga->autofan = true;
  509. }
  510. return NULL;
  511. }
  512. _SET_INT_LIST(gpu_memclock , (v >= 1 && v < 9999), gpu_memclock )
  513. static
  514. const char *opencl_set_gpu_memclock(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  515. {
  516. if (set_memoryclock(proc->device_id, atoi(newvalue)))
  517. return "Failed to set gpu_memclock";
  518. return NULL;
  519. }
  520. _SET_INT_LIST(gpu_memdiff , (v >= -9999 && v <= 9999), gpu_memdiff )
  521. static
  522. const char *opencl_set_gpu_memdiff(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  523. {
  524. struct opencl_device_data * const data = proc->device_data;
  525. if (!_set_gpu_memdiff(proc, newvalue))
  526. return "Invalid value for gpu_memdiff";
  527. set_engineclock(proc->device_id, data->gpu_engine);
  528. return NULL;
  529. }
  530. _SET_INT_LIST(gpu_powertune, (v >= -99 && v <= 99), gpu_powertune)
  531. _SET_INT_LIST(gpu_vddc , (v >= 0 && v < 9999), gpu_vddc )
  532. static
  533. const char *opencl_set_gpu_vddc(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  534. {
  535. if (set_vddc(proc->device_id, atof(newvalue)))
  536. return "Failed to set gpu_vddc";
  537. return NULL;
  538. }
  539. _SET_INT_LIST(temp_overheat, (v >= 0 && v < 200), adl.overtemp )
  540. #endif
  541. #ifdef HAVE_OPENCL
  542. // SHA256d "intensity" has an artificial offset of -15
  543. double oclthreads_to_intensity(const unsigned long oclthreads, const bool is_sha256d)
  544. {
  545. double intensity = log2(oclthreads);
  546. if (is_sha256d)
  547. intensity -= 15.;
  548. return intensity;
  549. }
  550. unsigned long intensity_to_oclthreads(double intensity, const bool is_sha256d)
  551. {
  552. if (is_sha256d)
  553. intensity += 15;
  554. return pow(2, intensity);
  555. }
  556. double oclthreads_to_xintensity(const unsigned long oclthreads, const cl_uint max_compute_units)
  557. {
  558. return (double)oclthreads / (double)max_compute_units / 64.;
  559. }
  560. unsigned long xintensity_to_oclthreads(const double xintensity, const cl_uint max_compute_units)
  561. {
  562. return xintensity * max_compute_units * 0x40;
  563. }
  564. bool opencl_set_intensity_from_str(struct cgpu_info * const cgpu, const char *_val)
  565. {
  566. struct opencl_device_data * const data = cgpu->device_data;
  567. unsigned long oclthreads = 0;
  568. bool dynamic = false;
  569. if (!strncasecmp(_val, "d", 1))
  570. {
  571. dynamic = true;
  572. ++_val;
  573. }
  574. if (!strncasecmp(_val, "x", 1))
  575. {
  576. const double v = atof(&_val[1]);
  577. if (v < 1 || v > 9999)
  578. return false;
  579. // thr etc won't be initialised here, so avoid dereferencing it
  580. if (data->oclthreads)
  581. {
  582. struct thr_info * const thr = cgpu->thr[0];
  583. const int thr_id = thr->id;
  584. _clState * const clState = clStates[thr_id];
  585. oclthreads = xintensity_to_oclthreads(v, clState->max_compute_units);
  586. }
  587. }
  588. else
  589. if (isdigit(_val[0]))
  590. {
  591. const double v = atof(_val);
  592. if (v < MIN_INTENSITY || v > MAX_GPU_INTENSITY)
  593. return false;
  594. oclthreads = intensity_to_oclthreads(v, !opt_scrypt);
  595. }
  596. // Make actual assignments after we know the values are valid
  597. data->dynamic = dynamic;
  598. if (data->oclthreads)
  599. {
  600. data->oclthreads = oclthreads;
  601. pause_dynamic_threads(cgpu->device_id);
  602. }
  603. else
  604. {
  605. if (unlikely(data->_init_intensity))
  606. free(data->_init_intensity);
  607. data->_init_intensity = strdup(_val);
  608. }
  609. return true;
  610. }
  611. #define _set_intensity opencl_set_intensity_from_str
  612. _SET_INTERFACE(intensity)
  613. const char *set_intensity(char *arg)
  614. {
  615. return _set_list(arg, "Invalid value passed to intensity", _set_intensity);
  616. }
  617. _SET_INT_LIST2(gpu_threads, (v >= 1 && v <= 10), cgpu->threads)
  618. #endif
  619. void write_config_opencl(FILE * const fcfg)
  620. {
  621. #ifdef HAVE_ADL
  622. if (opt_reorder)
  623. fprintf(fcfg, ",\n\"gpu-reorder\" : true");
  624. #endif
  625. }
  626. #ifdef HAVE_OPENCL
  627. BFG_REGISTER_DRIVER(opencl_api)
  628. static const struct bfg_set_device_definition opencl_set_device_funcs_probe[];
  629. static const struct bfg_set_device_definition opencl_set_device_funcs[];
  630. char *print_ndevs_and_exit(int *ndevs)
  631. {
  632. opt_log_output = true;
  633. opencl_api.drv_detect();
  634. clear_adl(*ndevs);
  635. applog(LOG_INFO, "%i GPU devices max detected", *ndevs);
  636. exit(*ndevs);
  637. }
  638. #endif
  639. struct cgpu_info gpus[MAX_GPUDEVICES]; /* Maximum number apparently possible */
  640. struct cgpu_info *cpus;
  641. #ifdef HAVE_OPENCL
  642. /* In dynamic mode, only the first thread of each device will be in use.
  643. * This potentially could start a thread that was stopped with the start-stop
  644. * options if one were to disable dynamic from the menu on a paused GPU */
  645. void pause_dynamic_threads(int gpu)
  646. {
  647. struct cgpu_info *cgpu = &gpus[gpu];
  648. struct opencl_device_data * const data = cgpu->device_data;
  649. int i;
  650. for (i = 1; i < cgpu->threads; i++) {
  651. struct thr_info *thr;
  652. thr = cgpu->thr[i];
  653. if (!thr->pause && data->dynamic) {
  654. applog(LOG_WARNING, "Disabling extra threads due to dynamic mode.");
  655. }
  656. thr->pause = data->dynamic;
  657. if (!data->dynamic && cgpu->deven != DEV_DISABLED)
  658. mt_enable(thr);
  659. }
  660. }
  661. struct device_drv opencl_api;
  662. #endif /* HAVE_OPENCL */
  663. #if defined(HAVE_OPENCL) && defined(HAVE_CURSES)
  664. static
  665. void opencl_wlogprint_status(struct cgpu_info *cgpu)
  666. {
  667. struct opencl_device_data * const data = cgpu->device_data;
  668. struct thr_info *thr = cgpu->thr[0];
  669. const int thr_id = thr->id;
  670. _clState * const clState = clStates[thr_id];
  671. int i;
  672. char checkin[40];
  673. double displayed_rolling;
  674. bool mhash_base = !(cgpu->rolling < 1);
  675. char logline[255];
  676. strcpy(logline, ""); // In case it has no data
  677. {
  678. double intensity = oclthreads_to_intensity(data->oclthreads, !opt_scrypt);
  679. double xintensity = oclthreads_to_xintensity(data->oclthreads, clState->max_compute_units);
  680. const char *iunit = "";
  681. if (xintensity - (int)xintensity < intensity - (int)intensity)
  682. {
  683. intensity = xintensity;
  684. iunit = "x";
  685. }
  686. tailsprintf(logline, sizeof(logline), "I:%s%s%g ",
  687. (data->dynamic ? "d" : ""),
  688. iunit,
  689. intensity);
  690. }
  691. #ifdef HAVE_ADL
  692. if (data->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 (fanspeed != -1 || fanpercent != -1) {
  697. tailsprintf(logline, sizeof(logline), "F:");
  698. if (fanspeed > 9999)
  699. fanspeed = 9999;
  700. if (fanpercent != -1)
  701. {
  702. tailsprintf(logline, sizeof(logline), "%d%%", fanpercent);
  703. if (fanspeed != -1)
  704. tailsprintf(logline, sizeof(logline), "(%dRPM)", fanspeed);
  705. }
  706. else
  707. tailsprintf(logline, sizeof(logline), "%dRPM", fanspeed);
  708. tailsprintf(logline, sizeof(logline), " ");
  709. }
  710. if (engineclock != -1)
  711. tailsprintf(logline, sizeof(logline), "E:%dMHz ", engineclock);
  712. if (memclock != -1)
  713. tailsprintf(logline, sizeof(logline), "M:%dMHz ", memclock);
  714. if (vddc != -1)
  715. tailsprintf(logline, sizeof(logline), "V:%.3fV ", vddc);
  716. if (activity != -1)
  717. tailsprintf(logline, sizeof(logline), "A:%d%% ", activity);
  718. if (powertune != -1)
  719. tailsprintf(logline, sizeof(logline), "P:%d%%", powertune);
  720. }
  721. }
  722. #endif
  723. wlogprint("%s\n", logline);
  724. wlogprint("Last initialised: %s\n", cgpu->init);
  725. for (i = 0; i < mining_threads; i++) {
  726. thr = get_thread(i);
  727. if (thr->cgpu != cgpu)
  728. continue;
  729. get_datestamp(checkin, sizeof(checkin), time(NULL) - timer_elapsed(&thr->last, NULL));
  730. displayed_rolling = thr->rolling;
  731. if (!mhash_base)
  732. displayed_rolling *= 1000;
  733. snprintf(logline, sizeof(logline), "Thread %d: %.1f %sh/s %s ", i, displayed_rolling, mhash_base ? "M" : "K" , cgpu->deven != DEV_DISABLED ? "Enabled" : "Disabled");
  734. switch (cgpu->status) {
  735. default:
  736. case LIFE_WELL:
  737. tailsprintf(logline, sizeof(logline), "ALIVE");
  738. break;
  739. case LIFE_SICK:
  740. tailsprintf(logline, sizeof(logline), "SICK reported in %s", checkin);
  741. break;
  742. case LIFE_DEAD:
  743. tailsprintf(logline, sizeof(logline), "DEAD reported in %s", checkin);
  744. break;
  745. case LIFE_INIT:
  746. case LIFE_NOSTART:
  747. tailsprintf(logline, sizeof(logline), "Never started");
  748. break;
  749. }
  750. if (thr->pause)
  751. tailsprintf(logline, sizeof(logline), " paused");
  752. wlogprint("%s\n", logline);
  753. }
  754. }
  755. static
  756. void opencl_tui_wlogprint_choices(struct cgpu_info *cgpu)
  757. {
  758. wlogprint("[I]ntensity [R]estart GPU ");
  759. #ifdef HAVE_ADL
  760. struct opencl_device_data * const data = cgpu->device_data;
  761. if (data->has_adl)
  762. wlogprint("[C]hange settings ");
  763. #endif
  764. }
  765. static
  766. const char *opencl_tui_handle_choice(struct cgpu_info *cgpu, int input)
  767. {
  768. struct opencl_device_data * const data = cgpu->device_data;
  769. switch (input)
  770. {
  771. case 'i': case 'I':
  772. {
  773. char *intvar;
  774. if (opt_scrypt) {
  775. intvar = curses_input("Set GPU scan intensity (d or "
  776. MIN_SCRYPT_INTENSITY_STR " -> "
  777. MAX_SCRYPT_INTENSITY_STR ")");
  778. } else {
  779. intvar = curses_input("Set GPU scan intensity (d or "
  780. MIN_SHA_INTENSITY_STR " -> "
  781. MAX_SHA_INTENSITY_STR ")");
  782. }
  783. if (!intvar)
  784. return "Invalid intensity\n";
  785. if (!strncasecmp(intvar, "d", 1)) {
  786. data->dynamic = true;
  787. pause_dynamic_threads(cgpu->device_id);
  788. free(intvar);
  789. return "Dynamic mode enabled\n";
  790. }
  791. if (!_set_intensity(cgpu, intvar))
  792. return "Invalid intensity (out of range)\n";
  793. free(intvar);
  794. return "Intensity changed\n";
  795. }
  796. case 'r': case 'R':
  797. reinit_device(cgpu);
  798. return "Attempting to restart\n";
  799. case 'c': case 'C':
  800. {
  801. char logline[256];
  802. clear_logwin();
  803. get_statline3(logline, sizeof(logline), cgpu, true, true);
  804. wattron(logwin, A_BOLD);
  805. wlogprint("%s", logline);
  806. wattroff(logwin, A_BOLD);
  807. wlogprint("\n");
  808. change_gpusettings(cgpu->device_id);
  809. return ""; // Force refresh
  810. }
  811. }
  812. return NULL;
  813. }
  814. #endif
  815. #ifdef HAVE_OPENCL
  816. #define CL_SET_BLKARG(blkvar) status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->blkvar)
  817. #define CL_SET_ARG(var) status |= clSetKernelArg(*kernel, num++, sizeof(var), (void *)&var)
  818. #define CL_SET_VARG(args, var) status |= clSetKernelArg(*kernel, num++, args * sizeof(uint), (void *)var)
  819. static
  820. void *_opencl_work_data_dup(struct work * const work)
  821. {
  822. struct opencl_work_data *p = malloc(sizeof(*p));
  823. memcpy(p, work->device_data, sizeof(*p));
  824. #ifdef USE_SCRYPT
  825. p->work = work;
  826. #endif
  827. return p;
  828. }
  829. static
  830. void _opencl_work_data_free(struct work * const work)
  831. {
  832. free(work->device_data);
  833. }
  834. static
  835. struct opencl_work_data *_opencl_work_data(struct work * const work)
  836. {
  837. if (!work->device_data)
  838. {
  839. work->device_data = calloc(1, sizeof(struct opencl_work_data));
  840. work->device_data_dup_func = _opencl_work_data_dup;
  841. work->device_data_free_func = _opencl_work_data_free;
  842. }
  843. return work->device_data;
  844. }
  845. static
  846. cl_int queue_poclbm_kernel(_clState * const clState, struct work * const work, const cl_uint threads)
  847. {
  848. struct opencl_work_data * const blk = _opencl_work_data(work);
  849. cl_kernel *kernel = &clState->kernel;
  850. unsigned int num = 0;
  851. cl_int status = 0;
  852. CL_SET_BLKARG(ctx_a);
  853. CL_SET_BLKARG(ctx_b);
  854. CL_SET_BLKARG(ctx_c);
  855. CL_SET_BLKARG(ctx_d);
  856. CL_SET_BLKARG(ctx_e);
  857. CL_SET_BLKARG(ctx_f);
  858. CL_SET_BLKARG(ctx_g);
  859. CL_SET_BLKARG(ctx_h);
  860. CL_SET_BLKARG(cty_b);
  861. CL_SET_BLKARG(cty_c);
  862. CL_SET_BLKARG(cty_f);
  863. CL_SET_BLKARG(cty_g);
  864. CL_SET_BLKARG(cty_h);
  865. if (!clState->goffset) {
  866. cl_uint vwidth = clState->vwidth;
  867. uint *nonces = alloca(sizeof(uint) * vwidth);
  868. unsigned int i;
  869. for (i = 0; i < vwidth; i++)
  870. nonces[i] = work->blk.nonce + (i * threads);
  871. CL_SET_VARG(vwidth, nonces);
  872. }
  873. CL_SET_BLKARG(fW0);
  874. CL_SET_BLKARG(fW1);
  875. CL_SET_BLKARG(fW2);
  876. CL_SET_BLKARG(fW3);
  877. CL_SET_BLKARG(fW15);
  878. CL_SET_BLKARG(fW01r);
  879. CL_SET_BLKARG(D1A);
  880. CL_SET_BLKARG(C1addK5);
  881. CL_SET_BLKARG(B1addK6);
  882. CL_SET_BLKARG(W16addK16);
  883. CL_SET_BLKARG(W17addK17);
  884. CL_SET_BLKARG(PreVal4addT1);
  885. CL_SET_BLKARG(PreVal0);
  886. CL_SET_ARG(clState->outputBuffer);
  887. return status;
  888. }
  889. static
  890. cl_int queue_phatk_kernel(_clState * const clState, struct work * const work, __maybe_unused const cl_uint threads)
  891. {
  892. struct opencl_work_data * const blk = _opencl_work_data(work);
  893. cl_kernel *kernel = &clState->kernel;
  894. cl_uint vwidth = clState->vwidth;
  895. unsigned int i, num = 0;
  896. cl_int status = 0;
  897. uint *nonces;
  898. CL_SET_BLKARG(ctx_a);
  899. CL_SET_BLKARG(ctx_b);
  900. CL_SET_BLKARG(ctx_c);
  901. CL_SET_BLKARG(ctx_d);
  902. CL_SET_BLKARG(ctx_e);
  903. CL_SET_BLKARG(ctx_f);
  904. CL_SET_BLKARG(ctx_g);
  905. CL_SET_BLKARG(ctx_h);
  906. CL_SET_BLKARG(cty_b);
  907. CL_SET_BLKARG(cty_c);
  908. CL_SET_BLKARG(cty_d);
  909. CL_SET_BLKARG(cty_f);
  910. CL_SET_BLKARG(cty_g);
  911. CL_SET_BLKARG(cty_h);
  912. nonces = alloca(sizeof(uint) * vwidth);
  913. for (i = 0; i < vwidth; i++)
  914. nonces[i] = work->blk.nonce + i;
  915. CL_SET_VARG(vwidth, nonces);
  916. CL_SET_BLKARG(W16);
  917. CL_SET_BLKARG(W17);
  918. CL_SET_BLKARG(PreVal4_2);
  919. CL_SET_BLKARG(PreVal0);
  920. CL_SET_BLKARG(PreW18);
  921. CL_SET_BLKARG(PreW19);
  922. CL_SET_BLKARG(PreW31);
  923. CL_SET_BLKARG(PreW32);
  924. CL_SET_ARG(clState->outputBuffer);
  925. return status;
  926. }
  927. static
  928. cl_int queue_diakgcn_kernel(_clState * const clState, struct work * const work, __maybe_unused const cl_uint threads)
  929. {
  930. struct opencl_work_data * const blk = _opencl_work_data(work);
  931. cl_kernel *kernel = &clState->kernel;
  932. unsigned int num = 0;
  933. cl_int status = 0;
  934. if (!clState->goffset) {
  935. cl_uint vwidth = clState->vwidth;
  936. uint *nonces = alloca(sizeof(uint) * vwidth);
  937. unsigned int i;
  938. for (i = 0; i < vwidth; i++)
  939. nonces[i] = work->blk.nonce + i;
  940. CL_SET_VARG(vwidth, nonces);
  941. }
  942. CL_SET_BLKARG(PreVal0);
  943. CL_SET_BLKARG(PreVal4_2);
  944. CL_SET_BLKARG(cty_h);
  945. CL_SET_BLKARG(D1A);
  946. CL_SET_BLKARG(cty_b);
  947. CL_SET_BLKARG(cty_c);
  948. CL_SET_BLKARG(cty_f);
  949. CL_SET_BLKARG(cty_g);
  950. CL_SET_BLKARG(C1addK5);
  951. CL_SET_BLKARG(B1addK6);
  952. CL_SET_BLKARG(PreVal0addK7);
  953. CL_SET_BLKARG(W16addK16);
  954. CL_SET_BLKARG(W17addK17);
  955. CL_SET_BLKARG(PreW18);
  956. CL_SET_BLKARG(PreW19);
  957. CL_SET_BLKARG(W16);
  958. CL_SET_BLKARG(W17);
  959. CL_SET_BLKARG(PreW31);
  960. CL_SET_BLKARG(PreW32);
  961. CL_SET_BLKARG(ctx_a);
  962. CL_SET_BLKARG(ctx_b);
  963. CL_SET_BLKARG(ctx_c);
  964. CL_SET_BLKARG(ctx_d);
  965. CL_SET_BLKARG(ctx_e);
  966. CL_SET_BLKARG(ctx_f);
  967. CL_SET_BLKARG(ctx_g);
  968. CL_SET_BLKARG(ctx_h);
  969. CL_SET_BLKARG(zeroA);
  970. CL_SET_BLKARG(zeroB);
  971. CL_SET_BLKARG(oneA);
  972. CL_SET_BLKARG(twoA);
  973. CL_SET_BLKARG(threeA);
  974. CL_SET_BLKARG(fourA);
  975. CL_SET_BLKARG(fiveA);
  976. CL_SET_BLKARG(sixA);
  977. CL_SET_BLKARG(sevenA);
  978. CL_SET_ARG(clState->outputBuffer);
  979. return status;
  980. }
  981. static
  982. cl_int queue_diablo_kernel(_clState * const clState, struct work * const work, const cl_uint threads)
  983. {
  984. struct opencl_work_data * const blk = _opencl_work_data(work);
  985. cl_kernel *kernel = &clState->kernel;
  986. unsigned int num = 0;
  987. cl_int status = 0;
  988. if (!clState->goffset) {
  989. cl_uint vwidth = clState->vwidth;
  990. uint *nonces = alloca(sizeof(uint) * vwidth);
  991. unsigned int i;
  992. for (i = 0; i < vwidth; i++)
  993. nonces[i] = work->blk.nonce + (i * threads);
  994. CL_SET_VARG(vwidth, nonces);
  995. }
  996. CL_SET_BLKARG(PreVal0);
  997. CL_SET_BLKARG(PreVal0addK7);
  998. CL_SET_BLKARG(PreVal4addT1);
  999. CL_SET_BLKARG(PreW18);
  1000. CL_SET_BLKARG(PreW19);
  1001. CL_SET_BLKARG(W16);
  1002. CL_SET_BLKARG(W17);
  1003. CL_SET_BLKARG(W16addK16);
  1004. CL_SET_BLKARG(W17addK17);
  1005. CL_SET_BLKARG(PreW31);
  1006. CL_SET_BLKARG(PreW32);
  1007. CL_SET_BLKARG(D1A);
  1008. CL_SET_BLKARG(cty_b);
  1009. CL_SET_BLKARG(cty_c);
  1010. CL_SET_BLKARG(cty_h);
  1011. CL_SET_BLKARG(cty_f);
  1012. CL_SET_BLKARG(cty_g);
  1013. CL_SET_BLKARG(C1addK5);
  1014. CL_SET_BLKARG(B1addK6);
  1015. CL_SET_BLKARG(ctx_a);
  1016. CL_SET_BLKARG(ctx_b);
  1017. CL_SET_BLKARG(ctx_c);
  1018. CL_SET_BLKARG(ctx_d);
  1019. CL_SET_BLKARG(ctx_e);
  1020. CL_SET_BLKARG(ctx_f);
  1021. CL_SET_BLKARG(ctx_g);
  1022. CL_SET_BLKARG(ctx_h);
  1023. CL_SET_ARG(clState->outputBuffer);
  1024. return status;
  1025. }
  1026. #ifdef USE_SCRYPT
  1027. static
  1028. cl_int queue_scrypt_kernel(_clState * const clState, struct work * const work, __maybe_unused const cl_uint threads)
  1029. {
  1030. unsigned char *midstate = work->midstate;
  1031. cl_kernel *kernel = &clState->kernel;
  1032. unsigned int num = 0;
  1033. cl_uint le_target;
  1034. cl_int status = 0;
  1035. le_target = *(cl_uint *)(work->target + 28);
  1036. clState->cldata = work->data;
  1037. status = clEnqueueWriteBuffer(clState->commandQueue, clState->CLbuffer0, true, 0, 80, clState->cldata, 0, NULL,NULL);
  1038. CL_SET_ARG(clState->CLbuffer0);
  1039. CL_SET_ARG(clState->outputBuffer);
  1040. CL_SET_ARG(clState->padbuffer8);
  1041. CL_SET_VARG(4, &midstate[0]);
  1042. CL_SET_VARG(4, &midstate[16]);
  1043. CL_SET_ARG(le_target);
  1044. return status;
  1045. }
  1046. #endif
  1047. #endif /* HAVE_OPENCL */
  1048. #ifdef HAVE_OPENCL
  1049. /* We have only one thread that ever re-initialises GPUs, thus if any GPU
  1050. * init command fails due to a completely wedged GPU, the thread will never
  1051. * return, unable to harm other GPUs. If it does return, it means we only had
  1052. * a soft failure and then the reinit_gpu thread is ready to tackle another
  1053. * GPU */
  1054. void *reinit_gpu(void *userdata)
  1055. {
  1056. struct thr_info *mythr = userdata;
  1057. struct cgpu_info *cgpu, *sel_cgpu;
  1058. struct thr_info *thr;
  1059. char name[256];
  1060. int thr_id;
  1061. int i;
  1062. pthread_detach(pthread_self());
  1063. RenameThread("reinit_gpu");
  1064. select_cgpu:
  1065. sel_cgpu =
  1066. cgpu = tq_pop(mythr->q, NULL);
  1067. if (!cgpu)
  1068. goto out;
  1069. struct opencl_device_data * const data = cgpu->device_data;
  1070. if (clDevicesNum() != nDevs) {
  1071. applog(LOG_WARNING, "Hardware not reporting same number of active devices, will not attempt to restart GPU");
  1072. goto out;
  1073. }
  1074. for (i = 0; i < cgpu->threads; ++i)
  1075. {
  1076. thr = cgpu->thr[i];
  1077. thr_id = thr->id;
  1078. thr->rolling = thr->cgpu->rolling = 0;
  1079. /* Reports the last time we tried to revive a sick GPU */
  1080. cgtime(&thr->sick);
  1081. if (!pthread_cancel(thr->pth)) {
  1082. applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
  1083. } else
  1084. applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
  1085. }
  1086. for (i = 0; i < cgpu->threads; ++i)
  1087. {
  1088. int virtual_gpu;
  1089. thr = cgpu->thr[i];
  1090. thr_id = thr->id;
  1091. virtual_gpu = data->virtual_gpu;
  1092. /* Lose this ram cause we may get stuck here! */
  1093. //tq_freeze(thr->q);
  1094. thr->q = tq_new();
  1095. if (!thr->q)
  1096. quithere(1, "Failed to tq_new");
  1097. /* Lose this ram cause we may dereference in the dying thread! */
  1098. //free(clState);
  1099. applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
  1100. clStates[thr_id] = initCl(virtual_gpu, name, sizeof(name));
  1101. if (!clStates[thr_id]) {
  1102. applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
  1103. goto select_cgpu;
  1104. }
  1105. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1106. if (unlikely(thr_info_create(thr, NULL, miner_thread, thr))) {
  1107. applog(LOG_ERR, "thread %d create failed", thr_id);
  1108. return NULL;
  1109. }
  1110. applog(LOG_WARNING, "Thread %d restarted", thr_id);
  1111. }
  1112. get_now_datestamp(sel_cgpu->init, sizeof(sel_cgpu->init));
  1113. proc_enable(cgpu);
  1114. goto select_cgpu;
  1115. out:
  1116. return NULL;
  1117. }
  1118. #else
  1119. void *reinit_gpu(__maybe_unused void *userdata)
  1120. {
  1121. return NULL;
  1122. }
  1123. #endif
  1124. #ifdef HAVE_OPENCL
  1125. struct device_drv opencl_api;
  1126. static int opencl_autodetect()
  1127. {
  1128. RUNONCE(0);
  1129. #ifndef WIN32
  1130. if (!getenv("DISPLAY")) {
  1131. applog(LOG_DEBUG, "DISPLAY not set, setting :0 just in case");
  1132. setenv("DISPLAY", ":0", 1);
  1133. }
  1134. #endif
  1135. if (!load_opencl_symbols()) {
  1136. nDevs = 0;
  1137. return 0;
  1138. }
  1139. int i;
  1140. nDevs = clDevicesNum();
  1141. if (nDevs < 0) {
  1142. applog(LOG_ERR, "clDevicesNum returned error, no GPUs usable");
  1143. nDevs = 0;
  1144. }
  1145. if (!nDevs)
  1146. return 0;
  1147. /* If opt_g_threads is not set, use default 1 thread on scrypt and
  1148. * 2 for regular mining */
  1149. if (opt_g_threads == -1) {
  1150. if (opt_scrypt)
  1151. opt_g_threads = 1;
  1152. else
  1153. opt_g_threads = 2;
  1154. }
  1155. #ifdef HAVE_SENSORS
  1156. const sensors_chip_name *cn;
  1157. int c = 0;
  1158. sensors_init(NULL);
  1159. sensors_chip_name cnm;
  1160. if (sensors_parse_chip_name("radeon-*", &cnm))
  1161. c = -1;
  1162. #endif
  1163. for (i = 0; i < nDevs; ++i) {
  1164. struct cgpu_info *cgpu;
  1165. cgpu = &gpus[i];
  1166. struct opencl_device_data * const data = cgpu->device_data;
  1167. cgpu->deven = DEV_ENABLED;
  1168. cgpu->drv = &opencl_api;
  1169. cgpu->device_id = i;
  1170. if (cgpu->threads == 0)
  1171. cgpu->threads = opt_g_threads;
  1172. data->virtual_gpu = i;
  1173. #ifdef HAVE_SENSORS
  1174. cn = (c == -1) ? NULL : sensors_get_detected_chips(&cnm, &c);
  1175. data->sensor = cn;
  1176. #endif
  1177. cgpu->set_device_funcs = opencl_set_device_funcs_probe;
  1178. cgpu_set_defaults(cgpu);
  1179. cgpu->set_device_funcs = opencl_set_device_funcs;
  1180. add_cgpu(cgpu);
  1181. }
  1182. if (!opt_noadl)
  1183. init_adl(nDevs);
  1184. return nDevs;
  1185. }
  1186. static void opencl_detect()
  1187. {
  1188. int flags = 0;
  1189. if (!opt_scrypt)
  1190. flags |= GDF_DEFAULT_NOAUTO;
  1191. generic_detect(&opencl_api, NULL, opencl_autodetect, flags);
  1192. }
  1193. static void reinit_opencl_device(struct cgpu_info *gpu)
  1194. {
  1195. #ifdef HAVE_ADL
  1196. struct opencl_device_data * const data = gpu->device_data;
  1197. if (adl_active && data->has_adl && gpu_activity(gpu->device_id) > 50)
  1198. {
  1199. applogr(, LOG_ERR, "%s: Still showing activity (suggests a hard hang); cancelling reinitialise.",
  1200. gpu->dev_repr);
  1201. }
  1202. #endif
  1203. tq_push(control_thr[gpur_thr_id].q, gpu);
  1204. }
  1205. static
  1206. bool opencl_get_stats(struct cgpu_info * const gpu)
  1207. {
  1208. __maybe_unused struct opencl_device_data * const data = gpu->device_data;
  1209. #ifdef HAVE_SENSORS
  1210. if (data->sensor)
  1211. {
  1212. const sensors_chip_name *cn = data->sensor;
  1213. const sensors_feature *feat;
  1214. for (int f = 0; (feat = sensors_get_features(cn, &f)); )
  1215. {
  1216. const sensors_subfeature *subf;
  1217. subf = sensors_get_subfeature(cn, feat, SENSORS_SUBFEATURE_TEMP_INPUT);
  1218. if (!(subf && subf->flags & SENSORS_MODE_R))
  1219. continue;
  1220. double val;
  1221. int rc = sensors_get_value(cn, subf->number, &val);
  1222. if (rc)
  1223. continue;
  1224. gpu->temp = val;
  1225. return true;
  1226. }
  1227. }
  1228. #endif
  1229. #ifdef HAVE_ADL
  1230. if (data->has_adl) {
  1231. int gpuid = gpu->device_id;
  1232. gpu_temp(gpuid);
  1233. gpu_fanspeed(gpuid);
  1234. }
  1235. #endif
  1236. return true;
  1237. }
  1238. static
  1239. void opencl_watchdog(struct cgpu_info * const cgpu, __maybe_unused const struct timeval * const tv_now)
  1240. {
  1241. #ifdef HAVE_ADL
  1242. struct opencl_device_data * const data = cgpu->device_data;
  1243. const int gpu = cgpu->device_id;
  1244. enum dev_enable *denable = &cgpu->deven;
  1245. if (adl_active && data->has_adl)
  1246. gpu_autotune(gpu, denable);
  1247. if (opt_debug && data->has_adl) {
  1248. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  1249. float temp = 0, vddc = 0;
  1250. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune))
  1251. applog(LOG_DEBUG, "%.1f C F: %d%%(%dRPM) E: %dMHz M: %dMHz V: %.3fV A: %d%% P: %d%%",
  1252. temp, fanpercent, fanspeed, engineclock, memclock, vddc, activity, powertune);
  1253. }
  1254. #endif
  1255. }
  1256. static struct api_data*
  1257. get_opencl_api_extra_device_status(struct cgpu_info *gpu)
  1258. {
  1259. struct opencl_device_data * const data = gpu->device_data;
  1260. struct thr_info * const thr = gpu->thr[0];
  1261. const int thr_id = thr->id;
  1262. _clState * const clState = clStates[thr_id];
  1263. struct api_data*root = NULL;
  1264. float gt, gv;
  1265. int ga, gf, gp, gc, gm, pt;
  1266. #ifdef HAVE_ADL
  1267. if (!gpu_stats(gpu->device_id, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  1268. #endif
  1269. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  1270. root = api_add_int(root, "Fan Speed", &gf, true);
  1271. root = api_add_int(root, "Fan Percent", &gp, true);
  1272. root = api_add_int(root, "GPU Clock", &gc, true);
  1273. root = api_add_int(root, "Memory Clock", &gm, true);
  1274. root = api_add_volts(root, "GPU Voltage", &gv, true);
  1275. root = api_add_int(root, "GPU Activity", &ga, true);
  1276. root = api_add_int(root, "Powertune", &pt, true);
  1277. char intensity[20];
  1278. uint32_t oclthreads = data->oclthreads;
  1279. double intensityf = oclthreads_to_intensity(oclthreads, !opt_scrypt);
  1280. double xintensity = oclthreads_to_xintensity(oclthreads, clState->max_compute_units);
  1281. if (data->dynamic)
  1282. strcpy(intensity, "D");
  1283. else
  1284. sprintf(intensity, "%g", intensityf);
  1285. root = api_add_string(root, "Intensity", intensity, true);
  1286. root = api_add_uint32(root, "OCLThreads", &oclthreads, true);
  1287. root = api_add_double(root, "CIntensity", &intensityf, true);
  1288. root = api_add_double(root, "XIntensity", &xintensity, true);
  1289. return root;
  1290. }
  1291. struct opencl_thread_data {
  1292. cl_int (*queue_kernel_parameters)(_clState *, struct work *, cl_uint);
  1293. uint32_t *res;
  1294. };
  1295. static uint32_t *blank_res;
  1296. static bool opencl_thread_prepare(struct thr_info *thr)
  1297. {
  1298. char name[256];
  1299. struct cgpu_info *cgpu = thr->cgpu;
  1300. struct opencl_device_data * const data = cgpu->device_data;
  1301. int gpu = cgpu->device_id;
  1302. int virtual_gpu = data->virtual_gpu;
  1303. int i = thr->id;
  1304. static bool failmessage = false;
  1305. int buffersize = opt_scrypt ? SCRYPT_BUFFERSIZE : BUFFERSIZE;
  1306. if (!blank_res)
  1307. blank_res = calloc(buffersize, 1);
  1308. if (!blank_res) {
  1309. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1310. return false;
  1311. }
  1312. strcpy(name, "");
  1313. applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
  1314. clStates[i] = initCl(virtual_gpu, name, sizeof(name));
  1315. if (!clStates[i]) {
  1316. #ifdef HAVE_CURSES
  1317. if (use_curses)
  1318. enable_curses();
  1319. #endif
  1320. applog(LOG_ERR, "Failed to init GPU thread %d, disabling device %d", i, gpu);
  1321. if (!failmessage) {
  1322. applog(LOG_ERR, "Restarting the GPU from the menu will not fix this.");
  1323. applog(LOG_ERR, "Try restarting BFGMiner.");
  1324. failmessage = true;
  1325. #ifdef HAVE_CURSES
  1326. char *buf;
  1327. if (use_curses) {
  1328. buf = curses_input("Press enter to continue");
  1329. if (buf)
  1330. free(buf);
  1331. }
  1332. #endif
  1333. }
  1334. cgpu->deven = DEV_DISABLED;
  1335. cgpu->status = LIFE_NOSTART;
  1336. dev_error(cgpu, REASON_DEV_NOSTART);
  1337. return false;
  1338. }
  1339. if (!cgpu->name)
  1340. cgpu->name = trimmed_strdup(name);
  1341. if (!cgpu->kname)
  1342. {
  1343. switch (clStates[i]->chosen_kernel) {
  1344. case KL_DIABLO:
  1345. cgpu->kname = "diablo";
  1346. break;
  1347. case KL_DIAKGCN:
  1348. cgpu->kname = "diakgcn";
  1349. break;
  1350. case KL_PHATK:
  1351. cgpu->kname = "phatk";
  1352. break;
  1353. #ifdef USE_SCRYPT
  1354. case KL_SCRYPT:
  1355. cgpu->kname = "scrypt";
  1356. break;
  1357. #endif
  1358. case KL_POCLBM:
  1359. cgpu->kname = "poclbm";
  1360. break;
  1361. default:
  1362. break;
  1363. }
  1364. }
  1365. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1366. get_now_datestamp(cgpu->init, sizeof(cgpu->init));
  1367. have_opencl = true;
  1368. return true;
  1369. }
  1370. static bool opencl_thread_init(struct thr_info *thr)
  1371. {
  1372. const int thr_id = thr->id;
  1373. struct cgpu_info *gpu = thr->cgpu;
  1374. struct opencl_thread_data *thrdata;
  1375. _clState *clState = clStates[thr_id];
  1376. cl_int status = 0;
  1377. thrdata = calloc(1, sizeof(*thrdata));
  1378. thr->cgpu_data = thrdata;
  1379. int buffersize = opt_scrypt ? SCRYPT_BUFFERSIZE : BUFFERSIZE;
  1380. if (!thrdata) {
  1381. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1382. return false;
  1383. }
  1384. switch (clState->chosen_kernel) {
  1385. case KL_POCLBM:
  1386. thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
  1387. break;
  1388. case KL_PHATK:
  1389. thrdata->queue_kernel_parameters = &queue_phatk_kernel;
  1390. break;
  1391. case KL_DIAKGCN:
  1392. thrdata->queue_kernel_parameters = &queue_diakgcn_kernel;
  1393. break;
  1394. #ifdef USE_SCRYPT
  1395. case KL_SCRYPT:
  1396. thrdata->queue_kernel_parameters = &queue_scrypt_kernel;
  1397. gpu->min_nonce_diff = 1./0x10000;
  1398. break;
  1399. #endif
  1400. default:
  1401. case KL_DIABLO:
  1402. thrdata->queue_kernel_parameters = &queue_diablo_kernel;
  1403. break;
  1404. }
  1405. thrdata->res = calloc(buffersize, 1);
  1406. if (!thrdata->res) {
  1407. free(thrdata);
  1408. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1409. return false;
  1410. }
  1411. status |= clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  1412. buffersize, blank_res, 0, NULL, NULL);
  1413. if (unlikely(status != CL_SUCCESS)) {
  1414. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1415. return false;
  1416. }
  1417. gpu->status = LIFE_WELL;
  1418. gpu->device_last_well = time(NULL);
  1419. return true;
  1420. }
  1421. static bool opencl_prepare_work(struct thr_info __maybe_unused *thr, struct work *work)
  1422. {
  1423. #ifdef USE_SCRYPT
  1424. if (!opt_scrypt)
  1425. #endif
  1426. {
  1427. struct opencl_work_data * const blk = _opencl_work_data(work);
  1428. precalc_hash(blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1429. }
  1430. return true;
  1431. }
  1432. extern int opt_dynamic_interval;
  1433. static int64_t opencl_scanhash(struct thr_info *thr, struct work *work,
  1434. int64_t __maybe_unused max_nonce)
  1435. {
  1436. const int thr_id = thr->id;
  1437. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1438. struct cgpu_info *gpu = thr->cgpu;
  1439. struct opencl_device_data * const data = gpu->device_data;
  1440. _clState *clState = clStates[thr_id];
  1441. const cl_kernel *kernel = &clState->kernel;
  1442. const int dynamic_us = opt_dynamic_interval * 1000;
  1443. cl_int status;
  1444. size_t globalThreads[1];
  1445. size_t localThreads[1] = { clState->wsize };
  1446. int64_t hashes;
  1447. int found = opt_scrypt ? SCRYPT_FOUND : FOUND;
  1448. int buffersize = opt_scrypt ? SCRYPT_BUFFERSIZE : BUFFERSIZE;
  1449. /* Windows' timer resolution is only 15ms so oversample 5x */
  1450. if (data->dynamic && (++data->intervals * dynamic_us) > 70000) {
  1451. struct timeval tv_gpuend;
  1452. double gpu_us;
  1453. cgtime(&tv_gpuend);
  1454. gpu_us = us_tdiff(&tv_gpuend, &data->tv_gpustart) / data->intervals;
  1455. if (gpu_us > dynamic_us) {
  1456. const unsigned long min_oclthreads = intensity_to_oclthreads(MIN_INTENSITY, !opt_scrypt);
  1457. data->oclthreads /= 2;
  1458. if (data->oclthreads < min_oclthreads)
  1459. data->oclthreads = min_oclthreads;
  1460. } else if (gpu_us < dynamic_us / 2) {
  1461. const unsigned long max_oclthreads = intensity_to_oclthreads(MAX_INTENSITY, !opt_scrypt);
  1462. data->oclthreads *= 2;
  1463. if (data->oclthreads > max_oclthreads)
  1464. data->oclthreads = max_oclthreads;
  1465. }
  1466. memcpy(&(data->tv_gpustart), &tv_gpuend, sizeof(struct timeval));
  1467. data->intervals = 0;
  1468. }
  1469. if (data->oclthreads < localThreads[0])
  1470. data->oclthreads = localThreads[0];
  1471. globalThreads[0] = data->oclthreads;
  1472. hashes = globalThreads[0];
  1473. hashes *= clState->vwidth;
  1474. if (hashes > gpu->max_hashes)
  1475. gpu->max_hashes = hashes;
  1476. status = thrdata->queue_kernel_parameters(clState, work, globalThreads[0]);
  1477. if (unlikely(status != CL_SUCCESS)) {
  1478. applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
  1479. return -1;
  1480. }
  1481. if (clState->goffset) {
  1482. size_t global_work_offset[1];
  1483. global_work_offset[0] = work->blk.nonce;
  1484. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, global_work_offset,
  1485. globalThreads, localThreads, 0, NULL, NULL);
  1486. } else
  1487. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1488. globalThreads, localThreads, 0, NULL, NULL);
  1489. if (unlikely(status != CL_SUCCESS)) {
  1490. applog(LOG_ERR, "Error %d: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)", status);
  1491. return -1;
  1492. }
  1493. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1494. buffersize, thrdata->res, 0, NULL, NULL);
  1495. if (unlikely(status != CL_SUCCESS)) {
  1496. applog(LOG_ERR, "Error: clEnqueueReadBuffer failed error %d. (clEnqueueReadBuffer)", status);
  1497. return -1;
  1498. }
  1499. /* The amount of work scanned can fluctuate when intensity changes
  1500. * and since we do this one cycle behind, we increment the work more
  1501. * than enough to prevent repeating work */
  1502. work->blk.nonce += gpu->max_hashes;
  1503. /* This finish flushes the readbuffer set with CL_FALSE in clEnqueueReadBuffer */
  1504. clFinish(clState->commandQueue);
  1505. /* FOUND entry is used as a counter to say how many nonces exist */
  1506. if (thrdata->res[found]) {
  1507. /* Clear the buffer again */
  1508. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1509. buffersize, blank_res, 0, NULL, NULL);
  1510. if (unlikely(status != CL_SUCCESS)) {
  1511. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1512. return -1;
  1513. }
  1514. applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
  1515. postcalc_hash_async(thr, work, thrdata->res);
  1516. memset(thrdata->res, 0, buffersize);
  1517. /* This finish flushes the writebuffer set with CL_FALSE in clEnqueueWriteBuffer */
  1518. clFinish(clState->commandQueue);
  1519. }
  1520. return hashes;
  1521. }
  1522. static void opencl_thread_shutdown(struct thr_info *thr)
  1523. {
  1524. const int thr_id = thr->id;
  1525. _clState *clState = clStates[thr_id];
  1526. clReleaseKernel(clState->kernel);
  1527. clReleaseProgram(clState->program);
  1528. clReleaseCommandQueue(clState->commandQueue);
  1529. clReleaseContext(clState->context);
  1530. }
  1531. static
  1532. const char *opencl_cannot_set(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  1533. {
  1534. return "Cannot set at runtime (use --set-device on commandline)";
  1535. }
  1536. static const struct bfg_set_device_definition opencl_set_device_funcs_probe[] = {
  1537. {"intensity", opencl_init_intensity},
  1538. {"kernel", opencl_init_kernel},
  1539. {"threads", opencl_init_gpu_threads},
  1540. {"vector", opencl_init_vector},
  1541. {"work_size", opencl_init_worksize},
  1542. {"binary", opencl_init_binary},
  1543. #ifdef HAVE_ADL
  1544. {"adl_mapping", opencl_init_gpu_map},
  1545. {"clock", opencl_init_gpu_engine},
  1546. {"fan", opencl_init_gpu_fan},
  1547. {"memclock", opencl_init_gpu_memclock},
  1548. {"memdiff", opencl_init_gpu_memdiff},
  1549. {"powertune", opencl_init_gpu_powertune},
  1550. {"temp_overheat", opencl_init_temp_overheat},
  1551. {"voltage", opencl_init_gpu_vddc},
  1552. #endif
  1553. #ifdef USE_SCRYPT
  1554. {"shaders", opencl_init_shaders},
  1555. {"lookup_gap", opencl_init_lookup_gap},
  1556. {"thread_concurrency", opencl_init_thread_concurrency},
  1557. #endif
  1558. {NULL}
  1559. };
  1560. static const struct bfg_set_device_definition opencl_set_device_funcs[] = {
  1561. {"intensity", opencl_init_intensity, "Intensity of GPU scanning (d, -10 -> 31, or x1 to x9999)"},
  1562. {"kernel", opencl_cannot_set, "Mining kernel code to use"},
  1563. {"threads", opencl_cannot_set, "Number of threads"},
  1564. {"vector", opencl_cannot_set, ""},
  1565. {"work_size", opencl_cannot_set, ""},
  1566. {"binary", opencl_cannot_set, ""},
  1567. #ifdef HAVE_ADL
  1568. {"adl_mapping", opencl_cannot_set, "Map to ADL device"},
  1569. {"clock", opencl_set_gpu_engine, "GPU engine clock"},
  1570. {"fan", opencl_set_gpu_fan, "GPU fan percentage range"},
  1571. {"memclock", opencl_set_gpu_memclock, "GPU memory clock"},
  1572. {"memdiff", opencl_set_gpu_memdiff, "Clock speed difference between GPU and memory (auto-gpu mode only)"},
  1573. {"powertune", opencl_cannot_set, "GPU powertune percentage"},
  1574. {"temp_overheat", opencl_init_temp_overheat, "Overheat temperature when automatically managing fan and GPU speeds"},
  1575. {"voltage", opencl_set_gpu_vddc, "GPU voltage"},
  1576. #endif
  1577. #ifdef USE_SCRYPT
  1578. {"shaders", opencl_cannot_set, "GPU shaders per card (scrypt only)"},
  1579. {"lookup_gap", opencl_cannot_set, "GPU lookup gap (scrypt only)"},
  1580. {"thread_concurrency", opencl_cannot_set, "GPU thread concurrency (scrypt only)"},
  1581. #endif
  1582. {NULL}
  1583. };
  1584. struct device_drv opencl_api = {
  1585. .dname = "opencl",
  1586. .name = "OCL",
  1587. .probe_priority = 110,
  1588. .supported_algos = POW_SHA256D | POW_SCRYPT,
  1589. .drv_detect = opencl_detect,
  1590. .reinit_device = reinit_opencl_device,
  1591. .watchdog = opencl_watchdog,
  1592. .get_stats = opencl_get_stats,
  1593. #ifdef HAVE_CURSES
  1594. .proc_wlogprint_status = opencl_wlogprint_status,
  1595. .proc_tui_wlogprint_choices = opencl_tui_wlogprint_choices,
  1596. .proc_tui_handle_choice = opencl_tui_handle_choice,
  1597. #endif
  1598. .get_api_extra_device_status = get_opencl_api_extra_device_status,
  1599. .thread_prepare = opencl_thread_prepare,
  1600. .thread_init = opencl_thread_init,
  1601. .prepare_work = opencl_prepare_work,
  1602. .scanhash = opencl_scanhash,
  1603. .thread_shutdown = opencl_thread_shutdown,
  1604. };
  1605. #endif