ocl.c 18 KB

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