ocl.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * Copyright 2011 Con Kolivas
  3. */
  4. #include "config.h"
  5. #ifdef HAVE_OPENCL
  6. #include <signal.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <sys/types.h>
  11. #ifdef WIN32
  12. #include <winsock2.h>
  13. #else
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <netdb.h>
  17. #endif
  18. #include <time.h>
  19. #include <sys/time.h>
  20. #include <pthread.h>
  21. #include <sys/stat.h>
  22. #include <unistd.h>
  23. #include "findnonce.h"
  24. #include "ocl.h"
  25. extern int opt_vectors;
  26. extern int opt_worksize;
  27. char *file_contents(const char *filename, int *length)
  28. {
  29. char *fullpath = alloca(PATH_MAX);
  30. void *buffer;
  31. FILE *f;
  32. strcpy(fullpath, opt_kernel_path);
  33. strcat(fullpath, filename);
  34. /* Try in the optional kernel path or installed prefix first */
  35. f = fopen(fullpath, "rb");
  36. if (!f) {
  37. /* Then try from the path cgminer was called */
  38. strcpy(fullpath, cgminer_path);
  39. strcat(fullpath, filename);
  40. f = fopen(fullpath, "rb");
  41. }
  42. /* Finally try opening it directly */
  43. if (!f)
  44. f = fopen(filename, "rb");
  45. if (!f) {
  46. applog(LOG_ERR, "Unable to open %s or %s for reading", filename, fullpath);
  47. return NULL;
  48. }
  49. fseek(f, 0, SEEK_END);
  50. *length = ftell(f);
  51. fseek(f, 0, SEEK_SET);
  52. buffer = malloc(*length+1);
  53. *length = fread(buffer, 1, *length, f);
  54. fclose(f);
  55. ((char*)buffer)[*length] = '\0';
  56. return (char*)buffer;
  57. }
  58. int clDevicesNum() {
  59. cl_int status = 0;
  60. cl_uint numPlatforms;
  61. cl_platform_id platform = NULL;
  62. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  63. /* If this fails, assume no GPUs. */
  64. if (status != CL_SUCCESS) {
  65. applog(LOG_INFO, "clGetPlatformsIDs failed (no GPU?)");
  66. return 0;
  67. }
  68. if (numPlatforms > 0) {
  69. cl_platform_id* platforms = (cl_platform_id *)malloc(numPlatforms*sizeof(cl_platform_id));
  70. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  71. if (status != CL_SUCCESS)
  72. {
  73. applog(LOG_ERR, "Error: Getting Platform Ids. (clGetPlatformsIDs)");
  74. return -1;
  75. }
  76. unsigned int i;
  77. for (i = 0; i < numPlatforms; ++i) {
  78. char pbuff[100];
  79. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  80. if (status != CL_SUCCESS)
  81. {
  82. applog(LOG_ERR, "Error: Getting Platform Info. (clGetPlatformInfo)");
  83. free(platforms);
  84. return -1;
  85. }
  86. platform = platforms[i];
  87. if (!strcmp(pbuff, "Advanced Micro Devices, Inc."))
  88. {
  89. break;
  90. }
  91. }
  92. free(platforms);
  93. }
  94. if (platform == NULL) {
  95. perror("NULL platform found!\n");
  96. return -1;
  97. }
  98. cl_uint numDevices;
  99. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  100. if (status != CL_SUCCESS) {
  101. applog(LOG_ERR, "Error: Getting Device IDs (num)");
  102. return -1;
  103. }
  104. return numDevices;
  105. }
  106. static int advance(char **area, unsigned *remaining, const char *marker)
  107. {
  108. char *find = memmem(*area, *remaining, marker, strlen(marker));
  109. if (!find) {
  110. if (opt_debug)
  111. applog(LOG_DEBUG, "Marker \"%s\" not found", marker);
  112. return 0;
  113. }
  114. *remaining -= find - *area;
  115. *area = find;
  116. return 1;
  117. }
  118. #define OP3_INST_BFE_UINT 4ULL
  119. #define OP3_INST_BFE_INT 5ULL
  120. #define OP3_INST_BFI_INT 6ULL
  121. #define OP3_INST_BIT_ALIGN_INT 12ULL
  122. #define OP3_INST_BYTE_ALIGN_INT 13ULL
  123. void patch_opcodes(char *w, unsigned remaining)
  124. {
  125. uint64_t *opcode = (uint64_t *)w;
  126. int patched = 0;
  127. int count_bfe_int = 0;
  128. int count_bfe_uint = 0;
  129. int count_byte_align = 0;
  130. while (42) {
  131. int clamp = (*opcode >> (32 + 31)) & 0x1;
  132. int dest_rel = (*opcode >> (32 + 28)) & 0x1;
  133. int alu_inst = (*opcode >> (32 + 13)) & 0x1f;
  134. int s2_neg = (*opcode >> (32 + 12)) & 0x1;
  135. int s2_rel = (*opcode >> (32 + 9)) & 0x1;
  136. int pred_sel = (*opcode >> 29) & 0x3;
  137. if (!clamp && !dest_rel && !s2_neg && !s2_rel && !pred_sel) {
  138. if (alu_inst == OP3_INST_BFE_INT) {
  139. count_bfe_int++;
  140. } else if (alu_inst == OP3_INST_BFE_UINT) {
  141. count_bfe_uint++;
  142. } else if (alu_inst == OP3_INST_BYTE_ALIGN_INT) {
  143. count_byte_align++;
  144. // patch this instruction to BFI_INT
  145. *opcode &= 0xfffc1fffffffffffULL;
  146. *opcode |= OP3_INST_BFI_INT << (32 + 13);
  147. patched++;
  148. }
  149. }
  150. if (remaining <= 8)
  151. break;
  152. opcode++;
  153. remaining -= 8;
  154. }
  155. if (opt_debug) {
  156. applog(LOG_DEBUG, "Potential OP3 instructions identified: "
  157. "%i BFE_INT, %i BFE_UINT, %i BYTE_ALIGN",
  158. count_bfe_int, count_bfe_uint, count_byte_align);
  159. applog(LOG_DEBUG, "Patched a total of %i BFI_INT instructions", patched);
  160. }
  161. }
  162. _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
  163. {
  164. bool patchbfi = false, prog_built = false;
  165. cl_int status = 0;
  166. unsigned int i;
  167. _clState *clState = calloc(1, sizeof(_clState));
  168. cl_uint numPlatforms;
  169. cl_platform_id platform = NULL;
  170. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  171. if (status != CL_SUCCESS) {
  172. applog(LOG_ERR, "Error: Getting Platforms. (clGetPlatformsIDs)");
  173. return NULL;
  174. }
  175. if (numPlatforms > 0) {
  176. cl_platform_id* platforms = (cl_platform_id *)malloc(numPlatforms*sizeof(cl_platform_id));
  177. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  178. if (status != CL_SUCCESS)
  179. {
  180. applog(LOG_ERR, "Error: Getting Platform Ids. (clGetPlatformsIDs)");
  181. return NULL;
  182. }
  183. for(i = 0; i < numPlatforms; ++i) {
  184. char pbuff[100];
  185. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  186. if (status != CL_SUCCESS)
  187. {
  188. applog(LOG_ERR, "Error: Getting Platform Info. (clGetPlatformInfo)");
  189. free(platforms);
  190. return NULL;
  191. }
  192. platform = platforms[i];
  193. if (!strcmp(pbuff, "Advanced Micro Devices, Inc."))
  194. {
  195. break;
  196. }
  197. }
  198. free(platforms);
  199. }
  200. if (platform == NULL) {
  201. perror("NULL platform found!\n");
  202. return NULL;
  203. }
  204. cl_uint numDevices;
  205. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  206. if (status != CL_SUCCESS) {
  207. applog(LOG_ERR, "Error: Getting Device IDs (num)");
  208. return NULL;
  209. }
  210. cl_device_id *devices;
  211. if (numDevices > 0 ) {
  212. devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  213. /* Now, get the device list data */
  214. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  215. if (status != CL_SUCCESS) {
  216. applog(LOG_ERR, "Error: Getting Device IDs (list)");
  217. return NULL;
  218. }
  219. applog(LOG_INFO, "List of devices:");
  220. unsigned int i;
  221. for (i = 0; i < numDevices; i++) {
  222. char pbuff[100];
  223. status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  224. if (status != CL_SUCCESS) {
  225. applog(LOG_ERR, "Error: Getting Device Info");
  226. return NULL;
  227. }
  228. applog(LOG_INFO, "\t%i\t%s", i, pbuff);
  229. }
  230. if (gpu < numDevices) {
  231. char pbuff[100];
  232. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  233. if (status != CL_SUCCESS) {
  234. applog(LOG_ERR, "Error: Getting Device Info");
  235. return NULL;
  236. }
  237. applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
  238. strncpy(name, pbuff, nameSize);
  239. } else {
  240. applog(LOG_ERR, "Invalid GPU %i", gpu);
  241. return NULL;
  242. }
  243. } else return NULL;
  244. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
  245. clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
  246. if (status != CL_SUCCESS) {
  247. applog(LOG_ERR, "Error: Creating Context. (clCreateContextFromType)");
  248. return NULL;
  249. }
  250. /* Check for BFI INT support. Hopefully people don't mix devices with
  251. * and without it! */
  252. char * extensions = malloc(1024);
  253. const char * camo = "cl_amd_media_ops";
  254. char *find;
  255. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_EXTENSIONS, 1024, (void *)extensions, NULL);
  256. if (status != CL_SUCCESS) {
  257. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS");
  258. return NULL;
  259. }
  260. find = strstr(extensions, camo);
  261. if (find)
  262. clState->hasBitAlign = true;
  263. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&clState->preferred_vwidth, NULL);
  264. if (status != CL_SUCCESS) {
  265. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT");
  266. return NULL;
  267. }
  268. if (opt_debug)
  269. applog(LOG_DEBUG, "Preferred vector width reported %d", clState->preferred_vwidth);
  270. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&clState->max_work_size, NULL);
  271. if (status != CL_SUCCESS) {
  272. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE");
  273. return NULL;
  274. }
  275. if (opt_debug)
  276. applog(LOG_DEBUG, "Max work group size reported %d", clState->max_work_size);
  277. /* For some reason 2 vectors is still better even if the card says
  278. * otherwise, and many cards lie about their max so use 256 as max
  279. * unless explicitly set on the command line. 79x0 cards perform
  280. * better without vectors */
  281. if (clState->preferred_vwidth > 1) {
  282. if (strstr(name, "Tahiti"))
  283. clState->preferred_vwidth = 1;
  284. else
  285. clState->preferred_vwidth = 2;
  286. }
  287. if (opt_vectors)
  288. clState->preferred_vwidth = opt_vectors;
  289. if (opt_worksize && opt_worksize <= clState->max_work_size)
  290. clState->work_size = opt_worksize;
  291. else
  292. clState->work_size = (clState->max_work_size <= 256 ? clState->max_work_size : 256) /
  293. clState->preferred_vwidth;
  294. /* Create binary filename based on parameters passed to opencl
  295. * compiler to ensure we only load a binary that matches what would
  296. * have otherwise created. The filename is:
  297. * name + kernelname +/i bitalign + v + vectors + w + work_size + sizeof(long) + .bin
  298. */
  299. char binaryfilename[255];
  300. char numbuf[10];
  301. char filename[16];
  302. if (chosen_kernel == KL_NONE) {
  303. if (clState->hasBitAlign)
  304. chosen_kernel = KL_PHATK;
  305. else
  306. chosen_kernel = KL_POCLBM;
  307. }
  308. switch (chosen_kernel) {
  309. case KL_POCLBM:
  310. strcpy(filename, "poclbm110817.cl");
  311. strcpy(binaryfilename, "poclbm110817");
  312. break;
  313. case KL_NONE: /* Shouldn't happen */
  314. case KL_PHATK:
  315. strcpy(filename, "phatk110817.cl");
  316. strcpy(binaryfilename, "phatk110817");
  317. break;
  318. }
  319. FILE *binaryfile;
  320. size_t *binary_sizes;
  321. char **binaries;
  322. int pl;
  323. char *source = file_contents(filename, &pl);
  324. size_t sourceSize[] = {(size_t)pl};
  325. if (!source)
  326. return NULL;
  327. binary_sizes = (size_t *)malloc(sizeof(size_t)*numDevices);
  328. if (unlikely(!binary_sizes)) {
  329. applog(LOG_ERR, "Unable to malloc binary_sizes");
  330. return NULL;
  331. }
  332. binaries = (char **)malloc(sizeof(char *)*numDevices);
  333. if (unlikely(!binaries)) {
  334. applog(LOG_ERR, "Unable to malloc binaries");
  335. return NULL;
  336. }
  337. strcat(binaryfilename, name);
  338. if (clState->hasBitAlign)
  339. strcat(binaryfilename, "bitalign");
  340. strcat(binaryfilename, "v");
  341. sprintf(numbuf, "%d", clState->preferred_vwidth);
  342. strcat(binaryfilename, numbuf);
  343. strcat(binaryfilename, "w");
  344. sprintf(numbuf, "%d", (int)clState->work_size);
  345. strcat(binaryfilename, numbuf);
  346. strcat(binaryfilename, "long");
  347. sprintf(numbuf, "%d", (int)sizeof(long));
  348. strcat(binaryfilename, numbuf);
  349. strcat(binaryfilename, ".bin");
  350. binaryfile = fopen(binaryfilename, "rb");
  351. if (!binaryfile) {
  352. if (opt_debug)
  353. applog(LOG_DEBUG, "No binary found, generating from source");
  354. } else {
  355. struct stat binary_stat;
  356. if (unlikely(stat(binaryfilename, &binary_stat))) {
  357. if (opt_debug)
  358. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  359. fclose(binaryfile);
  360. goto build;
  361. }
  362. if (!binary_stat.st_size)
  363. goto build;
  364. binary_sizes[gpu] = binary_stat.st_size;
  365. binaries[gpu] = (char *)malloc(binary_sizes[gpu]);
  366. if (unlikely(!binaries[gpu])) {
  367. applog(LOG_ERR, "Unable to malloc binaries");
  368. fclose(binaryfile);
  369. return NULL;
  370. }
  371. if (fread(binaries[gpu], 1, binary_sizes[gpu], binaryfile) != binary_sizes[gpu]) {
  372. applog(LOG_ERR, "Unable to fread binaries[gpu]");
  373. fclose(binaryfile);
  374. free(binaries[gpu]);
  375. goto build;
  376. }
  377. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[gpu], (const unsigned char **)&binaries[gpu], &status, NULL);
  378. if (status != CL_SUCCESS) {
  379. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  380. fclose(binaryfile);
  381. free(binaries[gpu]);
  382. goto build;
  383. }
  384. fclose(binaryfile);
  385. if (opt_debug)
  386. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  387. /* We don't need to patch this already loaded image, but need to
  388. * set the flag for status later */
  389. if (clState->hasBitAlign)
  390. patchbfi = true;
  391. free(binaries[gpu]);
  392. goto built;
  393. }
  394. /////////////////////////////////////////////////////////////////
  395. // Load CL file, build CL program object, create CL kernel object
  396. /////////////////////////////////////////////////////////////////
  397. build:
  398. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  399. if (status != CL_SUCCESS) {
  400. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithSource)");
  401. return NULL;
  402. }
  403. /* create a cl program executable for all the devices specified */
  404. char *CompilerOptions = calloc(1, 256);
  405. sprintf(CompilerOptions, "-D WORKSIZE=%d -D VECTORS%d",
  406. (int)clState->work_size, clState->preferred_vwidth);
  407. if (opt_debug)
  408. applog(LOG_DEBUG, "Setting worksize to %d", clState->work_size);
  409. if (clState->preferred_vwidth > 1 && opt_debug)
  410. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->preferred_vwidth);
  411. if (clState->hasBitAlign) {
  412. strcat(CompilerOptions, " -D BITALIGN");
  413. if (opt_debug)
  414. applog(LOG_DEBUG, "cl_amd_media_ops found, setting BITALIGN");
  415. if (strstr(name, "Cedar") ||
  416. strstr(name, "Redwood") ||
  417. strstr(name, "Juniper") ||
  418. strstr(name, "Cypress" ) ||
  419. strstr(name, "Hemlock" ) ||
  420. strstr(name, "Caicos" ) ||
  421. strstr(name, "Turks" ) ||
  422. strstr(name, "Barts" ) ||
  423. strstr(name, "Cayman" ) ||
  424. strstr(name, "Antilles" ) ||
  425. strstr(name, "Wrestler" ) ||
  426. strstr(name, "Zacate" ) ||
  427. strstr(name, "WinterPark" ) ||
  428. strstr(name, "BeaverCreek" ))
  429. patchbfi = true;
  430. } else if (opt_debug)
  431. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not set BITALIGN");
  432. if (patchbfi) {
  433. strcat(CompilerOptions, " -D BFI_INT");
  434. if (opt_debug)
  435. applog(LOG_DEBUG, "BFI_INT patch requiring device found, patched source with BFI_INT");
  436. } else if (opt_debug)
  437. applog(LOG_DEBUG, "BFI_INT patch requiring device not found, will not BFI_INT patch");
  438. if (opt_debug)
  439. applog(LOG_DEBUG, "CompilerOptions: %s", CompilerOptions);
  440. status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
  441. free(CompilerOptions);
  442. if (status != CL_SUCCESS) {
  443. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  444. size_t logSize;
  445. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  446. char *log = malloc(logSize);
  447. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  448. applog(LOG_INFO, "%s", log);
  449. return NULL;
  450. }
  451. prog_built = true;
  452. status = clGetProgramInfo( clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*numDevices, binary_sizes, NULL );
  453. if (unlikely(status != CL_SUCCESS)) {
  454. applog(LOG_ERR, "Error: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetPlatformInfo)");
  455. return NULL;
  456. }
  457. /* copy over all of the generated binaries. */
  458. if (opt_debug)
  459. applog(LOG_DEBUG, "binary size %d : %d", gpu, binary_sizes[gpu]);
  460. if (!binary_sizes[gpu]) {
  461. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, may need to reboot!");
  462. return NULL;
  463. }
  464. binaries[gpu] = (char *)malloc( sizeof(char)*binary_sizes[gpu]);
  465. status = clGetProgramInfo( clState->program, CL_PROGRAM_BINARIES, sizeof(char *)*numDevices, binaries, NULL );
  466. if (unlikely(status != CL_SUCCESS)) {
  467. applog(LOG_ERR, "Error: Getting program info. (clGetPlatformInfo)");
  468. return NULL;
  469. }
  470. /* Patch the kernel if the hardware supports BFI_INT but it needs to
  471. * be hacked in */
  472. if (patchbfi) {
  473. unsigned remaining = binary_sizes[gpu];
  474. char *w = binaries[gpu];
  475. unsigned int start, length;
  476. /* Find 2nd incidence of .text, and copy the program's
  477. * position and length at a fixed offset from that. Then go
  478. * back and find the 2nd incidence of \x7ELF (rewind by one
  479. * from ELF) and then patch the opcocdes */
  480. if (!advance(&w, &remaining, ".text"))
  481. {patchbfi = 0; goto build;}
  482. w++; remaining--;
  483. if (!advance(&w, &remaining, ".text")) {
  484. /* 32 bit builds only one ELF */
  485. w--; remaining++;
  486. }
  487. memcpy(&start, w + 285, 4);
  488. memcpy(&length, w + 289, 4);
  489. w = binaries[gpu]; remaining = binary_sizes[gpu];
  490. if (!advance(&w, &remaining, "ELF"))
  491. {patchbfi = 0; goto build;}
  492. w++; remaining--;
  493. if (!advance(&w, &remaining, "ELF")) {
  494. /* 32 bit builds only one ELF */
  495. w--; remaining++;
  496. }
  497. w--; remaining++;
  498. w += start; remaining -= start;
  499. if (opt_debug)
  500. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  501. w, remaining);
  502. patch_opcodes(w, length);
  503. status = clReleaseProgram(clState->program);
  504. if (status != CL_SUCCESS) {
  505. applog(LOG_ERR, "Error: Releasing program. (clReleaseProgram)");
  506. return NULL;
  507. }
  508. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[gpu], (const unsigned char **)&binaries[gpu], &status, NULL);
  509. if (status != CL_SUCCESS) {
  510. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  511. return NULL;
  512. }
  513. }
  514. free(source);
  515. /* Save the binary to be loaded next time */
  516. binaryfile = fopen(binaryfilename, "wb");
  517. if (!binaryfile) {
  518. /* Not a fatal problem, just means we build it again next time */
  519. if (opt_debug)
  520. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  521. } else {
  522. if (unlikely(fwrite(binaries[gpu], 1, binary_sizes[gpu], binaryfile) != binary_sizes[gpu])) {
  523. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  524. return NULL;
  525. }
  526. fclose(binaryfile);
  527. }
  528. if (binaries[gpu])
  529. free(binaries[gpu]);
  530. built:
  531. free(binaries);
  532. free(binary_sizes);
  533. applog(LOG_INFO, "Initialising kernel %s with%s BFI_INT, %d vectors and worksize %d",
  534. filename, patchbfi ? "" : "out", clState->preferred_vwidth, clState->work_size);
  535. if (!prog_built) {
  536. /* create a cl program executable for all the devices specified */
  537. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  538. if (status != CL_SUCCESS) {
  539. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  540. size_t logSize;
  541. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  542. char *log = malloc(logSize);
  543. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  544. applog(LOG_INFO, "%s", log);
  545. return NULL;
  546. }
  547. }
  548. /* get a kernel object handle for a kernel with the given name */
  549. clState->kernel = clCreateKernel(clState->program, "search", &status);
  550. if (status != CL_SUCCESS) {
  551. applog(LOG_ERR, "Error: Creating Kernel from program. (clCreateKernel)");
  552. return NULL;
  553. }
  554. /////////////////////////////////////////////////////////////////
  555. // Create an OpenCL command queue
  556. /////////////////////////////////////////////////////////////////
  557. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu],
  558. CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &status);
  559. if (status != CL_SUCCESS) /* Try again without OOE enable */
  560. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0 , &status);
  561. if (status != CL_SUCCESS) {
  562. applog(LOG_ERR, "Creating Command Queue. (clCreateCommandQueue)");
  563. return NULL;
  564. }
  565. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, BUFFERSIZE, NULL, &status);
  566. if (status != CL_SUCCESS) {
  567. applog(LOG_ERR, "Error: clCreateBuffer (outputBuffer)");
  568. return NULL;
  569. }
  570. return clState;
  571. }
  572. #endif /* HAVE_OPENCL */