driver-opencl.c 53 KB

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