ocl.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. /*
  2. * Copyright 2011-2013 Con Kolivas
  3. * Copyright 2012-2014 Luke Dashjr
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #ifdef USE_OPENCL
  12. #include <ctype.h>
  13. #include <limits.h>
  14. #include <signal.h>
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <time.h>
  22. #include <sys/time.h>
  23. #include <pthread.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #define OMIT_OPENCL_API
  27. #include "deviceapi.h"
  28. #include "driver-opencl.h"
  29. #include "findnonce.h"
  30. #include "logging.h"
  31. #include "miner.h"
  32. #include "ocl.h"
  33. #include "sha2.h"
  34. #include "util.h"
  35. /* Platform API */
  36. extern
  37. CL_API_ENTRY cl_int CL_API_CALL
  38. (*clGetPlatformIDs)(cl_uint /* num_entries */,
  39. cl_platform_id * /* platforms */,
  40. cl_uint * /* num_platforms */) CL_API_SUFFIX__VERSION_1_0;
  41. extern
  42. CL_API_ENTRY cl_int CL_API_CALL
  43. (*clGetPlatformInfo)(cl_platform_id /* platform */,
  44. cl_platform_info /* param_name */,
  45. size_t /* param_value_size */,
  46. void * /* param_value */,
  47. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  48. /* Device APIs */
  49. extern
  50. CL_API_ENTRY cl_int CL_API_CALL
  51. (*clGetDeviceIDs)(cl_platform_id /* platform */,
  52. cl_device_type /* device_type */,
  53. cl_uint /* num_entries */,
  54. cl_device_id * /* devices */,
  55. cl_uint * /* num_devices */) CL_API_SUFFIX__VERSION_1_0;
  56. extern
  57. CL_API_ENTRY cl_int CL_API_CALL
  58. (*clGetDeviceInfo)(cl_device_id /* device */,
  59. cl_device_info /* param_name */,
  60. size_t /* param_value_size */,
  61. void * /* param_value */,
  62. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  63. /* Context APIs */
  64. extern
  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. extern
  72. CL_API_ENTRY cl_int CL_API_CALL
  73. (*clReleaseContext)(cl_context /* context */) CL_API_SUFFIX__VERSION_1_0;
  74. /* Command Queue APIs */
  75. extern
  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. extern
  82. CL_API_ENTRY cl_int CL_API_CALL
  83. (*clReleaseCommandQueue)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  84. /* Memory Object APIs */
  85. extern
  86. CL_API_ENTRY cl_mem CL_API_CALL
  87. (*clCreateBuffer)(cl_context /* context */,
  88. cl_mem_flags /* flags */,
  89. size_t /* size */,
  90. void * /* host_ptr */,
  91. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  92. /* Program Object APIs */
  93. extern
  94. CL_API_ENTRY cl_program CL_API_CALL
  95. (*clCreateProgramWithSource)(cl_context /* context */,
  96. cl_uint /* count */,
  97. const char ** /* strings */,
  98. const size_t * /* lengths */,
  99. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  100. extern
  101. CL_API_ENTRY cl_program CL_API_CALL
  102. (*clCreateProgramWithBinary)(cl_context /* context */,
  103. cl_uint /* num_devices */,
  104. const cl_device_id * /* device_list */,
  105. const size_t * /* lengths */,
  106. const unsigned char ** /* binaries */,
  107. cl_int * /* binary_status */,
  108. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  109. extern
  110. CL_API_ENTRY cl_int CL_API_CALL
  111. (*clReleaseProgram)(cl_program /* program */) CL_API_SUFFIX__VERSION_1_0;
  112. extern
  113. CL_API_ENTRY cl_int CL_API_CALL
  114. (*clBuildProgram)(cl_program /* program */,
  115. cl_uint /* num_devices */,
  116. const cl_device_id * /* device_list */,
  117. const char * /* options */,
  118. void (CL_CALLBACK * /* pfn_notify */)(cl_program /* program */, void * /* user_data */),
  119. void * /* user_data */) CL_API_SUFFIX__VERSION_1_0;
  120. extern
  121. CL_API_ENTRY cl_int CL_API_CALL
  122. (*clGetProgramInfo)(cl_program /* program */,
  123. cl_program_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. extern
  128. CL_API_ENTRY cl_int CL_API_CALL
  129. (*clGetProgramBuildInfo)(cl_program /* program */,
  130. cl_device_id /* device */,
  131. cl_program_build_info /* param_name */,
  132. size_t /* param_value_size */,
  133. void * /* param_value */,
  134. size_t * /* param_value_size_ret */) CL_API_SUFFIX__VERSION_1_0;
  135. /* Kernel Object APIs */
  136. extern
  137. CL_API_ENTRY cl_kernel CL_API_CALL
  138. (*clCreateKernel)(cl_program /* program */,
  139. const char * /* kernel_name */,
  140. cl_int * /* errcode_ret */) CL_API_SUFFIX__VERSION_1_0;
  141. extern
  142. CL_API_ENTRY cl_int CL_API_CALL
  143. (*clReleaseKernel)(cl_kernel /* kernel */) CL_API_SUFFIX__VERSION_1_0;
  144. extern
  145. CL_API_ENTRY cl_int CL_API_CALL
  146. (*clSetKernelArg)(cl_kernel /* kernel */,
  147. cl_uint /* arg_index */,
  148. size_t /* arg_size */,
  149. const void * /* arg_value */) CL_API_SUFFIX__VERSION_1_0;
  150. /* Flush and Finish APIs */
  151. extern
  152. CL_API_ENTRY cl_int CL_API_CALL
  153. (*clFinish)(cl_command_queue /* command_queue */) CL_API_SUFFIX__VERSION_1_0;
  154. /* Enqueued Commands APIs */
  155. extern
  156. CL_API_ENTRY cl_int CL_API_CALL
  157. (*clEnqueueReadBuffer)(cl_command_queue /* command_queue */,
  158. cl_mem /* buffer */,
  159. cl_bool /* blocking_read */,
  160. size_t /* offset */,
  161. size_t /* size */,
  162. void * /* ptr */,
  163. cl_uint /* num_events_in_wait_list */,
  164. const cl_event * /* event_wait_list */,
  165. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  166. extern
  167. CL_API_ENTRY cl_int CL_API_CALL
  168. (*clEnqueueWriteBuffer)(cl_command_queue /* command_queue */,
  169. cl_mem /* buffer */,
  170. cl_bool /* blocking_write */,
  171. size_t /* offset */,
  172. size_t /* size */,
  173. const void * /* ptr */,
  174. cl_uint /* num_events_in_wait_list */,
  175. const cl_event * /* event_wait_list */,
  176. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  177. extern
  178. CL_API_ENTRY cl_int CL_API_CALL
  179. (*clEnqueueNDRangeKernel)(cl_command_queue /* command_queue */,
  180. cl_kernel /* kernel */,
  181. cl_uint /* work_dim */,
  182. const size_t * /* global_work_offset */,
  183. const size_t * /* global_work_size */,
  184. const size_t * /* local_work_size */,
  185. cl_uint /* num_events_in_wait_list */,
  186. const cl_event * /* event_wait_list */,
  187. cl_event * /* event */) CL_API_SUFFIX__VERSION_1_0;
  188. int opt_platform_id = -1;
  189. FILE *opencl_open_kernel(const char * const filename)
  190. {
  191. char *fullpath = alloca(PATH_MAX);
  192. FILE *f;
  193. /* Try in the optional kernel path or installed prefix first */
  194. f = open_bitstream("opencl", filename);
  195. if (!f) {
  196. /* Then try from the path BFGMiner was called */
  197. strcpy(fullpath, cgminer_path);
  198. strcat(fullpath, filename);
  199. f = fopen(fullpath, "rb");
  200. }
  201. /* Finally try opening it directly */
  202. if (!f)
  203. f = fopen(filename, "rb");
  204. return f;
  205. }
  206. char *file_contents(const char *filename, int *length)
  207. {
  208. void *buffer;
  209. FILE *f;
  210. f = opencl_open_kernel(filename);
  211. if (!f) {
  212. applog(LOG_ERR, "Unable to open %s for reading", filename);
  213. return NULL;
  214. }
  215. fseek(f, 0, SEEK_END);
  216. *length = ftell(f);
  217. fseek(f, 0, SEEK_SET);
  218. buffer = malloc(*length+1);
  219. *length = fread(buffer, 1, *length, f);
  220. fclose(f);
  221. ((char*)buffer)[*length] = '\0';
  222. return (char*)buffer;
  223. }
  224. char *opencl_kernel_source(const char * const filename, int * const out_sourcelen, enum cl_kernels * const out_kinterface)
  225. {
  226. char *source = file_contents(filename, out_sourcelen);
  227. if (!source)
  228. return NULL;
  229. char *s = strstr(source, "kernel-interface:"), *q;
  230. if (s)
  231. {
  232. for (s = &s[17]; s[0] && isspace(s[0]); ++s)
  233. if (s[0] == '\n' || s[0] == '\r')
  234. break;
  235. for (q = s; q[0] && !isspace(q[0]); ++q)
  236. {} // Find end of string
  237. const size_t kinamelen = q - s;
  238. char kiname[kinamelen + 1];
  239. memcpy(kiname, s, kinamelen);
  240. kiname[kinamelen] = '\0';
  241. *out_kinterface = select_kernel(kiname);
  242. }
  243. else
  244. *out_kinterface = KL_NONE;
  245. return source;
  246. }
  247. extern int opt_g_threads;
  248. int clDevicesNum(void) {
  249. cl_int status;
  250. char pbuff[256];
  251. cl_uint numDevices;
  252. cl_uint numPlatforms;
  253. int most_devices = -1;
  254. cl_platform_id *platforms;
  255. cl_platform_id platform = NULL;
  256. unsigned int i, mdplatform = 0;
  257. bool mdmesa = false;
  258. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  259. /* If this fails, assume no GPUs. */
  260. if (status != CL_SUCCESS) {
  261. applog(LOG_ERR, "Error %d: clGetPlatformsIDs failed (no OpenCL SDK installed?)", status);
  262. return -1;
  263. }
  264. if (numPlatforms == 0) {
  265. applog(LOG_ERR, "clGetPlatformsIDs returned no platforms (no OpenCL SDK installed?)");
  266. return -1;
  267. }
  268. platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
  269. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  270. if (status != CL_SUCCESS) {
  271. applog(LOG_ERR, "Error %d: Getting Platform Ids. (clGetPlatformsIDs)", status);
  272. return -1;
  273. }
  274. for (i = 0; i < numPlatforms; i++) {
  275. if (opt_platform_id >= 0 && (int)i != opt_platform_id)
  276. continue;
  277. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  278. if (status != CL_SUCCESS) {
  279. applog(LOG_ERR, "Error %d: Getting Platform Info. (clGetPlatformInfo)", status);
  280. return -1;
  281. }
  282. platform = platforms[i];
  283. applog(LOG_INFO, "CL Platform %d vendor: %s", i, pbuff);
  284. status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
  285. if (status == CL_SUCCESS)
  286. applog(LOG_INFO, "CL Platform %d name: %s", i, pbuff);
  287. status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(pbuff), pbuff, NULL);
  288. if (status == CL_SUCCESS)
  289. applog(LOG_INFO, "CL Platform %d version: %s", i, pbuff);
  290. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  291. if (status != CL_SUCCESS) {
  292. applog(LOG_INFO, "Error %d: Getting Device IDs (num)", status);
  293. continue;
  294. }
  295. applog(LOG_INFO, "Platform %d devices: %d", i, numDevices);
  296. if ((int)numDevices > most_devices) {
  297. most_devices = numDevices;
  298. mdplatform = i;
  299. mdmesa = strstr(pbuff, "MESA");
  300. }
  301. if (numDevices) {
  302. unsigned int j;
  303. cl_device_id *devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  304. clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  305. for (j = 0; j < numDevices; j++) {
  306. clGetDeviceInfo(devices[j], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  307. applog(LOG_INFO, "\t%i\t%s", j, pbuff);
  308. }
  309. free(devices);
  310. }
  311. }
  312. if (opt_platform_id < 0)
  313. opt_platform_id = mdplatform;
  314. if (mdmesa && opt_g_threads == -1)
  315. opt_g_threads = 1;
  316. return most_devices;
  317. }
  318. cl_int bfg_clBuildProgram(cl_program * const program, const cl_device_id devid, const char * const CompilerOptions)
  319. {
  320. cl_int status;
  321. status = clBuildProgram(*program, 1, &devid, CompilerOptions, NULL, NULL);
  322. if (status != CL_SUCCESS)
  323. {
  324. applog(LOG_ERR, "Error %d: Building Program (clBuildProgram)", status);
  325. size_t logSize;
  326. status = clGetProgramBuildInfo(*program, devid, CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  327. char *log = malloc(logSize ?: 1);
  328. status = clGetProgramBuildInfo(*program, devid, CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  329. if (logSize > 0 && log[0])
  330. applog(LOG_ERR, "%s", log);
  331. free(log);
  332. }
  333. return status;
  334. }
  335. static int advance(char **area, unsigned *remaining, const char *marker)
  336. {
  337. char *find = memmem(*area, *remaining, marker, strlen(marker));
  338. if (!find) {
  339. applog(LOG_DEBUG, "Marker \"%s\" not found", marker);
  340. return 0;
  341. }
  342. *remaining -= find - *area;
  343. *area = find;
  344. return 1;
  345. }
  346. #define OP3_INST_BFE_UINT 4ULL
  347. #define OP3_INST_BFE_INT 5ULL
  348. #define OP3_INST_BFI_INT 6ULL
  349. #define OP3_INST_BIT_ALIGN_INT 12ULL
  350. #define OP3_INST_BYTE_ALIGN_INT 13ULL
  351. void patch_opcodes(char *w, unsigned remaining)
  352. {
  353. uint64_t *opcode = (uint64_t *)w;
  354. int patched = 0;
  355. int count_bfe_int = 0;
  356. int count_bfe_uint = 0;
  357. int count_byte_align = 0;
  358. while (42) {
  359. int clamp = (*opcode >> (32 + 31)) & 0x1;
  360. int dest_rel = (*opcode >> (32 + 28)) & 0x1;
  361. int alu_inst = (*opcode >> (32 + 13)) & 0x1f;
  362. int s2_neg = (*opcode >> (32 + 12)) & 0x1;
  363. int s2_rel = (*opcode >> (32 + 9)) & 0x1;
  364. int pred_sel = (*opcode >> 29) & 0x3;
  365. if (!clamp && !dest_rel && !s2_neg && !s2_rel && !pred_sel) {
  366. if (alu_inst == OP3_INST_BFE_INT) {
  367. count_bfe_int++;
  368. } else if (alu_inst == OP3_INST_BFE_UINT) {
  369. count_bfe_uint++;
  370. } else if (alu_inst == OP3_INST_BYTE_ALIGN_INT) {
  371. count_byte_align++;
  372. // patch this instruction to BFI_INT
  373. *opcode &= 0xfffc1fffffffffffULL;
  374. *opcode |= OP3_INST_BFI_INT << (32 + 13);
  375. patched++;
  376. }
  377. }
  378. if (remaining <= 8)
  379. break;
  380. opcode++;
  381. remaining -= 8;
  382. }
  383. applog(LOG_DEBUG, "Potential OP3 instructions identified: "
  384. "%i BFE_INT, %i BFE_UINT, %i BYTE_ALIGN",
  385. count_bfe_int, count_bfe_uint, count_byte_align);
  386. applog(LOG_DEBUG, "Patched a total of %i BFI_INT instructions", patched);
  387. }
  388. _clState *opencl_create_clState(unsigned int gpu, char *name, size_t nameSize)
  389. {
  390. _clState *clState = calloc(1, sizeof(_clState));
  391. struct cgpu_info *cgpu = &gpus[gpu];
  392. struct opencl_device_data * const data = cgpu->device_data;
  393. cl_platform_id platform = NULL;
  394. char pbuff[256], vbuff[255];
  395. char *s;
  396. cl_platform_id* platforms;
  397. cl_device_id *devices;
  398. cl_uint numPlatforms;
  399. cl_uint numDevices;
  400. cl_int status;
  401. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  402. if (status != CL_SUCCESS) {
  403. applog(LOG_ERR, "Error %d: Getting Platforms. (clGetPlatformsIDs)", status);
  404. return NULL;
  405. }
  406. platforms = (cl_platform_id *)alloca(numPlatforms*sizeof(cl_platform_id));
  407. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  408. if (status != CL_SUCCESS) {
  409. applog(LOG_ERR, "Error %d: Getting Platform Ids. (clGetPlatformsIDs)", status);
  410. return NULL;
  411. }
  412. if (opt_platform_id >= (int)numPlatforms) {
  413. applog(LOG_ERR, "Specified platform that does not exist");
  414. return NULL;
  415. }
  416. status = clGetPlatformInfo(platforms[opt_platform_id], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  417. if (status != CL_SUCCESS) {
  418. applog(LOG_ERR, "Error %d: Getting Platform Info. (clGetPlatformInfo)", status);
  419. return NULL;
  420. }
  421. platform = platforms[opt_platform_id];
  422. if (platform == NULL) {
  423. perror("NULL platform found!\n");
  424. return NULL;
  425. }
  426. applog(LOG_INFO, "CL Platform vendor: %s", pbuff);
  427. status = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(pbuff), pbuff, NULL);
  428. if (status == CL_SUCCESS)
  429. applog(LOG_INFO, "CL Platform name: %s", pbuff);
  430. status = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(vbuff), vbuff, NULL);
  431. if (status == CL_SUCCESS)
  432. applog(LOG_INFO, "CL Platform version: %s", vbuff);
  433. clState->platform_ver_str = strdup(vbuff);
  434. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  435. if (status != CL_SUCCESS) {
  436. applog(LOG_ERR, "Error %d: Getting Device IDs (num)", status);
  437. return NULL;
  438. }
  439. if (numDevices > 0 ) {
  440. devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  441. /* Now, get the device list data */
  442. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  443. if (status != CL_SUCCESS) {
  444. applog(LOG_ERR, "Error %d: Getting Device IDs (list)", status);
  445. return NULL;
  446. }
  447. applog(LOG_INFO, "List of devices:");
  448. unsigned int i;
  449. for (i = 0; i < numDevices; i++) {
  450. status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  451. if (status != CL_SUCCESS) {
  452. applog(LOG_ERR, "Error %d: Getting Device Info", status);
  453. return NULL;
  454. }
  455. applog(LOG_INFO, "\t%i\t%s", i, pbuff);
  456. }
  457. if (gpu < numDevices) {
  458. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  459. if (status != CL_SUCCESS) {
  460. applog(LOG_ERR, "Error %d: Getting Device Info", status);
  461. return NULL;
  462. }
  463. applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
  464. strncpy(name, pbuff, nameSize);
  465. } else {
  466. applog(LOG_ERR, "Invalid GPU %i", gpu);
  467. return NULL;
  468. }
  469. } else return NULL;
  470. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
  471. clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
  472. if (status != CL_SUCCESS) {
  473. applog(LOG_ERR, "Error %d: Creating Context. (clCreateContextFromType)", status);
  474. return NULL;
  475. }
  476. /////////////////////////////////////////////////////////////////
  477. // Create an OpenCL command queue
  478. /////////////////////////////////////////////////////////////////
  479. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu],
  480. CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &status);
  481. if (status != CL_SUCCESS) /* Try again without OOE enable */
  482. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0 , &status);
  483. if (status != CL_SUCCESS) {
  484. applog(LOG_ERR, "Error %d: Creating Command Queue. (clCreateCommandQueue)", status);
  485. return NULL;
  486. }
  487. /* Check for BFI INT support. Hopefully people don't mix devices with
  488. * and without it! */
  489. char * extensions = malloc(1024);
  490. const char * camo = "cl_amd_media_ops";
  491. char *find;
  492. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_EXTENSIONS, 1024, (void *)extensions, NULL);
  493. if (status != CL_SUCCESS) {
  494. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS", status);
  495. return NULL;
  496. }
  497. find = strstr(extensions, camo);
  498. if (find)
  499. clState->hasBitAlign = true;
  500. free(extensions);
  501. /* Check for OpenCL >= 1.0 support, needed for global offset parameter usage. */
  502. char * devoclver = malloc(1024);
  503. const char * ocl10 = "OpenCL 1.0";
  504. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_VERSION, 1024, (void *)devoclver, NULL);
  505. if (status != CL_SUCCESS) {
  506. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_VERSION", status);
  507. return NULL;
  508. }
  509. find = strstr(devoclver, ocl10);
  510. if (!find)
  511. clState->hasOpenCL11plus = true;
  512. free(devoclver);
  513. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&clState->preferred_vwidth, NULL);
  514. if (status != CL_SUCCESS) {
  515. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT", status);
  516. return NULL;
  517. }
  518. applog(LOG_DEBUG, "Preferred vector width reported %d", clState->preferred_vwidth);
  519. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&clState->max_work_size, NULL);
  520. if (status != CL_SUCCESS) {
  521. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE", status);
  522. return NULL;
  523. }
  524. applog(LOG_DEBUG, "Max work group size reported %"PRId64, (int64_t)clState->max_work_size);
  525. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(clState->max_compute_units), (void *)&clState->max_compute_units, NULL);
  526. if (status != CL_SUCCESS) {
  527. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_COMPUTE_UNITS", status);
  528. return NULL;
  529. }
  530. if (data->_init_intensity)
  531. {
  532. data->oclthreads = 1; // Needed to ensure we don't just try to re-save the string (which would free before strduping and segfault anyway)
  533. opencl_set_intensity_from_str(cgpu, data->_init_intensity);
  534. }
  535. else
  536. {
  537. data->oclthreads = 1;
  538. data->intensity = INT_MIN;
  539. }
  540. applog(LOG_DEBUG, "Max compute units reported %u", (unsigned)clState->max_compute_units);
  541. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_MEM_ALLOC_SIZE , sizeof(cl_ulong), (void *)&data->max_alloc, NULL);
  542. if (status != CL_SUCCESS) {
  543. applog(LOG_ERR, "Error %d: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_MEM_ALLOC_SIZE", status);
  544. return NULL;
  545. }
  546. applog(LOG_DEBUG, "Max mem alloc size is %lu", (unsigned long)data->max_alloc);
  547. find = strstr(vbuff, "MESA");
  548. if (find)
  549. {
  550. long int major = strtol(&find[4], &s, 10), minor = 0;
  551. if (!major)
  552. {} // No version number at all
  553. else
  554. if (s[0] == '.')
  555. minor = strtol(&s[1], NULL, 10);
  556. if (major < 10 || (major == 10 && minor < 1))
  557. {
  558. if (data->opt_opencl_binaries == OBU_DEFAULT)
  559. {
  560. applog(LOG_DEBUG, "Mesa OpenCL platform detected (v%ld.%ld), disabling OpenCL kernel binaries and bitalign", major, minor);
  561. data->opt_opencl_binaries = OBU_NONE;
  562. }
  563. else
  564. applog(LOG_DEBUG, "Mesa OpenCL platform detected (v%ld.%ld), disabling bitalign", major, minor);
  565. clState->hasBitAlign = false;
  566. }
  567. else
  568. applog(LOG_DEBUG, "Mesa OpenCL platform detected (v%ld.%ld)", major, minor);
  569. clState->is_mesa = true;
  570. }
  571. if (data->opt_opencl_binaries == OBU_DEFAULT)
  572. {
  573. #ifdef __APPLE__
  574. // Apple OpenCL doesn't like using binaries this way
  575. data->opt_opencl_binaries = OBU_NONE;
  576. #else
  577. data->opt_opencl_binaries = OBU_LOADSAVE;
  578. #endif
  579. }
  580. clState->devid = devices[gpu];
  581. free(devices);
  582. /* For some reason 2 vectors is still better even if the card says
  583. * otherwise, and many cards lie about their max so use 256 as max
  584. * unless explicitly set on the command line. Tahiti prefers 1 */
  585. if (strstr(name, "Tahiti"))
  586. clState->preferred_vwidth = 1;
  587. else
  588. if (clState->preferred_vwidth > 2)
  589. clState->preferred_vwidth = 2;
  590. if (data->vwidth)
  591. clState->vwidth = data->vwidth;
  592. else {
  593. clState->vwidth = clState->preferred_vwidth;
  594. data->vwidth = clState->preferred_vwidth;
  595. }
  596. clState->outputBuffer = clCreateBuffer(clState->context, 0, OPENCL_MAX_BUFFERSIZE, NULL, &status);
  597. if (status != CL_SUCCESS) {
  598. applog(LOG_ERR, "Error %d: clCreateBuffer (outputBuffer)", status);
  599. return false;
  600. }
  601. return clState;
  602. }
  603. bool opencl_load_kernel(struct cgpu_info * const cgpu, _clState * const clState, const char * const name, struct opencl_kernel_info * const kernelinfo, const char * const kernel_file, __maybe_unused const struct mining_algorithm * const malgo)
  604. {
  605. const int gpu = cgpu->device_id;
  606. #ifdef USE_SHA256D
  607. bool patchbfi = false;
  608. #endif
  609. bool prog_built = false;
  610. struct opencl_device_data * const data = cgpu->device_data;
  611. const char * const vbuff = clState->platform_ver_str;
  612. char *s;
  613. cl_int status;
  614. /* Create binary filename based on parameters passed to opencl
  615. * compiler to ensure we only load a binary that matches what would
  616. * have otherwise created. The filename is:
  617. * kernelname + name +/- g(offset) + v + vectors + w + work_size + l + sizeof(long) + p + platform version + .bin
  618. * For scrypt the filename is:
  619. * kernelname + name + g + lg + lookup_gap + tc + thread_concurrency + w + work_size + l + sizeof(long) + p + platform version + .bin
  620. */
  621. char binaryfilename[255];
  622. char filename[255];
  623. char numbuf[32];
  624. snprintf(filename, sizeof(filename), "%s.cl", kernel_file);
  625. snprintf(binaryfilename, sizeof(filename), "%s", kernel_file);
  626. int pl;
  627. char *source = opencl_kernel_source(filename, &pl, &kernelinfo->interface);
  628. if (!source)
  629. return false;
  630. {
  631. uint8_t hash[0x20];
  632. char hashhex[7];
  633. sha256((void*)source, pl, hash);
  634. bin2hex(hashhex, hash, 3);
  635. tailsprintf(binaryfilename, sizeof(binaryfilename), "-%s", hashhex);
  636. }
  637. switch (kernelinfo->interface)
  638. {
  639. case KL_NONE:
  640. applog(LOG_ERR, "%s: Failed to identify kernel interface for %s",
  641. cgpu->dev_repr, kernel_file);
  642. free(source);
  643. return false;
  644. #ifdef USE_SHA256D
  645. case KL_PHATK:
  646. if ((strstr(vbuff, "844.4") || strstr(vbuff, "851.4") ||
  647. strstr(vbuff, "831.4") || strstr(vbuff, "898.1") ||
  648. strstr(vbuff, "923.1") || strstr(vbuff, "938.2") ||
  649. strstr(vbuff, "1113.2"))) {
  650. applog(LOG_WARNING, "WARNING: You have selected the phatk kernel.");
  651. applog(LOG_WARNING, "You are running SDK 2.6+ which performs poorly with this kernel.");
  652. applog(LOG_WARNING, "Downgrade your SDK and delete any .bin files before starting again.");
  653. applog(LOG_WARNING, "Or allow BFGMiner to automatically choose a more suitable kernel.");
  654. }
  655. #endif
  656. default:
  657. ;
  658. }
  659. applog(LOG_DEBUG, "%s: Using kernel %s with interface %s",
  660. cgpu->dev_repr, kernel_file,
  661. opencl_get_kernel_interface_name(kernelinfo->interface));
  662. {
  663. int kernel_goffset_support = 0; // 0 = none; 1 = optional; 2 = required
  664. switch (kernelinfo->interface)
  665. {
  666. #ifdef USE_SHA256D
  667. case KL_DIABLO:
  668. case KL_DIAKGCN:
  669. case KL_POCLBM:
  670. kernel_goffset_support = 1;
  671. break;
  672. case KL_PHATK:
  673. kernel_goffset_support = 0;
  674. break;
  675. #endif
  676. case KL_NONE: case OPENCL_KERNEL_INTERFACE_COUNT:
  677. #ifdef USE_SCRYPT
  678. case KL_SCRYPT:
  679. #endif
  680. kernel_goffset_support = 2;
  681. break;
  682. }
  683. const bool device_goffset_support = (clState->hasOpenCL11plus && !clState->is_mesa);
  684. if (device_goffset_support)
  685. {
  686. if (kernel_goffset_support)
  687. kernelinfo->goffset = true;
  688. }
  689. else
  690. if (kernel_goffset_support == 2)
  691. {
  692. // FIXME: Determine this before min_nonce_diff returns positive
  693. applog(LOG_ERR, "%s: Need goffset support!", cgpu->dev_repr);
  694. return false;
  695. }
  696. }
  697. if (data->work_size && data->work_size <= clState->max_work_size)
  698. kernelinfo->wsize = data->work_size;
  699. else
  700. #ifdef USE_SCRYPT
  701. if (malgo->algo == POW_SCRYPT)
  702. kernelinfo->wsize = 256;
  703. else
  704. #endif
  705. if (strstr(name, "Tahiti"))
  706. kernelinfo->wsize = 64;
  707. else
  708. kernelinfo->wsize = (clState->max_work_size <= 256 ? clState->max_work_size : 256) / clState->vwidth;
  709. #ifdef USE_SCRYPT
  710. if (kernelinfo->interface == KL_SCRYPT)
  711. {
  712. if (!data->thread_concurrency)
  713. {
  714. unsigned int sixtyfours;
  715. sixtyfours = data->max_alloc / 131072 / 64 - 1;
  716. data->thread_concurrency = sixtyfours * 64;
  717. if (data->shaders && data->thread_concurrency > data->shaders) {
  718. data->thread_concurrency -= data->thread_concurrency % data->shaders;
  719. if (data->thread_concurrency > data->shaders * 5)
  720. data->thread_concurrency = data->shaders * 5;
  721. }
  722. applog(LOG_DEBUG, "GPU %u: selecting thread concurrency of %lu", gpu, (unsigned long)data->thread_concurrency);
  723. }
  724. }
  725. #endif
  726. FILE *binaryfile;
  727. size_t *binary_sizes;
  728. char **binaries;
  729. size_t sourceSize[] = {(size_t)pl};
  730. cl_uint slot, cpnd;
  731. slot = cpnd = 0;
  732. binary_sizes = calloc(sizeof(size_t) * MAX_GPUDEVICES * 4, 1);
  733. if (unlikely(!binary_sizes)) {
  734. applog(LOG_ERR, "Unable to calloc binary_sizes");
  735. return false;
  736. }
  737. binaries = calloc(sizeof(char *) * MAX_GPUDEVICES * 4, 1);
  738. if (unlikely(!binaries)) {
  739. applog(LOG_ERR, "Unable to calloc binaries");
  740. return false;
  741. }
  742. strcat(binaryfilename, name);
  743. if (kernelinfo->goffset)
  744. strcat(binaryfilename, "g");
  745. #ifdef USE_SCRYPT
  746. if (kernelinfo->interface == KL_SCRYPT)
  747. {
  748. sprintf(numbuf, "lg%utc%u", data->lookup_gap, (unsigned int)data->thread_concurrency);
  749. strcat(binaryfilename, numbuf);
  750. }
  751. else
  752. #endif
  753. {
  754. sprintf(numbuf, "v%d", clState->vwidth);
  755. strcat(binaryfilename, numbuf);
  756. }
  757. sprintf(numbuf, "w%d", (int)kernelinfo->wsize);
  758. strcat(binaryfilename, numbuf);
  759. sprintf(numbuf, "l%d", (int)sizeof(long));
  760. strcat(binaryfilename, numbuf);
  761. strcat(binaryfilename, "p");
  762. strcat(binaryfilename, vbuff);
  763. sanestr(binaryfilename, binaryfilename);
  764. applog(LOG_DEBUG, "OCL%2u: Configured OpenCL kernel name: %s", gpu, binaryfilename);
  765. strcat(binaryfilename, ".bin");
  766. if (!(data->opt_opencl_binaries & OBU_LOAD))
  767. goto build;
  768. binaryfile = fopen(binaryfilename, "rb");
  769. if (!binaryfile) {
  770. applog(LOG_DEBUG, "No binary found, generating from source");
  771. } else {
  772. struct stat binary_stat;
  773. if (unlikely(stat(binaryfilename, &binary_stat))) {
  774. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  775. fclose(binaryfile);
  776. goto build;
  777. }
  778. if (!binary_stat.st_size)
  779. goto build;
  780. binary_sizes[slot] = binary_stat.st_size;
  781. binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
  782. if (unlikely(!binaries[slot])) {
  783. applog(LOG_ERR, "Unable to calloc binaries");
  784. fclose(binaryfile);
  785. return false;
  786. }
  787. if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
  788. applog(LOG_ERR, "Unable to fread binaries");
  789. fclose(binaryfile);
  790. free(binaries[slot]);
  791. goto build;
  792. }
  793. kernelinfo->program = clCreateProgramWithBinary(clState->context, 1, &clState->devid, &binary_sizes[slot], (const unsigned char **)binaries, &status, NULL);
  794. if (status != CL_SUCCESS) {
  795. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  796. fclose(binaryfile);
  797. free(binaries[slot]);
  798. goto build;
  799. }
  800. fclose(binaryfile);
  801. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  802. goto built;
  803. }
  804. /////////////////////////////////////////////////////////////////
  805. // Load CL file, build CL program object, create CL kernel object
  806. /////////////////////////////////////////////////////////////////
  807. build:
  808. kernelinfo->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  809. if (status != CL_SUCCESS) {
  810. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithSource)", status);
  811. return false;
  812. }
  813. /* create a cl program executable for all the devices specified */
  814. char *CompilerOptions = calloc(1, 256);
  815. #ifdef USE_SCRYPT
  816. if (kernelinfo->interface == KL_SCRYPT)
  817. sprintf(CompilerOptions, "-D LOOKUP_GAP=%d -D CONCURRENT_THREADS=%d -D WORKSIZE=%d",
  818. data->lookup_gap, (unsigned int)data->thread_concurrency, (int)kernelinfo->wsize);
  819. else
  820. #endif
  821. {
  822. sprintf(CompilerOptions, "-D WORKSIZE=%d -D VECTORS%d -D WORKVEC=%d",
  823. (int)kernelinfo->wsize, clState->vwidth, (int)kernelinfo->wsize * clState->vwidth);
  824. }
  825. applog(LOG_DEBUG, "Setting worksize to %"PRId64, (int64_t)kernelinfo->wsize);
  826. if (clState->vwidth > 1)
  827. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->vwidth);
  828. if (clState->hasBitAlign) {
  829. strcat(CompilerOptions, " -D BITALIGN");
  830. applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
  831. #ifdef USE_SHA256D
  832. if (strstr(name, "Cedar") ||
  833. strstr(name, "Redwood") ||
  834. strstr(name, "Juniper") ||
  835. strstr(name, "Cypress" ) ||
  836. strstr(name, "Hemlock" ) ||
  837. strstr(name, "Caicos" ) ||
  838. strstr(name, "Turks" ) ||
  839. strstr(name, "Barts" ) ||
  840. strstr(name, "Cayman" ) ||
  841. strstr(name, "Antilles" ) ||
  842. strstr(name, "Wrestler" ) ||
  843. strstr(name, "Zacate" ) ||
  844. strstr(name, "WinterPark" ))
  845. {
  846. // BFI_INT patching only works with AMD-APP up to 1084
  847. if (strstr(vbuff, "ATI-Stream"))
  848. patchbfi = true;
  849. else
  850. if ((s = strstr(vbuff, "AMD-APP")) && (s = strchr(s, '(')) && atoi(&s[1]) < 1085)
  851. patchbfi = true;
  852. }
  853. #endif
  854. } else
  855. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
  856. #ifdef USE_SHA256D
  857. switch (kernelinfo->interface)
  858. {
  859. case KL_DIABLO: case KL_DIAKGCN: case KL_PHATK: case KL_POCLBM:
  860. // Okay, these actually use BFI_INT hacking
  861. break;
  862. default:
  863. // Anything else has never needed it
  864. patchbfi = false;
  865. break;
  866. }
  867. if (patchbfi) {
  868. if (data->opt_opencl_binaries == OBU_LOADSAVE)
  869. {
  870. strcat(CompilerOptions, " -D BFI_INT");
  871. applog(LOG_DEBUG, "BFI_INT patch requiring device found, patched source with BFI_INT");
  872. }
  873. else
  874. {
  875. patchbfi = false;
  876. applog(LOG_WARNING, "BFI_INT patch requiring device found, but OpenCL binary usage disabled; cannot BFI_INT patch");
  877. }
  878. } else
  879. applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
  880. #endif
  881. if (kernelinfo->goffset)
  882. strcat(CompilerOptions, " -D GOFFSET");
  883. if (!clState->hasOpenCL11plus)
  884. strcat(CompilerOptions, " -D OCL1");
  885. applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
  886. status = bfg_clBuildProgram(&kernelinfo->program, clState->devid, CompilerOptions);
  887. free(CompilerOptions);
  888. if (status != CL_SUCCESS)
  889. return false;
  890. prog_built = true;
  891. if (!(data->opt_opencl_binaries & OBU_SAVE))
  892. goto built;
  893. status = clGetProgramInfo(kernelinfo->program, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &cpnd, NULL);
  894. if (unlikely(status != CL_SUCCESS)) {
  895. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_NUM_DEVICES. (clGetProgramInfo)", status);
  896. return false;
  897. }
  898. status = clGetProgramInfo(kernelinfo->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*cpnd, binary_sizes, NULL);
  899. if (unlikely(status != CL_SUCCESS)) {
  900. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetProgramInfo)", status);
  901. return false;
  902. }
  903. /* The actual compiled binary ends up in a RANDOM slot! Grr, so we have
  904. * to iterate over all the binary slots and find where the real program
  905. * is. What the heck is this!? */
  906. for (slot = 0; slot < cpnd; slot++)
  907. if (binary_sizes[slot])
  908. break;
  909. /* copy over all of the generated binaries. */
  910. applog(LOG_DEBUG, "Binary size for gpu %u found in binary slot %u: %"PRId64,
  911. gpu, (unsigned)slot, (int64_t)binary_sizes[slot]);
  912. if (!binary_sizes[slot]) {
  913. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, FAIL!");
  914. return false;
  915. }
  916. binaries[slot] = calloc(sizeof(char) * binary_sizes[slot], 1);
  917. status = clGetProgramInfo(kernelinfo->program, CL_PROGRAM_BINARIES, sizeof(char *) * cpnd, binaries, NULL );
  918. if (unlikely(status != CL_SUCCESS)) {
  919. applog(LOG_ERR, "Error %d: Getting program info. CL_PROGRAM_BINARIES (clGetProgramInfo)", status);
  920. return false;
  921. }
  922. #ifdef USE_SHA256D
  923. /* Patch the kernel if the hardware supports BFI_INT but it needs to
  924. * be hacked in */
  925. if (patchbfi) {
  926. unsigned remaining = binary_sizes[slot];
  927. char *w = binaries[slot];
  928. unsigned int start, length;
  929. /* Find 2nd incidence of .text, and copy the program's
  930. * position and length at a fixed offset from that. Then go
  931. * back and find the 2nd incidence of \x7ELF (rewind by one
  932. * from ELF) and then patch the opcocdes */
  933. if (!advance(&w, &remaining, ".text"))
  934. goto build;
  935. w++; remaining--;
  936. if (!advance(&w, &remaining, ".text")) {
  937. /* 32 bit builds only one ELF */
  938. w--; remaining++;
  939. }
  940. memcpy(&start, w + 285, 4);
  941. memcpy(&length, w + 289, 4);
  942. w = binaries[slot]; remaining = binary_sizes[slot];
  943. if (!advance(&w, &remaining, "ELF"))
  944. goto build;
  945. w++; remaining--;
  946. if (!advance(&w, &remaining, "ELF")) {
  947. /* 32 bit builds only one ELF */
  948. w--; remaining++;
  949. }
  950. w--; remaining++;
  951. w += start; remaining -= start;
  952. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  953. w, remaining);
  954. patch_opcodes(w, length);
  955. status = clReleaseProgram(kernelinfo->program);
  956. if (status != CL_SUCCESS) {
  957. applog(LOG_ERR, "Error %d: Releasing program. (clReleaseProgram)", status);
  958. return false;
  959. }
  960. kernelinfo->program = clCreateProgramWithBinary(clState->context, 1, &clState->devid, &binary_sizes[slot], (const unsigned char **)&binaries[slot], &status, NULL);
  961. if (status != CL_SUCCESS) {
  962. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  963. return false;
  964. }
  965. /* Program needs to be rebuilt */
  966. prog_built = false;
  967. }
  968. #endif
  969. free(source);
  970. /* Save the binary to be loaded next time */
  971. binaryfile = fopen(binaryfilename, "wb");
  972. if (!binaryfile) {
  973. /* Not a fatal problem, just means we build it again next time */
  974. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  975. } else {
  976. if (unlikely(fwrite(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot])) {
  977. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  978. return false;
  979. }
  980. fclose(binaryfile);
  981. }
  982. built:
  983. if (binaries[slot])
  984. free(binaries[slot]);
  985. free(binaries);
  986. free(binary_sizes);
  987. applog(LOG_INFO, "Initialising kernel %s with%s bitalign, %"PRId64" vectors and worksize %"PRIu64,
  988. filename, clState->hasBitAlign ? "" : "out", (int64_t)clState->vwidth, (uint64_t)kernelinfo->wsize);
  989. if (!prog_built) {
  990. /* create a cl program executable for all the devices specified */
  991. status = bfg_clBuildProgram(&kernelinfo->program, clState->devid, NULL);
  992. if (status != CL_SUCCESS)
  993. return false;
  994. }
  995. /* get a kernel object handle for a kernel with the given name */
  996. kernelinfo->kernel = clCreateKernel(kernelinfo->program, "search", &status);
  997. if (status != CL_SUCCESS) {
  998. applog(LOG_ERR, "Error %d: Creating Kernel from program. (clCreateKernel)", status);
  999. return false;
  1000. }
  1001. free((void*)cgpu->kname);
  1002. cgpu->kname = strdup(kernel_file);
  1003. #ifdef USE_SCRYPT
  1004. if (kernelinfo->interface == KL_SCRYPT && !clState->padbufsize)
  1005. {
  1006. size_t ipt = (1024 / data->lookup_gap + (1024 % data->lookup_gap > 0));
  1007. size_t bufsize = 128 * ipt * data->thread_concurrency;
  1008. /* Use the max alloc value which has been rounded to a power of
  1009. * 2 greater >= required amount earlier */
  1010. if (bufsize > data->max_alloc) {
  1011. applog(LOG_WARNING, "Maximum buffer memory device %d supports says %lu", gpu, (unsigned long)data->max_alloc);
  1012. applog(LOG_WARNING, "Your scrypt settings come to %lu", (unsigned long)bufsize);
  1013. }
  1014. applog(LOG_DEBUG, "Creating scrypt buffer sized %lu", (unsigned long)bufsize);
  1015. clState->padbufsize = bufsize;
  1016. /* This buffer is weird and might work to some degree even if
  1017. * the create buffer call has apparently failed, so check if we
  1018. * get anything back before we call it a failure. */
  1019. clState->padbuffer8 = NULL;
  1020. clState->padbuffer8 = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, bufsize, NULL, &status);
  1021. if (status != CL_SUCCESS && !clState->padbuffer8) {
  1022. applog(LOG_ERR, "Error %d: clCreateBuffer (padbuffer8), decrease TC or increase LG", status);
  1023. return false;
  1024. }
  1025. clState->CLbuffer0 = clCreateBuffer(clState->context, CL_MEM_READ_ONLY, 128, NULL, &status);
  1026. if (status != CL_SUCCESS) {
  1027. applog(LOG_ERR, "Error %d: clCreateBuffer (CLbuffer0)", status);
  1028. return false;
  1029. }
  1030. }
  1031. #endif
  1032. kernelinfo->loaded = true;
  1033. return true;
  1034. }
  1035. #endif /* USE_OPENCL */