ocl.c 38 KB

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