ocl.c 18 KB

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