driver-opencl.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  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 WIN32
  13. #include <winsock2.h>
  14. #endif
  15. #ifdef HAVE_CURSES
  16. // Must be before stdbool, since pdcurses typedefs bool :/
  17. #include <curses.h>
  18. #endif
  19. #ifndef WIN32
  20. #include <dlfcn.h>
  21. #else
  22. #include <windows.h>
  23. #endif
  24. #include <string.h>
  25. #include <stdbool.h>
  26. #include <stdint.h>
  27. #include <sys/types.h>
  28. #ifndef WIN32
  29. #include <sys/resource.h>
  30. #endif
  31. #include <ccan/opt/opt.h>
  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. #ifdef HAVE_CURSES
  225. extern WINDOW *mainwin, *statuswin, *logwin;
  226. extern void enable_curses(void);
  227. #endif
  228. extern int mining_threads;
  229. extern int opt_g_threads;
  230. extern bool ping;
  231. extern bool opt_loginput;
  232. extern char *opt_kernel_path;
  233. extern int gpur_thr_id;
  234. extern bool opt_noadl;
  235. extern bool have_opencl;
  236. extern void *miner_thread(void *userdata);
  237. extern int dev_from_id(int thr_id);
  238. extern void decay_time(double *f, double fadd);
  239. /**********************************************/
  240. #ifdef HAVE_ADL
  241. extern float gpu_temp(int gpu);
  242. extern int gpu_fanspeed(int gpu);
  243. extern int gpu_fanpercent(int gpu);
  244. #endif
  245. #ifdef HAVE_SENSORS
  246. #include <sensors/sensors.h>
  247. struct opencl_device_data {
  248. const sensors_chip_name *sensor;
  249. };
  250. #endif
  251. #ifdef HAVE_OPENCL
  252. char *set_vector(char *arg)
  253. {
  254. int i, val = 0, device = 0;
  255. char *nextptr;
  256. nextptr = strtok(arg, ",");
  257. if (nextptr == NULL)
  258. return "Invalid parameters for set vector";
  259. val = atoi(nextptr);
  260. if (val != 1 && val != 2 && val != 4)
  261. return "Invalid value passed to set_vector";
  262. gpus[device++].vwidth = val;
  263. while ((nextptr = strtok(NULL, ",")) != NULL) {
  264. val = atoi(nextptr);
  265. if (val != 1 && val != 2 && val != 4)
  266. return "Invalid value passed to set_vector";
  267. gpus[device++].vwidth = val;
  268. }
  269. if (device == 1) {
  270. for (i = device; i < MAX_GPUDEVICES; i++)
  271. gpus[i].vwidth = gpus[0].vwidth;
  272. }
  273. return NULL;
  274. }
  275. char *set_worksize(char *arg)
  276. {
  277. int i, val = 0, device = 0;
  278. char *nextptr;
  279. nextptr = strtok(arg, ",");
  280. if (nextptr == NULL)
  281. return "Invalid parameters for set work size";
  282. val = atoi(nextptr);
  283. if (val < 1 || val > 9999)
  284. return "Invalid value passed to set_worksize";
  285. gpus[device++].work_size = val;
  286. while ((nextptr = strtok(NULL, ",")) != NULL) {
  287. val = atoi(nextptr);
  288. if (val < 1 || val > 9999)
  289. return "Invalid value passed to set_worksize";
  290. gpus[device++].work_size = val;
  291. }
  292. if (device == 1) {
  293. for (i = device; i < MAX_GPUDEVICES; i++)
  294. gpus[i].work_size = gpus[0].work_size;
  295. }
  296. return NULL;
  297. }
  298. #ifdef USE_SCRYPT
  299. char *set_shaders(char *arg)
  300. {
  301. int i, val = 0, device = 0;
  302. char *nextptr;
  303. nextptr = strtok(arg, ",");
  304. if (nextptr == NULL)
  305. return "Invalid parameters for set lookup gap";
  306. val = atoi(nextptr);
  307. gpus[device++].shaders = val;
  308. while ((nextptr = strtok(NULL, ",")) != NULL) {
  309. val = atoi(nextptr);
  310. gpus[device++].shaders = val;
  311. }
  312. if (device == 1) {
  313. for (i = device; i < MAX_GPUDEVICES; i++)
  314. gpus[i].shaders = gpus[0].shaders;
  315. }
  316. return NULL;
  317. }
  318. char *set_lookup_gap(char *arg)
  319. {
  320. int i, val = 0, device = 0;
  321. char *nextptr;
  322. nextptr = strtok(arg, ",");
  323. if (nextptr == NULL)
  324. return "Invalid parameters for set lookup gap";
  325. val = atoi(nextptr);
  326. gpus[device++].opt_lg = val;
  327. while ((nextptr = strtok(NULL, ",")) != NULL) {
  328. val = atoi(nextptr);
  329. gpus[device++].opt_lg = val;
  330. }
  331. if (device == 1) {
  332. for (i = device; i < MAX_GPUDEVICES; i++)
  333. gpus[i].opt_lg = gpus[0].opt_lg;
  334. }
  335. return NULL;
  336. }
  337. char *set_thread_concurrency(char *arg)
  338. {
  339. int i, val = 0, device = 0;
  340. char *nextptr;
  341. nextptr = strtok(arg, ",");
  342. if (nextptr == NULL)
  343. return "Invalid parameters for set thread concurrency";
  344. val = atoi(nextptr);
  345. gpus[device++].opt_tc = val;
  346. while ((nextptr = strtok(NULL, ",")) != NULL) {
  347. val = atoi(nextptr);
  348. gpus[device++].opt_tc = val;
  349. }
  350. if (device == 1) {
  351. for (i = device; i < MAX_GPUDEVICES; i++)
  352. gpus[i].opt_tc = gpus[0].opt_tc;
  353. }
  354. return NULL;
  355. }
  356. #endif
  357. static enum cl_kernels select_kernel(char *arg)
  358. {
  359. if (!strcmp(arg, "diablo"))
  360. return KL_DIABLO;
  361. if (!strcmp(arg, "diakgcn"))
  362. return KL_DIAKGCN;
  363. if (!strcmp(arg, "poclbm"))
  364. return KL_POCLBM;
  365. if (!strcmp(arg, "phatk"))
  366. return KL_PHATK;
  367. #ifdef USE_SCRYPT
  368. if (!strcmp(arg, "scrypt"))
  369. return KL_SCRYPT;
  370. #endif
  371. return KL_NONE;
  372. }
  373. char *set_kernel(char *arg)
  374. {
  375. enum cl_kernels kern;
  376. int i, device = 0;
  377. char *nextptr;
  378. if (opt_scrypt)
  379. return "Cannot specify a kernel with scrypt";
  380. nextptr = strtok(arg, ",");
  381. if (nextptr == NULL)
  382. return "Invalid parameters for set kernel";
  383. kern = select_kernel(nextptr);
  384. if (kern == KL_NONE)
  385. return "Invalid parameter to set_kernel";
  386. gpus[device++].kernel = kern;
  387. while ((nextptr = strtok(NULL, ",")) != NULL) {
  388. kern = select_kernel(nextptr);
  389. if (kern == KL_NONE)
  390. return "Invalid parameter to set_kernel";
  391. gpus[device++].kernel = kern;
  392. }
  393. if (device == 1) {
  394. for (i = device; i < MAX_GPUDEVICES; i++)
  395. gpus[i].kernel = gpus[0].kernel;
  396. }
  397. return NULL;
  398. }
  399. #endif
  400. #ifdef HAVE_ADL
  401. /* This function allows us to map an adl device to an opencl device for when
  402. * simple enumeration has failed to match them. */
  403. char *set_gpu_map(char *arg)
  404. {
  405. int val1 = 0, val2 = 0;
  406. char *nextptr;
  407. nextptr = strtok(arg, ",");
  408. if (nextptr == NULL)
  409. return "Invalid parameters for set gpu map";
  410. if (sscanf(arg, "%d:%d", &val1, &val2) != 2)
  411. return "Invalid description for map pair";
  412. if (val1 < 0 || val1 > MAX_GPUDEVICES || val2 < 0 || val2 > MAX_GPUDEVICES)
  413. return "Invalid value passed to set_gpu_map";
  414. gpus[val1].virtual_adl = val2;
  415. gpus[val1].mapped = true;
  416. while ((nextptr = strtok(NULL, ",")) != NULL) {
  417. if (sscanf(nextptr, "%d:%d", &val1, &val2) != 2)
  418. return "Invalid description for map pair";
  419. if (val1 < 0 || val1 > MAX_GPUDEVICES || val2 < 0 || val2 > MAX_GPUDEVICES)
  420. return "Invalid value passed to set_gpu_map";
  421. gpus[val1].virtual_adl = val2;
  422. gpus[val1].mapped = true;
  423. }
  424. return NULL;
  425. }
  426. char *set_gpu_engine(char *arg)
  427. {
  428. int i, val1 = 0, val2 = 0, device = 0;
  429. char *nextptr;
  430. nextptr = strtok(arg, ",");
  431. if (nextptr == NULL)
  432. return "Invalid parameters for set gpu engine";
  433. get_intrange(nextptr, &val1, &val2);
  434. if (val1 < 0 || val1 > 9999 || val2 < 0 || val2 > 9999)
  435. return "Invalid value passed to set_gpu_engine";
  436. gpus[device].min_engine = val1;
  437. gpus[device].gpu_engine = val2;
  438. device++;
  439. while ((nextptr = strtok(NULL, ",")) != NULL) {
  440. get_intrange(nextptr, &val1, &val2);
  441. if (val1 < 0 || val1 > 9999 || val2 < 0 || val2 > 9999)
  442. return "Invalid value passed to set_gpu_engine";
  443. gpus[device].min_engine = val1;
  444. gpus[device].gpu_engine = val2;
  445. device++;
  446. }
  447. if (device == 1) {
  448. for (i = 1; i < MAX_GPUDEVICES; i++) {
  449. gpus[i].min_engine = gpus[0].min_engine;
  450. gpus[i].gpu_engine = gpus[0].gpu_engine;
  451. }
  452. }
  453. return NULL;
  454. }
  455. char *set_gpu_fan(char *arg)
  456. {
  457. int i, val1 = 0, val2 = 0, device = 0;
  458. char *nextptr;
  459. nextptr = strtok(arg, ",");
  460. if (nextptr == NULL)
  461. return "Invalid parameters for set gpu fan";
  462. get_intrange(nextptr, &val1, &val2);
  463. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100)
  464. return "Invalid value passed to set_gpu_fan";
  465. gpus[device].min_fan = val1;
  466. gpus[device].gpu_fan = val2;
  467. device++;
  468. while ((nextptr = strtok(NULL, ",")) != NULL) {
  469. get_intrange(nextptr, &val1, &val2);
  470. if (val1 < 0 || val1 > 100 || val2 < 0 || val2 > 100)
  471. return "Invalid value passed to set_gpu_fan";
  472. gpus[device].min_fan = val1;
  473. gpus[device].gpu_fan = val2;
  474. device++;
  475. }
  476. if (device == 1) {
  477. for (i = 1; i < MAX_GPUDEVICES; i++) {
  478. gpus[i].min_fan = gpus[0].min_fan;
  479. gpus[i].gpu_fan = gpus[0].gpu_fan;
  480. }
  481. }
  482. return NULL;
  483. }
  484. char *set_gpu_memclock(char *arg)
  485. {
  486. int i, val = 0, device = 0;
  487. char *nextptr;
  488. nextptr = strtok(arg, ",");
  489. if (nextptr == NULL)
  490. return "Invalid parameters for set gpu memclock";
  491. val = atoi(nextptr);
  492. if (val < 0 || val >= 9999)
  493. return "Invalid value passed to set_gpu_memclock";
  494. gpus[device++].gpu_memclock = val;
  495. while ((nextptr = strtok(NULL, ",")) != NULL) {
  496. val = atoi(nextptr);
  497. if (val < 0 || val >= 9999)
  498. return "Invalid value passed to set_gpu_memclock";
  499. gpus[device++].gpu_memclock = val;
  500. }
  501. if (device == 1) {
  502. for (i = device; i < MAX_GPUDEVICES; i++)
  503. gpus[i].gpu_memclock = gpus[0].gpu_memclock;
  504. }
  505. return NULL;
  506. }
  507. char *set_gpu_memdiff(char *arg)
  508. {
  509. int i, val = 0, device = 0;
  510. char *nextptr;
  511. nextptr = strtok(arg, ",");
  512. if (nextptr == NULL)
  513. return "Invalid parameters for set gpu memdiff";
  514. val = atoi(nextptr);
  515. if (val < -9999 || val > 9999)
  516. return "Invalid value passed to set_gpu_memdiff";
  517. gpus[device++].gpu_memdiff = val;
  518. while ((nextptr = strtok(NULL, ",")) != NULL) {
  519. val = atoi(nextptr);
  520. if (val < -9999 || val > 9999)
  521. return "Invalid value passed to set_gpu_memdiff";
  522. gpus[device++].gpu_memdiff = val;
  523. }
  524. if (device == 1) {
  525. for (i = device; i < MAX_GPUDEVICES; i++)
  526. gpus[i].gpu_memdiff = gpus[0].gpu_memdiff;
  527. }
  528. return NULL;
  529. }
  530. char *set_gpu_powertune(char *arg)
  531. {
  532. int i, val = 0, device = 0;
  533. char *nextptr;
  534. nextptr = strtok(arg, ",");
  535. if (nextptr == NULL)
  536. return "Invalid parameters for set gpu powertune";
  537. val = atoi(nextptr);
  538. if (val < -99 || val > 99)
  539. return "Invalid value passed to set_gpu_powertune";
  540. gpus[device++].gpu_powertune = val;
  541. while ((nextptr = strtok(NULL, ",")) != NULL) {
  542. val = atoi(nextptr);
  543. if (val < -99 || val > 99)
  544. return "Invalid value passed to set_gpu_powertune";
  545. gpus[device++].gpu_powertune = val;
  546. }
  547. if (device == 1) {
  548. for (i = device; i < MAX_GPUDEVICES; i++)
  549. gpus[i].gpu_powertune = gpus[0].gpu_powertune;
  550. }
  551. return NULL;
  552. }
  553. char *set_gpu_vddc(char *arg)
  554. {
  555. int i, device = 0;
  556. float val = 0;
  557. char *nextptr;
  558. nextptr = strtok(arg, ",");
  559. if (nextptr == NULL)
  560. return "Invalid parameters for set gpu vddc";
  561. val = atof(nextptr);
  562. if (val < 0 || val >= 9999)
  563. return "Invalid value passed to set_gpu_vddc";
  564. gpus[device++].gpu_vddc = val;
  565. while ((nextptr = strtok(NULL, ",")) != NULL) {
  566. val = atof(nextptr);
  567. if (val < 0 || val >= 9999)
  568. return "Invalid value passed to set_gpu_vddc";
  569. gpus[device++].gpu_vddc = val;
  570. }
  571. if (device == 1) {
  572. for (i = device; i < MAX_GPUDEVICES; i++)
  573. gpus[i].gpu_vddc = gpus[0].gpu_vddc;
  574. }
  575. return NULL;
  576. }
  577. char *set_temp_overheat(char *arg)
  578. {
  579. int i, val = 0, device = 0, *to;
  580. char *nextptr;
  581. nextptr = strtok(arg, ",");
  582. if (nextptr == NULL)
  583. return "Invalid parameters for set temp overheat";
  584. val = atoi(nextptr);
  585. if (val < 0 || val > 200)
  586. return "Invalid value passed to set temp overheat";
  587. to = &gpus[device++].adl.overtemp;
  588. *to = val;
  589. while ((nextptr = strtok(NULL, ",")) != NULL) {
  590. val = atoi(nextptr);
  591. if (val < 0 || val > 200)
  592. return "Invalid value passed to set temp overheat";
  593. to = &gpus[device++].adl.overtemp;
  594. *to = val;
  595. }
  596. if (device == 1) {
  597. for (i = device; i < MAX_GPUDEVICES; i++) {
  598. to = &gpus[i].adl.overtemp;
  599. *to = val;
  600. }
  601. }
  602. return NULL;
  603. }
  604. #endif
  605. #ifdef HAVE_OPENCL
  606. char *set_intensity(char *arg)
  607. {
  608. int i, device = 0, *tt;
  609. char *nextptr, val = 0;
  610. nextptr = strtok(arg, ",");
  611. if (nextptr == NULL)
  612. return "Invalid parameters for set intensity";
  613. if (!strncasecmp(nextptr, "d", 1))
  614. gpus[device].dynamic = true;
  615. else {
  616. gpus[device].dynamic = false;
  617. val = atoi(nextptr);
  618. if (val < MIN_INTENSITY || val > MAX_GPU_INTENSITY)
  619. return "Invalid value passed to set intensity";
  620. tt = &gpus[device].intensity;
  621. *tt = val;
  622. }
  623. device++;
  624. while ((nextptr = strtok(NULL, ",")) != NULL) {
  625. if (!strncasecmp(nextptr, "d", 1))
  626. gpus[device].dynamic = true;
  627. else {
  628. gpus[device].dynamic = false;
  629. val = atoi(nextptr);
  630. if (val < MIN_INTENSITY || val > MAX_GPU_INTENSITY)
  631. return "Invalid value passed to set intensity";
  632. tt = &gpus[device].intensity;
  633. *tt = val;
  634. }
  635. device++;
  636. }
  637. if (device == 1) {
  638. for (i = device; i < MAX_GPUDEVICES; i++) {
  639. gpus[i].dynamic = gpus[0].dynamic;
  640. gpus[i].intensity = gpus[0].intensity;
  641. }
  642. }
  643. return NULL;
  644. }
  645. #endif
  646. #ifdef HAVE_OPENCL
  647. struct device_drv opencl_api;
  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. int i;
  667. for (i = 1; i < cgpu->threads; i++) {
  668. struct thr_info *thr;
  669. thr = get_thread(i);
  670. if (!thr->pause && cgpu->dynamic) {
  671. applog(LOG_WARNING, "Disabling extra threads due to dynamic mode.");
  672. applog(LOG_WARNING, "Tune dynamic intensity with --gpu-dyninterval");
  673. }
  674. thr->pause = cgpu->dynamic;
  675. if (!cgpu->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 thr_info *thr;
  686. int i;
  687. char checkin[40];
  688. double displayed_rolling;
  689. bool mhash_base = !(cgpu->rolling < 1);
  690. char logline[255];
  691. strcpy(logline, ""); // In case it has no data
  692. tailsprintf(logline, sizeof(logline), "I:%s%d ", (cgpu->dynamic ? "d" : ""), cgpu->intensity);
  693. #ifdef HAVE_ADL
  694. if (cgpu->has_adl) {
  695. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  696. float temp = 0, vddc = 0;
  697. if (gpu_stats(cgpu->device_id, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune)) {
  698. if (fanspeed != -1 || fanpercent != -1) {
  699. tailsprintf(logline, sizeof(logline), "F: ");
  700. if (fanspeed > 9999)
  701. fanspeed = 9999;
  702. if (fanpercent != -1)
  703. {
  704. tailsprintf(logline, sizeof(logline), "%d%% ", fanpercent);
  705. if (fanspeed != -1)
  706. tailsprintf(logline, sizeof(logline), "(%d RPM) ", fanspeed);
  707. }
  708. else
  709. tailsprintf(logline, sizeof(logline), "%d RPM ", fanspeed);
  710. tailsprintf(logline, sizeof(logline), " ");
  711. }
  712. if (engineclock != -1)
  713. tailsprintf(logline, sizeof(logline), "E: %d MHz ", engineclock);
  714. if (memclock != -1)
  715. tailsprintf(logline, sizeof(logline), "M: %d MHz ", memclock);
  716. if (vddc != -1)
  717. tailsprintf(logline, sizeof(logline), "V: %.3fV ", vddc);
  718. if (activity != -1)
  719. tailsprintf(logline, sizeof(logline), "A: %d%% ", activity);
  720. if (powertune != -1)
  721. tailsprintf(logline, sizeof(logline), "P: %d%%", powertune);
  722. }
  723. }
  724. #endif
  725. wlogprint("%s\n", logline);
  726. wlogprint("Last initialised: %s\n", cgpu->init);
  727. for (i = 0; i < mining_threads; i++) {
  728. thr = get_thread(i);
  729. if (thr->cgpu != cgpu)
  730. continue;
  731. get_datestamp(checkin, sizeof(checkin), time(NULL) - timer_elapsed(&thr->last, NULL));
  732. displayed_rolling = thr->rolling;
  733. if (!mhash_base)
  734. displayed_rolling *= 1000;
  735. snprintf(logline, sizeof(logline), "Thread %d: %.1f %sh/s %s ", i, displayed_rolling, mhash_base ? "M" : "K" , cgpu->deven != DEV_DISABLED ? "Enabled" : "Disabled");
  736. switch (cgpu->status) {
  737. default:
  738. case LIFE_WELL:
  739. tailsprintf(logline, sizeof(logline), "ALIVE");
  740. break;
  741. case LIFE_SICK:
  742. tailsprintf(logline, sizeof(logline), "SICK reported in %s", checkin);
  743. break;
  744. case LIFE_DEAD:
  745. tailsprintf(logline, sizeof(logline), "DEAD reported in %s", checkin);
  746. break;
  747. case LIFE_INIT:
  748. case LIFE_NOSTART:
  749. tailsprintf(logline, sizeof(logline), "Never started");
  750. break;
  751. }
  752. if (thr->pause)
  753. tailsprintf(logline, sizeof(logline), " paused");
  754. wlogprint("%s\n", logline);
  755. }
  756. }
  757. static
  758. void opencl_tui_wlogprint_choices(struct cgpu_info *cgpu)
  759. {
  760. wlogprint("[I]ntensity [R]estart GPU ");
  761. #ifdef HAVE_ADL
  762. if (cgpu->has_adl)
  763. wlogprint("[C]hange settings ");
  764. #endif
  765. }
  766. static
  767. const char *opencl_tui_handle_choice(struct cgpu_info *cgpu, int input)
  768. {
  769. switch (input)
  770. {
  771. case 'i': case 'I':
  772. {
  773. int intensity;
  774. char *intvar;
  775. if (opt_scrypt) {
  776. intvar = curses_input("Set GPU scan intensity (d or "
  777. MIN_SCRYPT_INTENSITY_STR " -> "
  778. MAX_SCRYPT_INTENSITY_STR ")");
  779. } else {
  780. intvar = curses_input("Set GPU scan intensity (d or "
  781. MIN_SHA_INTENSITY_STR " -> "
  782. MAX_SHA_INTENSITY_STR ")");
  783. }
  784. if (!intvar)
  785. return "Invalid intensity\n";
  786. if (!strncasecmp(intvar, "d", 1)) {
  787. cgpu->dynamic = true;
  788. pause_dynamic_threads(cgpu->device_id);
  789. free(intvar);
  790. return "Dynamic mode enabled\n";
  791. }
  792. intensity = atoi(intvar);
  793. free(intvar);
  794. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY)
  795. return "Invalid intensity (out of range)\n";
  796. cgpu->dynamic = false;
  797. cgpu->intensity = intensity;
  798. pause_dynamic_threads(cgpu->device_id);
  799. return "Intensity changed\n";
  800. }
  801. case 'r': case 'R':
  802. reinit_device(cgpu);
  803. return "Attempting to restart\n";
  804. case 'c': case 'C':
  805. {
  806. char logline[256];
  807. clear_logwin();
  808. get_statline3(logline, sizeof(logline), cgpu, true, true);
  809. wattron(logwin, A_BOLD);
  810. wlogprint("%s", logline);
  811. wattroff(logwin, A_BOLD);
  812. wlogprint("\n");
  813. change_gpusettings(cgpu->device_id);
  814. return ""; // Force refresh
  815. }
  816. }
  817. return NULL;
  818. }
  819. #endif
  820. #ifdef HAVE_OPENCL
  821. static _clState *clStates[MAX_GPUDEVICES];
  822. #define CL_SET_BLKARG(blkvar) status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->blkvar)
  823. #define CL_SET_ARG(var) status |= clSetKernelArg(*kernel, num++, sizeof(var), (void *)&var)
  824. #define CL_SET_VARG(args, var) status |= clSetKernelArg(*kernel, num++, args * sizeof(uint), (void *)var)
  825. static cl_int queue_poclbm_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  826. {
  827. cl_kernel *kernel = &clState->kernel;
  828. unsigned int num = 0;
  829. cl_int status = 0;
  830. CL_SET_BLKARG(ctx_a);
  831. CL_SET_BLKARG(ctx_b);
  832. CL_SET_BLKARG(ctx_c);
  833. CL_SET_BLKARG(ctx_d);
  834. CL_SET_BLKARG(ctx_e);
  835. CL_SET_BLKARG(ctx_f);
  836. CL_SET_BLKARG(ctx_g);
  837. CL_SET_BLKARG(ctx_h);
  838. CL_SET_BLKARG(cty_b);
  839. CL_SET_BLKARG(cty_c);
  840. CL_SET_BLKARG(cty_f);
  841. CL_SET_BLKARG(cty_g);
  842. CL_SET_BLKARG(cty_h);
  843. if (!clState->goffset) {
  844. cl_uint vwidth = clState->vwidth;
  845. uint *nonces = alloca(sizeof(uint) * vwidth);
  846. unsigned int i;
  847. for (i = 0; i < vwidth; i++)
  848. nonces[i] = blk->nonce + (i * threads);
  849. CL_SET_VARG(vwidth, nonces);
  850. }
  851. CL_SET_BLKARG(fW0);
  852. CL_SET_BLKARG(fW1);
  853. CL_SET_BLKARG(fW2);
  854. CL_SET_BLKARG(fW3);
  855. CL_SET_BLKARG(fW15);
  856. CL_SET_BLKARG(fW01r);
  857. CL_SET_BLKARG(D1A);
  858. CL_SET_BLKARG(C1addK5);
  859. CL_SET_BLKARG(B1addK6);
  860. CL_SET_BLKARG(W16addK16);
  861. CL_SET_BLKARG(W17addK17);
  862. CL_SET_BLKARG(PreVal4addT1);
  863. CL_SET_BLKARG(PreVal0);
  864. CL_SET_ARG(clState->outputBuffer);
  865. return status;
  866. }
  867. static cl_int queue_phatk_kernel(_clState *clState, dev_blk_ctx *blk,
  868. __maybe_unused cl_uint threads)
  869. {
  870. cl_kernel *kernel = &clState->kernel;
  871. cl_uint vwidth = clState->vwidth;
  872. unsigned int i, num = 0;
  873. cl_int status = 0;
  874. uint *nonces;
  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(cty_b);
  884. CL_SET_BLKARG(cty_c);
  885. CL_SET_BLKARG(cty_d);
  886. CL_SET_BLKARG(cty_f);
  887. CL_SET_BLKARG(cty_g);
  888. CL_SET_BLKARG(cty_h);
  889. nonces = alloca(sizeof(uint) * vwidth);
  890. for (i = 0; i < vwidth; i++)
  891. nonces[i] = blk->nonce + i;
  892. CL_SET_VARG(vwidth, nonces);
  893. CL_SET_BLKARG(W16);
  894. CL_SET_BLKARG(W17);
  895. CL_SET_BLKARG(PreVal4_2);
  896. CL_SET_BLKARG(PreVal0);
  897. CL_SET_BLKARG(PreW18);
  898. CL_SET_BLKARG(PreW19);
  899. CL_SET_BLKARG(PreW31);
  900. CL_SET_BLKARG(PreW32);
  901. CL_SET_ARG(clState->outputBuffer);
  902. return status;
  903. }
  904. static cl_int queue_diakgcn_kernel(_clState *clState, dev_blk_ctx *blk,
  905. __maybe_unused cl_uint threads)
  906. {
  907. cl_kernel *kernel = &clState->kernel;
  908. unsigned int num = 0;
  909. cl_int status = 0;
  910. if (!clState->goffset) {
  911. cl_uint vwidth = clState->vwidth;
  912. uint *nonces = alloca(sizeof(uint) * vwidth);
  913. unsigned int i;
  914. for (i = 0; i < vwidth; i++)
  915. nonces[i] = blk->nonce + i;
  916. CL_SET_VARG(vwidth, nonces);
  917. }
  918. CL_SET_BLKARG(PreVal0);
  919. CL_SET_BLKARG(PreVal4_2);
  920. CL_SET_BLKARG(cty_h);
  921. CL_SET_BLKARG(D1A);
  922. CL_SET_BLKARG(cty_b);
  923. CL_SET_BLKARG(cty_c);
  924. CL_SET_BLKARG(cty_f);
  925. CL_SET_BLKARG(cty_g);
  926. CL_SET_BLKARG(C1addK5);
  927. CL_SET_BLKARG(B1addK6);
  928. CL_SET_BLKARG(PreVal0addK7);
  929. CL_SET_BLKARG(W16addK16);
  930. CL_SET_BLKARG(W17addK17);
  931. CL_SET_BLKARG(PreW18);
  932. CL_SET_BLKARG(PreW19);
  933. CL_SET_BLKARG(W16);
  934. CL_SET_BLKARG(W17);
  935. CL_SET_BLKARG(PreW31);
  936. CL_SET_BLKARG(PreW32);
  937. CL_SET_BLKARG(ctx_a);
  938. CL_SET_BLKARG(ctx_b);
  939. CL_SET_BLKARG(ctx_c);
  940. CL_SET_BLKARG(ctx_d);
  941. CL_SET_BLKARG(ctx_e);
  942. CL_SET_BLKARG(ctx_f);
  943. CL_SET_BLKARG(ctx_g);
  944. CL_SET_BLKARG(ctx_h);
  945. CL_SET_BLKARG(zeroA);
  946. CL_SET_BLKARG(zeroB);
  947. CL_SET_BLKARG(oneA);
  948. CL_SET_BLKARG(twoA);
  949. CL_SET_BLKARG(threeA);
  950. CL_SET_BLKARG(fourA);
  951. CL_SET_BLKARG(fiveA);
  952. CL_SET_BLKARG(sixA);
  953. CL_SET_BLKARG(sevenA);
  954. CL_SET_ARG(clState->outputBuffer);
  955. return status;
  956. }
  957. static cl_int queue_diablo_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  958. {
  959. cl_kernel *kernel = &clState->kernel;
  960. unsigned int num = 0;
  961. cl_int status = 0;
  962. if (!clState->goffset) {
  963. cl_uint vwidth = clState->vwidth;
  964. uint *nonces = alloca(sizeof(uint) * vwidth);
  965. unsigned int i;
  966. for (i = 0; i < vwidth; i++)
  967. nonces[i] = blk->nonce + (i * threads);
  968. CL_SET_VARG(vwidth, nonces);
  969. }
  970. CL_SET_BLKARG(PreVal0);
  971. CL_SET_BLKARG(PreVal0addK7);
  972. CL_SET_BLKARG(PreVal4addT1);
  973. CL_SET_BLKARG(PreW18);
  974. CL_SET_BLKARG(PreW19);
  975. CL_SET_BLKARG(W16);
  976. CL_SET_BLKARG(W17);
  977. CL_SET_BLKARG(W16addK16);
  978. CL_SET_BLKARG(W17addK17);
  979. CL_SET_BLKARG(PreW31);
  980. CL_SET_BLKARG(PreW32);
  981. CL_SET_BLKARG(D1A);
  982. CL_SET_BLKARG(cty_b);
  983. CL_SET_BLKARG(cty_c);
  984. CL_SET_BLKARG(cty_h);
  985. CL_SET_BLKARG(cty_f);
  986. CL_SET_BLKARG(cty_g);
  987. CL_SET_BLKARG(C1addK5);
  988. CL_SET_BLKARG(B1addK6);
  989. CL_SET_BLKARG(ctx_a);
  990. CL_SET_BLKARG(ctx_b);
  991. CL_SET_BLKARG(ctx_c);
  992. CL_SET_BLKARG(ctx_d);
  993. CL_SET_BLKARG(ctx_e);
  994. CL_SET_BLKARG(ctx_f);
  995. CL_SET_BLKARG(ctx_g);
  996. CL_SET_BLKARG(ctx_h);
  997. CL_SET_ARG(clState->outputBuffer);
  998. return status;
  999. }
  1000. #ifdef USE_SCRYPT
  1001. static cl_int queue_scrypt_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_unused cl_uint threads)
  1002. {
  1003. unsigned char *midstate = blk->work->midstate;
  1004. cl_kernel *kernel = &clState->kernel;
  1005. unsigned int num = 0;
  1006. cl_uint le_target;
  1007. cl_int status = 0;
  1008. le_target = *(cl_uint *)(blk->work->target + 28);
  1009. clState->cldata = blk->work->data;
  1010. status = clEnqueueWriteBuffer(clState->commandQueue, clState->CLbuffer0, true, 0, 80, clState->cldata, 0, NULL,NULL);
  1011. CL_SET_ARG(clState->CLbuffer0);
  1012. CL_SET_ARG(clState->outputBuffer);
  1013. CL_SET_ARG(clState->padbuffer8);
  1014. CL_SET_VARG(4, &midstate[0]);
  1015. CL_SET_VARG(4, &midstate[16]);
  1016. CL_SET_ARG(le_target);
  1017. return status;
  1018. }
  1019. #endif
  1020. static void set_threads_hashes(unsigned int vectors,int64_t *hashes, size_t *globalThreads,
  1021. unsigned int minthreads, __maybe_unused int *intensity)
  1022. {
  1023. unsigned int threads = 0;
  1024. while (threads < minthreads) {
  1025. threads = 1 << ((opt_scrypt ? 0 : 15) + *intensity);
  1026. if (threads < minthreads) {
  1027. if (likely(*intensity < MAX_INTENSITY))
  1028. (*intensity)++;
  1029. else
  1030. threads = minthreads;
  1031. }
  1032. }
  1033. *globalThreads = threads;
  1034. *hashes = threads * vectors;
  1035. }
  1036. #endif /* HAVE_OPENCL */
  1037. #ifdef HAVE_OPENCL
  1038. /* We have only one thread that ever re-initialises GPUs, thus if any GPU
  1039. * init command fails due to a completely wedged GPU, the thread will never
  1040. * return, unable to harm other GPUs. If it does return, it means we only had
  1041. * a soft failure and then the reinit_gpu thread is ready to tackle another
  1042. * GPU */
  1043. void *reinit_gpu(void *userdata)
  1044. {
  1045. struct thr_info *mythr = userdata;
  1046. struct cgpu_info *cgpu, *sel_cgpu;
  1047. struct thr_info *thr;
  1048. char name[256];
  1049. int thr_id;
  1050. int i;
  1051. pthread_detach(pthread_self());
  1052. RenameThread("reinit_gpu");
  1053. select_cgpu:
  1054. sel_cgpu =
  1055. cgpu = tq_pop(mythr->q, NULL);
  1056. if (!cgpu)
  1057. goto out;
  1058. if (clDevicesNum() != nDevs) {
  1059. applog(LOG_WARNING, "Hardware not reporting same number of active devices, will not attempt to restart GPU");
  1060. goto out;
  1061. }
  1062. for (i = 0; i < cgpu->threads; ++i)
  1063. {
  1064. thr = cgpu->thr[i];
  1065. thr_id = thr->id;
  1066. thr->rolling = thr->cgpu->rolling = 0;
  1067. /* Reports the last time we tried to revive a sick GPU */
  1068. cgtime(&thr->sick);
  1069. if (!pthread_cancel(thr->pth)) {
  1070. applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
  1071. } else
  1072. applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
  1073. }
  1074. for (i = 0; i < cgpu->threads; ++i)
  1075. {
  1076. int virtual_gpu;
  1077. thr = cgpu->thr[i];
  1078. thr_id = thr->id;
  1079. virtual_gpu = cgpu->virtual_gpu;
  1080. /* Lose this ram cause we may get stuck here! */
  1081. //tq_freeze(thr->q);
  1082. thr->q = tq_new();
  1083. if (!thr->q)
  1084. quithere(1, "Failed to tq_new");
  1085. /* Lose this ram cause we may dereference in the dying thread! */
  1086. //free(clState);
  1087. applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
  1088. clStates[thr_id] = initCl(virtual_gpu, name, sizeof(name));
  1089. if (!clStates[thr_id]) {
  1090. applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
  1091. goto select_cgpu;
  1092. }
  1093. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1094. if (unlikely(thr_info_create(thr, NULL, miner_thread, thr))) {
  1095. applog(LOG_ERR, "thread %d create failed", thr_id);
  1096. return NULL;
  1097. }
  1098. applog(LOG_WARNING, "Thread %d restarted", thr_id);
  1099. }
  1100. get_now_datestamp(sel_cgpu->init, sizeof(sel_cgpu->init));
  1101. proc_enable(cgpu);
  1102. goto select_cgpu;
  1103. out:
  1104. return NULL;
  1105. }
  1106. #else
  1107. void *reinit_gpu(__maybe_unused void *userdata)
  1108. {
  1109. return NULL;
  1110. }
  1111. #endif
  1112. #ifdef HAVE_OPENCL
  1113. struct device_drv opencl_api;
  1114. static int opencl_autodetect()
  1115. {
  1116. RUNONCE(0);
  1117. #ifndef WIN32
  1118. if (!getenv("DISPLAY")) {
  1119. applog(LOG_DEBUG, "DISPLAY not set, setting :0 just in case");
  1120. setenv("DISPLAY", ":0", 1);
  1121. }
  1122. #endif
  1123. if (!load_opencl_symbols()) {
  1124. nDevs = 0;
  1125. return 0;
  1126. }
  1127. int i;
  1128. nDevs = clDevicesNum();
  1129. if (nDevs < 0) {
  1130. applog(LOG_ERR, "clDevicesNum returned error, no GPUs usable");
  1131. nDevs = 0;
  1132. }
  1133. if (!nDevs)
  1134. return 0;
  1135. /* If opt_g_threads is not set, use default 1 thread on scrypt and
  1136. * 2 for regular mining */
  1137. if (opt_g_threads == -1) {
  1138. if (opt_scrypt)
  1139. opt_g_threads = 1;
  1140. else
  1141. opt_g_threads = 2;
  1142. }
  1143. #ifdef HAVE_SENSORS
  1144. struct opencl_device_data *data;
  1145. const sensors_chip_name *cn;
  1146. int c = 0;
  1147. sensors_init(NULL);
  1148. sensors_chip_name cnm;
  1149. if (sensors_parse_chip_name("radeon-*", &cnm))
  1150. c = -1;
  1151. #endif
  1152. for (i = 0; i < nDevs; ++i) {
  1153. struct cgpu_info *cgpu;
  1154. cgpu = &gpus[i];
  1155. cgpu->devtype = "GPU";
  1156. cgpu->deven = DEV_ENABLED;
  1157. cgpu->drv = &opencl_api;
  1158. cgpu->device_id = i;
  1159. cgpu->threads = opt_g_threads;
  1160. cgpu->virtual_gpu = i;
  1161. #ifdef HAVE_SENSORS
  1162. cn = (c == -1) ? NULL : sensors_get_detected_chips(&cnm, &c);
  1163. cgpu->device_data = data = malloc(sizeof(*data));
  1164. *data = (struct opencl_device_data){
  1165. .sensor = cn,
  1166. };
  1167. #endif
  1168. add_cgpu(cgpu);
  1169. }
  1170. if (!opt_noadl)
  1171. init_adl(nDevs);
  1172. return nDevs;
  1173. }
  1174. static void opencl_detect()
  1175. {
  1176. if (total_devices || total_devices_new)
  1177. // If there are any other devices, only act if the user has explicitly enabled OpenCL
  1178. noserial_detect_manual(&opencl_api, opencl_autodetect);
  1179. else
  1180. noserial_detect(&opencl_api, opencl_autodetect);
  1181. }
  1182. static void reinit_opencl_device(struct cgpu_info *gpu)
  1183. {
  1184. tq_push(control_thr[gpur_thr_id].q, gpu);
  1185. }
  1186. // FIXME: Legacy (called by TUI) for side effects
  1187. static
  1188. bool override_opencl_statline_temp(char *buf, size_t bufsz, struct cgpu_info *gpu, __maybe_unused bool per_processor)
  1189. {
  1190. #ifdef HAVE_SENSORS
  1191. struct opencl_device_data *data = gpu->device_data;
  1192. if (data->sensor)
  1193. {
  1194. const sensors_chip_name *cn = data->sensor;
  1195. const sensors_feature *feat;
  1196. for (int f = 0; (feat = sensors_get_features(cn, &f)); )
  1197. {
  1198. const sensors_subfeature *subf;
  1199. subf = sensors_get_subfeature(cn, feat, SENSORS_SUBFEATURE_TEMP_INPUT);
  1200. if (!(subf && subf->flags & SENSORS_MODE_R))
  1201. continue;
  1202. double val;
  1203. int rc = sensors_get_value(cn, subf->number, &val);
  1204. if (rc)
  1205. continue;
  1206. gpu->temp = val;
  1207. return false;
  1208. }
  1209. }
  1210. #endif
  1211. #ifdef HAVE_ADL
  1212. if (gpu->has_adl) {
  1213. int gpuid = gpu->device_id;
  1214. gpu_temp(gpuid);
  1215. gpu_fanspeed(gpuid);
  1216. }
  1217. #endif
  1218. return false;
  1219. }
  1220. static struct api_data*
  1221. get_opencl_api_extra_device_status(struct cgpu_info *gpu)
  1222. {
  1223. struct api_data*root = NULL;
  1224. float gt, gv;
  1225. int ga, gf, gp, gc, gm, pt;
  1226. #ifdef HAVE_ADL
  1227. if (!gpu_stats(gpu->device_id, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  1228. #endif
  1229. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  1230. root = api_add_int(root, "Fan Speed", &gf, true);
  1231. root = api_add_int(root, "Fan Percent", &gp, true);
  1232. root = api_add_int(root, "GPU Clock", &gc, true);
  1233. root = api_add_int(root, "Memory Clock", &gm, true);
  1234. root = api_add_volts(root, "GPU Voltage", &gv, true);
  1235. root = api_add_int(root, "GPU Activity", &ga, true);
  1236. root = api_add_int(root, "Powertune", &pt, true);
  1237. char intensity[20];
  1238. if (gpu->dynamic)
  1239. strcpy(intensity, "D");
  1240. else
  1241. sprintf(intensity, "%d", gpu->intensity);
  1242. root = api_add_string(root, "Intensity", intensity, true);
  1243. return root;
  1244. }
  1245. struct opencl_thread_data {
  1246. cl_int (*queue_kernel_parameters)(_clState *, dev_blk_ctx *, cl_uint);
  1247. uint32_t *res;
  1248. };
  1249. static uint32_t *blank_res;
  1250. static bool opencl_thread_prepare(struct thr_info *thr)
  1251. {
  1252. char name[256];
  1253. struct cgpu_info *cgpu = thr->cgpu;
  1254. int gpu = cgpu->device_id;
  1255. int virtual_gpu = cgpu->virtual_gpu;
  1256. int i = thr->id;
  1257. static bool failmessage = false;
  1258. int buffersize = opt_scrypt ? SCRYPT_BUFFERSIZE : BUFFERSIZE;
  1259. if (!blank_res)
  1260. blank_res = calloc(buffersize, 1);
  1261. if (!blank_res) {
  1262. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1263. return false;
  1264. }
  1265. strcpy(name, "");
  1266. applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
  1267. clStates[i] = initCl(virtual_gpu, name, sizeof(name));
  1268. if (!clStates[i]) {
  1269. #ifdef HAVE_CURSES
  1270. if (use_curses)
  1271. enable_curses();
  1272. #endif
  1273. applog(LOG_ERR, "Failed to init GPU thread %d, disabling device %d", i, gpu);
  1274. if (!failmessage) {
  1275. applog(LOG_ERR, "Restarting the GPU from the menu will not fix this.");
  1276. applog(LOG_ERR, "Try restarting BFGMiner.");
  1277. failmessage = true;
  1278. #ifdef HAVE_CURSES
  1279. char *buf;
  1280. if (use_curses) {
  1281. buf = curses_input("Press enter to continue");
  1282. if (buf)
  1283. free(buf);
  1284. }
  1285. #endif
  1286. }
  1287. cgpu->deven = DEV_DISABLED;
  1288. cgpu->status = LIFE_NOSTART;
  1289. dev_error(cgpu, REASON_DEV_NOSTART);
  1290. return false;
  1291. }
  1292. if (!cgpu->name)
  1293. cgpu->name = strdup(name);
  1294. if (!cgpu->kname)
  1295. {
  1296. switch (clStates[i]->chosen_kernel) {
  1297. case KL_DIABLO:
  1298. cgpu->kname = "diablo";
  1299. break;
  1300. case KL_DIAKGCN:
  1301. cgpu->kname = "diakgcn";
  1302. break;
  1303. case KL_PHATK:
  1304. cgpu->kname = "phatk";
  1305. break;
  1306. #ifdef USE_SCRYPT
  1307. case KL_SCRYPT:
  1308. cgpu->kname = "scrypt";
  1309. break;
  1310. #endif
  1311. case KL_POCLBM:
  1312. cgpu->kname = "poclbm";
  1313. break;
  1314. default:
  1315. break;
  1316. }
  1317. }
  1318. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1319. get_now_datestamp(cgpu->init, sizeof(cgpu->init));
  1320. have_opencl = true;
  1321. return true;
  1322. }
  1323. static bool opencl_thread_init(struct thr_info *thr)
  1324. {
  1325. const int thr_id = thr->id;
  1326. struct cgpu_info *gpu = thr->cgpu;
  1327. struct opencl_thread_data *thrdata;
  1328. _clState *clState = clStates[thr_id];
  1329. cl_int status = 0;
  1330. thrdata = calloc(1, sizeof(*thrdata));
  1331. thr->cgpu_data = thrdata;
  1332. int buffersize = opt_scrypt ? SCRYPT_BUFFERSIZE : BUFFERSIZE;
  1333. if (!thrdata) {
  1334. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1335. return false;
  1336. }
  1337. switch (clState->chosen_kernel) {
  1338. case KL_POCLBM:
  1339. thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
  1340. break;
  1341. case KL_PHATK:
  1342. thrdata->queue_kernel_parameters = &queue_phatk_kernel;
  1343. break;
  1344. case KL_DIAKGCN:
  1345. thrdata->queue_kernel_parameters = &queue_diakgcn_kernel;
  1346. break;
  1347. #ifdef USE_SCRYPT
  1348. case KL_SCRYPT:
  1349. thrdata->queue_kernel_parameters = &queue_scrypt_kernel;
  1350. break;
  1351. #endif
  1352. default:
  1353. case KL_DIABLO:
  1354. thrdata->queue_kernel_parameters = &queue_diablo_kernel;
  1355. break;
  1356. }
  1357. thrdata->res = calloc(buffersize, 1);
  1358. if (!thrdata->res) {
  1359. free(thrdata);
  1360. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1361. return false;
  1362. }
  1363. status |= clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  1364. buffersize, blank_res, 0, NULL, NULL);
  1365. if (unlikely(status != CL_SUCCESS)) {
  1366. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1367. return false;
  1368. }
  1369. gpu->status = LIFE_WELL;
  1370. gpu->device_last_well = time(NULL);
  1371. return true;
  1372. }
  1373. static bool opencl_prepare_work(struct thr_info __maybe_unused *thr, struct work *work)
  1374. {
  1375. #ifdef USE_SCRYPT
  1376. if (opt_scrypt)
  1377. work->blk.work = work;
  1378. else
  1379. #endif
  1380. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1381. return true;
  1382. }
  1383. extern int opt_dynamic_interval;
  1384. static int64_t opencl_scanhash(struct thr_info *thr, struct work *work,
  1385. int64_t __maybe_unused max_nonce)
  1386. {
  1387. const int thr_id = thr->id;
  1388. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1389. struct cgpu_info *gpu = thr->cgpu;
  1390. _clState *clState = clStates[thr_id];
  1391. const cl_kernel *kernel = &clState->kernel;
  1392. const int dynamic_us = opt_dynamic_interval * 1000;
  1393. cl_int status;
  1394. size_t globalThreads[1];
  1395. size_t localThreads[1] = { clState->wsize };
  1396. int64_t hashes;
  1397. int found = opt_scrypt ? SCRYPT_FOUND : FOUND;
  1398. int buffersize = opt_scrypt ? SCRYPT_BUFFERSIZE : BUFFERSIZE;
  1399. /* Windows' timer resolution is only 15ms so oversample 5x */
  1400. if (gpu->dynamic && (++gpu->intervals * dynamic_us) > 70000) {
  1401. struct timeval tv_gpuend;
  1402. double gpu_us;
  1403. cgtime(&tv_gpuend);
  1404. gpu_us = us_tdiff(&tv_gpuend, &gpu->tv_gpustart) / gpu->intervals;
  1405. if (gpu_us > dynamic_us) {
  1406. if (gpu->intensity > MIN_INTENSITY)
  1407. --gpu->intensity;
  1408. } else if (gpu_us < dynamic_us / 2) {
  1409. if (gpu->intensity < MAX_INTENSITY)
  1410. ++gpu->intensity;
  1411. }
  1412. memcpy(&(gpu->tv_gpustart), &tv_gpuend, sizeof(struct timeval));
  1413. gpu->intervals = 0;
  1414. }
  1415. set_threads_hashes(clState->vwidth, &hashes, globalThreads, localThreads[0], &gpu->intensity);
  1416. if (hashes > gpu->max_hashes)
  1417. gpu->max_hashes = hashes;
  1418. status = thrdata->queue_kernel_parameters(clState, &work->blk, globalThreads[0]);
  1419. if (unlikely(status != CL_SUCCESS)) {
  1420. applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
  1421. return -1;
  1422. }
  1423. if (clState->goffset) {
  1424. size_t global_work_offset[1];
  1425. global_work_offset[0] = work->blk.nonce;
  1426. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, global_work_offset,
  1427. globalThreads, localThreads, 0, NULL, NULL);
  1428. } else
  1429. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1430. globalThreads, localThreads, 0, NULL, NULL);
  1431. if (unlikely(status != CL_SUCCESS)) {
  1432. applog(LOG_ERR, "Error %d: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)", status);
  1433. return -1;
  1434. }
  1435. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1436. buffersize, thrdata->res, 0, NULL, NULL);
  1437. if (unlikely(status != CL_SUCCESS)) {
  1438. applog(LOG_ERR, "Error: clEnqueueReadBuffer failed error %d. (clEnqueueReadBuffer)", status);
  1439. return -1;
  1440. }
  1441. /* The amount of work scanned can fluctuate when intensity changes
  1442. * and since we do this one cycle behind, we increment the work more
  1443. * than enough to prevent repeating work */
  1444. work->blk.nonce += gpu->max_hashes;
  1445. /* This finish flushes the readbuffer set with CL_FALSE in clEnqueueReadBuffer */
  1446. clFinish(clState->commandQueue);
  1447. /* FOUND entry is used as a counter to say how many nonces exist */
  1448. if (thrdata->res[found]) {
  1449. /* Clear the buffer again */
  1450. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1451. buffersize, blank_res, 0, NULL, NULL);
  1452. if (unlikely(status != CL_SUCCESS)) {
  1453. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1454. return -1;
  1455. }
  1456. applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
  1457. postcalc_hash_async(thr, work, thrdata->res);
  1458. memset(thrdata->res, 0, buffersize);
  1459. /* This finish flushes the writebuffer set with CL_FALSE in clEnqueueWriteBuffer */
  1460. clFinish(clState->commandQueue);
  1461. }
  1462. return hashes;
  1463. }
  1464. static void opencl_thread_shutdown(struct thr_info *thr)
  1465. {
  1466. const int thr_id = thr->id;
  1467. _clState *clState = clStates[thr_id];
  1468. clReleaseKernel(clState->kernel);
  1469. clReleaseProgram(clState->program);
  1470. clReleaseCommandQueue(clState->commandQueue);
  1471. clReleaseContext(clState->context);
  1472. }
  1473. struct device_drv opencl_api = {
  1474. .dname = "opencl",
  1475. .name = "OCL",
  1476. .drv_detect = opencl_detect,
  1477. .reinit_device = reinit_opencl_device,
  1478. .override_statline_temp2 = override_opencl_statline_temp,
  1479. #ifdef HAVE_CURSES
  1480. .proc_wlogprint_status = opencl_wlogprint_status,
  1481. .proc_tui_wlogprint_choices = opencl_tui_wlogprint_choices,
  1482. .proc_tui_handle_choice = opencl_tui_handle_choice,
  1483. #endif
  1484. .get_api_extra_device_status = get_opencl_api_extra_device_status,
  1485. .thread_prepare = opencl_thread_prepare,
  1486. .thread_init = opencl_thread_init,
  1487. .prepare_work = opencl_prepare_work,
  1488. .scanhash = opencl_scanhash,
  1489. .thread_shutdown = opencl_thread_shutdown,
  1490. };
  1491. #endif