ocl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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. FILE *f = fopen(filename, "r");
  30. void *buffer;
  31. if (!f) {
  32. applog(LOG_ERR, "Unable to open %s for reading", filename);
  33. return NULL;
  34. }
  35. fseek(f, 0, SEEK_END);
  36. *length = ftell(f);
  37. fseek(f, 0, SEEK_SET);
  38. buffer = malloc(*length+1);
  39. *length = fread(buffer, 1, *length, f);
  40. fclose(f);
  41. ((char*)buffer)[*length] = '\0';
  42. return (char*)buffer;
  43. }
  44. int clDevicesNum() {
  45. cl_int status = 0;
  46. cl_uint numPlatforms;
  47. cl_platform_id platform = NULL;
  48. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  49. if (status != CL_SUCCESS)
  50. {
  51. applog(LOG_ERR, "Error: Getting Platforms. (clGetPlatformsIDs)");
  52. return -1;
  53. }
  54. if (numPlatforms > 0)
  55. {
  56. cl_platform_id* platforms = (cl_platform_id *)malloc(numPlatforms*sizeof(cl_platform_id));
  57. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  58. if (status != CL_SUCCESS)
  59. {
  60. applog(LOG_ERR, "Error: Getting Platform Ids. (clGetPlatformsIDs)");
  61. return -1;
  62. }
  63. unsigned int i;
  64. for(i=0; i < numPlatforms; ++i)
  65. {
  66. char pbuff[100];
  67. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  68. if (status != CL_SUCCESS)
  69. {
  70. applog(LOG_ERR, "Error: Getting Platform Info. (clGetPlatformInfo)");
  71. free(platforms);
  72. return -1;
  73. }
  74. platform = platforms[i];
  75. if (!strcmp(pbuff, "Advanced Micro Devices, Inc."))
  76. {
  77. break;
  78. }
  79. }
  80. free(platforms);
  81. }
  82. if (platform == NULL) {
  83. perror("NULL platform found!\n");
  84. return -1;
  85. }
  86. cl_uint numDevices;
  87. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  88. if (status != CL_SUCCESS)
  89. {
  90. applog(LOG_ERR, "Error: Getting Device IDs (num)");
  91. return -1;
  92. }
  93. return numDevices;
  94. }
  95. static int advance(char **area, unsigned *remaining, const char *marker)
  96. {
  97. char *find = memmem(*area, *remaining, marker, strlen(marker));
  98. if (!find) {
  99. if (opt_debug)
  100. applog(LOG_DEBUG, "Marker \"%s\" not found", marker);
  101. return 0;
  102. }
  103. *remaining -= find - *area;
  104. *area = find;
  105. return 1;
  106. }
  107. #define OP3_INST_BFE_UINT 4ULL
  108. #define OP3_INST_BFE_INT 5ULL
  109. #define OP3_INST_BFI_INT 6ULL
  110. #define OP3_INST_BIT_ALIGN_INT 12ULL
  111. #define OP3_INST_BYTE_ALIGN_INT 13ULL
  112. void patch_opcodes(char *w, unsigned remaining)
  113. {
  114. uint64_t *opcode = (uint64_t *)w;
  115. int patched = 0;
  116. int count_bfe_int = 0;
  117. int count_bfe_uint = 0;
  118. int count_byte_align = 0;
  119. while (42)
  120. {
  121. int clamp = (*opcode >> (32 + 31)) & 0x1;
  122. int dest_rel = (*opcode >> (32 + 28)) & 0x1;
  123. int alu_inst = (*opcode >> (32 + 13)) & 0x1f;
  124. int s2_neg = (*opcode >> (32 + 12)) & 0x1;
  125. int s2_rel = (*opcode >> (32 + 9)) & 0x1;
  126. int pred_sel = (*opcode >> 29) & 0x3;
  127. if (!clamp && !dest_rel && !s2_neg && !s2_rel && !pred_sel) {
  128. if (alu_inst == OP3_INST_BFE_INT) {
  129. count_bfe_int++;
  130. } else if (alu_inst == OP3_INST_BFE_UINT) {
  131. count_bfe_uint++;
  132. } else if (alu_inst == OP3_INST_BYTE_ALIGN_INT) {
  133. count_byte_align++;
  134. // patch this instruction to BFI_INT
  135. *opcode &= 0xfffc1fffffffffffUL;
  136. *opcode |= OP3_INST_BFI_INT << (32 + 13);
  137. patched++;
  138. }
  139. }
  140. if (remaining <= 8) {
  141. break;
  142. }
  143. opcode++;
  144. remaining -= 8;
  145. }
  146. if (opt_debug) {
  147. applog(LOG_DEBUG, "Potential OP3 instructions identified: "
  148. "%i BFE_INT, %i BFE_UINT, %i BYTE_ALIGN",
  149. count_bfe_int, count_bfe_uint, count_byte_align);
  150. applog(LOG_DEBUG, "Patched a total of %i BFI_INT instructions", patched);
  151. }
  152. }
  153. _clState *initCl(unsigned int gpu, char *name, size_t nameSize)
  154. {
  155. int patchbfi = 0;
  156. cl_int status = 0;
  157. unsigned int i;
  158. _clState *clState = calloc(1, sizeof(_clState));
  159. cl_uint numPlatforms;
  160. cl_platform_id platform = NULL;
  161. status = clGetPlatformIDs(0, NULL, &numPlatforms);
  162. if (status != CL_SUCCESS)
  163. {
  164. applog(LOG_ERR, "Error: Getting Platforms. (clGetPlatformsIDs)");
  165. return NULL;
  166. }
  167. if (numPlatforms > 0)
  168. {
  169. cl_platform_id* platforms = (cl_platform_id *)malloc(numPlatforms*sizeof(cl_platform_id));
  170. status = clGetPlatformIDs(numPlatforms, platforms, NULL);
  171. if (status != CL_SUCCESS)
  172. {
  173. applog(LOG_ERR, "Error: Getting Platform Ids. (clGetPlatformsIDs)");
  174. return NULL;
  175. }
  176. for(i = 0; i < numPlatforms; ++i)
  177. {
  178. char pbuff[100];
  179. status = clGetPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, sizeof(pbuff), pbuff, NULL);
  180. if (status != CL_SUCCESS)
  181. {
  182. applog(LOG_ERR, "Error: Getting Platform Info. (clGetPlatformInfo)");
  183. free(platforms);
  184. return NULL;
  185. }
  186. platform = platforms[i];
  187. if (!strcmp(pbuff, "Advanced Micro Devices, Inc."))
  188. {
  189. break;
  190. }
  191. }
  192. free(platforms);
  193. }
  194. if (platform == NULL) {
  195. perror("NULL platform found!\n");
  196. return NULL;
  197. }
  198. size_t nDevices;
  199. cl_uint numDevices;
  200. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
  201. if (status != CL_SUCCESS)
  202. {
  203. applog(LOG_ERR, "Error: Getting Device IDs (num)");
  204. return NULL;
  205. }
  206. cl_device_id *devices;
  207. if (numDevices > 0 ) {
  208. devices = (cl_device_id *)malloc(numDevices*sizeof(cl_device_id));
  209. /* Now, get the device list data */
  210. status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
  211. if (status != CL_SUCCESS)
  212. {
  213. applog(LOG_ERR, "Error: Getting Device IDs (list)");
  214. return NULL;
  215. }
  216. applog(LOG_INFO, "List of devices:");
  217. unsigned int i;
  218. for(i=0; i<numDevices; i++) {
  219. char pbuff[100];
  220. status = clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(pbuff), pbuff, NULL);
  221. if (status != CL_SUCCESS)
  222. {
  223. applog(LOG_ERR, "Error: Getting Device Info");
  224. return NULL;
  225. }
  226. applog(LOG_INFO, "\t%i\t%s", i, pbuff);
  227. }
  228. if (gpu < numDevices) {
  229. char pbuff[100];
  230. status = clGetDeviceInfo(devices[gpu], CL_DEVICE_NAME, sizeof(pbuff), pbuff, &nDevices);
  231. if (status != CL_SUCCESS)
  232. {
  233. applog(LOG_ERR, "Error: Getting Device Info");
  234. return NULL;
  235. }
  236. applog(LOG_INFO, "Selected %i: %s", gpu, pbuff);
  237. strncpy(name, pbuff, nameSize);
  238. } else {
  239. applog(LOG_ERR, "Invalid GPU %i", gpu);
  240. return NULL;
  241. }
  242. } else return NULL;
  243. cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };
  244. clState->context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
  245. if (status != CL_SUCCESS)
  246. {
  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 = patchbfi = 1;
  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. /* Some nvidia cards report 1024 but fail when set larger than 512 !? */
  278. if (clState->max_work_size > 512)
  279. clState->max_work_size = 512;
  280. /* For some reason 2 vectors is still better even if the card says
  281. * otherwise */
  282. if (clState->preferred_vwidth > 1)
  283. clState->preferred_vwidth = 2;
  284. if (opt_vectors)
  285. clState->preferred_vwidth = opt_vectors;
  286. if (opt_worksize && opt_worksize <= clState->max_work_size)
  287. clState->work_size = opt_worksize;
  288. else
  289. clState->work_size = clState->max_work_size / clState->preferred_vwidth;
  290. /* Create binary filename based on parameters passed to opencl
  291. * compiler to ensure we only load a binary that matches what would
  292. * have otherwise created. The filename is:
  293. * name + kernelname +/i bitalign + v + vectors + w + work_size + sizeof(long) + .bin
  294. */
  295. char binaryfilename[255];
  296. char numbuf[10];
  297. char filename[10];
  298. if (clState->hasBitAlign)
  299. strcpy(filename, "phatk.cl");
  300. else
  301. strcpy(filename, "poclbm.cl");
  302. FILE *binaryfile;
  303. size_t *binary_sizes;
  304. char **binaries;
  305. int pl;
  306. char *source, *rawsource = file_contents(filename, &pl);
  307. size_t sourceSize[] = {(size_t)pl};
  308. source = malloc(pl);
  309. if (!source) {
  310. applog(LOG_ERR, "Unable to malloc source");
  311. return NULL;
  312. }
  313. binary_sizes = (size_t *)malloc(sizeof(size_t)*nDevices);
  314. if (unlikely(!binary_sizes)) {
  315. applog(LOG_ERR, "Unable to malloc binary_sizes");
  316. return NULL;
  317. }
  318. binaries = (char **)malloc(sizeof(char *)*nDevices);
  319. if (unlikely(!binaries)) {
  320. applog(LOG_ERR, "Unable to malloc binaries");
  321. return NULL;
  322. }
  323. strcpy(binaryfilename, name);
  324. if (clState->hasBitAlign) {
  325. strcat(binaryfilename, "phatk");
  326. strcat(binaryfilename, "bitalign");
  327. } else
  328. strcat(binaryfilename, "poclbm");
  329. strcat(binaryfilename, "v");
  330. sprintf(numbuf, "%d", clState->preferred_vwidth);
  331. strcat(binaryfilename, numbuf);
  332. strcat(binaryfilename, "w");
  333. sprintf(numbuf, "%d", (int)clState->work_size);
  334. strcat(binaryfilename, numbuf);
  335. strcat(binaryfilename, "long");
  336. sprintf(numbuf, "%d", (int)sizeof(long));
  337. strcat(binaryfilename, numbuf);
  338. strcat(binaryfilename, ".bin");
  339. binaryfile = fopen(binaryfilename, "r");
  340. if (!binaryfile) {
  341. if (opt_debug)
  342. applog(LOG_DEBUG, "No binary found, generating from source");
  343. } else {
  344. struct stat binary_stat;
  345. if (unlikely(stat(binaryfilename, &binary_stat))) {
  346. if (opt_debug)
  347. applog(LOG_DEBUG, "Unable to stat binary, generating from source");
  348. fclose(binaryfile);
  349. goto build;
  350. }
  351. binary_sizes[gpu] = binary_stat.st_size;
  352. binaries[gpu] = (char *)malloc(binary_sizes[gpu]);
  353. if (unlikely(!binaries[gpu])) {
  354. applog(LOG_ERR, "Unable to malloc binaries");
  355. fclose(binaryfile);
  356. return NULL;
  357. }
  358. if (fread(binaries[gpu], 1, binary_sizes[gpu], binaryfile) != binary_sizes[gpu]) {
  359. applog(LOG_ERR, "Unable to fread binaries[gpu]");
  360. fclose(binaryfile);
  361. goto build;
  362. }
  363. fclose(binaryfile);
  364. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[gpu], (const unsigned char **)&binaries[gpu], &status, NULL);
  365. if (status != CL_SUCCESS)
  366. {
  367. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  368. return NULL;
  369. }
  370. if (opt_debug)
  371. applog(LOG_DEBUG, "Loaded binary image %s", binaryfilename);
  372. free(binaries[gpu]);
  373. goto built;
  374. }
  375. /////////////////////////////////////////////////////////////////
  376. // Load CL file, build CL program object, create CL kernel object
  377. /////////////////////////////////////////////////////////////////
  378. build:
  379. memcpy(source, rawsource, pl);
  380. /* Patch the source file with the preferred_vwidth */
  381. if (clState->preferred_vwidth > 1) {
  382. char *find = strstr(source, "VECTORSX");
  383. if (unlikely(!find)) {
  384. applog(LOG_ERR, "Unable to find VECTORSX in source");
  385. return NULL;
  386. }
  387. find += 7; // "VECTORS"
  388. if (clState->preferred_vwidth == 2)
  389. strncpy(find, "2", 1);
  390. else
  391. strncpy(find, "4", 1);
  392. if (opt_debug)
  393. applog(LOG_DEBUG, "Patched source to suit %d vectors", clState->preferred_vwidth);
  394. }
  395. /* Patch the source file defining BITALIGN */
  396. if (clState->hasBitAlign) {
  397. char *find = strstr(source, "BITALIGNX");
  398. if (unlikely(!find)) {
  399. applog(LOG_ERR, "Unable to find BITALIGNX in source");
  400. return NULL;
  401. }
  402. find += 8; // "BITALIGN"
  403. strncpy(find, " ", 1);
  404. if (opt_debug)
  405. applog(LOG_DEBUG, "cl_amd_media_ops found, patched source with BITALIGN");
  406. } else if (opt_debug)
  407. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not BITALIGN patch");
  408. if (patchbfi) {
  409. char *find = strstr(source, "BFI_INTX");
  410. if (unlikely(!find)) {
  411. applog(LOG_ERR, "Unable to find BFI_INTX in source");
  412. return NULL;
  413. }
  414. find += 7; // "BFI_INT"
  415. strncpy(find, " ", 1);
  416. if (opt_debug)
  417. applog(LOG_DEBUG, "cl_amd_media_ops found, patched source with BFI_INT");
  418. } else if (opt_debug)
  419. applog(LOG_DEBUG, "cl_amd_media_ops not found, will not BFI_INT patch");
  420. clState->program = clCreateProgramWithSource(clState->context, 1, (const char **)&source, sourceSize, &status);
  421. if (status != CL_SUCCESS)
  422. {
  423. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithSource)");
  424. return NULL;
  425. }
  426. /* create a cl program executable for all the devices specified */
  427. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  428. if (status != CL_SUCCESS)
  429. {
  430. applog(LOG_ERR, "Error: Building Program (clBuildProgram)");
  431. size_t logSize;
  432. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, 0, NULL, &logSize);
  433. char *log = malloc(logSize);
  434. status = clGetProgramBuildInfo(clState->program, devices[gpu], CL_PROGRAM_BUILD_LOG, logSize, log, NULL);
  435. applog(LOG_INFO, "%s", log);
  436. return NULL;
  437. }
  438. status = clGetProgramInfo( clState->program, CL_PROGRAM_BINARY_SIZES, sizeof(size_t)*nDevices, binary_sizes, NULL );
  439. if (unlikely(status != CL_SUCCESS))
  440. {
  441. applog(LOG_ERR, "Error: Getting program info CL_PROGRAM_BINARY_SIZES. (clGetPlatformInfo)");
  442. return NULL;
  443. }
  444. /* copy over all of the generated binaries. */
  445. if (opt_debug)
  446. applog(LOG_DEBUG, "binary size %d : %d", gpu, binary_sizes[gpu]);
  447. binaries[gpu] = (char *)malloc( sizeof(char)*binary_sizes[gpu]);
  448. status = clGetProgramInfo( clState->program, CL_PROGRAM_BINARIES, sizeof(char *)*nDevices, binaries, NULL );
  449. if (unlikely(status != CL_SUCCESS))
  450. {
  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. {
  489. applog(LOG_ERR, "Error: Releasing program. (clReleaseProgram)");
  490. return NULL;
  491. }
  492. clState->program = clCreateProgramWithBinary(clState->context, 1, &devices[gpu], &binary_sizes[gpu], (const unsigned char **)&binaries[gpu], &status, NULL);
  493. if (status != CL_SUCCESS)
  494. {
  495. applog(LOG_ERR, "Error: Loading Binary into cl_program (clCreateProgramWithBinary)");
  496. return NULL;
  497. }
  498. }
  499. free(source);
  500. free(rawsource);
  501. /* Save the binary to be loaded next time */
  502. binaryfile = fopen(binaryfilename, "w");
  503. if (!binaryfile) {
  504. /* Not a fatal problem, just means we build it again next time */
  505. if (opt_debug)
  506. applog(LOG_DEBUG, "Unable to create file %s", binaryfilename);
  507. } else {
  508. if (unlikely(fwrite(binaries[gpu], 1, binary_sizes[gpu], binaryfile) != binary_sizes[gpu])) {
  509. applog(LOG_ERR, "Unable to fwrite to binaryfile");
  510. return NULL;
  511. }
  512. fclose(binaryfile);
  513. }
  514. if (binaries[gpu])
  515. free(binaries[gpu]);
  516. built:
  517. free(binaries);
  518. free(binary_sizes);
  519. applog(LOG_INFO, "Initialising kernel %s with%s BFI_INT patching, %d vectors and worksize %d",
  520. filename, patchbfi ? "" : "out", clState->preferred_vwidth, clState->work_size);
  521. /* create a cl program executable for all the devices specified */
  522. status = clBuildProgram(clState->program, 1, &devices[gpu], NULL, NULL, NULL);
  523. if (status != CL_SUCCESS)
  524. {
  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. {
  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], 0, &status);
  544. if (status != CL_SUCCESS)
  545. {
  546. applog(LOG_ERR, "Creating Command Queue. (clCreateCommandQueue)");
  547. return NULL;
  548. }
  549. clState->outputBuffer = clCreateBuffer(clState->context, CL_MEM_READ_WRITE, BUFFERSIZE, NULL, &status);
  550. if (status != CL_SUCCESS) {
  551. applog(LOG_ERR, "Error: clCreateBuffer (outputBuffer)");
  552. return NULL;
  553. }
  554. return clState;
  555. }
  556. #endif /* HAVE_OPENCL */