driver-opencl.c 45 KB

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