ocl.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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 HAVE_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, CL_MEM_WRITE_ONLY, 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. bool patchbfi = false, prog_built = false;
  607. struct opencl_device_data * const data = cgpu->device_data;
  608. const char * const vbuff = clState->platform_ver_str;
  609. char *s;
  610. cl_int status;
  611. /* Create binary filename based on parameters passed to opencl
  612. * compiler to ensure we only load a binary that matches what would
  613. * have otherwise created. The filename is:
  614. * kernelname + name +/- g(offset) + v + vectors + w + work_size + l + sizeof(long) + p + platform version + .bin
  615. * For scrypt the filename is:
  616. * kernelname + name + g + lg + lookup_gap + tc + thread_concurrency + w + work_size + l + sizeof(long) + p + platform version + .bin
  617. */
  618. char binaryfilename[255];
  619. char filename[255];
  620. char numbuf[32];
  621. snprintf(filename, sizeof(filename), "%s.cl", kernel_file);
  622. snprintf(binaryfilename, sizeof(filename), "%s", kernel_file);
  623. int pl;
  624. char *source = opencl_kernel_source(filename, &pl, &kernelinfo->interface);
  625. if (!source)
  626. return false;
  627. {
  628. uint8_t hash[0x20];
  629. char hashhex[7];
  630. sha256((void*)source, pl, hash);
  631. bin2hex(hashhex, hash, 3);
  632. tailsprintf(binaryfilename, sizeof(binaryfilename), "-%s", hashhex);
  633. }
  634. switch (kernelinfo->interface)
  635. {
  636. case KL_NONE:
  637. applog(LOG_ERR, "%s: Failed to identify kernel interface for %s",
  638. cgpu->dev_repr, kernel_file);
  639. free(source);
  640. return false;
  641. case KL_PHATK:
  642. if ((strstr(vbuff, "844.4") || strstr(vbuff, "851.4") ||
  643. strstr(vbuff, "831.4") || strstr(vbuff, "898.1") ||
  644. strstr(vbuff, "923.1") || strstr(vbuff, "938.2") ||
  645. strstr(vbuff, "1113.2"))) {
  646. applog(LOG_WARNING, "WARNING: You have selected the phatk kernel.");
  647. applog(LOG_WARNING, "You are running SDK 2.6+ which performs poorly with this kernel.");
  648. applog(LOG_WARNING, "Downgrade your SDK and delete any .bin files before starting again.");
  649. applog(LOG_WARNING, "Or allow BFGMiner to automatically choose a more suitable kernel.");
  650. }
  651. default:
  652. ;
  653. }
  654. applog(LOG_DEBUG, "%s: Using kernel %s with interface %s",
  655. cgpu->dev_repr, kernel_file,
  656. opencl_get_kernel_interface_name(kernelinfo->interface));
  657. if (((kernelinfo->interface == KL_POCLBM || kernelinfo->interface == KL_DIABLO || kernelinfo->interface == KL_DIAKGCN || kernelinfo->interface) && clState->vwidth == 1 && clState->hasOpenCL11plus) || kernelinfo->interface == KL_SCRYPT)
  658. kernelinfo->goffset = true;
  659. if (data->work_size && data->work_size <= clState->max_work_size)
  660. kernelinfo->wsize = data->work_size;
  661. else
  662. #ifdef USE_SCRYPT
  663. if (malgo->algo == POW_SCRYPT)
  664. kernelinfo->wsize = 256;
  665. else
  666. #endif
  667. if (strstr(name, "Tahiti"))
  668. kernelinfo->wsize = 64;
  669. else
  670. kernelinfo->wsize = (clState->max_work_size <= 256 ? clState->max_work_size : 256) / clState->vwidth;
  671. #ifdef USE_SCRYPT
  672. if (kernelinfo->interface == KL_SCRYPT)
  673. {
  674. if (!data->thread_concurrency)
  675. {
  676. unsigned int sixtyfours;
  677. sixtyfours = data->max_alloc / 131072 / 64 - 1;
  678. data->thread_concurrency = sixtyfours * 64;
  679. if (data->shaders && data->thread_concurrency > data->shaders) {
  680. data->thread_concurrency -= data->thread_concurrency % data->shaders;
  681. if (data->thread_concurrency > data->shaders * 5)
  682. data->thread_concurrency = data->shaders * 5;
  683. }
  684. applog(LOG_DEBUG, "GPU %u: selecting thread concurrency of %lu", gpu, (unsigned long)data->thread_concurrency);
  685. }
  686. }
  687. #endif
  688. FILE *binaryfile;
  689. size_t *binary_sizes;
  690. char **binaries;
  691. size_t sourceSize[] = {(size_t)pl};
  692. cl_uint slot, cpnd;
  693. slot = cpnd = 0;
  694. binary_sizes = calloc(sizeof(size_t) * MAX_GPUDEVICES * 4, 1);
  695. if (unlikely(!binary_sizes)) {
  696. applog(LOG_ERR, "Unable to calloc binary_sizes");
  697. return false;
  698. }
  699. binaries = calloc(sizeof(char *) * MAX_GPUDEVICES * 4, 1);
  700. if (unlikely(!binaries)) {
  701. applog(LOG_ERR, "Unable to calloc binaries");
  702. return false;
  703. }
  704. strcat(binaryfilename, name);
  705. if (kernelinfo->goffset)
  706. strcat(binaryfilename, "g");
  707. #ifdef USE_SCRYPT
  708. if (kernelinfo->interface == KL_SCRYPT)
  709. {
  710. sprintf(numbuf, "lg%utc%u", data->lookup_gap, (unsigned int)data->thread_concurrency);
  711. strcat(binaryfilename, numbuf);
  712. }
  713. else
  714. #endif
  715. {
  716. sprintf(numbuf, "v%d", clState->vwidth);
  717. strcat(binaryfilename, numbuf);
  718. }
  719. sprintf(numbuf, "w%d", (int)kernelinfo->wsize);
  720. strcat(binaryfilename, numbuf);
  721. sprintf(numbuf, "l%d", (int)sizeof(long));
  722. strcat(binaryfilename, numbuf);
  723. strcat(binaryfilename, "p");
  724. strcat(binaryfilename, vbuff);
  725. sanestr(binaryfilename, binaryfilename);
  726. applog(LOG_DEBUG, "OCL%2u: Configured OpenCL kernel name: %s", gpu, binaryfilename);
  727. strcat(binaryfilename, ".bin");
  728. if (!(data->opt_opencl_binaries & OBU_LOAD))
  729. goto build;
  730. binaryfile = fopen(binaryfilename, "rb");
  731. if (!binaryfile) {
  732. applog(LOG_DEBUG, "No binary found, generating from source");
  733. } else {
  734. struct stat binary_stat;
  735. if (unlikely(stat(binaryfilename, &binary_stat))) {
  736. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  737. fclose(binaryfile);
  738. goto build;
  739. }
  740. if (!binary_stat.st_size)
  741. goto build;
  742. binary_sizes[slot] = binary_stat.st_size;
  743. binaries[slot] = (char *)calloc(binary_sizes[slot], 1);
  744. if (unlikely(!binaries[slot])) {
  745. applog(LOG_ERR, "Unable to calloc binaries");
  746. fclose(binaryfile);
  747. return false;
  748. }
  749. if (fread(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot]) {
  750. applog(LOG_ERR, "Unable to fread binaries");
  751. fclose(binaryfile);
  752. free(binaries[slot]);
  753. goto build;
  754. }
  755. kernelinfo->program = clCreateProgramWithBinary(clState->context, 1, &clState->devid, &binary_sizes[slot], (const unsigned char **)binaries, &status, NULL);
  756. if (status != CL_SUCCESS) {
  757. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  758. fclose(binaryfile);
  759. free(binaries[slot]);
  760. goto build;
  761. }
  762. fclose(binaryfile);
  763. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  764. goto built;
  765. }
  766. /////////////////////////////////////////////////////////////////
  767. // Load CL file, build CL program object, create CL kernel object
  768. /////////////////////////////////////////////////////////////////
  769. build:
  770. kernelinfo->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  771. if (status != CL_SUCCESS) {
  772. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithSource)", status);
  773. return false;
  774. }
  775. /* create a cl program executable for all the devices specified */
  776. char *CompilerOptions = calloc(1, 256);
  777. #ifdef USE_SCRYPT
  778. if (kernelinfo->interface == KL_SCRYPT)
  779. sprintf(CompilerOptions, "-D LOOKUP_GAP=%d -D CONCURRENT_THREADS=%d -D WORKSIZE=%d",
  780. data->lookup_gap, (unsigned int)data->thread_concurrency, (int)kernelinfo->wsize);
  781. else
  782. #endif
  783. {
  784. sprintf(CompilerOptions, "-D WORKSIZE=%d -D VECTORS%d -D WORKVEC=%d",
  785. (int)kernelinfo->wsize, clState->vwidth, (int)kernelinfo->wsize * clState->vwidth);
  786. }
  787. applog(LOG_DEBUG, "Setting worksize to %"PRId64, (int64_t)kernelinfo->wsize);
  788. if (clState->vwidth > 1)
  789. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->vwidth);
  790. if (clState->hasBitAlign) {
  791. strcat(CompilerOptions, " -D BITALIGN");
  792. applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
  793. if (strstr(name, "Cedar") ||
  794. strstr(name, "Redwood") ||
  795. strstr(name, "Juniper") ||
  796. strstr(name, "Cypress" ) ||
  797. strstr(name, "Hemlock" ) ||
  798. strstr(name, "Caicos" ) ||
  799. strstr(name, "Turks" ) ||
  800. strstr(name, "Barts" ) ||
  801. strstr(name, "Cayman" ) ||
  802. strstr(name, "Antilles" ) ||
  803. strstr(name, "Wrestler" ) ||
  804. strstr(name, "Zacate" ) ||
  805. strstr(name, "WinterPark" ))
  806. {
  807. // BFI_INT patching only works with AMD-APP up to 1084
  808. if (strstr(vbuff, "ATI-Stream"))
  809. patchbfi = true;
  810. else
  811. if ((s = strstr(vbuff, "AMD-APP")) && (s = strchr(s, '(')) && atoi(&s[1]) < 1085)
  812. patchbfi = true;
  813. }
  814. } else
  815. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
  816. if (patchbfi) {
  817. if (data->opt_opencl_binaries == OBU_LOADSAVE)
  818. {
  819. strcat(CompilerOptions, " -D BFI_INT");
  820. applog(LOG_DEBUG, "BFI_INT patch requiring device found, patched source with BFI_INT");
  821. }
  822. else
  823. {
  824. patchbfi = false;
  825. applog(LOG_WARNING, "BFI_INT patch requiring device found, but OpenCL binary usage disabled; cannot BFI_INT patch");
  826. }
  827. } else
  828. applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
  829. if (kernelinfo->goffset)
  830. strcat(CompilerOptions, " -D GOFFSET");
  831. if (!clState->hasOpenCL11plus)
  832. strcat(CompilerOptions, " -D OCL1");
  833. applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
  834. status = bfg_clBuildProgram(&kernelinfo->program, clState->devid, CompilerOptions);
  835. free(CompilerOptions);
  836. if (status != CL_SUCCESS)
  837. return false;
  838. prog_built = true;
  839. if (!(data->opt_opencl_binaries & OBU_SAVE))
  840. goto built;
  841. status = clGetProgramInfo(kernelinfo->program, CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), &cpnd, NULL);
  842. if (unlikely(status != CL_SUCCESS)) {
  843. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_NUM_DEVICES. (clGetProgramInfo)", status);
  844. return false;
  845. }
  846. status = clGetProgramInfo(kernelinfo->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*cpnd, binary_sizes, NULL);
  847. if (unlikely(status != CL_SUCCESS)) {
  848. applog(LOG_ERR, "Error %d: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetProgramInfo)", status);
  849. return false;
  850. }
  851. /* The actual compiled binary ends up in a RANDOM slot! Grr, so we have
  852. * to iterate over all the binary slots and find where the real program
  853. * is. What the heck is this!? */
  854. for (slot = 0; slot < cpnd; slot++)
  855. if (binary_sizes[slot])
  856. break;
  857. /* copy over all of the generated binaries. */
  858. applog(LOG_DEBUG, "Binary size for gpu %u found in binary slot %u: %"PRId64,
  859. gpu, (unsigned)slot, (int64_t)binary_sizes[slot]);
  860. if (!binary_sizes[slot]) {
  861. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, FAIL!");
  862. return false;
  863. }
  864. binaries[slot] = calloc(sizeof(char) * binary_sizes[slot], 1);
  865. status = clGetProgramInfo(kernelinfo->program, CL_PROGRAM_BINARIES, sizeof(char *) * cpnd, binaries, NULL );
  866. if (unlikely(status != CL_SUCCESS)) {
  867. applog(LOG_ERR, "Error %d: Getting program info. CL_PROGRAM_BINARIES (clGetProgramInfo)", status);
  868. return false;
  869. }
  870. /* Patch the kernel if the hardware supports BFI_INT but it needs to
  871. * be hacked in */
  872. if (patchbfi) {
  873. unsigned remaining = binary_sizes[slot];
  874. char *w = binaries[slot];
  875. unsigned int start, length;
  876. /* Find 2nd incidence of .text, and copy the program's
  877. * position and length at a fixed offset from that. Then go
  878. * back and find the 2nd incidence of \x7ELF (rewind by one
  879. * from ELF) and then patch the opcocdes */
  880. if (!advance(&w, &remaining, ".text"))
  881. goto build;
  882. w++; remaining--;
  883. if (!advance(&w, &remaining, ".text")) {
  884. /* 32 bit builds only one ELF */
  885. w--; remaining++;
  886. }
  887. memcpy(&start, w + 285, 4);
  888. memcpy(&length, w + 289, 4);
  889. w = binaries[slot]; remaining = binary_sizes[slot];
  890. if (!advance(&w, &remaining, "ELF"))
  891. goto build;
  892. w++; remaining--;
  893. if (!advance(&w, &remaining, "ELF")) {
  894. /* 32 bit builds only one ELF */
  895. w--; remaining++;
  896. }
  897. w--; remaining++;
  898. w += start; remaining -= start;
  899. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  900. w, remaining);
  901. patch_opcodes(w, length);
  902. status = clReleaseProgram(kernelinfo->program);
  903. if (status != CL_SUCCESS) {
  904. applog(LOG_ERR, "Error %d: Releasing program. (clReleaseProgram)", status);
  905. return false;
  906. }
  907. kernelinfo->program = clCreateProgramWithBinary(clState->context, 1, &clState->devid, &binary_sizes[slot], (const unsigned char **)&binaries[slot], &status, NULL);
  908. if (status != CL_SUCCESS) {
  909. applog(LOG_ERR, "Error %d: Loading Binary into cl_program (clCreateProgramWithBinary)", status);
  910. return false;
  911. }
  912. /* Program needs to be rebuilt */
  913. prog_built = false;
  914. }
  915. free(source);
  916. /* Save the binary to be loaded next time */
  917. binaryfile = fopen(binaryfilename, "wb");
  918. if (!binaryfile) {
  919. /* Not a fatal problem, just means we build it again next time */
  920. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  921. } else {
  922. if (unlikely(fwrite(binaries[slot], 1, binary_sizes[slot], binaryfile) != binary_sizes[slot])) {
  923. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  924. return false;
  925. }
  926. fclose(binaryfile);
  927. }
  928. built:
  929. if (binaries[slot])
  930. free(binaries[slot]);
  931. free(binaries);
  932. free(binary_sizes);
  933. applog(LOG_INFO, "Initialising kernel %s with%s bitalign, %"PRId64" vectors and worksize %"PRIu64,
  934. filename, clState->hasBitAlign ? "" : "out", (int64_t)clState->vwidth, (uint64_t)kernelinfo->wsize);
  935. if (!prog_built) {
  936. /* create a cl program executable for all the devices specified */
  937. status = bfg_clBuildProgram(&kernelinfo->program, clState->devid, NULL);
  938. if (status != CL_SUCCESS)
  939. return false;
  940. }
  941. /* get a kernel object handle for a kernel with the given name */
  942. kernelinfo->kernel = clCreateKernel(kernelinfo->program, "search", &status);
  943. if (status != CL_SUCCESS) {
  944. applog(LOG_ERR, "Error %d: Creating Kernel from program. (clCreateKernel)", status);
  945. return false;
  946. }
  947. free((void*)cgpu->kname);
  948. cgpu->kname = strdup(kernel_file);
  949. #ifdef USE_SCRYPT
  950. if (kernelinfo->interface == KL_SCRYPT && !clState->padbufsize)
  951. {
  952. size_t ipt = (1024 / data->lookup_gap + (1024 % data->lookup_gap > 0));
  953. size_t bufsize = 128 * ipt * data->thread_concurrency;
  954. /* Use the max alloc value which has been rounded to a power of
  955. * 2 greater >= required amount earlier */
  956. if (bufsize > data->max_alloc) {
  957. applog(LOG_WARNING, "Maximum buffer memory device %d supports says %lu", gpu, (unsigned long)data->max_alloc);
  958. applog(LOG_WARNING, "Your scrypt settings come to %lu", (unsigned long)bufsize);
  959. }
  960. applog(LOG_DEBUG, "Creating scrypt buffer sized %lu", (unsigned long)bufsize);
  961. clState->padbufsize = bufsize;
  962. /* This buffer is weird and might work to some degree even if
  963. * the create buffer call has apparently failed, so check if we
  964. * get anything back before we call it a failure. */
  965. clState->padbuffer8 = NULL;
  966. clState->padbuffer8 = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, bufsize, NULL, &status);
  967. if (status != CL_SUCCESS && !clState->padbuffer8) {
  968. applog(LOG_ERR, "Error %d: clCreateBuffer (padbuffer8), decrease TC or increase LG", status);
  969. return false;
  970. }
  971. clState->CLbuffer0 = clCreateBuffer(clState->context, CL_MEM_READ_ONLY, 128, NULL, &status);
  972. if (status != CL_SUCCESS) {
  973. applog(LOG_ERR, "Error %d: clCreateBuffer (CLbuffer0)", status);
  974. return false;
  975. }
  976. }
  977. #endif
  978. kernelinfo->loaded = true;
  979. return true;
  980. }
  981. #endif /* HAVE_OPENCL */