ocl.c 20 KB

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