driver-opencl.c 56 KB

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