ocl.c 39 KB

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