ocl.c 39 KB

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