driver-opencl.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820
  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 "driver-opencl.h"
  35. #include "findnonce.h"
  36. #include "ocl.h"
  37. #include "adl.h"
  38. /* TODO: cleanup externals ********************/
  39. #ifdef HAVE_OPENCL
  40. /* Platform API */
  41. CL_API_ENTRY cl_int CL_API_CALL
  42. (*clGetPlatformIDs)(cl_uint /* num_entries */,
  43. cl_platform_id * /* platforms */,
  44. cl_uint * /* num_platforms */) CL_API_SUFFIX__VERSION_1_0;
  45. CL_API_ENTRY cl_int CL_API_CALL
  46. (*clGetPlatformInfo)(cl_platform_id /* platform */,
  47. cl_platform_info /* param_name */,
  48. size_t /* param_value_size */,
  49. void * /* param_value */,
  50. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  51. /* Device APIs */
  52. CL_API_ENTRY cl_int CL_API_CALL
  53. (*clGetDeviceIDs)(cl_platform_id /* platform */,
  54. cl_device_type /* device_type */,
  55. cl_uint /* num_entries */,
  56. cl_device_id * /* devices */,
  57. cl_uint * /* num_devices */) CL_API_SUFFIX__VERSION_1_0;
  58. CL_API_ENTRY cl_int CL_API_CALL
  59. (*clGetDeviceInfo)(cl_device_id /* device */,
  60. cl_device_info /* param_name */,
  61. size_t /* param_value_size */,
  62. void * /* param_value */,
  63. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  64. /* Context APIs */
  65. CL_API_ENTRY cl_context CL_API_CALL
  66. (*clCreateContextFromType)(const cl_context_properties * /* properties */,
  67. cl_device_type /* device_type */,
  68. void (CL_CALLBACK * /* pfn_notify*/ )(const char *, const void *, size_t, void *),
  69. void * /* user_data */,
  70. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  71. CL_API_ENTRY cl_int CL_API_CALL
  72. (*clReleaseContext)(cl_context /* context */) CL_API_SUFFIX__VERSION_1_0;
  73. /* Command Queue APIs */
  74. CL_API_ENTRY cl_command_queue CL_API_CALL
  75. (*clCreateCommandQueue)(cl_context /* context */,
  76. cl_device_id /* device */,
  77. cl_command_queue_properties /* properties */,
  78. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  79. CL_API_ENTRY cl_int CL_API_CALL
  80. (*clReleaseCommandQueue)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  81. /* Memory Object APIs */
  82. CL_API_ENTRY cl_mem CL_API_CALL
  83. (*clCreateBuffer)(cl_context /* context */,
  84. cl_mem_flags /* flags */,
  85. size_t /* size */,
  86. void * /* host_ptr */,
  87. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  88. /* Program Object APIs */
  89. CL_API_ENTRY cl_program CL_API_CALL
  90. (*clCreateProgramWithSource)(cl_context /* context */,
  91. cl_uint /* count */,
  92. const char ** /* strings */,
  93. const size_t * /* lengths */,
  94. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  95. CL_API_ENTRY cl_program CL_API_CALL
  96. (*clCreateProgramWithBinary)(cl_context /* context */,
  97. cl_uint /* num_devices */,
  98. const cl_device_id * /* device_list */,
  99. const size_t * /* lengths */,
  100. const unsigned char ** /* binaries */,
  101. cl_int * /* binary_status */,
  102. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  103. CL_API_ENTRY cl_int CL_API_CALL
  104. (*clReleaseProgram)(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0;
  105. CL_API_ENTRY cl_int CL_API_CALL
  106. (*clBuildProgram)(cl_program /* program */,
  107. cl_uint /* num_devices */,
  108. const cl_device_id * /* device_list */,
  109. const char * /* options */,
  110. void (CL_CALLBACK * /* pfn_notify */)(cl_program /* program */, void * /* user_data */),
  111. void * /* user_data */) CL_API_SUFFIX__VERSION_1_0;
  112. CL_API_ENTRY cl_int CL_API_CALL
  113. (*clGetProgramInfo)(cl_program /* program */,
  114. cl_program_info /* param_name */,
  115. size_t /* param_value_size */,
  116. void * /* param_value */,
  117. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  118. CL_API_ENTRY cl_int CL_API_CALL
  119. (*clGetProgramBuildInfo)(cl_program /* program */,
  120. cl_device_id /* device */,
  121. cl_program_build_info /* param_name */,
  122. size_t /* param_value_size */,
  123. void * /* param_value */,
  124. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  125. /* Kernel Object APIs */
  126. CL_API_ENTRY cl_kernel CL_API_CALL
  127. (*clCreateKernel)(cl_program /* program */,
  128. const char * /* kernel_name */,
  129. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  130. CL_API_ENTRY cl_int CL_API_CALL
  131. (*clReleaseKernel)(cl_kernel /* kernel */) CL_API_SUFFIX__VERSION_1_0;
  132. CL_API_ENTRY cl_int CL_API_CALL
  133. (*clSetKernelArg)(cl_kernel /* kernel */,
  134. cl_uint /* arg_index */,
  135. size_t /* arg_size */,
  136. const void * /* arg_value */) CL_API_SUFFIX__VERSION_1_0;
  137. /* Flush and Finish APIs */
  138. CL_API_ENTRY cl_int CL_API_CALL
  139. (*clFinish)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  140. /* Enqueued Commands APIs */
  141. CL_API_ENTRY cl_int CL_API_CALL
  142. (*clEnqueueReadBuffer)(cl_command_queue /* command_queue */,
  143. cl_mem /* buffer */,
  144. cl_bool /* blocking_read */,
  145. size_t /* offset */,
  146. size_t /* size */,
  147. void * /* ptr */,
  148. cl_uint /* num_events_in_wait_list */,
  149. const cl_event * /* event_wait_list */,
  150. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  151. CL_API_ENTRY cl_int CL_API_CALL
  152. (*clEnqueueWriteBuffer)(cl_command_queue /* command_queue */,
  153. cl_mem /* buffer */,
  154. cl_bool /* blocking_write */,
  155. size_t /* offset */,
  156. size_t /* size */,
  157. const void * /* ptr */,
  158. cl_uint /* num_events_in_wait_list */,
  159. const cl_event * /* event_wait_list */,
  160. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  161. CL_API_ENTRY cl_int CL_API_CALL
  162. (*clEnqueueNDRangeKernel)(cl_command_queue /* command_queue */,
  163. cl_kernel /* kernel */,
  164. cl_uint /* work_dim */,
  165. const size_t * /* global_work_offset */,
  166. const size_t * /* global_work_size */,
  167. const size_t * /* local_work_size */,
  168. cl_uint /* num_events_in_wait_list */,
  169. const cl_event * /* event_wait_list */,
  170. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  171. #ifdef WIN32
  172. #define dlsym (void*)GetProcAddress
  173. #define dlclose FreeLibrary
  174. #endif
  175. #define LOAD_OCL_SYM(sym) do { \
  176. if (!(sym = dlsym(cl, #sym))) { \
  177. applog(LOG_ERR, "Failed to load OpenCL symbol " #sym ", no GPUs usable"); \
  178. dlclose(cl); \
  179. return false; \
  180. } \
  181. } while(0)
  182. static bool
  183. load_opencl_symbols() {
  184. #if defined(__APPLE__)
  185. void *cl = dlopen("/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL", RTLD_LAZY);
  186. #elif !defined(WIN32)
  187. void *cl = dlopen("libOpenCL.so", RTLD_LAZY);
  188. #else
  189. HMODULE cl = LoadLibrary("OpenCL.dll");
  190. #endif
  191. if (!cl)
  192. {
  193. applog(LOG_ERR, "Failed to load OpenCL library, no GPUs usable");
  194. return false;
  195. }
  196. LOAD_OCL_SYM(clGetPlatformIDs);
  197. LOAD_OCL_SYM(clGetPlatformInfo);
  198. LOAD_OCL_SYM(clGetDeviceIDs);
  199. LOAD_OCL_SYM(clGetDeviceInfo);
  200. LOAD_OCL_SYM(clCreateContextFromType);
  201. LOAD_OCL_SYM(clReleaseContext);
  202. LOAD_OCL_SYM(clCreateCommandQueue);
  203. LOAD_OCL_SYM(clReleaseCommandQueue);
  204. LOAD_OCL_SYM(clCreateBuffer);
  205. LOAD_OCL_SYM(clCreateProgramWithSource);
  206. LOAD_OCL_SYM(clCreateProgramWithBinary);
  207. LOAD_OCL_SYM(clReleaseProgram);
  208. LOAD_OCL_SYM(clBuildProgram);
  209. LOAD_OCL_SYM(clGetProgramInfo);
  210. LOAD_OCL_SYM(clGetProgramBuildInfo);
  211. LOAD_OCL_SYM(clCreateKernel);
  212. LOAD_OCL_SYM(clReleaseKernel);
  213. LOAD_OCL_SYM(clSetKernelArg);
  214. LOAD_OCL_SYM(clFinish);
  215. LOAD_OCL_SYM(clEnqueueReadBuffer);
  216. LOAD_OCL_SYM(clEnqueueWriteBuffer);
  217. LOAD_OCL_SYM(clEnqueueNDRangeKernel);
  218. return true;
  219. }
  220. #endif
  221. #ifdef HAVE_CURSES
  222. extern WINDOW *mainwin, *statuswin, *logwin;
  223. extern void enable_curses(void);
  224. #endif
  225. extern int mining_threads;
  226. extern double total_secs;
  227. extern int opt_g_threads;
  228. extern bool ping;
  229. extern bool opt_loginput;
  230. extern char *opt_kernel_path;
  231. extern int gpur_thr_id;
  232. extern bool opt_noadl;
  233. extern bool have_opencl;
  234. extern void *miner_thread(void *userdata);
  235. extern int dev_from_id(int thr_id);
  236. extern void decay_time(double *f, double fadd);
  237. /**********************************************/
  238. #ifdef HAVE_ADL
  239. extern float gpu_temp(int gpu);
  240. extern int gpu_fanspeed(int gpu);
  241. extern int gpu_fanpercent(int gpu);
  242. #endif
  243. #ifdef HAVE_OPENCL
  244. char *set_vector(char *arg)
  245. {
  246. int i, val = 0, device = 0;
  247. char *nextptr;
  248. nextptr = strtok(arg, ",");
  249. if (nextptr == NULL)
  250. return "Invalid parameters for set vector";
  251. val = atoi(nextptr);
  252. if (val != 1 && val != 2 && val != 4)
  253. return "Invalid value passed to set_vector";
  254. gpus[device++].vwidth = val;
  255. while ((nextptr = strtok(NULL, ",")) != NULL) {
  256. val = atoi(nextptr);
  257. if (val != 1 && val != 2 && val != 4)
  258. return "Invalid value passed to set_vector";
  259. gpus[device++].vwidth = val;
  260. }
  261. if (device == 1) {
  262. for (i = device; i < MAX_GPUDEVICES; i++)
  263. gpus[i].vwidth = gpus[0].vwidth;
  264. }
  265. return NULL;
  266. }
  267. char *set_worksize(char *arg)
  268. {
  269. int i, val = 0, device = 0;
  270. char *nextptr;
  271. nextptr = strtok(arg, ",");
  272. if (nextptr == NULL)
  273. return "Invalid parameters for set work size";
  274. val = atoi(nextptr);
  275. if (val < 1 || val > 9999)
  276. return "Invalid value passed to set_worksize";
  277. gpus[device++].work_size = val;
  278. while ((nextptr = strtok(NULL, ",")) != NULL) {
  279. val = atoi(nextptr);
  280. if (val < 1 || val > 9999)
  281. return "Invalid value passed to set_worksize";
  282. gpus[device++].work_size = val;
  283. }
  284. if (device == 1) {
  285. for (i = device; i < MAX_GPUDEVICES; i++)
  286. gpus[i].work_size = gpus[0].work_size;
  287. }
  288. return NULL;
  289. }
  290. #ifdef USE_SCRYPT
  291. char *set_shaders(char *arg)
  292. {
  293. int i, val = 0, device = 0;
  294. char *nextptr;
  295. nextptr = strtok(arg, ",");
  296. if (nextptr == NULL)
  297. return "Invalid parameters for set lookup gap";
  298. val = atoi(nextptr);
  299. gpus[device++].shaders = val;
  300. while ((nextptr = strtok(NULL, ",")) != NULL) {
  301. val = atoi(nextptr);
  302. gpus[device++].shaders = val;
  303. }
  304. if (device == 1) {
  305. for (i = device; i < MAX_GPUDEVICES; i++)
  306. gpus[i].shaders = gpus[0].shaders;
  307. }
  308. return NULL;
  309. }
  310. char *set_lookup_gap(char *arg)
  311. {
  312. int i, val = 0, device = 0;
  313. char *nextptr;
  314. nextptr = strtok(arg, ",");
  315. if (nextptr == NULL)
  316. return "Invalid parameters for set lookup gap";
  317. val = atoi(nextptr);
  318. gpus[device++].opt_lg = val;
  319. while ((nextptr = strtok(NULL, ",")) != NULL) {
  320. val = atoi(nextptr);
  321. gpus[device++].opt_lg = val;
  322. }
  323. if (device == 1) {
  324. for (i = device; i < MAX_GPUDEVICES; i++)
  325. gpus[i].opt_lg = gpus[0].opt_lg;
  326. }
  327. return NULL;
  328. }
  329. char *set_thread_concurrency(char *arg)
  330. {
  331. int i, val = 0, device = 0;
  332. char *nextptr;
  333. nextptr = strtok(arg, ",");
  334. if (nextptr == NULL)
  335. return "Invalid parameters for set thread concurrency";
  336. val = atoi(nextptr);
  337. gpus[device++].opt_tc = val;
  338. while ((nextptr = strtok(NULL, ",")) != NULL) {
  339. val = atoi(nextptr);
  340. gpus[device++].opt_tc = val;
  341. }
  342. if (device == 1) {
  343. for (i = device; i < MAX_GPUDEVICES; i++)
  344. gpus[i].opt_tc = gpus[0].opt_tc;
  345. }
  346. return NULL;
  347. }
  348. #endif
  349. static enum cl_kernels select_kernel(char *arg)
  350. {
  351. if (!strcmp(arg, "diablo"))
  352. return KL_DIABLO;
  353. if (!strcmp(arg, "diakgcn"))
  354. return KL_DIAKGCN;
  355. if (!strcmp(arg, "poclbm"))
  356. return KL_POCLBM;
  357. if (!strcmp(arg, "phatk"))
  358. return KL_PHATK;
  359. #ifdef USE_SCRYPT
  360. if (!strcmp(arg, "scrypt"))
  361. return KL_SCRYPT;
  362. #endif
  363. return KL_NONE;
  364. }
  365. char *set_kernel(char *arg)
  366. {
  367. enum cl_kernels kern;
  368. int i, device = 0;
  369. char *nextptr;
  370. if (opt_scrypt)
  371. return "Cannot use sha256 kernel with scrypt";
  372. nextptr = strtok(arg, ",");
  373. if (nextptr == NULL)
  374. return "Invalid parameters for set kernel";
  375. kern = select_kernel(nextptr);
  376. if (kern == KL_NONE)
  377. return "Invalid parameter to set_kernel";
  378. gpus[device++].kernel = kern;
  379. while ((nextptr = strtok(NULL, ",")) != NULL) {
  380. kern = select_kernel(nextptr);
  381. if (kern == KL_NONE)
  382. return "Invalid parameter to set_kernel";
  383. gpus[device++].kernel = kern;
  384. }
  385. if (device == 1) {
  386. for (i = device; i < MAX_GPUDEVICES; i++)
  387. gpus[i].kernel = gpus[0].kernel;
  388. }
  389. return NULL;
  390. }
  391. #endif
  392. #ifdef HAVE_ADL
  393. /* This function allows us to map an adl device to an opencl device for when
  394. * simple enumeration has failed to match them. */
  395. char *set_gpu_map(char *arg)
  396. {
  397. int val1 = 0, val2 = 0;
  398. char *nextptr;
  399. nextptr = strtok(arg, ",");
  400. if (nextptr == NULL)
  401. return "Invalid parameters for set gpu map";
  402. if (sscanf(arg, "%d:%d", &val1, &val2) != 2)
  403. return "Invalid description for map pair";
  404. if (val1 < 0 || val1 > MAX_GPUDEVICES || val2 < 0 || val2 > MAX_GPUDEVICES)
  405. return "Invalid value passed to set_gpu_map";
  406. gpus[val1].virtual_adl = val2;
  407. gpus[val1].mapped = true;
  408. while ((nextptr = strtok(NULL, ",")) != NULL) {
  409. if (sscanf(nextptr, "%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. }
  416. return NULL;
  417. }
  418. void get_intrange(char *arg, int *val1, int *val2)
  419. {
  420. if (sscanf(arg, "%d-%d", val1, val2) == 1) {
  421. *val2 = *val1;
  422. *val1 = 0;
  423. }
  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_api opencl_api;
  647. char *print_ndevs_and_exit(int *ndevs)
  648. {
  649. opt_log_output = true;
  650. opencl_api.api_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 = &thr_info[i];
  668. if (!thr->pause && cgpu->dynamic) {
  669. applog(LOG_WARNING, "Disabling extra threads due to dynamic mode.");
  670. applog(LOG_WARNING, "Tune dynamic intensity with --gpu-dyninterval");
  671. }
  672. thr->pause = cgpu->dynamic;
  673. if (!cgpu->dynamic && cgpu->deven != DEV_DISABLED)
  674. tq_push(thr->q, &ping);
  675. }
  676. }
  677. struct device_api opencl_api;
  678. #endif /* HAVE_OPENCL */
  679. #if defined(HAVE_OPENCL) && defined(HAVE_CURSES)
  680. void manage_gpu(void)
  681. {
  682. struct thr_info *thr;
  683. int selected, gpu, i;
  684. char checkin[40];
  685. char input;
  686. if (!opt_g_threads)
  687. return;
  688. opt_loginput = true;
  689. immedok(logwin, true);
  690. clear_logwin();
  691. retry:
  692. for (gpu = 0; gpu < nDevs; gpu++) {
  693. struct cgpu_info *cgpu = &gpus[gpu];
  694. double displayed_rolling, displayed_total;
  695. bool mhash_base = true;
  696. displayed_rolling = cgpu->rolling;
  697. displayed_total = cgpu->total_mhashes / total_secs;
  698. if (displayed_rolling < 1) {
  699. displayed_rolling *= 1000;
  700. displayed_total *= 1000;
  701. mhash_base = false;
  702. }
  703. wlog("GPU %d: %.1f / %.1f %sh/s | A:%d R:%d HW:%d U:%.2f/m I:%d\n",
  704. gpu, displayed_rolling, displayed_total, mhash_base ? "M" : "K",
  705. cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
  706. cgpu->utility, cgpu->intensity);
  707. #ifdef HAVE_ADL
  708. if (gpus[gpu].has_adl) {
  709. int engineclock = 0, memclock = 0, activity = 0, fanspeed = 0, fanpercent = 0, powertune = 0;
  710. float temp = 0, vddc = 0;
  711. if (gpu_stats(gpu, &temp, &engineclock, &memclock, &vddc, &activity, &fanspeed, &fanpercent, &powertune)) {
  712. char logline[255];
  713. strcpy(logline, ""); // In case it has no data
  714. if (temp != -1)
  715. sprintf(logline, "%.1f C ", temp);
  716. if (fanspeed != -1 || fanpercent != -1) {
  717. tailsprintf(logline, "F: ");
  718. if (fanpercent != -1)
  719. tailsprintf(logline, "%d%% ", fanpercent);
  720. if (fanspeed != -1)
  721. tailsprintf(logline, "(%d RPM) ", fanspeed);
  722. tailsprintf(logline, " ");
  723. }
  724. if (engineclock != -1)
  725. tailsprintf(logline, "E: %d MHz ", engineclock);
  726. if (memclock != -1)
  727. tailsprintf(logline, "M: %d MHz ", memclock);
  728. if (vddc != -1)
  729. tailsprintf(logline, "V: %.3fV ", vddc);
  730. if (activity != -1)
  731. tailsprintf(logline, "A: %d%% ", activity);
  732. if (powertune != -1)
  733. tailsprintf(logline, "P: %d%%", powertune);
  734. tailsprintf(logline, "\n");
  735. wlog("%s", logline);
  736. }
  737. }
  738. #endif
  739. wlog("Last initialised: %s\n", cgpu->init);
  740. wlog("Intensity: ");
  741. if (gpus[gpu].dynamic)
  742. wlog("Dynamic (only one thread in use)\n");
  743. else
  744. wlog("%d\n", gpus[gpu].intensity);
  745. for (i = 0; i < mining_threads; i++) {
  746. thr = &thr_info[i];
  747. if (thr->cgpu != cgpu)
  748. continue;
  749. get_datestamp(checkin, &thr->last);
  750. displayed_rolling = thr->rolling;
  751. if (!mhash_base)
  752. displayed_rolling *= 1000;
  753. wlog("Thread %d: %.1f %sh/s %s ", i, displayed_rolling, mhash_base ? "M" : "K" , cgpu->deven != DEV_DISABLED ? "Enabled" : "Disabled");
  754. switch (cgpu->status) {
  755. default:
  756. case LIFE_WELL:
  757. wlog("ALIVE");
  758. break;
  759. case LIFE_SICK:
  760. wlog("SICK reported in %s", checkin);
  761. break;
  762. case LIFE_DEAD:
  763. wlog("DEAD reported in %s", checkin);
  764. break;
  765. case LIFE_INIT:
  766. case LIFE_NOSTART:
  767. wlog("Never started");
  768. break;
  769. }
  770. if (thr->pause)
  771. wlog(" paused");
  772. wlog("\n");
  773. }
  774. wlog("\n");
  775. }
  776. wlogprint("[E]nable [D]isable [I]ntensity [R]estart GPU %s\n",adl_active ? "[C]hange settings" : "");
  777. wlogprint("Or press any other key to continue\n");
  778. input = getch();
  779. if (nDevs == 1)
  780. selected = 0;
  781. else
  782. selected = -1;
  783. if (!strncasecmp(&input, "e", 1)) {
  784. struct cgpu_info *cgpu;
  785. if (selected)
  786. selected = curses_int("Select GPU to enable");
  787. if (selected < 0 || selected >= nDevs) {
  788. wlogprint("Invalid selection\n");
  789. goto retry;
  790. }
  791. if (gpus[selected].deven != DEV_DISABLED) {
  792. wlogprint("Device already enabled\n");
  793. goto retry;
  794. }
  795. gpus[selected].deven = DEV_ENABLED;
  796. for (i = 0; i < mining_threads; ++i) {
  797. thr = &thr_info[i];
  798. cgpu = thr->cgpu;
  799. if (cgpu->api != &opencl_api)
  800. continue;
  801. if (dev_from_id(i) != selected)
  802. continue;
  803. if (cgpu->status != LIFE_WELL) {
  804. wlogprint("Must restart device before enabling it");
  805. goto retry;
  806. }
  807. applog(LOG_DEBUG, "Pushing ping to thread %d", thr->id);
  808. tq_push(thr->q, &ping);
  809. }
  810. goto retry;
  811. } if (!strncasecmp(&input, "d", 1)) {
  812. if (selected)
  813. selected = curses_int("Select GPU to disable");
  814. if (selected < 0 || selected >= nDevs) {
  815. wlogprint("Invalid selection\n");
  816. goto retry;
  817. }
  818. if (gpus[selected].deven == DEV_DISABLED) {
  819. wlogprint("Device already disabled\n");
  820. goto retry;
  821. }
  822. gpus[selected].deven = DEV_DISABLED;
  823. goto retry;
  824. } else if (!strncasecmp(&input, "i", 1)) {
  825. int intensity;
  826. char *intvar;
  827. if (selected)
  828. selected = curses_int("Select GPU to change intensity on");
  829. if (selected < 0 || selected >= nDevs) {
  830. wlogprint("Invalid selection\n");
  831. goto retry;
  832. }
  833. intvar = curses_input("Set GPU scan intensity (d or " _MIN_INTENSITY_STR " -> " _MAX_INTENSITY_STR ")");
  834. if (!intvar) {
  835. wlogprint("Invalid input\n");
  836. goto retry;
  837. }
  838. if (!strncasecmp(intvar, "d", 1)) {
  839. wlogprint("Dynamic mode enabled on gpu %d\n", selected);
  840. gpus[selected].dynamic = true;
  841. pause_dynamic_threads(selected);
  842. free(intvar);
  843. goto retry;
  844. }
  845. intensity = atoi(intvar);
  846. free(intvar);
  847. if (intensity < MIN_INTENSITY || intensity > MAX_INTENSITY) {
  848. wlogprint("Invalid selection\n");
  849. goto retry;
  850. }
  851. gpus[selected].dynamic = false;
  852. gpus[selected].intensity = intensity;
  853. wlogprint("Intensity on gpu %d set to %d\n", selected, intensity);
  854. pause_dynamic_threads(selected);
  855. goto retry;
  856. } else if (!strncasecmp(&input, "r", 1)) {
  857. if (selected)
  858. selected = curses_int("Select GPU to attempt to restart");
  859. if (selected < 0 || selected >= nDevs) {
  860. wlogprint("Invalid selection\n");
  861. goto retry;
  862. }
  863. wlogprint("Attempting to restart threads of GPU %d\n", selected);
  864. reinit_device(&gpus[selected]);
  865. goto retry;
  866. } else if (adl_active && (!strncasecmp(&input, "c", 1))) {
  867. if (selected)
  868. selected = curses_int("Select GPU to change settings on");
  869. if (selected < 0 || selected >= nDevs) {
  870. wlogprint("Invalid selection\n");
  871. goto retry;
  872. }
  873. change_gpusettings(selected);
  874. goto retry;
  875. } else
  876. clear_logwin();
  877. immedok(logwin, false);
  878. opt_loginput = false;
  879. }
  880. #else
  881. void manage_gpu(void)
  882. {
  883. }
  884. #endif
  885. #ifdef HAVE_OPENCL
  886. static _clState *clStates[MAX_GPUDEVICES];
  887. #define CL_SET_BLKARG(blkvar) status |= clSetKernelArg(*kernel, num++, sizeof(uint), (void *)&blk->blkvar)
  888. #define CL_SET_ARG(var) status |= clSetKernelArg(*kernel, num++, sizeof(var), (void *)&var)
  889. #define CL_SET_VARG(args, var) status |= clSetKernelArg(*kernel, num++, args * sizeof(uint), (void *)var)
  890. static cl_int queue_poclbm_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  891. {
  892. cl_kernel *kernel = &clState->kernel;
  893. unsigned int num = 0;
  894. cl_int status = 0;
  895. CL_SET_BLKARG(ctx_a);
  896. CL_SET_BLKARG(ctx_b);
  897. CL_SET_BLKARG(ctx_c);
  898. CL_SET_BLKARG(ctx_d);
  899. CL_SET_BLKARG(ctx_e);
  900. CL_SET_BLKARG(ctx_f);
  901. CL_SET_BLKARG(ctx_g);
  902. CL_SET_BLKARG(ctx_h);
  903. CL_SET_BLKARG(cty_b);
  904. CL_SET_BLKARG(cty_c);
  905. CL_SET_BLKARG(cty_f);
  906. CL_SET_BLKARG(cty_g);
  907. CL_SET_BLKARG(cty_h);
  908. if (!clState->goffset) {
  909. cl_uint vwidth = clState->vwidth;
  910. uint *nonces = alloca(sizeof(uint) * vwidth);
  911. unsigned int i;
  912. for (i = 0; i < vwidth; i++)
  913. nonces[i] = blk->nonce + (i * threads);
  914. CL_SET_VARG(vwidth, nonces);
  915. }
  916. CL_SET_BLKARG(fW0);
  917. CL_SET_BLKARG(fW1);
  918. CL_SET_BLKARG(fW2);
  919. CL_SET_BLKARG(fW3);
  920. CL_SET_BLKARG(fW15);
  921. CL_SET_BLKARG(fW01r);
  922. CL_SET_BLKARG(D1A);
  923. CL_SET_BLKARG(C1addK5);
  924. CL_SET_BLKARG(B1addK6);
  925. CL_SET_BLKARG(W16addK16);
  926. CL_SET_BLKARG(W17addK17);
  927. CL_SET_BLKARG(PreVal4addT1);
  928. CL_SET_BLKARG(PreVal0);
  929. CL_SET_ARG(clState->outputBuffer);
  930. return status;
  931. }
  932. static cl_int queue_phatk_kernel(_clState *clState, dev_blk_ctx *blk,
  933. __maybe_unused cl_uint threads)
  934. {
  935. cl_kernel *kernel = &clState->kernel;
  936. cl_uint vwidth = clState->vwidth;
  937. unsigned int i, num = 0;
  938. cl_int status = 0;
  939. uint *nonces;
  940. CL_SET_BLKARG(ctx_a);
  941. CL_SET_BLKARG(ctx_b);
  942. CL_SET_BLKARG(ctx_c);
  943. CL_SET_BLKARG(ctx_d);
  944. CL_SET_BLKARG(ctx_e);
  945. CL_SET_BLKARG(ctx_f);
  946. CL_SET_BLKARG(ctx_g);
  947. CL_SET_BLKARG(ctx_h);
  948. CL_SET_BLKARG(cty_b);
  949. CL_SET_BLKARG(cty_c);
  950. CL_SET_BLKARG(cty_d);
  951. CL_SET_BLKARG(cty_f);
  952. CL_SET_BLKARG(cty_g);
  953. CL_SET_BLKARG(cty_h);
  954. nonces = alloca(sizeof(uint) * vwidth);
  955. for (i = 0; i < vwidth; i++)
  956. nonces[i] = blk->nonce + i;
  957. CL_SET_VARG(vwidth, nonces);
  958. CL_SET_BLKARG(W16);
  959. CL_SET_BLKARG(W17);
  960. CL_SET_BLKARG(PreVal4_2);
  961. CL_SET_BLKARG(PreVal0);
  962. CL_SET_BLKARG(PreW18);
  963. CL_SET_BLKARG(PreW19);
  964. CL_SET_BLKARG(PreW31);
  965. CL_SET_BLKARG(PreW32);
  966. CL_SET_ARG(clState->outputBuffer);
  967. return status;
  968. }
  969. static cl_int queue_diakgcn_kernel(_clState *clState, dev_blk_ctx *blk,
  970. __maybe_unused cl_uint threads)
  971. {
  972. cl_kernel *kernel = &clState->kernel;
  973. unsigned int num = 0;
  974. cl_int status = 0;
  975. if (!clState->goffset) {
  976. cl_uint vwidth = clState->vwidth;
  977. uint *nonces = alloca(sizeof(uint) * vwidth);
  978. unsigned int i;
  979. for (i = 0; i < vwidth; i++)
  980. nonces[i] = blk->nonce + i;
  981. CL_SET_VARG(vwidth, nonces);
  982. }
  983. CL_SET_BLKARG(PreVal0);
  984. CL_SET_BLKARG(PreVal4_2);
  985. CL_SET_BLKARG(cty_h);
  986. CL_SET_BLKARG(D1A);
  987. CL_SET_BLKARG(cty_b);
  988. CL_SET_BLKARG(cty_c);
  989. CL_SET_BLKARG(cty_f);
  990. CL_SET_BLKARG(cty_g);
  991. CL_SET_BLKARG(C1addK5);
  992. CL_SET_BLKARG(B1addK6);
  993. CL_SET_BLKARG(PreVal0addK7);
  994. CL_SET_BLKARG(W16addK16);
  995. CL_SET_BLKARG(W17addK17);
  996. CL_SET_BLKARG(PreW18);
  997. CL_SET_BLKARG(PreW19);
  998. CL_SET_BLKARG(W16);
  999. CL_SET_BLKARG(W17);
  1000. CL_SET_BLKARG(PreW31);
  1001. CL_SET_BLKARG(PreW32);
  1002. CL_SET_BLKARG(ctx_a);
  1003. CL_SET_BLKARG(ctx_b);
  1004. CL_SET_BLKARG(ctx_c);
  1005. CL_SET_BLKARG(ctx_d);
  1006. CL_SET_BLKARG(ctx_e);
  1007. CL_SET_BLKARG(ctx_f);
  1008. CL_SET_BLKARG(ctx_g);
  1009. CL_SET_BLKARG(ctx_h);
  1010. CL_SET_BLKARG(zeroA);
  1011. CL_SET_BLKARG(zeroB);
  1012. CL_SET_BLKARG(oneA);
  1013. CL_SET_BLKARG(twoA);
  1014. CL_SET_BLKARG(threeA);
  1015. CL_SET_BLKARG(fourA);
  1016. CL_SET_BLKARG(fiveA);
  1017. CL_SET_BLKARG(sixA);
  1018. CL_SET_BLKARG(sevenA);
  1019. CL_SET_ARG(clState->outputBuffer);
  1020. return status;
  1021. }
  1022. static cl_int queue_diablo_kernel(_clState *clState, dev_blk_ctx *blk, cl_uint threads)
  1023. {
  1024. cl_kernel *kernel = &clState->kernel;
  1025. unsigned int num = 0;
  1026. cl_int status = 0;
  1027. if (!clState->goffset) {
  1028. cl_uint vwidth = clState->vwidth;
  1029. uint *nonces = alloca(sizeof(uint) * vwidth);
  1030. unsigned int i;
  1031. for (i = 0; i < vwidth; i++)
  1032. nonces[i] = blk->nonce + (i * threads);
  1033. CL_SET_VARG(vwidth, nonces);
  1034. }
  1035. CL_SET_BLKARG(PreVal0);
  1036. CL_SET_BLKARG(PreVal0addK7);
  1037. CL_SET_BLKARG(PreVal4addT1);
  1038. CL_SET_BLKARG(PreW18);
  1039. CL_SET_BLKARG(PreW19);
  1040. CL_SET_BLKARG(W16);
  1041. CL_SET_BLKARG(W17);
  1042. CL_SET_BLKARG(W16addK16);
  1043. CL_SET_BLKARG(W17addK17);
  1044. CL_SET_BLKARG(PreW31);
  1045. CL_SET_BLKARG(PreW32);
  1046. CL_SET_BLKARG(D1A);
  1047. CL_SET_BLKARG(cty_b);
  1048. CL_SET_BLKARG(cty_c);
  1049. CL_SET_BLKARG(cty_h);
  1050. CL_SET_BLKARG(cty_f);
  1051. CL_SET_BLKARG(cty_g);
  1052. CL_SET_BLKARG(C1addK5);
  1053. CL_SET_BLKARG(B1addK6);
  1054. CL_SET_BLKARG(ctx_a);
  1055. CL_SET_BLKARG(ctx_b);
  1056. CL_SET_BLKARG(ctx_c);
  1057. CL_SET_BLKARG(ctx_d);
  1058. CL_SET_BLKARG(ctx_e);
  1059. CL_SET_BLKARG(ctx_f);
  1060. CL_SET_BLKARG(ctx_g);
  1061. CL_SET_BLKARG(ctx_h);
  1062. CL_SET_ARG(clState->outputBuffer);
  1063. return status;
  1064. }
  1065. #ifdef USE_SCRYPT
  1066. static cl_int queue_scrypt_kernel(_clState *clState, dev_blk_ctx *blk, __maybe_unused cl_uint threads)
  1067. {
  1068. unsigned char *midstate = blk->work->midstate;
  1069. cl_kernel *kernel = &clState->kernel;
  1070. unsigned int num = 0;
  1071. cl_uint le_target;
  1072. cl_int status = 0;
  1073. le_target = *(cl_uint *)(blk->work->target + 28);
  1074. clState->cldata = blk->work->data;
  1075. status = clEnqueueWriteBuffer(clState->commandQueue, clState->CLbuffer0, true, 0, 80, clState->cldata, 0, NULL,NULL);
  1076. CL_SET_ARG(clState->CLbuffer0);
  1077. CL_SET_ARG(clState->outputBuffer);
  1078. CL_SET_ARG(clState->padbuffer8);
  1079. CL_SET_VARG(4, &midstate[0]);
  1080. CL_SET_VARG(4, &midstate[16]);
  1081. CL_SET_ARG(le_target);
  1082. return status;
  1083. }
  1084. #endif
  1085. static void set_threads_hashes(unsigned int vectors,int64_t *hashes, size_t *globalThreads,
  1086. unsigned int minthreads, __maybe_unused int *intensity)
  1087. {
  1088. unsigned int threads = 0;
  1089. while (threads < minthreads) {
  1090. threads = 1 << ((opt_scrypt ? 0 : 15) + *intensity);
  1091. if (threads < minthreads) {
  1092. if (likely(*intensity < MAX_INTENSITY))
  1093. (*intensity)++;
  1094. else
  1095. threads = minthreads;
  1096. }
  1097. }
  1098. *globalThreads = threads;
  1099. *hashes = threads * vectors;
  1100. }
  1101. #endif /* HAVE_OPENCL */
  1102. #ifdef HAVE_OPENCL
  1103. /* We have only one thread that ever re-initialises GPUs, thus if any GPU
  1104. * init command fails due to a completely wedged GPU, the thread will never
  1105. * return, unable to harm other GPUs. If it does return, it means we only had
  1106. * a soft failure and then the reinit_gpu thread is ready to tackle another
  1107. * GPU */
  1108. void *reinit_gpu(void *userdata)
  1109. {
  1110. struct thr_info *mythr = userdata;
  1111. struct cgpu_info *cgpu, *sel_cgpu;
  1112. struct thr_info *thr;
  1113. struct timeval now;
  1114. char name[256];
  1115. int thr_id;
  1116. int gpu;
  1117. pthread_detach(pthread_self());
  1118. RenameThread("reinit_gpu");
  1119. select_cgpu:
  1120. sel_cgpu =
  1121. cgpu = tq_pop(mythr->q, NULL);
  1122. if (!cgpu)
  1123. goto out;
  1124. if (clDevicesNum() != nDevs) {
  1125. applog(LOG_WARNING, "Hardware not reporting same number of active devices, will not attempt to restart GPU");
  1126. goto out;
  1127. }
  1128. gpu = cgpu->device_id;
  1129. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  1130. thr = &thr_info[thr_id];
  1131. cgpu = thr->cgpu;
  1132. if (cgpu->api != &opencl_api)
  1133. continue;
  1134. if (dev_from_id(thr_id) != gpu)
  1135. continue;
  1136. thr = &thr_info[thr_id];
  1137. if (!thr) {
  1138. applog(LOG_WARNING, "No reference to thread %d exists", thr_id);
  1139. continue;
  1140. }
  1141. thr->rolling = thr->cgpu->rolling = 0;
  1142. /* Reports the last time we tried to revive a sick GPU */
  1143. gettimeofday(&thr->sick, NULL);
  1144. if (!pthread_cancel(thr->pth)) {
  1145. applog(LOG_WARNING, "Thread %d still exists, killing it off", thr_id);
  1146. } else
  1147. applog(LOG_WARNING, "Thread %d no longer exists", thr_id);
  1148. }
  1149. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  1150. int virtual_gpu;
  1151. thr = &thr_info[thr_id];
  1152. cgpu = thr->cgpu;
  1153. if (cgpu->api != &opencl_api)
  1154. continue;
  1155. if (dev_from_id(thr_id) != gpu)
  1156. continue;
  1157. virtual_gpu = cgpu->virtual_gpu;
  1158. /* Lose this ram cause we may get stuck here! */
  1159. //tq_freeze(thr->q);
  1160. thr->q = tq_new();
  1161. if (!thr->q)
  1162. quit(1, "Failed to tq_new in reinit_gpu");
  1163. /* Lose this ram cause we may dereference in the dying thread! */
  1164. //free(clState);
  1165. applog(LOG_INFO, "Reinit GPU thread %d", thr_id);
  1166. clStates[thr_id] = initCl(virtual_gpu, name, sizeof(name));
  1167. if (!clStates[thr_id]) {
  1168. applog(LOG_ERR, "Failed to reinit GPU thread %d", thr_id);
  1169. goto select_cgpu;
  1170. }
  1171. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1172. if (unlikely(thr_info_create(thr, NULL, miner_thread, thr))) {
  1173. applog(LOG_ERR, "thread %d create failed", thr_id);
  1174. return NULL;
  1175. }
  1176. applog(LOG_WARNING, "Thread %d restarted", thr_id);
  1177. }
  1178. gettimeofday(&now, NULL);
  1179. get_datestamp(sel_cgpu->init, &now);
  1180. for (thr_id = 0; thr_id < mining_threads; ++thr_id) {
  1181. thr = &thr_info[thr_id];
  1182. cgpu = thr->cgpu;
  1183. if (cgpu->api != &opencl_api)
  1184. continue;
  1185. if (dev_from_id(thr_id) != gpu)
  1186. continue;
  1187. tq_push(thr->q, &ping);
  1188. }
  1189. goto select_cgpu;
  1190. out:
  1191. return NULL;
  1192. }
  1193. #else
  1194. void *reinit_gpu(__maybe_unused void *userdata)
  1195. {
  1196. return NULL;
  1197. }
  1198. #endif
  1199. #ifdef HAVE_OPENCL
  1200. struct device_api opencl_api;
  1201. static void opencl_detect()
  1202. {
  1203. #ifndef WIN32
  1204. if (!getenv("DISPLAY")) {
  1205. applog(LOG_DEBUG, "DISPLAY not set, setting :0 just in case");
  1206. setenv("DISPLAY", ":0", 1);
  1207. }
  1208. #endif
  1209. if (!load_opencl_symbols()) {
  1210. nDevs = 0;
  1211. return;
  1212. }
  1213. int i;
  1214. nDevs = clDevicesNum();
  1215. if (nDevs < 0) {
  1216. applog(LOG_ERR, "clDevicesNum returned error, no GPUs usable");
  1217. nDevs = 0;
  1218. }
  1219. if (!nDevs)
  1220. return;
  1221. for (i = 0; i < nDevs; ++i) {
  1222. struct cgpu_info *cgpu;
  1223. cgpu = &gpus[i];
  1224. cgpu->devtype = "GPU";
  1225. cgpu->deven = DEV_ENABLED;
  1226. cgpu->api = &opencl_api;
  1227. cgpu->device_id = i;
  1228. cgpu->threads = opt_g_threads;
  1229. cgpu->virtual_gpu = i;
  1230. add_cgpu(cgpu);
  1231. }
  1232. if (!opt_noadl)
  1233. init_adl(nDevs);
  1234. }
  1235. static void reinit_opencl_device(struct cgpu_info *gpu)
  1236. {
  1237. tq_push(thr_info[gpur_thr_id].q, gpu);
  1238. }
  1239. #ifdef HAVE_ADL
  1240. static void get_opencl_statline_before(char *buf, struct cgpu_info *gpu)
  1241. {
  1242. if (gpu->has_adl) {
  1243. int gpuid = gpu->device_id;
  1244. float gt = gpu_temp(gpuid);
  1245. int gf = gpu_fanspeed(gpuid);
  1246. int gp;
  1247. if (gt != -1)
  1248. tailsprintf(buf, "%5.1fC ", gt);
  1249. else
  1250. tailsprintf(buf, " ");
  1251. if (gf != -1)
  1252. // show invalid as 9999
  1253. tailsprintf(buf, "%4dRPM ", gf > 9999 ? 9999 : gf);
  1254. else if ((gp = gpu_fanpercent(gpuid)) != -1)
  1255. tailsprintf(buf, "%3d%% ", gp);
  1256. else
  1257. tailsprintf(buf, " ");
  1258. tailsprintf(buf, "| ");
  1259. }
  1260. else
  1261. tailsprintf(buf, " | ");
  1262. }
  1263. #endif
  1264. static struct api_data*
  1265. get_opencl_api_extra_device_status(struct cgpu_info *gpu)
  1266. {
  1267. struct api_data*root = NULL;
  1268. float gt, gv;
  1269. int ga, gf, gp, gc, gm, pt;
  1270. #ifdef HAVE_ADL
  1271. if (!gpu_stats(gpu->device_id, &gt, &gc, &gm, &gv, &ga, &gf, &gp, &pt))
  1272. #endif
  1273. gt = gv = gm = gc = ga = gf = gp = pt = 0;
  1274. root = api_add_int(root, "Fan Speed", &gf, true);
  1275. root = api_add_int(root, "Fan Percent", &gp, true);
  1276. root = api_add_int(root, "GPU Clock", &gc, true);
  1277. root = api_add_int(root, "Memory Clock", &gm, true);
  1278. root = api_add_volts(root, "GPU Voltage", &gv, true);
  1279. root = api_add_int(root, "GPU Activity", &ga, true);
  1280. root = api_add_int(root, "Powertune", &pt, true);
  1281. char intensity[20];
  1282. if (gpu->dynamic)
  1283. strcpy(intensity, "D");
  1284. else
  1285. sprintf(intensity, "%d", gpu->intensity);
  1286. root = api_add_string(root, "Intensity", intensity, true);
  1287. return root;
  1288. }
  1289. struct opencl_thread_data {
  1290. cl_int (*queue_kernel_parameters)(_clState *, dev_blk_ctx *, cl_uint);
  1291. uint32_t *res;
  1292. };
  1293. static uint32_t *blank_res;
  1294. static bool opencl_thread_prepare(struct thr_info *thr)
  1295. {
  1296. char name[256];
  1297. struct timeval now;
  1298. struct cgpu_info *cgpu = thr->cgpu;
  1299. int gpu = cgpu->device_id;
  1300. int virtual_gpu = cgpu->virtual_gpu;
  1301. int i = thr->id;
  1302. static bool failmessage = false;
  1303. if (!blank_res)
  1304. blank_res = calloc(BUFFERSIZE, 1);
  1305. if (!blank_res) {
  1306. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1307. return false;
  1308. }
  1309. strcpy(name, "");
  1310. applog(LOG_INFO, "Init GPU thread %i GPU %i virtual GPU %i", i, gpu, virtual_gpu);
  1311. clStates[i] = initCl(virtual_gpu, name, sizeof(name));
  1312. if (!clStates[i]) {
  1313. #ifdef HAVE_CURSES
  1314. if (use_curses)
  1315. enable_curses();
  1316. #endif
  1317. applog(LOG_ERR, "Failed to init GPU thread %d, disabling device %d", i, gpu);
  1318. if (!failmessage) {
  1319. applog(LOG_ERR, "Restarting the GPU from the menu will not fix this.");
  1320. applog(LOG_ERR, "Try restarting BFGMiner.");
  1321. failmessage = true;
  1322. #ifdef HAVE_CURSES
  1323. char *buf;
  1324. if (use_curses) {
  1325. buf = curses_input("Press enter to continue");
  1326. if (buf)
  1327. free(buf);
  1328. }
  1329. #endif
  1330. }
  1331. cgpu->deven = DEV_DISABLED;
  1332. cgpu->status = LIFE_NOSTART;
  1333. dev_error(cgpu, REASON_DEV_NOSTART);
  1334. return false;
  1335. }
  1336. if (!cgpu->name)
  1337. cgpu->name = strdup(name);
  1338. if (!cgpu->kname)
  1339. {
  1340. switch (clStates[i]->chosen_kernel) {
  1341. case KL_DIABLO:
  1342. cgpu->kname = "diablo";
  1343. break;
  1344. case KL_DIAKGCN:
  1345. cgpu->kname = "diakgcn";
  1346. break;
  1347. case KL_PHATK:
  1348. cgpu->kname = "phatk";
  1349. break;
  1350. #ifdef USE_SCRYPT
  1351. case KL_SCRYPT:
  1352. cgpu->kname = "scrypt";
  1353. break;
  1354. #endif
  1355. case KL_POCLBM:
  1356. cgpu->kname = "poclbm";
  1357. break;
  1358. default:
  1359. break;
  1360. }
  1361. }
  1362. applog(LOG_INFO, "initCl() finished. Found %s", name);
  1363. gettimeofday(&now, NULL);
  1364. get_datestamp(cgpu->init, &now);
  1365. have_opencl = true;
  1366. return true;
  1367. }
  1368. static bool opencl_thread_init(struct thr_info *thr)
  1369. {
  1370. const int thr_id = thr->id;
  1371. struct cgpu_info *gpu = thr->cgpu;
  1372. struct opencl_thread_data *thrdata;
  1373. _clState *clState = clStates[thr_id];
  1374. cl_int status = 0;
  1375. thrdata = calloc(1, sizeof(*thrdata));
  1376. thr->cgpu_data = thrdata;
  1377. if (!thrdata) {
  1378. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1379. return false;
  1380. }
  1381. switch (clState->chosen_kernel) {
  1382. case KL_POCLBM:
  1383. thrdata->queue_kernel_parameters = &queue_poclbm_kernel;
  1384. break;
  1385. case KL_PHATK:
  1386. thrdata->queue_kernel_parameters = &queue_phatk_kernel;
  1387. break;
  1388. case KL_DIAKGCN:
  1389. thrdata->queue_kernel_parameters = &queue_diakgcn_kernel;
  1390. break;
  1391. #ifdef USE_SCRYPT
  1392. case KL_SCRYPT:
  1393. thrdata->queue_kernel_parameters = &queue_scrypt_kernel;
  1394. break;
  1395. #endif
  1396. default:
  1397. case KL_DIABLO:
  1398. thrdata->queue_kernel_parameters = &queue_diablo_kernel;
  1399. break;
  1400. }
  1401. thrdata->res = calloc(BUFFERSIZE, 1);
  1402. if (!thrdata->res) {
  1403. free(thrdata);
  1404. applog(LOG_ERR, "Failed to calloc in opencl_thread_init");
  1405. return false;
  1406. }
  1407. status |= clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_TRUE, 0,
  1408. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1409. if (unlikely(status != CL_SUCCESS)) {
  1410. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1411. return false;
  1412. }
  1413. gpu->status = LIFE_WELL;
  1414. gpu->device_last_well = time(NULL);
  1415. return true;
  1416. }
  1417. static bool opencl_prepare_work(struct thr_info __maybe_unused *thr, struct work *work)
  1418. {
  1419. #ifdef USE_SCRYPT
  1420. if (opt_scrypt)
  1421. work->blk.work = work;
  1422. else
  1423. #endif
  1424. precalc_hash(&work->blk, (uint32_t *)(work->midstate), (uint32_t *)(work->data + 64));
  1425. return true;
  1426. }
  1427. extern int opt_dynamic_interval;
  1428. static int64_t opencl_scanhash(struct thr_info *thr, struct work *work,
  1429. int64_t __maybe_unused max_nonce)
  1430. {
  1431. const int thr_id = thr->id;
  1432. struct opencl_thread_data *thrdata = thr->cgpu_data;
  1433. struct cgpu_info *gpu = thr->cgpu;
  1434. _clState *clState = clStates[thr_id];
  1435. const cl_kernel *kernel = &clState->kernel;
  1436. const int dynamic_us = opt_dynamic_interval * 1000;
  1437. cl_int status;
  1438. size_t globalThreads[1];
  1439. size_t localThreads[1] = { clState->wsize };
  1440. int64_t hashes;
  1441. /* Windows' timer resolution is only 15ms so oversample 5x */
  1442. if (gpu->dynamic && (++gpu->intervals * dynamic_us) > 70000) {
  1443. struct timeval tv_gpuend;
  1444. double gpu_us;
  1445. gettimeofday(&tv_gpuend, NULL);
  1446. gpu_us = us_tdiff(&tv_gpuend, &gpu->tv_gpustart) / gpu->intervals;
  1447. if (gpu_us > dynamic_us) {
  1448. if (gpu->intensity > MIN_INTENSITY)
  1449. --gpu->intensity;
  1450. } else if (gpu_us < dynamic_us / 2) {
  1451. if (gpu->intensity < MAX_INTENSITY)
  1452. ++gpu->intensity;
  1453. }
  1454. memcpy(&(gpu->tv_gpustart), &tv_gpuend, sizeof(struct timeval));
  1455. gpu->intervals = 0;
  1456. }
  1457. set_threads_hashes(clState->vwidth, &hashes, globalThreads, localThreads[0], &gpu->intensity);
  1458. if (hashes > gpu->max_hashes)
  1459. gpu->max_hashes = hashes;
  1460. status = thrdata->queue_kernel_parameters(clState, &work->blk, globalThreads[0]);
  1461. if (unlikely(status != CL_SUCCESS)) {
  1462. applog(LOG_ERR, "Error: clSetKernelArg of all params failed.");
  1463. return -1;
  1464. }
  1465. if (clState->goffset) {
  1466. size_t global_work_offset[1];
  1467. global_work_offset[0] = work->blk.nonce;
  1468. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, global_work_offset,
  1469. globalThreads, localThreads, 0, NULL, NULL);
  1470. } else
  1471. status = clEnqueueNDRangeKernel(clState->commandQueue, *kernel, 1, NULL,
  1472. globalThreads, localThreads, 0, NULL, NULL);
  1473. if (unlikely(status != CL_SUCCESS)) {
  1474. applog(LOG_ERR, "Error %d: Enqueueing kernel onto command queue. (clEnqueueNDRangeKernel)", status);
  1475. return -1;
  1476. }
  1477. status = clEnqueueReadBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1478. BUFFERSIZE, thrdata->res, 0, NULL, NULL);
  1479. if (unlikely(status != CL_SUCCESS)) {
  1480. applog(LOG_ERR, "Error: clEnqueueReadBuffer failed error %d. (clEnqueueReadBuffer)", status);
  1481. return -1;
  1482. }
  1483. /* The amount of work scanned can fluctuate when intensity changes
  1484. * and since we do this one cycle behind, we increment the work more
  1485. * than enough to prevent repeating work */
  1486. work->blk.nonce += gpu->max_hashes;
  1487. /* This finish flushes the readbuffer set with CL_FALSE in clEnqueueReadBuffer */
  1488. clFinish(clState->commandQueue);
  1489. /* FOUND entry is used as a counter to say how many nonces exist */
  1490. if (thrdata->res[FOUND]) {
  1491. /* Clear the buffer again */
  1492. status = clEnqueueWriteBuffer(clState->commandQueue, clState->outputBuffer, CL_FALSE, 0,
  1493. BUFFERSIZE, blank_res, 0, NULL, NULL);
  1494. if (unlikely(status != CL_SUCCESS)) {
  1495. applog(LOG_ERR, "Error: clEnqueueWriteBuffer failed.");
  1496. return -1;
  1497. }
  1498. applog(LOG_DEBUG, "GPU %d found something?", gpu->device_id);
  1499. postcalc_hash_async(thr, work, thrdata->res);
  1500. memset(thrdata->res, 0, BUFFERSIZE);
  1501. /* This finish flushes the writebuffer set with CL_FALSE in clEnqueueWriteBuffer */
  1502. clFinish(clState->commandQueue);
  1503. }
  1504. return hashes;
  1505. }
  1506. static void opencl_thread_shutdown(struct thr_info *thr)
  1507. {
  1508. const int thr_id = thr->id;
  1509. _clState *clState = clStates[thr_id];
  1510. clReleaseCommandQueue(clState->commandQueue);
  1511. clReleaseKernel(clState->kernel);
  1512. clReleaseProgram(clState->program);
  1513. clReleaseContext(clState->context);
  1514. }
  1515. struct device_api opencl_api = {
  1516. .dname = "opencl",
  1517. .name = "OCL",
  1518. .api_detect = opencl_detect,
  1519. .reinit_device = reinit_opencl_device,
  1520. #ifdef HAVE_ADL
  1521. .get_statline_before = get_opencl_statline_before,
  1522. #endif
  1523. .get_api_extra_device_status = get_opencl_api_extra_device_status,
  1524. .thread_prepare = opencl_thread_prepare,
  1525. .thread_init = opencl_thread_init,
  1526. .prepare_work = opencl_prepare_work,
  1527. .scanhash = opencl_scanhash,
  1528. .thread_shutdown = opencl_thread_shutdown,
  1529. };
  1530. #endif