ocl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. cl_uint numDevices;
  206. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  207. if (status != CL_SUCCESS)
  208. {
  209. applog(LOG_ERR, "Error: Getting Device IDs (num)");
  210. return NULL;
  211. }
  212. cl_device_id *devices;
  213. if (numDevices > 0 ) {
  214. devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  215. /* Now, get the device list data */
  216. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  217. if (status != CL_SUCCESS)
  218. {
  219. applog(LOG_ERR, "Error: Getting Device IDs (list)");
  220. return NULL;
  221. }
  222. applog(LOG_INFO, "List of devices:");
  223. unsigned int i;
  224. for(i=0; i<numDevices; i++) {
  225. char pbuff[100];
  226. status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  227. if (status != CL_SUCCESS)
  228. {
  229. applog(LOG_ERR, "Error: Getting Device Info");
  230. return NULL;
  231. }
  232. applog(LOG_INFO, "\t%i\t%s", i, pbuff);
  233. }
  234. if (gpu < numDevices) {
  235. char pbuff[100];
  236. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  237. if (status != CL_SUCCESS)
  238. {
  239. applog(LOG_ERR, "Error: Getting Device Info");
  240. return NULL;
  241. }
  242. applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
  243. strncpy(name, pbuff, nameSize);
  244. } else {
  245. applog(LOG_ERR, "Invalid GPU %i", gpu);
  246. return NULL;
  247. }
  248. } else return NULL;
  249. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
  250. clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
  251. if (status != CL_SUCCESS)
  252. {
  253. applog(LOG_ERR, "Error: Creating Context. (clCreateContextFromType)");
  254. return NULL;
  255. }
  256. /* Check for BFI INT support. Hopefully people don't mix devices with
  257. * and without it! */
  258. char * extensions = malloc(1024);
  259. const char * camo = "cl_amd_media_ops";
  260. char *find;
  261. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_EXTENSIONS, 1024, (void *)extensions, NULL);
  262. if (status != CL_SUCCESS) {
  263. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_EXTENSIONS");
  264. return NULL;
  265. }
  266. find = strstr(extensions, camo);
  267. if (find)
  268. clState->hasBitAlign = patchbfi = 1;
  269. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, sizeof(cl_uint), (void *)&clState->preferred_vwidth, NULL);
  270. if (status != CL_SUCCESS) {
  271. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT");
  272. return NULL;
  273. }
  274. if (opt_debug)
  275. applog(LOG_DEBUG, "Preferred vector width reported %d", clState->preferred_vwidth);
  276. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(size_t), (void *)&clState->max_work_size, NULL);
  277. if (status != CL_SUCCESS) {
  278. applog(LOG_ERR, "Error: Failed to clGetDeviceInfo when trying to get CL_DEVICE_MAX_WORK_GROUP_SIZE");
  279. return NULL;
  280. }
  281. if (opt_debug)
  282. applog(LOG_DEBUG, "Max work group size reported %d", clState->max_work_size);
  283. /* For some reason 2 vectors is still better even if the card says
  284. * otherwise, and many cards lie about their max so use 256 as max
  285. * unless explicitly set on the command line */
  286. if (clState->preferred_vwidth > 1)
  287. clState->preferred_vwidth = 2;
  288. if (opt_vectors)
  289. clState->preferred_vwidth = opt_vectors;
  290. if (opt_worksize && opt_worksize <= clState->max_work_size)
  291. clState->work_size = opt_worksize;
  292. else
  293. clState->work_size = (clState->max_work_size <= 256 ? clState->max_work_size : 256) /
  294. clState->preferred_vwidth;
  295. /* Create binary filename based on parameters passed to opencl
  296. * compiler to ensure we only load a binary that matches what would
  297. * have otherwise created. The filename is:
  298. * name + kernelname +/i bitalign + v + vectors + w + work_size + sizeof(long) + .bin
  299. */
  300. char binaryfilename[255];
  301. char numbuf[10];
  302. char filename[16];
  303. if (chosen_kernel == KL_NONE) {
  304. if (clState->hasBitAlign)
  305. chosen_kernel = KL_PHATK;
  306. else
  307. chosen_kernel = KL_POCLBM;
  308. }
  309. switch (chosen_kernel) {
  310. case KL_POCLBM:
  311. strcpy(filename, "poclbm110817.cl");
  312. strcpy(binaryfilename, "poclbm110817");
  313. break;
  314. case KL_NONE: /* Shouldn't happen */
  315. case KL_PHATK:
  316. strcpy(filename, "phatk110817.cl");
  317. strcpy(binaryfilename, "phatk110817");
  318. break;
  319. }
  320. FILE *binaryfile;
  321. size_t *binary_sizes;
  322. char **binaries;
  323. int pl;
  324. char *source = file_contents(filename, &pl);
  325. size_t sourceSize[] = {(size_t)pl};
  326. if (!source)
  327. return NULL;
  328. binary_sizes = (size_t *)malloc(sizeof(size_t)*numDevices);
  329. if (unlikely(!binary_sizes)) {
  330. applog(LOG_ERR, "Unable to malloc binary_sizes");
  331. return NULL;
  332. }
  333. binaries = (char **)malloc(sizeof(char *)*numDevices);
  334. if (unlikely(!binaries)) {
  335. applog(LOG_ERR, "Unable to malloc binaries");
  336. return NULL;
  337. }
  338. strcat(binaryfilename, name);
  339. if (clState->hasBitAlign)
  340. strcat(binaryfilename, "bitalign");
  341. strcat(binaryfilename, "v");
  342. sprintf(numbuf, "%d", clState->preferred_vwidth);
  343. strcat(binaryfilename, numbuf);
  344. strcat(binaryfilename, "w");
  345. sprintf(numbuf, "%d", (int)clState->work_size);
  346. strcat(binaryfilename, numbuf);
  347. strcat(binaryfilename, "long");
  348. sprintf(numbuf, "%d", (int)sizeof(long));
  349. strcat(binaryfilename, numbuf);
  350. strcat(binaryfilename, ".bin");
  351. binaryfile = fopen(binaryfilename, "rb");
  352. if (!binaryfile) {
  353. if (opt_debug)
  354. applog(LOG_DEBUG, "No binary found, generating from source");
  355. } else {
  356. struct stat binary_stat;
  357. if (unlikely(stat(binaryfilename, &binary_stat))) {
  358. if (opt_debug)
  359. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  360. fclose(binaryfile);
  361. goto build;
  362. }
  363. binary_sizes[gpu] = binary_stat.st_size;
  364. binaries[gpu] = (char *)malloc(binary_sizes[gpu]);
  365. if (unlikely(!binaries[gpu])) {
  366. applog(LOG_ERR, "Unable to malloc binaries");
  367. fclose(binaryfile);
  368. return NULL;
  369. }
  370. if (fread(binaries[gpu], 1, binary_sizes[gpu], binaryfile) != binary_sizes[gpu]) {
  371. applog(LOG_ERR, "Unable to fread binaries[gpu]");
  372. fclose(binaryfile);
  373. goto build;
  374. }
  375. fclose(binaryfile);
  376. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[gpu], (const unsigned char **)&binaries[gpu], &status, NULL);
  377. if (status != CL_SUCCESS)
  378. {
  379. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  380. return NULL;
  381. }
  382. if (opt_debug)
  383. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  384. free(binaries[gpu]);
  385. goto built;
  386. }
  387. /////////////////////////////////////////////////////////////////
  388. // Load CL file, build CL program object, create CL kernel object
  389. /////////////////////////////////////////////////////////////////
  390. build:
  391. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  392. if (status != CL_SUCCESS) {
  393. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithSource)");
  394. return NULL;
  395. }
  396. clRetainProgram(clState->program);
  397. if (status != CL_SUCCESS) {
  398. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  399. return NULL;
  400. }
  401. /* create a cl program executable for all the devices specified */
  402. char CompilerOptions[256];
  403. sprintf(CompilerOptions, "-DWORKSIZE=%d -DVECTORS%d",
  404. (int)clState->work_size, clState->preferred_vwidth);
  405. if (opt_debug)
  406. applog(LOG_DEBUG, "Setting worksize to %d", clState->work_size);
  407. if (clState->preferred_vwidth > 1 && opt_debug)
  408. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->preferred_vwidth);
  409. if (clState->hasBitAlign) {
  410. strcat(CompilerOptions, " -DBITALIGN");
  411. if (opt_debug)
  412. applog(LOG_DEBUG, "cl_amd_media_ops found, patched source with BITALIGN");
  413. } else if (opt_debug)
  414. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not BITALIGN patch");
  415. if (patchbfi) {
  416. strcat(CompilerOptions, " -DBFI_INT");
  417. if (opt_debug)
  418. applog(LOG_DEBUG, "cl_amd_media_ops found, patched source with BFI_INT");
  419. } else if (opt_debug)
  420. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not BFI_INT patch");
  421. //int n = 1000;
  422. //while(n--)
  423. // printf("%s", CompilerOptions);
  424. //return 1;
  425. status = clBuildProgram(clState->program, 1, &devices[gpu], CompilerOptions , NULL, NULL);
  426. if (status != CL_SUCCESS) {
  427. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  428. size_t logSize;
  429. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  430. char *log = malloc(logSize);
  431. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  432. applog(LOG_INFO, "%s", log);
  433. return NULL;
  434. }
  435. status = clGetProgramInfo( clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*numDevices, binary_sizes, NULL );
  436. if (unlikely(status != CL_SUCCESS)) {
  437. applog(LOG_ERR, "Error: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetPlatformInfo)");
  438. return NULL;
  439. }
  440. /* copy over all of the generated binaries. */
  441. if (opt_debug)
  442. applog(LOG_DEBUG, "binary size %d : %d", gpu, binary_sizes[gpu]);
  443. if (!binary_sizes[gpu]) {
  444. applog(LOG_ERR, "OpenCL compiler generated a zero sized binary, may need to reboot!");
  445. return NULL;
  446. }
  447. binaries[gpu] = (char *)malloc( sizeof(char)*binary_sizes[gpu]);
  448. status = clGetProgramInfo( clState->program, CL_PROGRAM_BINARIES, sizeof(char *)*numDevices, binaries, NULL );
  449. if (unlikely(status != CL_SUCCESS)) {
  450. applog(LOG_ERR, "Error: Getting program info. (clGetPlatformInfo)");
  451. return NULL;
  452. }
  453. /* Patch the kernel if the hardware supports BFI_INT */
  454. if (patchbfi) {
  455. unsigned remaining = binary_sizes[gpu];
  456. char *w = binaries[gpu];
  457. unsigned int start, length;
  458. /* Find 2nd incidence of .text, and copy the program's
  459. * position and length at a fixed offset from that. Then go
  460. * back and find the 2nd incidence of \x7ELF (rewind by one
  461. * from ELF) and then patch the opcocdes */
  462. if (!advance(&w, &remaining, ".text"))
  463. {patchbfi = 0; goto build;}
  464. w++; remaining--;
  465. if (!advance(&w, &remaining, ".text")) {
  466. /* 32 bit builds only one ELF */
  467. w--; remaining++;
  468. }
  469. memcpy(&start, w + 285, 4);
  470. memcpy(&length, w + 289, 4);
  471. w = binaries[gpu]; remaining = binary_sizes[gpu];
  472. if (!advance(&w, &remaining, "ELF"))
  473. {patchbfi = 0; goto build;}
  474. w++; remaining--;
  475. if (!advance(&w, &remaining, "ELF")) {
  476. /* 32 bit builds only one ELF */
  477. w--; remaining++;
  478. }
  479. w--; remaining++;
  480. w += start; remaining -= start;
  481. if (opt_debug)
  482. applog(LOG_DEBUG, "At %p (%u rem. bytes), to begin patching",
  483. w, remaining);
  484. patch_opcodes(w, length);
  485. status = clReleaseProgram(clState->program);
  486. if (status != CL_SUCCESS) {
  487. applog(LOG_ERR, "Error: Releasing program. (clReleaseProgram)");
  488. return NULL;
  489. }
  490. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[gpu], (const unsigned char **)&binaries[gpu], &status, NULL);
  491. if (status != CL_SUCCESS) {
  492. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  493. return NULL;
  494. }
  495. clRetainProgram(clState->program);
  496. if (status != CL_SUCCESS) {
  497. applog(LOG_ERR, "Error: Retaining Program (clRetainProgram)");
  498. return NULL;
  499. }
  500. }
  501. free(source);
  502. /* Save the binary to be loaded next time */
  503. binaryfile = fopen(binaryfilename, "wb");
  504. if (!binaryfile) {
  505. /* Not a fatal problem, just means we build it again next time */
  506. if (opt_debug)
  507. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  508. } else {
  509. if (unlikely(fwrite(binaries[gpu], 1, binary_sizes[gpu], binaryfile) != binary_sizes[gpu])) {
  510. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  511. return NULL;
  512. }
  513. fclose(binaryfile);
  514. }
  515. if (binaries[gpu])
  516. free(binaries[gpu]);
  517. built:
  518. free(binaries);
  519. free(binary_sizes);
  520. applog(LOG_INFO, "Initialising kernel %s with%s BFI_INT patching, %d vectors and worksize %d",
  521. filename, patchbfi ? "" : "out", clState->preferred_vwidth, clState->work_size);
  522. /* create a cl program executable for all the devices specified */
  523. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  524. if (status != CL_SUCCESS) {
  525. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  526. size_t logSize;
  527. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  528. char *log = malloc(logSize);
  529. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  530. applog(LOG_INFO, "%s", log);
  531. return NULL;
  532. }
  533. /* get a kernel object handle for a kernel with the given name */
  534. clState->kernel = clCreateKernel(clState->program, "search", &status);
  535. if (status != CL_SUCCESS) {
  536. applog(LOG_ERR, "Error: Creating Kernel from program. (clCreateKernel)");
  537. return NULL;
  538. }
  539. /////////////////////////////////////////////////////////////////
  540. // Create an OpenCL command queue
  541. /////////////////////////////////////////////////////////////////
  542. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu],
  543. CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &status);
  544. if (status != CL_SUCCESS) /* Try again without OOE enable */
  545. clState->commandQueue = clCreateCommandQueue(clState->context, devices[gpu], 0 , &status);
  546. if (status != CL_SUCCESS) {
  547. applog(LOG_ERR, "Creating Command Queue. (clCreateCommandQueue)");
  548. return NULL;
  549. }
  550. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, BUFFERSIZE, NULL, &status);
  551. if (status != CL_SUCCESS) {
  552. applog(LOG_ERR, "Error: clCreateBuffer (outputBuffer)");
  553. return NULL;
  554. }
  555. return clState;
  556. }
  557. #endif /* HAVE_OPENCL */