driver-opencl.c 47 KB

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